Skip to content

Commit e510909

Browse files
committed
[Bug Fix] Theme toggle: skip initial dispatch + use setAttribute for double-hyphen data key
1 parent 43741fd commit e510909

4 files changed

Lines changed: 10 additions & 6 deletions

File tree

docs/app/javascript/controllers/ruby_ui/theme_toggle_controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ export default class extends Controller {
3333
const dark = theme === "dark"
3434
this.element.setAttribute("aria-pressed", dark ? "true" : "false")
3535
this.element.dataset.state = dark ? "on" : "off"
36-
this.element.dataset["rubyUi--TogglePressedValue"] = dark ? "true" : "false"
36+
this.element.setAttribute("data-ruby-ui--toggle-pressed-value", dark ? "true" : "false")
3737
}
3838
}

docs/app/javascript/controllers/ruby_ui/toggle_controller.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ export default class extends Controller {
1414
this.pressedValue = !this.pressedValue
1515
}
1616

17-
pressedValueChanged(current) {
17+
pressedValueChanged(current, previous) {
1818
this.element.setAttribute("aria-pressed", current ? "true" : "false")
1919
this.element.dataset.state = current ? "on" : "off"
2020

2121
if (this.hasInputTarget) {
2222
this.inputTarget.value = current ? this.valueValue : this.unpressedValueValue
2323
}
2424

25-
this.dispatch("change", { detail: { pressed: current }, bubbles: true })
25+
if (previous !== undefined) {
26+
this.dispatch("change", { detail: { pressed: current }, bubbles: true })
27+
}
2628
}
2729
}

gem/lib/ruby_ui/theme_toggle/theme_toggle_controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ export default class extends Controller {
3333
const dark = theme === "dark"
3434
this.element.setAttribute("aria-pressed", dark ? "true" : "false")
3535
this.element.dataset.state = dark ? "on" : "off"
36-
this.element.dataset["rubyUi--TogglePressedValue"] = dark ? "true" : "false"
36+
this.element.setAttribute("data-ruby-ui--toggle-pressed-value", dark ? "true" : "false")
3737
}
3838
}

gem/lib/ruby_ui/toggle/toggle_controller.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ export default class extends Controller {
1414
this.pressedValue = !this.pressedValue
1515
}
1616

17-
pressedValueChanged(current) {
17+
pressedValueChanged(current, previous) {
1818
this.element.setAttribute("aria-pressed", current ? "true" : "false")
1919
this.element.dataset.state = current ? "on" : "off"
2020

2121
if (this.hasInputTarget) {
2222
this.inputTarget.value = current ? this.valueValue : this.unpressedValueValue
2323
}
2424

25-
this.dispatch("change", { detail: { pressed: current }, bubbles: true })
25+
if (previous !== undefined) {
26+
this.dispatch("change", { detail: { pressed: current }, bubbles: true })
27+
}
2628
}
2729
}

0 commit comments

Comments
 (0)