Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/viewer/ui/Allocation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -21,7 +22,7 @@ const viewOf = (views: Record<string, 'bars' | 'pie'>, id: string): 'bars' | 'pi
export function ViewSwitch({ id }: { id: string }) {
const pr = useStore(prefs);
return (
<Toggle value={viewOf(pr.views, id)} label={t('Chart view')} a={{ value: 'pie', text: '◔', title: t('Show as donut') }} b={{ value: 'bars', text: '☰', title: t('Show as bars') }}
<Toggle value={viewOf(pr.views, id)} label={t('Chart view')} a={{ value: 'pie', text: <PieIcon />, title: t('Show as donut') }} b={{ value: 'bars', text: <BarsIcon />, title: t('Show as bars') }}
onChange={(v) => animate(() => setPrefs({ views: { ...prefs.get().views, [id]: v } }))} />
);
}
Expand Down
45 changes: 45 additions & 0 deletions src/viewer/ui/Toggle.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/// <reference types="bun" />
/// <reference types="node" />
/// <reference lib="esnext" />
// 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 = '<div id="root"></div>';
const root = createRoot(document.getElementById('root')!);
const seen: string[] = [];
await act(async () => root.render(<Toggle value="pie" label="Chart view" a={{ value: 'pie', text: <PieIcon />, title: 'Show as donut' }}
b={{ value: 'bars', text: <BarsIcon />, title: 'Show as bars' }} onChange={(v) => seen.push(v)} />));
const button = document.querySelector<HTMLButtonElement>('[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());
});
12 changes: 7 additions & 5 deletions src/viewer/ui/Toggle.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
/// <reference lib="esnext" />

export interface ToggleOption<T extends string> { value: T; text: string; title: string }
export interface ToggleOption<T extends string> { 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<T extends string>({ value, a, b, label, onChange }: { value: T; a: ToggleOption<T>; b: ToggleOption<T>; 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 (
<button type="button" role="switch" aria-checked={second} aria-label={label} title={second ? a.title : b.title}
onClick={() => onChange(second ? a.value : b.value)} data-toggle={value}
className="relative inline-grid h-[26px] flex-none cursor-pointer grid-cols-2 items-center rounded-xl bg-slate-100 p-0.5 text-xs dark:bg-slate-800">
className="relative inline-grid flex-none cursor-pointer grid-cols-2 items-stretch rounded-xl bg-slate-100 p-0.5 text-xs dark:bg-slate-800">
<span aria-hidden className={`absolute inset-y-0.5 left-0.5 w-[calc(50%-0.125rem)] rounded-full bg-white shadow-sm transition-transform duration-200 ease-out motion-reduce:transition-none dark:bg-slate-600 ${second ? 'translate-x-full' : ''}`} />
<span className={text(!second)}>{a.text}</span>
<span className={text(second)}>{b.text}</span>
Expand Down
9 changes: 9 additions & 0 deletions src/viewer/ui/icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ export const ExpandIcon = () => svg(<><path d="M15 3h6v6" /><path d="M9 21H3v-6"
// ⤡: the arrows of ExpandIcon turned inwards, back from one chart to the ones before
export const ShrinkIcon = () => svg(<><path d="M4 14h6v6" /><path d="M20 10h-6V4" /><path d="M14 10l7-7" /><path d="M3 21l7-7" /></>);
export const ResetIcon = () => svg(<><path d="M3 12a9 9 0 1 0 2.6-6.4L3 8" /><path d="M3 3v5h5" /></>);
// Icons as tall as the letters of a pill (text-xs, owner 2026-12-03): a 16 px line box whose drawing is 10 px, the height
// of its digits, in the text colour; the donut or bars switch of an allocation card (ui/Allocation.tsx ViewSwitch)
const glyph = (children: React.ReactNode) => (
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" className="size-4" aria-hidden>{children}</svg>
);
/** a donut with its first quarter filled (◔) */
export const PieIcon = () => glyph(<><circle cx="8" cy="8" r="4.7" /><path d="M8 3.3V8h4.7A4.7 4.7 0 0 0 8 3.3Z" fill="currentColor" stroke="none" /></>);
/** three bars (☰) */
export const BarsIcon = () => glyph(<path d="M3.5 4h9M3.5 8h9M3.5 12h9" />);
export const CloseIcon = () => svg(<path d="M18 6 6 18M6 6l12 12" />);
/** settings sliders: a control that configures what a view shows (the positions columns) */
export const SlidersIcon = () => svg(<><path d="M4 6h10M18 6h2M4 12h4M12 12h8M4 18h12" /><circle cx="16" cy="6" r="2" /><circle cx="10" cy="12" r="2" /><circle cx="18" cy="18" r="2" /></>);
Expand Down