diff --git a/src/viewer/ui/Allocation.tsx b/src/viewer/ui/Allocation.tsx index 157b302..1ccab69 100644 --- a/src/viewer/ui/Allocation.tsx +++ b/src/viewer/ui/Allocation.tsx @@ -6,6 +6,7 @@ import { prefs, setPrefs } from '../app/prefs'; import { animate } from './animate'; import { Donut } from './Donut'; import { pct, usd } from './format'; +import { BarsIcon, PieIcon } from './icons'; import { Toggle } from './Toggle'; import type { HeadColumn } from './TypedHead'; import { useStore } from './useStore'; @@ -21,7 +22,7 @@ const viewOf = (views: Record, id: string): 'bars' | 'pi export function ViewSwitch({ id }: { id: string }) { const pr = useStore(prefs); return ( - , title: t('Show as donut') }} b={{ value: 'bars', text: , title: t('Show as bars') }} onChange={(v) => animate(() => setPrefs({ views: { ...prefs.get().views, [id]: v } }))} /> ); } diff --git a/src/viewer/ui/Toggle.test.tsx b/src/viewer/ui/Toggle.test.tsx new file mode 100644 index 0000000..982cac7 --- /dev/null +++ b/src/viewer/ui/Toggle.test.tsx @@ -0,0 +1,45 @@ +/// +/// +/// +// The two-way switch is built like a row of Pills (owner 2026-12-03: "same font size / high"): the same padding and text +// size, icons in place of glyphs, a click flips it +import { afterAll, beforeAll, expect, test } from 'bun:test'; +import { GlobalRegistrator } from '@happy-dom/global-registrator'; + +beforeAll(() => { + GlobalRegistrator.register({ url: 'http://localhost:1237/' }); + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; +}); +afterAll(() => GlobalRegistrator.unregister()); + +test('the switch has the track of the pills, draws icon choices and flips on a click', async () => { + const { act } = await import('react'); + const { createRoot } = await import('react-dom/client'); + const { Toggle } = await import('./Toggle'); + const { TRACK } = await import('./Pills'); + const { BarsIcon, PieIcon } = await import('./icons'); + document.body.innerHTML = '
'; + const root = createRoot(document.getElementById('root')!); + const seen: string[] = []; + await act(async () => root.render(, title: 'Show as donut' }} + b={{ value: 'bars', text: , title: 'Show as bars' }} onChange={(v) => seen.push(v)} />)); + const button = document.querySelector('[data-toggle]')!; + // no fixed height: as tall as a pill row, from the same padding (TRACK p-0.5, a segment py-0.5) and text size + expect(button.className).not.toContain('h-['); + for (const c of ['p-0.5', 'rounded-xl']) { + expect(TRACK.split(' ')).toContain(c); + expect(button.className.split(' ')).toContain(c); + } + expect(button.className.split(' ')).toContain('text-xs'); + const choices = button.querySelectorAll(':scope > span:not([aria-hidden])'); + expect(choices).toHaveLength(2); + choices.forEach((s) => { + expect(s.className.split(' ')).toContain('py-0.5'); + expect(s.querySelector('svg')).not.toBeNull(); + }); + expect(button.getAttribute('aria-checked')).toBe('false'); + expect(button.title).toBe('Show as bars'); + await act(async () => button.click()); + expect(seen).toEqual(['bars']); + await act(async () => root.unmount()); +}); diff --git a/src/viewer/ui/Toggle.tsx b/src/viewer/ui/Toggle.tsx index 27c2cda..75c9c2b 100644 --- a/src/viewer/ui/Toggle.tsx +++ b/src/viewer/ui/Toggle.tsx @@ -1,18 +1,20 @@ /// -export interface ToggleOption { value: T; text: string; title: string } +export interface ToggleOption { value: T; text: React.ReactNode; title: string } // One switch for two choices (owner 2026-11-24: "toggle here, not two buttons", only one view at a time): both choices // stay readable, the thumb slides under the one in use, a click anywhere flips it. The look of every choice of one -// (Pills.tsx TRACK, segmentClass: the shared track, a raised thumb), as tall as the card's ⤢ button, 26 px (owner -// 2026-12-01: "use this style to replace these circles with % and $", "height consistent with our zoom button") +// (Pills.tsx TRACK, segmentClass: the shared track, a raised thumb; owner 2026-12-01: "use this style to replace these +// circles with % and $"), as tall as a row of Pills, 24 px, with letters of their size (owner 2026-12-03: "same font +// size / high as we have here", the Accounts sort pills): the same padding and line, a glyph switch draws icons of the +// letters' height (ui/icons.tsx PieIcon, BarsIcon), a ◔ of the font was half as tall export function Toggle({ value, a, b, label, onChange }: { value: T; a: ToggleOption; b: ToggleOption; label: string; onChange: (v: T) => void }) { const second = value === b.value; - const text = (on: boolean) => `relative z-10 w-7 text-center font-medium transition-colors duration-200 ${on ? 'text-slate-900 dark:text-white' : 'text-slate-600 hover:text-slate-900 dark:text-slate-400 dark:hover:text-slate-100'}`; + const text = (on: boolean) => `relative z-10 grid w-7 place-items-center py-0.5 font-medium transition-colors duration-200 ${on ? 'text-slate-900 dark:text-white' : 'text-slate-600 hover:text-slate-900 dark:text-slate-400 dark:hover:text-slate-100'}`; return (