-
Notifications
You must be signed in to change notification settings - Fork 63
[Render Preview] [Feature] Toggle + ToggleGroup; refactor ThemeToggle #392
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
djalmaaraujo
wants to merge
23
commits into
main
Choose a base branch
from
da/toggle
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,234
−240
Open
Changes from 16 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
0209f54
[Feature] Add Toggle component (button + aria-pressed + hidden input)
djalmaaraujo 51c89e3
[Feature] Toggle Stimulus controller
djalmaaraujo dc83bff
[Feature] Register toggle/toggle_group/theme_toggle component depende…
djalmaaraujo 23d5a16
[Feature] Add ToggleGroup + ToggleGroupItem (single/multiple, ARIA, f…
djalmaaraujo 15e141d
[Feature] ToggleGroup Stimulus controller (single/multiple, roving ta…
djalmaaraujo 5ed1d70
[Refactor] ThemeToggle composes Toggle; drop SetDarkMode/SetLightMode
djalmaaraujo 97222b2
[Documentation] Add Toggle docs page
djalmaaraujo 2c986f9
[Documentation] Add ToggleGroup docs page
djalmaaraujo 80d5ea7
[Documentation] Regenerate site files for toggle pages
djalmaaraujo 17c4ccd
[Bug Fix] Update docs navbar + theme_toggle view to new ThemeToggle API
djalmaaraujo f66e549
[Bug Fix] Render Toggle/ToggleGroup views from docs controller actions
djalmaaraujo 0191ed0
[Documentation] Ignore specs/ planning directory
djalmaaraujo 388bec3
[Bug Fix] Register Toggle/ToggleGroup Stimulus controllers in docs app
djalmaaraujo 7d2e27f
[Feature] ToggleGroup match shadcn look (joined default, spacing, ori…
djalmaaraujo e02135c
[Bug Fix] ToggleGroup joined corners use first-of-type/last-of-type; …
djalmaaraujo 47836a0
[Documentation] Add Multiple selection example to Toggle Group docs
djalmaaraujo 1343f31
[Refactor] Simplify Toggle/ToggleGroup: extract class builders, drop …
djalmaaraujo 43741fd
[Bug Fix] Update dark_mode getting-started docs to new ThemeToggle API
djalmaaraujo e510909
[Bug Fix] Theme toggle: skip initial dispatch + use setAttribute for …
djalmaaraujo 0480f5d
[Bug Fix] ThemeToggle action listens for correct event name (Stimulus…
djalmaaraujo ddaa88d
[Style] ThemeToggle uses default (borderless) variant
djalmaaraujo 49a1103
[Bug Fix] Wrap Toggle in span so hidden input is a Stimulus target; T…
djalmaaraujo 1e265a5
Merge remote-tracking branch 'origin/main' into da/toggle
djalmaaraujo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| CLAUDE.local.md | ||
| specs/ |
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
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
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
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
46 changes: 27 additions & 19 deletions
46
docs/app/javascript/controllers/ruby_ui/theme_toggle_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 |
|---|---|---|
| @@ -1,30 +1,38 @@ | ||
| import { Controller } from "@hotwired/stimulus" | ||
|
|
||
| // Connects to data-controller="ruby-ui--theme-toggle" | ||
| // Expects to sit on the same element as ruby-ui--toggle and listen to its | ||
| // ruby-ui:toggle:change event. pressed = dark mode. | ||
| export default class extends Controller { | ||
| initialize() { | ||
| this.setTheme() | ||
| connect() { | ||
| this.applyTheme(this.currentTheme()) | ||
| } | ||
|
|
||
| setTheme() { | ||
| // On page load or when changing themes, best to add inline in `head` to avoid FOUC | ||
| if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) { | ||
| document.documentElement.classList.add('dark') | ||
| document.documentElement.classList.remove('light') | ||
| } else { | ||
| document.documentElement.classList.remove('dark') | ||
| document.documentElement.classList.add('light') | ||
| } | ||
| apply(event) { | ||
| const pressed = event.detail?.pressed | ||
| const theme = pressed ? "dark" : "light" | ||
| localStorage.theme = theme | ||
| this.applyTheme(theme) | ||
| } | ||
|
|
||
| setLightTheme() { | ||
| // Whenever the user explicitly chooses light mode | ||
| localStorage.theme = 'light' | ||
| this.setTheme() | ||
| currentTheme() { | ||
| if (localStorage.theme === "dark") return "dark" | ||
| if (localStorage.theme === "light") return "light" | ||
| return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light" | ||
| } | ||
|
|
||
| setDarkTheme() { | ||
| // Whenever the user explicitly chooses dark mode | ||
| localStorage.theme = 'dark' | ||
| this.setTheme() | ||
| applyTheme(theme) { | ||
| const html = document.documentElement | ||
| if (theme === "dark") { | ||
| html.classList.add("dark") | ||
| html.classList.remove("light") | ||
| } else { | ||
| html.classList.add("light") | ||
| html.classList.remove("dark") | ||
| } | ||
| const dark = theme === "dark" | ||
| this.element.setAttribute("aria-pressed", dark ? "true" : "false") | ||
| this.element.dataset.state = dark ? "on" : "off" | ||
| this.element.dataset["rubyUi--TogglePressedValue"] = dark ? "true" : "false" | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
docs/app/javascript/controllers/ruby_ui/toggle_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,27 @@ | ||
| import { Controller } from "@hotwired/stimulus" | ||
|
|
||
| // Connects to data-controller="ruby-ui--toggle" | ||
| export default class extends Controller { | ||
| static targets = ["input"] | ||
| static values = { | ||
| pressed: Boolean, | ||
| value: String, | ||
| unpressedValue: String | ||
| } | ||
|
|
||
| toggle(event) { | ||
| if (this.element.disabled) return | ||
| this.pressedValue = !this.pressedValue | ||
| } | ||
|
|
||
| pressedValueChanged(current) { | ||
| this.element.setAttribute("aria-pressed", current ? "true" : "false") | ||
| this.element.dataset.state = current ? "on" : "off" | ||
|
|
||
| if (this.hasInputTarget) { | ||
| this.inputTarget.value = current ? this.valueValue : this.unpressedValueValue | ||
| } | ||
|
|
||
| this.dispatch("change", { detail: { pressed: current }, bubbles: true }) | ||
| } | ||
| } | ||
126 changes: 126 additions & 0 deletions
126
docs/app/javascript/controllers/ruby_ui/toggle_group_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,126 @@ | ||
| import { Controller } from "@hotwired/stimulus" | ||
|
|
||
| // Connects to data-controller="ruby-ui--toggle-group" | ||
| export default class extends Controller { | ||
| static targets = ["item", "input"] | ||
| static values = { type: String, name: String } | ||
|
|
||
| connect() { | ||
| this.reconcile() | ||
| } | ||
|
|
||
| select(event) { | ||
| const item = event.currentTarget | ||
| if (item.disabled) return | ||
|
|
||
| if (this.typeValue === "single") { | ||
| this.itemTargets.forEach(el => this.setPressed(el, el === item)) | ||
| } else { | ||
| this.setPressed(item, !this.isPressed(item)) | ||
| } | ||
|
|
||
| this.rebuildInputs() | ||
| this.updateRovingTabindex(item) | ||
| } | ||
|
|
||
| navigate(event) { | ||
| if (this.typeValue !== "single") return | ||
| const items = this.enabledItems() | ||
| if (items.length === 0) return | ||
|
|
||
| const isRtl = document.documentElement.dir === "rtl" | ||
| const currentIndex = items.indexOf(event.currentTarget) | ||
| let nextIndex = currentIndex | ||
|
|
||
| switch (event.key) { | ||
| case "ArrowRight": | ||
| case "ArrowDown": | ||
| nextIndex = (currentIndex + (isRtl && event.key === "ArrowRight" ? -1 : 1) + items.length) % items.length | ||
| break | ||
| case "ArrowLeft": | ||
| case "ArrowUp": | ||
| nextIndex = (currentIndex + (isRtl && event.key === "ArrowLeft" ? 1 : -1) + items.length) % items.length | ||
| break | ||
| case "Home": | ||
| nextIndex = 0 | ||
| break | ||
| case "End": | ||
| nextIndex = items.length - 1 | ||
| break | ||
| case " ": | ||
| case "Enter": | ||
| event.preventDefault() | ||
| event.currentTarget.click() | ||
| return | ||
| default: | ||
| return | ||
| } | ||
|
|
||
| event.preventDefault() | ||
| const next = items[nextIndex] | ||
| this.updateRovingTabindex(next) | ||
| next.focus() | ||
| } | ||
|
|
||
| reconcile() { | ||
| if (this.typeValue === "single") { | ||
| const pressed = this.itemTargets.find(el => this.isPressed(el)) | ||
| const first = pressed || this.enabledItems()[0] | ||
| this.itemTargets.forEach(el => { | ||
| el.setAttribute("tabindex", el === first ? "0" : "-1") | ||
| }) | ||
| } else { | ||
| this.itemTargets.forEach(el => el.setAttribute("tabindex", "0")) | ||
| } | ||
| this.rebuildInputs() | ||
| } | ||
|
|
||
| isPressed(item) { | ||
| return item.dataset.state === "on" | ||
| } | ||
|
|
||
| setPressed(item, pressed) { | ||
| item.dataset.state = pressed ? "on" : "off" | ||
| if (this.typeValue === "single") { | ||
| item.setAttribute("aria-checked", pressed ? "true" : "false") | ||
| } else { | ||
| item.setAttribute("aria-pressed", pressed ? "true" : "false") | ||
| } | ||
| } | ||
|
|
||
| updateRovingTabindex(focusedItem) { | ||
| if (this.typeValue !== "single") return | ||
| this.itemTargets.forEach(el => { | ||
| el.setAttribute("tabindex", el === focusedItem ? "0" : "-1") | ||
| }) | ||
| } | ||
|
|
||
| enabledItems() { | ||
| return this.itemTargets.filter(el => !el.disabled) | ||
| } | ||
|
|
||
| rebuildInputs() { | ||
| if (!this.nameValue) return | ||
| this.inputTargets.forEach(el => el.remove()) | ||
|
|
||
| const pressed = this.itemTargets.filter(el => this.isPressed(el)) | ||
|
|
||
| if (this.typeValue === "single") { | ||
| const val = pressed[0]?.dataset.value || "" | ||
| this.element.appendChild(this.buildInput(this.nameValue, val)) | ||
| } else { | ||
| pressed.forEach(item => { | ||
| this.element.appendChild(this.buildInput(`${this.nameValue}[]`, item.dataset.value)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| buildInput(name, value) { | ||
| const input = document.createElement("input") | ||
| input.type = "hidden" | ||
| input.name = name | ||
| input.value = value | ||
| input.setAttribute("data-ruby-ui--toggle-group-target", "input") | ||
| return input | ||
| } | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.