Repository navigation
feat: DialogSave emits save; swatch and link toggle fit their content; stories #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<typeof ColorInput>, | ||
| // Changes show in the Actions panel; a story's own spy replaces them. | ||
| render: (args) => ( | ||
| <ColorInput | ||
| {...args} | ||
| onChange={args.onChange ?? action('change')} | ||
| onSyncedChange={args.onSyncedChange ?? action('synced change')} | ||
| /> | ||
| ), | ||
| // Its tooltip needs a provider, which the theme panel gives it. | ||
| decorators: [ | ||
| (Story) => ( | ||
| <TooltipProvider> | ||
| <div className="max-w-sm"> | ||
| <Story /> | ||
| </div> | ||
| </TooltipProvider> | ||
| ), | ||
| ], | ||
| }); | ||
| 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'), | ||
| ); | ||
| }, | ||
| }); |
84 changes: 84 additions & 0 deletions
84
resources/js/react/components/ColorPickerPopover.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<typeof ColorPickerPopover>, | ||
| 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 ( | ||
| <ColorPickerPopover value={value} onChange={setValue}> | ||
| <Button variant="outline"> | ||
| <span | ||
| className="size-4 rounded-full border" | ||
| style={{ background: value }} | ||
| /> | ||
| {value} | ||
| </Button> | ||
| </ColorPickerPopover> | ||
| ); | ||
| }, | ||
| }); | ||
| 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(); | ||
| }, | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 ( | ||
| <> | ||
| <Button variant="outline" onClick={() => setOpen(true)}> | ||
| How to use this theme | ||
| </Button> | ||
| <DialogCommand {...args} open={open} onOpenChange={setOpen} /> | ||
| </> | ||
| ); | ||
| }, | ||
| }); | ||
| 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' } } }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<typeof DialogSave>, | ||
| // A button opens it, as the theme panel's does. | ||
| render: function Render(args) { | ||
| const [open, setOpen] = useState(args.open); | ||
|
|
||
| return ( | ||
| <> | ||
| <Button variant="outline" onClick={() => setOpen(true)}> | ||
| Save as… | ||
| </Button> | ||
| <DialogSave {...args} open={open} onOpenChange={setOpen} /> | ||
| </> | ||
| ); | ||
| }, | ||
| }); | ||
| 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(); | ||
| }, | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep the picker synchronized with the
valuecontrol.If a developer changes
valuefrom#4f46e5to#e11d48in Controls,useState(args.value)retains the initial value. The picker and trigger therefore ignore the new argument. Controls should update the rendered example. (react.dev)Synchronize local state when
args.valuechanges, or use Storybook args as the writable state. Preserve the existing feedback from picker selections.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Valid, stories only. Filed as sc-1261, https://app.shortcut.com/saucebase/story/1261; not blocking this release.