diff --git a/resources/boost/skills/saucebase-themes-development/SKILL.md b/resources/boost/skills/saucebase-themes-development/SKILL.md index 88472dd..e058cb4 100644 --- a/resources/boost/skills/saucebase-themes-development/SKILL.md +++ b/resources/boost/skills/saucebase-themes-development/SKILL.md @@ -147,9 +147,10 @@ documentElement inline styles ← set by applyThemeVars() when a theme is activ | `resources/js/lib/panel.ts` | `themeToJson` (panel state → payload, incl. the other mode's cached edits and both modes' shadow scales), `isPerMode`, `modeEdits`, mode sync helpers | | `resources/js/lib/color.ts` | Colour conversions, `colorToHsv`, `contrastingIconColor`, `swatchBackground` | | `resources/js/lib/tailwind.ts` | Tailwind palette for the colour picker, `filterPalette` | -| `resources/js/components/ThemePanel` | The editor | +| `resources/js/components/ThemePanel` | The editor; reads the page and sends every theme request (save, Save as, delete, Set as default). No stories: E2E covers it | | `resources/js/components/ThemePicker` | Theme switcher | -| `resources/js/components/DialogSave` | Save as dialog (same layout as the app confirm dialog) | +| `resources/js/components/DialogSave` | Save as dialog (same layout as the app confirm dialog): takes `saving`, emits `save` with the name; ThemePanel posts `themes.store` and closes it | +| `resources/js/{vue,react}/components/*.stories.{ts,tsx}` | Stories for the pickers, inputs and dialogs; they take props only, so they need no page | --- diff --git a/resources/js/react/components/ColorInput.stories.tsx b/resources/js/react/components/ColorInput.stories.tsx new file mode 100644 index 0000000..ecc11ba --- /dev/null +++ b/resources/js/react/components/ColorInput.stories.tsx @@ -0,0 +1,68 @@ +import preview from '@/.storybook/preview'; +import { TooltipProvider } from '@/components/ui/tooltip'; +import type { ComponentProps } from 'react'; +import { action } from 'storybook/actions'; +import { expect, fn, waitFor } from 'storybook/test'; +import ColorInput from './ColorInput'; + +/** + * A theme colour: a swatch that opens the colour picker, the colour's name, its value + * to type over, and a toggle that links it across light and dark mode. + */ +const meta = preview.meta({ + title: 'Themes/ColorInput', + component: ColorInput, + // The render passes the handlers: the cast says the args needn't. + args: { + label: 'Primary', + value: '#4f46e5', + synced: false, + } as ComponentProps, + // Changes show in the Actions panel; a story's own spy replaces them. + render: (args) => ( + + ), + // Its tooltip needs a provider, which the theme panel gives it. + decorators: [ + (Story) => ( + +
+ +
+
+ ), + ], +}); +export default meta; + +/** A colour, not linked across modes. */ +export const Default = meta.story({ + play: async ({ canvas }) => { + await expect(canvas.getByRole('textbox')).toHaveValue('#4f46e5'); + await expect( + canvas.getByRole('button', { name: 'Open Primary color picker' }), + ).toBeVisible(); + }, +}); + +/** Linked across light and dark mode. */ +export const Linked = meta.story({ args: { synced: true } }); + +/** Typing a value sets the colour. */ +export const Typing = meta.story({ + args: { onChange: fn() }, + play: async ({ args, canvas, userEvent }) => { + const field = canvas.getByRole('textbox'); + await userEvent.clear(field); + await userEvent.type(field, '#e11d48'); + await userEvent.tab(); + + await waitFor(() => + expect(args.onChange).toHaveBeenCalledWith('#e11d48'), + ); + }, +}); diff --git a/resources/js/react/components/ColorPickerPopover.stories.tsx b/resources/js/react/components/ColorPickerPopover.stories.tsx new file mode 100644 index 0000000..19b88c7 --- /dev/null +++ b/resources/js/react/components/ColorPickerPopover.stories.tsx @@ -0,0 +1,84 @@ +import preview from '@/.storybook/preview'; +import { Button } from '@/components/ui/button'; +import { type ComponentProps, useState } from 'react'; +import { expect, screen, waitFor } from 'storybook/test'; +import ColorPickerPopover from './ColorPickerPopover'; + +/** + * Picks a colour: drag over the gradient, move the hue, or type RGB or hex values; or + * pick one of Tailwind's in the second tab. Its children are the button that opens it. + */ +const meta = preview.meta({ + title: 'Themes/ColorPickerPopover', + component: ColorPickerPopover, + // The render passes the handlers: the cast says the args needn't. + args: { value: '#4f46e5' } as ComponentProps, + argTypes: { + // The story fills it itself: its control would do nothing. + children: { control: false }, + }, + // Its own value, so the button shows the colour picked. + render: function Render(args) { + const [value, setValue] = useState(args.value); + + return ( + + + + ); + }, +}); +export default meta; + +/** Closed until its button is clicked. Typing a hex value sets the colour. */ +export const Default = meta.story({ + play: async ({ canvas, userEvent }) => { + await userEvent.click(canvas.getByRole('button', { name: /#4f46e5/ })); + // The popover fades in: wait for it, not just for it to be in the page. + const picker = await screen.findByRole('slider', { + name: 'Saturation and brightness', + }); + await waitFor(() => expect(picker).toBeVisible()); + + await userEvent.click(screen.getByTitle('Switch to hex input')); + const hex = screen.getByRole('textbox', { name: 'Hex' }); + await userEvent.clear(hex); + await userEvent.type(hex, 'E11D48'); + + await userEvent.keyboard('{Escape}'); + + await waitFor(() => + expect( + canvas.getByRole('button', { name: /#e11d48/i }), + ).toBeVisible(), + ); + }, +}); + +/** Tailwind's colours. Picking one sets it and closes the picker. */ +export const TailwindColors = meta.story({ + play: async ({ canvas, userEvent }) => { + await userEvent.click(canvas.getByRole('button', { name: /#4f46e5/ })); + await userEvent.click( + await screen.findByRole('button', { name: 'Tailwind Colors' }), + ); + await userEvent.click( + screen.getByRole('button', { name: 'emerald-500' }), + ); + + await waitFor(() => + expect( + screen.queryByRole('button', { name: 'Tailwind Colors' }), + ).toBeNull(), + ); + await expect( + canvas.getByRole('button', { name: /oklch/ }), + ).toBeVisible(); + }, +}); diff --git a/resources/js/react/components/DialogCommand.stories.tsx b/resources/js/react/components/DialogCommand.stories.tsx new file mode 100644 index 0000000..93cbebd --- /dev/null +++ b/resources/js/react/components/DialogCommand.stories.tsx @@ -0,0 +1,52 @@ +import preview from '@/.storybook/preview'; +import { Button } from '@/components/ui/button'; +import { type ComponentProps, useState } from 'react'; +import { expect, screen, waitFor } from 'storybook/test'; +import DialogCommand from './DialogCommand'; + +/** The command that applies a theme to the project, to copy. */ +const meta = preview.meta({ + title: 'Themes/DialogCommand', + component: DialogCommand, + // The render passes the handlers: the cast says the args needn't. + args: { themeId: 'ocean', open: false } as ComponentProps< + typeof DialogCommand + >, + // A button opens it, as the theme panel's does. + render: function Render(args) { + const [open, setOpen] = useState(args.open); + + return ( + <> + + + + ); + }, +}); +export default meta; + +/** Shows the command for the chosen theme. */ +export const Default = meta.story({ + play: async ({ canvas, userEvent }) => { + await userEvent.click( + canvas.getByRole('button', { name: 'How to use this theme' }), + ); + const dialog = await screen.findByRole('dialog'); + await waitFor(() => expect(dialog).toBeVisible()); + await expect(screen.getByRole('textbox')).toHaveValue( + 'php artisan saucebase:theme:apply ocean', + ); + + await userEvent.click(screen.getByRole('button', { name: 'Close' })); + await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()); + }, +}); + +/** Open from the start. */ +export const Open = meta.story({ + args: { open: true }, + parameters: { docs: { story: { inline: false, iframeHeight: '320px' } } }, +}); diff --git a/resources/js/react/components/DialogSave.stories.tsx b/resources/js/react/components/DialogSave.stories.tsx new file mode 100644 index 0000000..c705f08 --- /dev/null +++ b/resources/js/react/components/DialogSave.stories.tsx @@ -0,0 +1,59 @@ +import preview from '@/.storybook/preview'; +import { Button } from '@/components/ui/button'; +import { type ComponentProps, useState } from 'react'; +import { expect, fn, screen, waitFor } from 'storybook/test'; +import DialogSave from './DialogSave'; + +/** + * Names a theme to save it. Save sends the name; whoever shows the dialog saves the + * theme and closes it. While `saving`, its buttons wait. + */ +const meta = preview.meta({ + title: 'Themes/DialogSave', + component: DialogSave, + // The render passes the handlers: the cast says the args needn't. + args: { saving: false, open: false } as ComponentProps, + // A button opens it, as the theme panel's does. + render: function Render(args) { + const [open, setOpen] = useState(args.open); + + return ( + <> + + + + ); + }, +}); +export default meta; + +/** Save waits for a name, then sends it. */ +export const Default = meta.story({ + args: { onSave: fn() }, + play: async ({ args, canvas, userEvent }) => { + await userEvent.click(canvas.getByRole('button', { name: 'Save as…' })); + const save = await screen.findByTestId('save-theme-submit'); + await expect(save).toBeDisabled(); + + await userEvent.type(screen.getByTestId('save-theme-name'), 'Ocean'); + await userEvent.click(save); + + await expect(args.onSave).toHaveBeenCalledWith('Ocean'); + await userEvent.click(screen.getByTestId('save-theme-cancel')); + await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()); + }, +}); + +/** Saving: the buttons wait. */ +export const Saving = meta.story({ + args: { open: true, saving: true }, + parameters: { docs: { story: { inline: false, iframeHeight: '360px' } } }, + play: async () => { + await expect( + await screen.findByTestId('save-theme-submit'), + ).toHaveTextContent('Saving...'); + await expect(screen.getByTestId('save-theme-cancel')).toBeDisabled(); + }, +}); diff --git a/resources/js/react/components/DialogSave.tsx b/resources/js/react/components/DialogSave.tsx index a93b16e..4af4853 100644 --- a/resources/js/react/components/DialogSave.tsx +++ b/resources/js/react/components/DialogSave.tsx @@ -7,71 +7,35 @@ import { DialogTitle, } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; -import { router } from '@inertiajs/react'; -import { useHttp } from '@saucebase/react/hooks/useHttp'; import { useT } from '@saucebase/react/i18n'; -import { useState } from 'react'; -import { toast } from 'sonner'; +import { useEffect, useState } from 'react'; import IconSave from '~icons/lucide/save'; -import type { ThemePayload } from '../../lib/panel'; -import type { Theme } from '../../types'; interface DialogSaveProps { open: boolean; onOpenChange: (open: boolean) => void; - toJson: (name: string) => ThemePayload; - /** Gets the reloaded theme list, which already includes the new theme. */ - onThemeSaved?: (id: string, themes: Theme[]) => void; + /** The theme is being saved: the buttons wait. */ + saving?: boolean; + /** Save, with the name. Whoever shows the dialog saves the theme and closes it. */ + onSave: (name: string) => void; } export default function DialogSave({ open, onOpenChange, - toJson, - onThemeSaved, + saving = false, + onSave, }: DialogSaveProps) { const t = useT(); - // The toast, plus re-enabling Save, which only this dialog knows about. - const http = useHttp({}, (message) => { - toast.error(t(message)); - setLoading(false); - }); const [name, setName] = useState(''); - const [loading, setLoading] = useState(false); - function close() { - onOpenChange(false); - setName(''); - } - - function handleSave() { - if (!name.trim() || loading) return; - const payload = toJson(name); + // A closed dialog starts over. + useEffect(() => { + if (!open) setName(''); + }, [open]); - setLoading(true); - http.transform(() => payload); - http.post(route('themes.store'), { - onSuccess() { - router.reload({ - only: ['themes'], - onSuccess: (reloaded) => { - onThemeSaved?.( - payload.name, - (reloaded.props.themes?.items as Theme[]) ?? [], - ); - close(); - toast.success(t('Theme saved successfully'), { - testId: 'theme-saved-toast', - }); - setLoading(false); - }, - }); - }, - onError(errors: Record) { - toast.error(t(errors?.name ?? 'Failed to save theme')); - setLoading(false); - }, - }); + function save() { + if (name.trim() && !saving) onSave(name); } return ( @@ -100,9 +64,9 @@ export default function DialogSave({ className="mt-4" data-testid="save-theme-name" placeholder={t('Theme name')} - disabled={loading} + disabled={saving} onChange={(e) => setName(e.target.value)} - onKeyDown={(e) => e.key === 'Enter' && handleSave()} + onKeyDown={(e) => e.key === 'Enter' && save()} />
@@ -111,18 +75,18 @@ export default function DialogSave({ variant="outline" className="dark:hover:bg-accent dark:hover:text-accent-foreground w-full" data-testid="save-theme-cancel" - disabled={loading} - onClick={close} + disabled={saving} + onClick={() => onOpenChange(false)} > {t('Cancel')}
diff --git a/resources/js/react/components/FontPicker.stories.tsx b/resources/js/react/components/FontPicker.stories.tsx new file mode 100644 index 0000000..25d435f --- /dev/null +++ b/resources/js/react/components/FontPicker.stories.tsx @@ -0,0 +1,88 @@ +import preview from '@/.storybook/preview'; +import { type ComponentProps, useState } from 'react'; +import { expect, screen, waitFor } from 'storybook/test'; +import FontPicker from './FontPicker'; + +const fonts = [ + { + family: 'Inter', + category: 'sans-serif', + variants: ['400', '700'], + variable: true, + }, + { + family: 'Nunito', + category: 'sans-serif', + variants: ['400', '700'], + variable: true, + }, + { family: 'Lora', category: 'serif', variants: ['400', '700'] }, + { + family: 'JetBrains Mono', + category: 'monospace', + variants: ['400'], + variable: true, + }, +]; + +/** + * Picks a theme's font from a list with a search. A font set elsewhere that isn't in the + * list still shows as chosen; with none, it says Default. + */ +const meta = preview.meta({ + title: 'Themes/FontPicker', + component: FontPicker, + // The render passes the handlers: the cast says the args needn't. + args: { fonts, value: 'Inter' } as ComponentProps, + decorators: [ + (Story) => ( +
+ +
+ ), + ], + // Its own value, so the button shows the font picked. + render: function Render(args) { + const [value, setValue] = useState(args.value); + + return ; + }, +}); +export default meta; + +/** A font chosen. A search narrows the list; picking a font sets it. */ +export const Default = meta.story({ + play: async ({ canvas, userEvent }) => { + await userEvent.click(canvas.getByRole('button', { name: /Inter/ })); + await userEvent.type( + await screen.findByRole('textbox', { name: 'Search fonts...' }), + 'lora', + ); + await userEvent.click(screen.getByRole('button', { name: /Lora/ })); + + await waitFor(() => + expect(canvas.getByRole('button', { name: /Lora/ })).toBeVisible(), + ); + await userEvent.keyboard('{Escape}'); + }, +}); + +/** No font chosen: the theme's default. */ +export const NoneChosen = meta.story({ + args: { value: '' }, + play: async ({ canvas }) => { + await expect( + canvas.getByRole('button', { name: /Default/ }), + ).toBeVisible(); + }, +}); + +/** A font that isn't in the list, such as a system font: it still shows as chosen. */ +export const NotInTheList = meta.story({ + args: { value: 'Helvetica Neue' }, + play: async ({ canvas }) => { + await expect( + canvas.getByRole('button', { name: /Helvetica Neue/ }), + ).toBeVisible(); + }, +}); diff --git a/resources/js/react/components/LinkToggle.stories.tsx b/resources/js/react/components/LinkToggle.stories.tsx new file mode 100644 index 0000000..6fc7485 --- /dev/null +++ b/resources/js/react/components/LinkToggle.stories.tsx @@ -0,0 +1,70 @@ +import preview from '@/.storybook/preview'; +import { TooltipProvider } from '@/components/ui/tooltip'; +import { type ComponentProps, useState } from 'react'; +import { expect } from 'storybook/test'; +import LinkToggle from './LinkToggle'; + +/** + * Links a setting across light and dark mode: locked when linked, open when not, and a + * dash when only some of a group's settings are linked. A click links or unlinks; the + * tooltip says which. + */ +const meta = preview.meta({ + title: 'Themes/LinkToggle', + component: LinkToggle, + // The render passes the handlers: the cast says the args needn't. + args: { + value: false, + tooltip: 'Link across light/dark modes', + tooltipActive: 'Synced across modes — click to unlink', + tooltipIndeterminate: 'Some colours are linked', + } as ComponentProps, + // Its tooltip needs a provider, which the theme panel gives it. + decorators: [ + (Story) => ( + + + + ), + ], + // Its own value, so a click shows the next state. + render: function Render(args) { + const [value, setValue] = useState(args.value); + + return ; + }, +}); +export default meta; + +/** Not linked. A click links it. */ +export const Unlinked = meta.story({ + play: async ({ canvas, userEvent }) => { + const toggle = canvas.getByRole('button', { + name: 'Link across light/dark modes', + }); + await expect(toggle).toHaveAttribute('aria-pressed', 'false'); + // A round button as big as its icon, wherever it is placed. + await expect(toggle.getBoundingClientRect().width).toBeLessThan(40); + + await userEvent.click(toggle); + + await expect( + canvas.getByRole('button', { + name: 'Synced across modes — click to unlink', + }), + ).toHaveAttribute('aria-pressed', 'true'); + }, +}); + +/** Linked. */ +export const Linked = meta.story({ args: { value: true } }); + +/** Some of a group's settings are linked. A click links them all. */ +export const Partial = meta.story({ + args: { value: null }, + play: async ({ canvas }) => { + await expect( + canvas.getByRole('button', { name: 'Some colours are linked' }), + ).toHaveAttribute('aria-pressed', 'mixed'); + }, +}); diff --git a/resources/js/react/components/LinkToggle.tsx b/resources/js/react/components/LinkToggle.tsx index 6d56736..0250996 100644 --- a/resources/js/react/components/LinkToggle.tsx +++ b/resources/js/react/components/LinkToggle.tsx @@ -58,7 +58,7 @@ export default function LinkToggle({ tabIndex={0} aria-label={label} aria-pressed={value === null ? 'mixed' : value} - className={`focus-visible:ring-ring flex cursor-pointer items-center rounded-full p-1.5 text-xs font-medium shadow-sm transition-colors focus-visible:ring-2 focus-visible:outline-none ${stateClass} ${className}`} + className={`focus-visible:ring-ring inline-flex cursor-pointer items-center rounded-full p-1.5 text-xs font-medium shadow-sm transition-colors focus-visible:ring-2 focus-visible:outline-none ${stateClass} ${className}`} onClick={toggle} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') toggle(e); diff --git a/resources/js/react/components/SliderInput.stories.tsx b/resources/js/react/components/SliderInput.stories.tsx new file mode 100644 index 0000000..776b9d3 --- /dev/null +++ b/resources/js/react/components/SliderInput.stories.tsx @@ -0,0 +1,68 @@ +import preview from '@/.storybook/preview'; +import { type ComponentProps, useState } from 'react'; +import { expect, waitFor } from 'storybook/test'; +import SliderInput from './SliderInput'; + +/** + * A number set with a slider or typed in, for a theme's sizes and amounts. The slider + * and the field stay in step; `unit` labels the field. + */ +const meta = preview.meta({ + title: 'Themes/SliderInput', + component: SliderInput, + // The render passes the handlers: the cast says the args needn't. + args: { + label: 'Radius', + unit: 'rem', + min: 0, + max: 2, + step: 0.125, + value: '0.625', + } as ComponentProps, + decorators: [ + (Story) => ( +
+ +
+ ), + ], + // Its own value, so the slider and the field can be moved. + render: function Render(args) { + const [value, setValue] = useState(args.value); + + return ; + }, +}); +export default meta; + +/** A size in rem. The field's buttons step it, and the slider follows. */ +export const Default = meta.story({ + play: async ({ canvas, userEvent }) => { + const field = canvas.getByRole('spinbutton'); + await expect(field).toHaveValue(0.625); + + await userEvent.click( + canvas.getByRole('button', { name: /increase/i }), + ); + + await waitFor(() => expect(field).toHaveValue(0.75)); + await expect(canvas.getByRole('slider')).toHaveAttribute( + 'aria-valuenow', + '0.75', + ); + }, +}); + +/** An amount from 0 to 1, without a unit. */ +export const Amount = meta.story({ + args: { + label: 'Opacity', + unit: undefined, + max: 1, + step: 0.01, + value: '0.1', + }, + play: async ({ canvas }) => { + await expect(canvas.queryByText('rem')).toBeNull(); + }, +}); diff --git a/resources/js/react/components/TailwindColorPicker.stories.tsx b/resources/js/react/components/TailwindColorPicker.stories.tsx new file mode 100644 index 0000000..30f4508 --- /dev/null +++ b/resources/js/react/components/TailwindColorPicker.stories.tsx @@ -0,0 +1,75 @@ +import preview from '@/.storybook/preview'; +import type { ComponentProps } from 'react'; +import { action } from 'storybook/actions'; +import { expect, fn } from 'storybook/test'; +import TailwindColorPicker from './TailwindColorPicker'; + +/** + * Tailwind's colours to pick from, in a list or a grid, with a search. Picking one sends + * its value. ColorPickerPopover shows it in its second tab. + */ +const meta = preview.meta({ + title: 'Themes/TailwindColorPicker', + component: TailwindColorPicker, + // The render passes the handler: the cast says the args needn't. + args: {} as ComponentProps, + // A pick shows in the Actions panel; a story's own spy replaces it. + render: (args) => ( + + ), + decorators: [ + (Story) => ( +
+ +
+ ), + ], +}); +export default meta; + +/** The list. Picking a colour sends its value. */ +export const Default = meta.story({ + args: { onSelect: fn() }, + play: async ({ args, canvas, userEvent }) => { + await userEvent.click(canvas.getByRole('button', { name: 'red-500' })); + + await expect(args.onSelect).toHaveBeenCalledOnce(); + }, +}); + +/** The grid: more colours at once, named on hover. */ +export const Grid = meta.story({ + play: async ({ canvas, userEvent }) => { + await userEvent.click( + canvas.getByRole('button', { name: 'Grid view' }), + ); + + await expect(canvas.getByTitle('red-500')).toBeVisible(); + }, +}); + +/** A search narrows the colours to a family or shade. */ +export const Searching = meta.story({ + play: async ({ canvas, userEvent }) => { + await userEvent.type(canvas.getByRole('textbox'), 'emerald'); + + await expect( + canvas.getByRole('button', { name: 'emerald-500' }), + ).toBeVisible(); + await expect( + canvas.queryByRole('button', { name: 'red-500' }), + ).toBeNull(); + }, +}); + +/** Nothing matches. */ +export const NoResults = meta.story({ + play: async ({ canvas, userEvent }) => { + await userEvent.type(canvas.getByRole('textbox'), 'chartreuse'); + + await expect(canvas.getByText('No colors found')).toBeVisible(); + }, +}); diff --git a/resources/js/react/components/ThemeColorSwatch.stories.tsx b/resources/js/react/components/ThemeColorSwatch.stories.tsx new file mode 100644 index 0000000..d4eba38 --- /dev/null +++ b/resources/js/react/components/ThemeColorSwatch.stories.tsx @@ -0,0 +1,59 @@ +import preview from '@/.storybook/preview'; +import { expect } from 'storybook/test'; +import ThemeColorSwatch from './ThemeColorSwatch'; + +/** + * A theme at a glance: its background, primary and secondary colours, and text. The + * corners follow the theme's radius. Theme pickers show one per theme. + */ +const meta = preview.meta({ + title: 'Themes/ThemeColorSwatch', + component: ThemeColorSwatch, + args: { + preview: { + background: '#ffffff', + primary: '#4f46e5', + secondary: '#f1f5f9', + foreground: '#0f172a', + }, + radius: '0.625rem', + }, +}); +export default meta; + +/** A light theme with rounded corners. */ +export const Default = meta.story({ + play: async ({ canvas }) => { + await expect(canvas.getByText('A')).toBeVisible(); + await expect(canvas.getByText('a')).toBeVisible(); + // A swatch is as wide as its four marks, wherever it is placed. + const swatch = canvas.getByText('A').parentElement!; + await expect(swatch.getBoundingClientRect().width).toBeLessThan(40); + }, +}); + +/** A dark theme. */ +export const Dark = meta.story({ + args: { + preview: { + background: '#0f172a', + primary: '#f59e0b', + secondary: '#1e293b', + foreground: '#f8fafc', + }, + }, +}); + +/** A theme with square corners. */ +export const Square = meta.story({ args: { radius: '0rem' } }); + +/** Without colours of its own: the current theme's. */ +export const CurrentTheme = meta.story({ + args: { preview: {}, radius: undefined }, + play: async ({ canvas }) => { + const swatch = canvas.getByText('A').parentElement!; + await expect(getComputedStyle(swatch).backgroundColor).not.toBe( + 'rgba(0, 0, 0, 0)', + ); + }, +}); diff --git a/resources/js/react/components/ThemeColorSwatch.tsx b/resources/js/react/components/ThemeColorSwatch.tsx index 05b14c3..d05285e 100644 --- a/resources/js/react/components/ThemeColorSwatch.tsx +++ b/resources/js/react/components/ThemeColorSwatch.tsx @@ -21,9 +21,9 @@ export default function ThemeColorSwatch({ return ( diff --git a/resources/js/react/components/ThemePanel.tsx b/resources/js/react/components/ThemePanel.tsx index 6ef126c..ff0a5a1 100644 --- a/resources/js/react/components/ThemePanel.tsx +++ b/resources/js/react/components/ThemePanel.tsx @@ -493,6 +493,45 @@ export default function ThemePanel() { }); } + // ── Save as (a new user theme) ────────────────────────────────────────── + + const [savingAs, setSavingAs] = useState(false); + // Its own request, so a failure re-enables the dialog's Save. + const saveAsHttp = useHttp({}, (message) => { + toast.error(t(message)); + setSavingAs(false); + }); + + function saveAs(name: string): void { + const payload = toJson(name); + + setSavingAs(true); + saveAsHttp.transform(() => payload); + saveAsHttp.post(route('themes.store'), { + onSuccess() { + router.reload({ + only: ['themes'], + onSuccess: (reloaded) => { + void selectTheme( + payload.name, + null, + (reloaded.props.themes?.items as Theme[]) ?? [], + ); + setSaveOpen(false); + toast.success(t('Theme saved successfully'), { + testId: 'theme-saved-toast', + }); + setSavingAs(false); + }, + }); + }, + onError(errors: Record) { + toast.error(t(errors?.name ?? 'Failed to save theme')); + setSavingAs(false); + }, + }); + } + async function remove(): Promise { const theme = findTheme(m.selectedId); if (!theme?.editable) return; @@ -890,10 +929,8 @@ export default function ThemePanel() { - void selectTheme(id, null, items) - } + saving={savingAs} + onSave={saveAs} /> diff --git a/resources/js/react/components/ThemePicker.stories.tsx b/resources/js/react/components/ThemePicker.stories.tsx new file mode 100644 index 0000000..939e0de --- /dev/null +++ b/resources/js/react/components/ThemePicker.stories.tsx @@ -0,0 +1,105 @@ +import preview from '@/.storybook/preview'; +import { type ComponentProps, useState } from 'react'; +import { expect, fn, screen, waitFor } from 'storybook/test'; +import type { Theme } from '../../types'; +import ThemePicker from './ThemePicker'; + +const theme = ( + id: string, + name: string, + primary: string, + radius = '0.625rem', +) => ({ + id, + name, + description: '', + editable: false, + light: { + '--background': '#ffffff', + '--primary': primary, + '--secondary': '#f1f5f9', + '--foreground': '#0f172a', + '--radius': radius, + }, + dark: { + '--background': '#0f172a', + '--primary': primary, + '--secondary': '#1e293b', + '--foreground': '#f8fafc', + '--radius': radius, + }, +}); + +const options: Theme[] = [ + theme('default', 'Default', '#4f46e5'), + theme('ocean', 'Ocean', '#0284c7'), + theme('forest', 'Forest', '#15803d', '0.25rem'), + theme('sunset', 'Sunset', '#ea580c', '1rem'), +]; + +/** + * Picks a theme: each shows its colours and corners in the current mode. A search + * narrows the list. Picking one sends `onPicked` with where it was clicked, so the + * theme can spread from there. + */ +const meta = preview.meta({ + title: 'Themes/ThemePicker', + component: ThemePicker, + // The render passes the handlers: the cast says the args needn't. + args: { options, value: 'default' } as ComponentProps, + decorators: [ + (Story) => ( +
+ +
+ ), + ], + // Its own value, so the button shows the theme picked. + render: function Render(args) { + const [value, setValue] = useState(args.value); + + return ( + { + setValue(id); + args.onPicked?.(id, origin); + }} + /> + ); + }, +}); +export default meta; + +/** Picking a theme sets it and closes the list. */ +export const Default = meta.story({ + args: { onPicked: fn() }, + play: async ({ args, canvas, userEvent }) => { + await userEvent.click(canvas.getByTestId('theme-picker-trigger')); + await userEvent.click(await screen.findByTestId('theme-option-ocean')); + + await expect(args.onPicked).toHaveBeenCalledWith( + 'ocean', + expect.anything(), + ); + await waitFor(() => + expect( + canvas.getByTestId('theme-picker-trigger'), + ).toHaveTextContent('Ocean'), + ); + }, +}); + +/** A search with no match. */ +export const NoResults = meta.story({ + play: async ({ canvas, userEvent }) => { + await userEvent.click(canvas.getByTestId('theme-picker-trigger')); + await userEvent.type(await screen.findByTestId('theme-search'), 'neon'); + + // The list fades in: wait for it, not just for it to be in the page. + const empty = await screen.findByText('No themes found'); + await waitFor(() => expect(empty).toBeVisible()); + await userEvent.keyboard('{Escape}'); + }, +}); diff --git a/resources/js/vue/components/ColorInput.stories.ts b/resources/js/vue/components/ColorInput.stories.ts new file mode 100644 index 0000000..15997b2 --- /dev/null +++ b/resources/js/vue/components/ColorInput.stories.ts @@ -0,0 +1,59 @@ +import preview from '@/.storybook/preview'; +import { TooltipProvider } from '@/components/ui/tooltip'; +import { expect, fn, waitFor } from 'storybook/test'; +import ColorInput from './ColorInput.vue'; + +/** + * A theme colour: a swatch that opens the colour picker, the colour's name, its value + * to type over, and a toggle that links it across light and dark mode. + */ +const meta = preview.meta({ + title: 'Themes/ColorInput', + component: ColorInput, + args: { label: 'Primary', modelValue: '#4f46e5', synced: false }, + // Its tooltip needs a provider, which the theme panel gives it. + decorators: [ + () => ({ + components: { TooltipProvider }, + template: + '
', + }), + ], +}); +export default meta; + +/** A colour, not linked across modes. */ +export const Default = meta.story({ + play: async ({ canvas }) => { + await expect(canvas.getByRole('textbox')).toHaveValue('#4f46e5'); + await expect( + canvas.getByRole('button', { name: 'Open Primary color picker' }), + ).toBeVisible(); + }, +}); + +/** Linked across light and dark mode. */ +export const Linked = meta.story({ args: { synced: true } }); + +/** A value shown in another form than the one it holds, such as a Tailwind name. */ +export const WithDisplayText = meta.story({ + args: { + modelValue: 'oklch(58.5% 0.233 277.117)', + displayText: 'indigo-500', + }, +}); + +/** Typing a value sets the colour. */ +export const Typing = meta.story({ + args: { 'onUpdate:modelValue': fn() }, + play: async ({ args, canvas, userEvent }) => { + const field = canvas.getByRole('textbox'); + await userEvent.clear(field); + await userEvent.type(field, '#e11d48'); + await userEvent.tab(); + + await waitFor(() => + expect(args['onUpdate:modelValue']).toHaveBeenCalledWith('#e11d48'), + ); + }, +}); diff --git a/resources/js/vue/components/ColorPickerPopover.stories.ts b/resources/js/vue/components/ColorPickerPopover.stories.ts new file mode 100644 index 0000000..351c554 --- /dev/null +++ b/resources/js/vue/components/ColorPickerPopover.stories.ts @@ -0,0 +1,79 @@ +import preview from '@/.storybook/preview'; +import { Button } from '@/components/ui/button'; +import { expect, screen, waitFor } from 'storybook/test'; +import { ref } from 'vue'; +import ColorPickerPopover from './ColorPickerPopover.vue'; + +/** + * Picks a colour: drag over the gradient, move the hue, or type RGB or hex values; or + * pick one of Tailwind's in the second tab. Its default slot is the button that opens it. + */ +const meta = preview.meta({ + title: 'Themes/ColorPickerPopover', + component: ColorPickerPopover, + args: { modelValue: '#4f46e5' }, + argTypes: { + // The story fills it itself: its control would do nothing. + default: { control: false }, + }, + // Its own value, so the button shows the colour picked. + render: (args) => ({ + components: { Button, ColorPickerPopover }, + setup: () => ({ value: ref(args.modelValue) }), + template: ` + + + `, + }), +}); +export default meta; + +/** Closed until its button is clicked. Typing a hex value sets the colour. */ +export const Default = meta.story({ + play: async ({ canvas, userEvent }) => { + await userEvent.click(canvas.getByRole('button', { name: /#4f46e5/ })); + // The popover fades in: wait for it, not just for it to be in the page. + const picker = await screen.findByRole('slider', { + name: 'Saturation and brightness', + }); + await waitFor(() => expect(picker).toBeVisible()); + + await userEvent.click(screen.getByTitle('Switch to hex input')); + const hex = screen.getByRole('textbox', { name: 'Hex' }); + await userEvent.clear(hex); + await userEvent.type(hex, 'E11D48'); + + await userEvent.keyboard('{Escape}'); + + await waitFor(() => + expect( + canvas.getByRole('button', { name: /#e11d48/i }), + ).toBeVisible(), + ); + }, +}); + +/** Tailwind's colours. Picking one sets it and closes the picker. */ +export const TailwindColors = meta.story({ + play: async ({ canvas, userEvent }) => { + await userEvent.click(canvas.getByRole('button', { name: /#4f46e5/ })); + await userEvent.click( + await screen.findByRole('button', { name: 'Tailwind Colors' }), + ); + await userEvent.click( + screen.getByRole('button', { name: 'emerald-500' }), + ); + + await waitFor(() => + expect( + screen.queryByRole('button', { name: 'Tailwind Colors' }), + ).toBeNull(), + ); + await expect( + canvas.getByRole('button', { name: /oklch/ }), + ).toBeVisible(); + }, +}); diff --git a/resources/js/vue/components/DialogCommand.stories.ts b/resources/js/vue/components/DialogCommand.stories.ts new file mode 100644 index 0000000..5c8b904 --- /dev/null +++ b/resources/js/vue/components/DialogCommand.stories.ts @@ -0,0 +1,44 @@ +import preview from '@/.storybook/preview'; +import { Button } from '@/components/ui/button'; +import { expect, screen, waitFor } from 'storybook/test'; +import { ref } from 'vue'; +import DialogCommand from './DialogCommand.vue'; + +/** The command that applies a theme to the project, to copy. */ +const meta = preview.meta({ + title: 'Themes/DialogCommand', + component: DialogCommand, + args: { themeId: 'ocean' }, + // A button opens it, as the theme panel's does. + render: (args) => ({ + components: { Button, DialogCommand }, + setup: () => ({ args, open: ref(args.modelValue ?? false) }), + template: ` + + `, + }), +}); +export default meta; + +/** Shows the command for the chosen theme. */ +export const Default = meta.story({ + play: async ({ canvas, userEvent }) => { + await userEvent.click( + canvas.getByRole('button', { name: 'How to use this theme' }), + ); + const dialog = await screen.findByRole('dialog'); + await waitFor(() => expect(dialog).toBeVisible()); + await expect(screen.getByRole('textbox')).toHaveValue( + 'php artisan saucebase:theme:apply ocean', + ); + + await userEvent.click(screen.getByRole('button', { name: 'Close' })); + await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()); + }, +}); + +/** Open from the start. */ +export const Open = meta.story({ + args: { modelValue: true }, + parameters: { docs: { story: { inline: false, iframeHeight: '320px' } } }, +}); diff --git a/resources/js/vue/components/DialogSave.stories.ts b/resources/js/vue/components/DialogSave.stories.ts new file mode 100644 index 0000000..0876a4d --- /dev/null +++ b/resources/js/vue/components/DialogSave.stories.ts @@ -0,0 +1,53 @@ +import preview from '@/.storybook/preview'; +import { Button } from '@/components/ui/button'; +import { expect, fn, screen, waitFor } from 'storybook/test'; +import { ref } from 'vue'; +import DialogSave from './DialogSave.vue'; + +/** + * Names a theme to save it. Save sends the name; whoever shows the dialog saves the + * theme and closes it. While `saving`, its buttons wait. + */ +const meta = preview.meta({ + title: 'Themes/DialogSave', + component: DialogSave, + args: { saving: false }, + // A button opens it, as the theme panel's does. + render: (args) => ({ + components: { Button, DialogSave }, + setup: () => ({ args, open: ref(args.modelValue ?? false) }), + template: ` + + `, + }), +}); +export default meta; + +/** Save waits for a name, then sends it. */ +export const Default = meta.story({ + args: { onSave: fn() }, + play: async ({ args, canvas, userEvent }) => { + await userEvent.click(canvas.getByRole('button', { name: 'Save as…' })); + const save = await screen.findByTestId('save-theme-submit'); + await expect(save).toBeDisabled(); + + await userEvent.type(screen.getByTestId('save-theme-name'), 'Ocean'); + await userEvent.click(save); + + await expect(args.onSave).toHaveBeenCalledWith('Ocean'); + await userEvent.click(screen.getByTestId('save-theme-cancel')); + await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()); + }, +}); + +/** Saving: the buttons wait. */ +export const Saving = meta.story({ + args: { modelValue: true, saving: true }, + parameters: { docs: { story: { inline: false, iframeHeight: '360px' } } }, + play: async () => { + await expect( + await screen.findByTestId('save-theme-submit'), + ).toHaveTextContent('Saving...'); + await expect(screen.getByTestId('save-theme-cancel')).toBeDisabled(); + }, +}); diff --git a/resources/js/vue/components/DialogSave.vue b/resources/js/vue/components/DialogSave.vue index 880a2c3..47e1f73 100644 --- a/resources/js/vue/components/DialogSave.vue +++ b/resources/js/vue/components/DialogSave.vue @@ -8,70 +8,31 @@ import { DialogTitle, } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; -import { useHttp } from '@saucebase/vue/composables/useHttp'; -import { router } from '@inertiajs/vue3'; -import { trans } from 'laravel-vue-i18n'; -import { computed, ref } from 'vue'; -import { toast } from 'vue-sonner'; +import { ref, watch } from 'vue'; import IconSave from '~icons/lucide/save'; const open = defineModel({ default: false }); -const props = defineProps<{ - toJson: (name: string) => { - name: string; - title: string; - description: string; - cssVars: { - theme?: Record; - light: Record; - dark: Record; - }; - }; - onThemeSaved?: (id: string) => void; +defineProps<{ + /** The theme is being saved: the buttons wait. */ + saving?: boolean; }>(); -const name = ref(''); -const loading = ref(false); -const isLoading = computed(() => loading.value); +const emit = defineEmits<{ + /** Save, with the name. Whoever shows the dialog saves the theme and closes it. */ + save: [name: string]; +}>(); -const handleCancel = () => { - open.value = false; - name.value = ''; -}; +const name = ref(''); -const handleSave = async () => { - const payload = props.toJson(name.value); - const http = useHttp(payload).withAllErrors(); +// A closed dialog starts over. +watch(open, (isOpen) => { + if (!isOpen) name.value = ''; +}); - http.post(route('themes.store'), { - onBefore() { - loading.value = true; - }, - onSuccess() { - router.reload({ - only: ['themes'], - onSuccess: () => { - props.onThemeSaved?.(payload.name); - open.value = false; - name.value = ''; - toast.success(trans('Theme saved successfully'), { - testId: 'theme-saved-toast', - }); - loading.value = false; - }, - }); - }, - onError: (errors) => { - if (errors?.name) toast.error(trans(errors.name)); - else toast.error(trans('Failed to save theme')); - return true; - }, - onFinish: () => { - loading.value = false; - }, - }); -}; +function save() { + if (name.value.trim()) emit('save', name.value); +} diff --git a/resources/js/vue/components/ThemePicker.stories.ts b/resources/js/vue/components/ThemePicker.stories.ts new file mode 100644 index 0000000..ea09cba --- /dev/null +++ b/resources/js/vue/components/ThemePicker.stories.ts @@ -0,0 +1,88 @@ +import preview from '@/.storybook/preview'; +import { expect, fn, screen, waitFor } from 'storybook/test'; +import { ref } from 'vue'; +import ThemePicker from './ThemePicker.vue'; + +const theme = ( + id: string, + name: string, + primary: string, + radius = '0.625rem', +) => ({ + id, + name, + description: '', + editable: false, + light: { + '--background': '#ffffff', + '--primary': primary, + '--secondary': '#f1f5f9', + '--foreground': '#0f172a', + '--radius': radius, + }, + dark: { + '--background': '#0f172a', + '--primary': primary, + '--secondary': '#1e293b', + '--foreground': '#f8fafc', + '--radius': radius, + }, +}); + +const options = [ + theme('default', 'Default', '#4f46e5'), + theme('ocean', 'Ocean', '#0284c7'), + theme('forest', 'Forest', '#15803d', '0.25rem'), + theme('sunset', 'Sunset', '#ea580c', '1rem'), +]; + +/** + * Picks a theme: each shows its colours and corners in the current mode. A search + * narrows the list. Picking one sends `picked` with where it was clicked, so the + * theme can spread from there. + */ +const meta = preview.meta({ + title: 'Themes/ThemePicker', + component: ThemePicker, + args: { options, modelValue: 'default' }, + decorators: [() => ({ template: '
' })], + // Its own value, so the button shows the theme picked. + render: (args) => ({ + components: { ThemePicker }, + setup: () => ({ args, value: ref(args.modelValue) }), + template: '', + }), +}); +export default meta; + +/** Picking a theme sets it and closes the list. */ +export const Default = meta.story({ + args: { onPicked: fn() }, + play: async ({ args, canvas, userEvent }) => { + await userEvent.click(canvas.getByTestId('theme-picker-trigger')); + await userEvent.click(await screen.findByTestId('theme-option-ocean')); + + await expect(args.onPicked).toHaveBeenCalledWith( + 'ocean', + expect.anything(), + ); + await waitFor(() => + expect( + canvas.getByTestId('theme-picker-trigger'), + ).toHaveTextContent('Ocean'), + ); + }, +}); + +/** A search with no match. */ +export const NoResults = meta.story({ + play: async ({ canvas, userEvent }) => { + await userEvent.click(canvas.getByTestId('theme-picker-trigger')); + await userEvent.type(await screen.findByTestId('theme-search'), 'neon'); + + // The list fades in: wait for it, not just for it to be in the page. + const empty = await screen.findByText('No themes found'); + await waitFor(() => expect(empty).toBeVisible()); + await userEvent.keyboard('{Escape}'); + }, +});