Files
TREK/client/src/mobile/components/MToggle.tsx
T
Maurice 25091dbd60 fix(mobile): switches keep their knob inside the track at every text size
The track was w-10, which is rem and followed the text size setting, while
the knob and its travel are px; with a smaller text size the knob hung over
the end of the track. The track is 40 px now.
2026-11-06 21:57:10 +01:00

37 lines
1.3 KiB
TypeScript

import { selectionHaptic } from '../../native/haptics'
interface MToggleProps {
checked: boolean
onChange: (checked: boolean) => void
ariaLabel?: string
disabled?: boolean
className?: string
}
/** iOS-style switch: --m-act track when on, --m-trackoff when off, 18px knob. */
export default function MToggle({ checked, onChange, ariaLabel, disabled = false, className = '' }: MToggleProps) {
return (
<button
type="button"
role="switch"
aria-checked={checked}
aria-label={ariaLabel}
disabled={disabled}
onClick={() => {
selectionHaptic()
onChange(!checked)
}}
// Width in px like the knob and its travel: w-10 is rem and followed the text size,
// so a smaller text setting left the knob hanging over the end of the track.
className={`box-border h-[22px] w-[40px] flex-none rounded-full p-[2px] transition-colors duration-[180ms] disabled:opacity-50 ${
checked ? 'bg-m-act' : 'bg-[color:var(--m-trackoff)]'
} ${className}`}
>
<span
className={`block h-[18px] w-[18px] rounded-full bg-[color:var(--m-knob)] transition-transform duration-[180ms] ${
checked ? 'translate-x-[18px] rtl:-translate-x-[18px]' : 'translate-x-0'
}`}
/>
</button>
)
}