-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: add modal (dialog) ui ( #84 ) * refactor: delete unnecessary className
- Loading branch information
Showing
5 changed files
with
336 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,100 @@ | ||
import { Button } from '../Button'; | ||
|
||
import { | ||
Dialog, | ||
DialogContent, | ||
DialogDescription, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from '.'; | ||
|
||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
type DialogPropsAndCustomArgs = React.ComponentProps<typeof Dialog> & { | ||
modal?: boolean; | ||
defaultOpen?: boolean; | ||
}; | ||
|
||
const meta: Meta<DialogPropsAndCustomArgs> = { | ||
component: Dialog, | ||
parameters: { | ||
layout: 'centered', | ||
}, | ||
tags: ['autodocs'], | ||
argTypes: { | ||
modal: { | ||
control: { type: 'boolean' }, | ||
defaultValue: true, | ||
description: | ||
'The modality of the dialog. When set to true, interaction with outside elements will be disabled and only dialog content will be visible to screen readers.', | ||
}, | ||
defaultOpen: { | ||
control: { type: 'boolean' }, | ||
defaultValue: false, | ||
description: | ||
'The open state of the dialog when it is initially rendered. Use when you do not need to control its open state.', | ||
}, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<DialogPropsAndCustomArgs>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
modal: true, | ||
}, | ||
render: (args) => ( | ||
<Dialog modal={args.modal || false} defaultOpen={args.defaultOpen || false}> | ||
<DialogTrigger asChild> | ||
<Button>Open</Button> | ||
</DialogTrigger> | ||
<DialogContent className="bg-primary text-primary-foreground"> | ||
<DialogHeader> | ||
<DialogTitle>Dialog Title</DialogTitle> | ||
<DialogDescription>Dialog Description</DialogDescription> | ||
</DialogHeader> | ||
</DialogContent> | ||
</Dialog> | ||
), | ||
}; | ||
|
||
export const Modal: Story = { | ||
args: { | ||
modal: true, | ||
}, | ||
render: (args) => ( | ||
<Dialog modal={args.modal || false} defaultOpen={args.defaultOpen || false}> | ||
<DialogTrigger asChild> | ||
<Button>Open</Button> | ||
</DialogTrigger> | ||
<DialogContent className="bg-primary text-primary-foreground"> | ||
<DialogHeader> | ||
<DialogTitle>Dialog Title</DialogTitle> | ||
<DialogDescription>Dialog Description</DialogDescription> | ||
</DialogHeader> | ||
</DialogContent> | ||
</Dialog> | ||
), | ||
}; | ||
|
||
export const NotModal: Story = { | ||
args: { | ||
modal: false, | ||
}, | ||
render: (args) => ( | ||
<Dialog modal={args.modal || false} defaultOpen={args.defaultOpen || false}> | ||
<DialogTrigger asChild> | ||
<Button>Open</Button> | ||
</DialogTrigger> | ||
<DialogContent className="bg-primary text-primary-foreground"> | ||
<DialogHeader> | ||
<DialogTitle>Dialog Title</DialogTitle> | ||
<DialogDescription>Dialog Description</DialogDescription> | ||
</DialogHeader> | ||
</DialogContent> | ||
</Dialog> | ||
), | ||
}; |
This file contains 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,70 @@ | ||
import { render } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import '@testing-library/jest-dom'; | ||
|
||
import { | ||
Dialog, | ||
DialogContent, | ||
DialogDescription, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from '.'; | ||
|
||
describe('ui/Dialog', () => { | ||
it('renders content correctly', () => { | ||
const header = 'Header'; | ||
const title = 'Title'; | ||
const description = 'Description'; | ||
|
||
const screen = render( | ||
<Dialog modal open> | ||
<DialogTrigger>Trigger</DialogTrigger> | ||
<DialogContent> | ||
<DialogHeader> | ||
{header} | ||
<DialogTitle>{title}</DialogTitle> | ||
<DialogDescription>{description}</DialogDescription> | ||
</DialogHeader> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
|
||
expect(screen.getByText(header)).toHaveClass( | ||
'flex flex-col space-y-1.5 text-center sm:text-left' | ||
); | ||
|
||
expect(screen.getByText(title)).toHaveClass( | ||
'text-lg font-semibold leading-none tracking-tight' | ||
); | ||
|
||
expect(screen.getByText(description)).toHaveClass( | ||
'text-sm text-muted-foreground' | ||
); | ||
}); | ||
|
||
it('opens correctly', async () => { | ||
const trigger = 'Trigger'; | ||
const content = 'Content'; | ||
|
||
const screen = render( | ||
<Dialog modal> | ||
<DialogTrigger>{trigger}</DialogTrigger> | ||
<DialogContent> | ||
<DialogHeader> | ||
<DialogTitle>Dialog Title</DialogTitle> | ||
<DialogDescription>{content}</DialogDescription> | ||
</DialogHeader> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
|
||
// closed by default | ||
expect(() => screen.getByText(content)).toThrow(); | ||
|
||
// open on trigger clicked | ||
await userEvent.click(screen.getByText(trigger)); | ||
expect(screen.getByText(content)).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains 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,128 @@ | ||
'use client'; | ||
|
||
import { forwardRef } from 'react'; | ||
import type { | ||
ElementRef, | ||
ComponentPropsWithoutRef, | ||
HTMLAttributes, | ||
} from 'react'; | ||
|
||
import * as DialogPrimitive from '@radix-ui/react-dialog'; | ||
import { X } from 'lucide-react'; | ||
|
||
import { cn } from '@/libs/utils'; | ||
|
||
const Dialog = DialogPrimitive.Root; | ||
|
||
const DialogTrigger = DialogPrimitive.Trigger; | ||
|
||
const DialogPortal = DialogPrimitive.Portal; | ||
|
||
const DialogClose = DialogPrimitive.Close; | ||
|
||
const DialogOverlay = forwardRef< | ||
ElementRef<typeof DialogPrimitive.Overlay>, | ||
ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay> | ||
>(({ className, ...props }, ref) => ( | ||
<DialogPrimitive.Overlay | ||
ref={ref} | ||
className={cn( | ||
'fixed inset-0 z-50 bg-background/80 backdrop-blur-sm data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0', | ||
className | ||
)} | ||
{...props} | ||
/> | ||
)); | ||
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName; | ||
|
||
const DialogContent = forwardRef< | ||
ElementRef<typeof DialogPrimitive.Content>, | ||
ComponentPropsWithoutRef<typeof DialogPrimitive.Content> | ||
>(({ className, children, ...props }, ref) => ( | ||
<DialogPortal> | ||
<DialogOverlay /> | ||
<DialogPrimitive.Content | ||
ref={ref} | ||
className={cn( | ||
'fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg', | ||
className | ||
)} | ||
{...props} | ||
> | ||
{children} | ||
<DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground"> | ||
<X className="h-4 w-4" /> | ||
<span className="sr-only">Close</span> | ||
</DialogPrimitive.Close> | ||
</DialogPrimitive.Content> | ||
</DialogPortal> | ||
)); | ||
DialogContent.displayName = DialogPrimitive.Content.displayName; | ||
|
||
const DialogHeader = ({ | ||
className, | ||
...props | ||
}: HTMLAttributes<HTMLDivElement>): JSX.Element => ( | ||
<div | ||
className={cn( | ||
'flex flex-col space-y-1.5 text-center sm:text-left', | ||
className | ||
)} | ||
{...props} | ||
/> | ||
); | ||
DialogHeader.displayName = 'DialogHeader'; | ||
|
||
const DialogFooter = ({ | ||
className, | ||
...props | ||
}: HTMLAttributes<HTMLDivElement>): JSX.Element => ( | ||
<div | ||
className={cn( | ||
'flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2', | ||
className | ||
)} | ||
{...props} | ||
/> | ||
); | ||
DialogFooter.displayName = 'DialogFooter'; | ||
|
||
const DialogTitle = forwardRef< | ||
ElementRef<typeof DialogPrimitive.Title>, | ||
ComponentPropsWithoutRef<typeof DialogPrimitive.Title> | ||
>(({ className, ...props }, ref) => ( | ||
<DialogPrimitive.Title | ||
ref={ref} | ||
className={cn( | ||
'text-lg font-semibold leading-none tracking-tight', | ||
className | ||
)} | ||
{...props} | ||
/> | ||
)); | ||
DialogTitle.displayName = DialogPrimitive.Title.displayName; | ||
|
||
const DialogDescription = forwardRef< | ||
ElementRef<typeof DialogPrimitive.Description>, | ||
ComponentPropsWithoutRef<typeof DialogPrimitive.Description> | ||
>(({ className, ...props }, ref) => ( | ||
<DialogPrimitive.Description | ||
ref={ref} | ||
className={cn('text-sm text-muted-foreground', className)} | ||
{...props} | ||
/> | ||
)); | ||
DialogDescription.displayName = DialogPrimitive.Description.displayName; | ||
|
||
export { | ||
Dialog, | ||
DialogPortal, | ||
DialogOverlay, | ||
DialogClose, | ||
DialogTrigger, | ||
DialogContent, | ||
DialogHeader, | ||
DialogFooter, | ||
DialogTitle, | ||
DialogDescription, | ||
}; |