Repository navigation
fix(ui): align toggle switch handle symmetrically in on state - #9714
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughUpdated enabled-state switch thumb positioning across workspace, feature-wall, sidebar, terminal, settings, and usage components from 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
The switch handle (translate-x-4 / 16px) left a 6px gap on the right in the on state while the off state had only a 2px gap on the left. Accounting for border-box sizing (1px border each side reduces content width to 34px), the correct on-position is translate-x-4.5 (18px) so both sides have a 2px inset.
51bda1d to
c01ffaf
Compare
|
@coderabbitai review |
✅ Action performedReview finished.
|
The `h-5 w-9` switch track is `border-box` with a 1px border, leaving 34px of content for the 14px `size-3.5` handle. `translate-x-0.5` insets the off state by 2px, so the on state needs 34 - 14 - 2 = 18px. It used `translate-x-4` (16px), leaving 4px on the right against 2px on the left. Adds a boundary test so the offset cannot drift again across the 26 hand-rolled switch call sites, and drops the docstrings the original patch added to satisfy a coverage bot (the surrounding files carry none). Original patch by @sei0. Co-authored-by: Orca <[email protected]>
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 51c4e44a-58c1-42b3-b3fa-997681c5bfb9
📒 Files selected for processing (27)
src/renderer/src/components/NewWorkspaceComposerCard.tsxsrc/renderer/src/components/contextual-tours/ContextualTourControl.tsxsrc/renderer/src/components/feature-wall/AiCommitPrSettingsSwitch.tsxsrc/renderer/src/components/feature-wall/KeepAwakeCard.tsxsrc/renderer/src/components/settings/AccountsPane.tsxsrc/renderer/src/components/settings/AgentAwakeSetting.tsxsrc/renderer/src/components/settings/AutoRenameBranchFromWorkSetting.tsxsrc/renderer/src/components/settings/BrowserLinkRoutingSetting.tsxsrc/renderer/src/components/settings/BrowserUseEnableSwitch.tsxsrc/renderer/src/components/settings/CliSection.tsxsrc/renderer/src/components/settings/CommitMessageAiPane.tsxsrc/renderer/src/components/settings/ExperimentalPane.tsxsrc/renderer/src/components/settings/GitPane.tsxsrc/renderer/src/components/settings/InputPane.tsxsrc/renderer/src/components/settings/NotificationSettingToggle.tsxsrc/renderer/src/components/settings/PrivacyPane.tsxsrc/renderer/src/components/settings/SettingsFormControls.tsxsrc/renderer/src/components/settings/TerminalWindowSection.tsxsrc/renderer/src/components/settings/VoiceDictationSettingsSection.tsxsrc/renderer/src/components/settings/WslCliRegistration.tsxsrc/renderer/src/components/sidebar/HostRemoveDialog.tsxsrc/renderer/src/components/stats/ClaudeUsageLoadingState.tsxsrc/renderer/src/components/stats/ClaudeUsagePane.tsxsrc/renderer/src/components/stats/CodexUsagePane.tsxsrc/renderer/src/components/stats/OpenCodeUsagePane.tsxsrc/renderer/src/components/terminal-quick-commands/TerminalQuickCommandAppendEnterSwitch.tsxsrc/renderer/src/components/toggle-switch-handle-alignment.test.ts
💤 Files with no reviewable changes (26)
- src/renderer/src/components/terminal-quick-commands/TerminalQuickCommandAppendEnterSwitch.tsx
- src/renderer/src/components/feature-wall/KeepAwakeCard.tsx
- src/renderer/src/components/settings/InputPane.tsx
- src/renderer/src/components/settings/BrowserUseEnableSwitch.tsx
- src/renderer/src/components/settings/BrowserLinkRoutingSetting.tsx
- src/renderer/src/components/contextual-tours/ContextualTourControl.tsx
- src/renderer/src/components/feature-wall/AiCommitPrSettingsSwitch.tsx
- src/renderer/src/components/stats/CodexUsagePane.tsx
- src/renderer/src/components/stats/OpenCodeUsagePane.tsx
- src/renderer/src/components/settings/CommitMessageAiPane.tsx
- src/renderer/src/components/settings/AccountsPane.tsx
- src/renderer/src/components/stats/ClaudeUsageLoadingState.tsx
- src/renderer/src/components/settings/WslCliRegistration.tsx
- src/renderer/src/components/settings/AgentAwakeSetting.tsx
- src/renderer/src/components/settings/TerminalWindowSection.tsx
- src/renderer/src/components/settings/CliSection.tsx
- src/renderer/src/components/stats/ClaudeUsagePane.tsx
- src/renderer/src/components/settings/NotificationSettingToggle.tsx
- src/renderer/src/components/NewWorkspaceComposerCard.tsx
- src/renderer/src/components/settings/ExperimentalPane.tsx
- src/renderer/src/components/settings/VoiceDictationSettingsSection.tsx
- src/renderer/src/components/settings/GitPane.tsx
- src/renderer/src/components/settings/SettingsFormControls.tsx
- src/renderer/src/components/settings/AutoRenameBranchFromWorkSetting.tsx
- src/renderer/src/components/sidebar/HostRemoveDialog.tsx
- src/renderer/src/components/settings/PrivacyPane.tsx
Scan the renderer with the readdirSync walk and TypeScript AST already used by no-top-level-translate.test.ts instead of shelling out to `git grep`, and find each knob inside its own track rather than by scanning source text forward. Why: gating on `role="switch"` silently skipped ClaudeUsageLoadingState.tsx (a loading skeleton, so a div with no role) and HiddenExperimentalGroup.tsx, so regressing the loading-state handle back to translate-x-4 left the guard green. The guard now also fails when the premise behind 34 - 14 - 2 = 18px stops holding: a track that loses its 1px border or gains padding, a conditional whose two branches no longer pair off/on, and a track whose knob moved into a child component where the scan would otherwise just stop seeing it.
|
Thanks for this — the diagnosis and the arithmetic both hold up. Heads up that I've pushed one commit to your branch, so the extra commit isn't a surprise. I measured it in the running app rather than trusting the maths: the track renders 36px wide with a 1px border (a 34px content box) and the handle is 14px, so the off state sits 3px from the outer edge while the on state was landing 5px from it. With What I changed — the guard test only, none of your component fixes:
One for later: you're right in your notes that the real problem is these being hand-rolled at every call site. Extracting a shared switch primitive would make this class of bug impossible, and it genuinely doesn't belong in an alignment fix — worth doing on its own. |
There was a problem hiding this comment.
Actionable comments posted: 2
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 584b7361-e5ba-493b-8b4c-8245a52193b2
📒 Files selected for processing (1)
src/renderer/src/components/toggle-switch-handle-alignment.test.ts
| const BORDER_WIDTH = /^border(?:-[xytrbles])?-(?:\d+|\[)/ | ||
| const TRACK_PADDING = /^p[xytrbles]?-(?:\d|\[)/ |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Detect variant-prefixed geometry classes.
md:border-2, dark:px-1, and custom-variant equivalents do not match these anchored expressions. trackDrift() then accepts a track whose active variant changes the available content box and invalidates the fixed handle offset. Match geometry tokens after variant prefixes.
Proposed fix
-const BORDER_WIDTH = /^border(?:-[xytrbles])?-(?:\d+|\[)/
-const TRACK_PADDING = /^p[xytrbles]?-(?:\d|\[)/
+const BORDER_WIDTH = /(?:^|:)!?border(?:-[xytrbles])?-(?:\d+|\[)/
+const TRACK_PADDING = /(?:^|:)!?p[xytrbles]?-(?:\d|\[)/📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const BORDER_WIDTH = /^border(?:-[xytrbles])?-(?:\d+|\[)/ | |
| const TRACK_PADDING = /^p[xytrbles]?-(?:\d|\[)/ | |
| const BORDER_WIDTH = /(?:^|:)!?border(?:-[xytrbles])?-(?:\d+|\[)/ | |
| const TRACK_PADDING = /(?:^|:)!?p[xytrbles]?-(?:\d|\[)/ |
| for (const branches of offsetBranches(className)) { | ||
| scan.toggles.push({ at: at(node), branches }) | ||
| } |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Inspect JSX-level toggle branches.
offsetBranches() only sees conditionals inside a handle className. If a switch conditionally renders two <span> handles, each static handle passes the offset test, no Toggle is recorded, and line 216 does not validate the off/on pair. Associate handles with conditional JSX branches in the track subtree.
|
Thanks for the PR! |
Electron UI validation (post-merge) — toggle handle alignmentIsolated Identity (
Settings → General (live switches exercised via CDP):
Track: 36×20px, Animation: Logs: no switch/UI errors. Only pre-existing noise on this disposable profile: Verdict: LAND (already merged). Screenshots attached in the prior comment ( |
…ai#9714) * fix(ui): align toggle switch handle symmetrically in on state The switch handle (translate-x-4 / 16px) left a 6px gap on the right in the on state while the off state had only a 2px gap on the left. Accounting for border-box sizing (1px border each side reduces content width to 34px), the correct on-position is translate-x-4.5 (18px) so both sides have a 2px inset. * docs: add JSDoc to exported functions in changed files for docstring coverage * fix(ui): align toggle switch handle symmetrically in on state The `h-5 w-9` switch track is `border-box` with a 1px border, leaving 34px of content for the 14px `size-3.5` handle. `translate-x-0.5` insets the off state by 2px, so the on state needs 34 - 14 - 2 = 18px. It used `translate-x-4` (16px), leaving 4px on the right against 2px on the left. Adds a boundary test so the offset cannot drift again across the 26 hand-rolled switch call sites, and drops the docstrings the original patch added to satisfy a coverage bot (the surrounding files carry none). Original patch by @sei0. Co-authored-by: Orca <[email protected]> * test(ui): widen the switch-handle alignment guard to every h-5 w-9 track Scan the renderer with the readdirSync walk and TypeScript AST already used by no-top-level-translate.test.ts instead of shelling out to `git grep`, and find each knob inside its own track rather than by scanning source text forward. Why: gating on `role="switch"` silently skipped ClaudeUsageLoadingState.tsx (a loading skeleton, so a div with no role) and HiddenExperimentalGroup.tsx, so regressing the loading-state handle back to translate-x-4 left the guard green. The guard now also fails when the premise behind 34 - 14 - 2 = 18px stops holding: a track that loses its 1px border or gains padding, a conditional whose two branches no longer pair off/on, and a track whose knob moved into a child component where the scan would otherwise just stop seeing it. --------- Co-authored-by: Neil <[email protected]> Co-authored-by: Orca <[email protected]> Co-authored-by: Brennan Benson <[email protected]>







Summary
The toggle switch's handle was not centred symmetrically in the on state: it sat 4px from the right edge while the off state sat 2px from the left.
The arithmetic, from the real classes: the track is
h-5 w-9withbox-sizing: border-boxand a 1px border, so the content box is 34px wide. The handle issize-3.5= 14px.translate-x-0.5insets the off state by 2px. A symmetric on state therefore needs34 − 14 − 2 = 18px, i.e.translate-x-4.5. The code usedtranslate-x-4(16px), leaving a 4px right gap against a 2px left gap.ELI5
A toggle is a little pill with a dot that slides between two ends. When the dot was on the left it sat 2 pixels from the edge; when you flipped it to the right it stopped 4 pixels short. Nobody would consciously notice, but it made the switch look subtly lopsided in the "on" position. This moves the dot 2 pixels further so both ends match exactly.
Proof of fix
The switches in this codebase are hand-rolled, not instances of a shared
ui/switch.tsxprimitive — there is no such file. The off/on offsets are written literally at each of 26 call sites, which is exactly why they drifted. So the fix has to touch each site, and the durable protection is a test that scans them all.Added
src/renderer/src/components/toggle-switch-handle-alignment.test.ts. It walks the source for two-state handle offsets and asserts every pair is the symmetrictranslate-x-0.5/translate-x-4.5.Reverting a single call site is enough to fail it — this is the drift-detection proof, and it means no future switch can silently regress:
With all 26 sites corrected:
The geometry is stated in the description so a reviewer can check the arithmetic without running anything:
34 − 14 − 2 = 18px = translate-x-4.5.Proof of no regressions
User-visible behavior that changes:
All 26 changed files receive the identical one-token substitution (
translate-x-4→translate-x-4.5) on the handle span. No component's structure, props, or behavior changes.Compatibility
translate-x-4.5andtranslate-x-4compile to the sametransform: translateX(...)property with a different constant; identical layout/paint cost and no extra style recalculation. No new classes, elements, state, or effects.Screenshots
The change is a 2px shift, which is real but hard to convey honestly in a scaled screenshot — at normal zoom the before/after images look identical, and a zoomed crop invites the reader to trust a rendering I would have had to set up by hand. I chose to make the guarantee checkable instead of photographable: the arithmetic is written out above, and
toggle-switch-handle-alignment.test.tsenforces it across all 26 sites in CI, failing if any one drifts.I did not capture before/after images — stating that plainly rather than attaching something that would not actually demonstrate the fix.
Testing
pnpm lint— not run in full. Rannode config/scripts/check-styled-scrollbars.mjs(part of the lint suite, relevant to a class-only change): clean.pnpm typecheck—npx tsc --noEmit -p config/tsconfig.tc.web.json, exit 0.pnpm test— not run in full (30+ min). Ran the settings directory plus the new suite: 101 files, 649 tests passed.pnpm build— not run.AI Review Report
Reviewed with Claude Code (Opus 5) and independently re-verified. Risks checked:
src/renderer/src/components/ui/switch.tsx; each switch is hand-rolled with literal offsets. Per-site edits are therefore unavoidable, and the scanning test is what prevents the next drift — it is the part of this PR that carries long-term value.w-9(36px) − 2×1px border = 34px content box;size-3.5= 14px handle;translate-x-0.5= 2px inset ⇒ on state = 18px =translate-x-4.5. Off and on insets are now equal.translate-x-4.5a real token? Verified — Tailwind 4.2.4 supports.5spacing natively, andtranslate-x-4.5already appears onmaininSidebarFilter.tsxandSidebarWorkspaceFilterSection.tsx. No new token, value, or config entry is introduced, perdocs/STYLEGUIDE.md.h-5 w-9/size-3.5geometry, so there is no second size whose maths would differ. The change is colour-independent, so light and dark themes are unaffected.pointer-events-noneand absolutely positioned within its track; shifting it 2px cannot affect the surrounding layout in any container.Security Audit
AccountsPane.tsx, but only the handle-offset token is modified — no auth logic, state, or rendering condition is affected.Notes
ui/switch.tsxwould make this class of bug structurally impossible, but it is a much larger refactor with real regression surface and does not belong in an alignment fix. The scanning test is the interim guard.Original patch by @sei0.