Skip to content

Commit 837ec6e

Browse files
committed
feature/Implement toggle component
1 parent d3507bb commit 837ec6e

File tree

4 files changed

+79
-52
lines changed

4 files changed

+79
-52
lines changed

app/javascript/controllers/checkbox_controller.js

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Controller } from 'stimulus';
2+
3+
export default class extends Controller {
4+
static targets = ['background', 'text', 'toggle', 'value']
5+
static classes = ['enabledColor', 'disabledColor', 'enabledTranslate', 'disabledTranslate']
6+
static values = {
7+
enabledText: String,
8+
disabledText: String,
9+
enabled: Boolean,
10+
flip: Boolean
11+
}
12+
13+
toggle() {
14+
this.toggleValue()
15+
this.toggleToggle()
16+
this.toggleText()
17+
}
18+
19+
toggleValue() {
20+
this.enabledValue = !this.enabledValue
21+
22+
if (this.flipValue) {
23+
this.valueTarget.value = !this.enabledValue
24+
} else {
25+
this.valueTarget = this.enabledValue
26+
}
27+
}
28+
29+
toggleToggle() {
30+
this.backgroundTarget.classList.toggle(this.enabledColorClass)
31+
this.backgroundTarget.classList.toggle(this.disabledColorClass)
32+
this.toggleTarget.classList.toggle(this.enabledTranslateClass)
33+
this.toggleTarget.classList.toggle(this.disabledTranslateClass)
34+
}
35+
36+
toggleText() {
37+
if (this.isEnabled) {
38+
this.textTarget.innerHTML = this.enabledTextValue
39+
} else {
40+
this.textTarget.innerHTML = this.disabledTextValue
41+
}
42+
}
43+
44+
get isEnabled() {
45+
return this.enabledValue
46+
}
47+
}

app/views/modals/snippets/new.html.erb

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,9 @@
1313
</div>
1414

1515
<div class="create-snippet--options-wrapper">
16-
<div class="flex v-center" style="cursor: pointer;"
17-
data-controller="checkbox"
18-
data-action="click->checkbox#toggle"
19-
data-checkbox-checked-text-value="Public"
20-
data-checkbox-checked-icon-value="/icons/lock-open.svg"
21-
data-checkbox-unchecked-text-value="Private"
22-
data-checkbox-unchecked-icon-value="/icons/lock-closed.svg"
23-
>
24-
<span data-checkbox-target="text">Public</span>
25-
<img data-checkbox-target="icon" src="/icons/lock-open.svg" width="20">
26-
<%= f.hidden_field :public, value: true, data: { checkbox_target: 'value' } %>
27-
</div>
16+
17+
<%= render partial: 'shared/form/toggle', locals: { form: f, field: :public, enabled_text: 'Private', disabled_text: 'Public', enabled: false, flip: true } %>
18+
2819
<div class="flex">
2920
<%= f.select :language_id, options_from_collection_for_select(@languages, :id, :name, @javascript.id), { prompt: 'Select language...' }, class: "block w-full pl-3 pr-10 py-1 text-base border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md", data: { action: 'change->codemirror#updateMode' } %>
3021
<%= f.select :folder_id, options_from_collection_for_select(@folders, :id, :name, params[:folder_id]), { prompt: 'Select folder...' }, { class: "ml-1 block w-full pl-3 pr-10 py-1 text-base border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md" } %>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<div
2+
class="flex items-center"
3+
data-controller="toggle"
4+
data-toggle-enabled-color-class="bg-cyan"
5+
data-toggle-disabled-color-class="bg-gray-200"
6+
data-toggle-enabled-translate-class="translate-x-6"
7+
data-toggle-disabled-translate-class="translate-x-0"
8+
data-toggle-enabled-text-value="<%= enabled_text %>"
9+
data-toggle-disabled-text-value="<%= disabled_text %>"
10+
data-toggle-enabled-value="<%= enabled %>"
11+
data-toggle-flip-value="<%= flip %>"
12+
>
13+
14+
<button
15+
type="button"
16+
class="bg-gray-200 relative inline-flex flex-shrink-0 h-5 w-11 border-2 border-transparent rounded-full cursor-pointer transition-colors ease-in-out duration-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
17+
aria-pressed="false"
18+
data-action="click->toggle#toggle"
19+
data-toggle-target="background"
20+
>
21+
<span aria-hidden="true" data-toggle-target="toggle" class="translate-x-0 pointer-events-none inline-block h-4 w-4 rounded-full bg-white shadow transform ring-0 transition ease-in-out duration-200"></span>
22+
</button>
23+
24+
<span class="ml-3" id="annual-billing-label">
25+
<span class="text-sm font-medium text-gray-900" data-toggle-target="text"><%= enabled ? enabled_text : disabled_text %></span>
26+
</span>
27+
28+
<%= form.hidden_field field, value: flip ? !enabled : enabled, data: { toggle_target: 'value' } %>
29+
</div>

0 commit comments

Comments
 (0)