- Notifications
You must be signed in to change notification settings - Fork328
make switch input#1083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
make switch input#1083
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletionspgml-dashboard/src/components/inputs/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletionspgml-dashboard/src/components/inputs/switch/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use crate::components::stimulus::stimulus_action::StimulusAction; | ||
use crate::components::stimulus::stimulus_target::StimulusTarget; | ||
use pgml_components::component; | ||
use sailfish::TemplateOnce; | ||
use std::fmt::{self, Display, Formatter}; | ||
pub enum State { | ||
Left, | ||
Right, | ||
} | ||
impl Display for State { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
match self { | ||
State::Left => write!(f, "left"), | ||
State::Right => write!(f, "right"), | ||
} | ||
} | ||
} | ||
#[derive(TemplateOnce)] | ||
#[template(path = "inputs/switch/template.html")] | ||
pub struct Switch { | ||
left_value: String, | ||
left_icon: String, | ||
right_value: String, | ||
right_icon: String, | ||
initial_state: State, | ||
on_toggle: Vec<StimulusAction>, | ||
target: StimulusTarget, | ||
} | ||
impl Switch { | ||
pub fn new() -> Switch { | ||
Switch { | ||
left_value: String::from("left"), | ||
left_icon: String::from(""), | ||
right_value: String::from("right"), | ||
right_icon: String::from(""), | ||
on_toggle: Vec::new(), | ||
initial_state: State::Left, | ||
target: StimulusTarget::new(), | ||
} | ||
} | ||
pub fn left(mut self, value: &str, icon: &str) -> Switch { | ||
self.left_value = value.into(); | ||
self.left_icon = icon.into(); | ||
self | ||
} | ||
pub fn right(mut self, value: &str, icon: &str) -> Switch { | ||
self.right_value = value.into(); | ||
self.right_icon = icon.into(); | ||
self | ||
} | ||
pub fn on_toggle(mut self, action: StimulusAction) -> Switch { | ||
self.on_toggle.push(action); | ||
self | ||
} | ||
pub fn start_toggled(mut self) -> Switch { | ||
self.initial_state = State::Right; | ||
self | ||
} | ||
pub fn target(mut self, target: StimulusTarget) -> Switch { | ||
self.target = target; | ||
self | ||
} | ||
} | ||
component!(Switch); |
43 changes: 43 additions & 0 deletionspgml-dashboard/src/components/inputs/switch/switch.scss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
div[data-controller="inputs-switch"] { | ||
&.switch-container { | ||
background: #{$gray-100}; | ||
border-radius: 5rem; | ||
border: 3px solid #{$gray-100}; | ||
position: relative; | ||
} | ||
.label { | ||
padding: 8px 20px; | ||
border-radius: 5rem; | ||
text-align: center; | ||
display: flex; | ||
@extend .gap-2; | ||
} | ||
.toggle { | ||
background: #{$neon-shade-100}; | ||
position: absolute; | ||
top: 0px; | ||
left: 0px; | ||
} | ||
.choice { | ||
background: #{$gray-100}; | ||
color: #{$neon-shade-100}; | ||
flex: 1; | ||
* { | ||
color: inherit; | ||
} | ||
} | ||
.left { | ||
left: 0; | ||
transition: all $animation-timer; | ||
} | ||
.right { | ||
left: 50%; | ||
transition: all $animation-timer; | ||
} | ||
} |
52 changes: 52 additions & 0 deletionspgml-dashboard/src/components/inputs/switch/switch_controller.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Controller } from '@hotwired/stimulus' | ||
export default class extends Controller { | ||
static targets = [ | ||
"toggle", | ||
"toggleText", | ||
"toggleIcon", | ||
] | ||
static values = { | ||
"left": String, | ||
"right": String, | ||
"initial": String, | ||
"leftIcon": String, | ||
"rightIcon": String, | ||
} | ||
toggle() { | ||
if (this.toggleTarget.classList.contains('right')) { | ||
this.onToggleLeft() | ||
} else { | ||
this.onToggleRight() | ||
} | ||
} | ||
onToggleLeft() { | ||
this.toggleTarget.classList.remove('right') | ||
this.toggleTarget.classList.add('left') | ||
this.toggleTextTarget.innerHTML = this.leftValue | ||
this.toggleIconTarget.innerHTML = this.leftIconValue | ||
this.element.dispatchEvent(new CustomEvent('toggle', {detail: this.leftValue})) | ||
} | ||
onToggleRight() { | ||
this.toggleTarget.classList.remove('left') | ||
this.toggleTarget.classList.add('right') | ||
this.toggleTextTarget.innerHTML = this.rightValue | ||
this.toggleIconTarget.innerHTML = this.rightIconValue | ||
this.element.dispatchEvent(new CustomEvent('toggle', {detail: this.rightValue})) | ||
} | ||
reset() { | ||
if( this.initialValue == "left" ) { | ||
console.log("toggling left") | ||
this.onToggleLeft() | ||
} else { | ||
console.log("toggling right") | ||
this.onToggleRight() | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletionspgml-dashboard/src/components/inputs/switch/template.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<% use crate::components::inputs::switch::State; %> | ||
<div data-controller="inputs-switch" | ||
class="switch-container d-flex flex-row" | ||
data-action='click->inputs-switch#toggle <% for action in on_toggle { %> toggle-><%- action %> <% } %> reset->inputs-switch#reset' | ||
data-inputs-switch-left-value="<%- left_value %>" | ||
data-inputs-switch-left-icon-value="<%- left_icon %>" | ||
data-inputs-switch-right-value="<%- right_value %>" | ||
data-inputs-switch-right-icon-value="<%- right_icon %>" | ||
data-inputs-switch-initial-value="<%- initial_state.to_string() %>" | ||
<%- target %>> | ||
<div class='label toggle w-50 <%- match initial_state {State::Left => "left".to_string(), State::Right => "right".to_string()} %>' data-inputs-switch-target="toggle"> | ||
<span class="material-symbols-outlined" data-inputs-switch-target="toggleIcon" > | ||
<%- match initial_state { | ||
State::Left => left_icon.to_string(), | ||
State::Right => right_icon.to_string(), | ||
} %> | ||
</span> | ||
<h5 class="h5 my-0" data-inputs-switch-target="toggleText"> | ||
<%- match initial_state { | ||
State::Left => left_value.to_string(), | ||
State::Right => right_value.to_string(), | ||
} %> | ||
</h5> | ||
</div> | ||
<div class="label choice"> | ||
<span class="material-symbols-outlined" ><%- left_icon %></span> | ||
<h5 class="h5 my-0"> | ||
<%- left_value %> | ||
</h5> | ||
</div> | ||
<div class="label choice"> | ||
<span class="material-symbols-outlined"><%- right_icon %></span> | ||
<h5 class="h5 my-0"> | ||
<%- right_value %> | ||
</h5> | ||
</div> | ||
</div> |
1 change: 1 addition & 0 deletionspgml-dashboard/static/css/modules.scss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
10 changes: 9 additions & 1 deletionpgml-dashboard/static/js/playground.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletionspgml-dashboard/templates/content/playground.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.