-
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.
Merge pull request #53 from getpingback/feat/drawer
feat: create drawer component
- Loading branch information
Showing
7 changed files
with
211 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React, { useState } from 'react'; | ||
import { Drawer } from './drawer'; | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { ArrowLeftIcon } from '@stash-ui/light-icons'; | ||
import { Button } from '../button/button'; | ||
|
||
const meta = { | ||
title: 'Components/Drawer', | ||
component: Drawer, | ||
tags: ['autodocs'] | ||
} satisfies Meta<typeof Drawer>; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
export default meta; | ||
|
||
export const Default: Story = { | ||
args: { | ||
title: 'Drawer', | ||
description: 'Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, quos.', | ||
hasDivider: true, | ||
open: true, | ||
prefixIcon: <ArrowLeftIcon />, | ||
children: ( | ||
<div className="flex flex-col gap-4"> | ||
<div>lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, quos</div> | ||
<div>lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, quos</div> | ||
<div>lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, quos</div> | ||
<div>lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, quos</div> | ||
<div>lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, quos</div> | ||
<div>lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, quos</div> | ||
</div> | ||
), | ||
footer: ( | ||
<div className="flex gap-4"> | ||
<Button variant="outline" className="w-full"> | ||
Cancel | ||
</Button> | ||
<Button variant="solid" className="w-full"> | ||
Save | ||
</Button> | ||
</div> | ||
) | ||
} | ||
}; | ||
|
||
export const Behavior: Story = { | ||
args: { | ||
title: 'Drawer' | ||
}, | ||
render: ({ title }) => { | ||
const [open, setOpen] = useState(false); | ||
|
||
return ( | ||
<> | ||
<Button variant="solid" onClick={() => setOpen(true)}> | ||
Open Drawer | ||
</Button> | ||
<Drawer title={title} open={open} onOpenChange={setOpen} /> | ||
</> | ||
); | ||
} | ||
}; |
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,85 @@ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import { Drawer } from './drawer'; | ||
import * as React from 'react'; | ||
|
||
describe('Drawer', () => { | ||
const defaultProps = { | ||
title: 'Test Drawer', | ||
open: true | ||
}; | ||
|
||
it('should render drawer with title', () => { | ||
render( | ||
<Drawer {...defaultProps}> | ||
<div>Drawer content</div> | ||
</Drawer> | ||
); | ||
|
||
expect(screen.getByText('Test Drawer')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render drawer with description', () => { | ||
render( | ||
<Drawer {...defaultProps} description="Test description"> | ||
<div>Drawer content</div> | ||
</Drawer> | ||
); | ||
|
||
expect(screen.getByText('Test description')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render drawer with preffix icon', () => { | ||
const TestIcon = () => <div data-testid="test-icon">Icon</div>; | ||
|
||
render( | ||
<Drawer {...defaultProps} prefixIcon={<TestIcon />}> | ||
<div>Drawer content</div> | ||
</Drawer> | ||
); | ||
|
||
expect(screen.getByTestId('test-icon')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render drawer with divider when hasDivider is true', () => { | ||
render( | ||
<Drawer {...defaultProps} hasDivider> | ||
<div>Drawer content</div> | ||
</Drawer> | ||
); | ||
|
||
expect(screen.getByTestId('divider')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render drawer with footer content', () => { | ||
render( | ||
<Drawer {...defaultProps} footer={<div>Footer content</div>}> | ||
<div>Drawer content</div> | ||
</Drawer> | ||
); | ||
|
||
expect(screen.getByText('Footer content')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render children content', () => { | ||
render( | ||
<Drawer {...defaultProps}> | ||
<div>Test children content</div> | ||
</Drawer> | ||
); | ||
|
||
expect(screen.getByText('Test children content')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should close drawer when clicking close button', () => { | ||
const onOpenChange = jest.fn(); | ||
|
||
render( | ||
<Drawer {...defaultProps} onOpenChange={onOpenChange}> | ||
<div>Drawer content</div> | ||
</Drawer> | ||
); | ||
|
||
fireEvent.click(screen.getByRole('button')); | ||
expect(onOpenChange).toHaveBeenCalledWith(false); | ||
}); | ||
}); |
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,42 @@ | ||
import * as Dialog from '@radix-ui/react-dialog'; | ||
import { TimesIcon } from '@stash-ui/light-icons'; | ||
import * as React from 'react'; | ||
|
||
interface DrawerProps extends Dialog.DialogProps { | ||
title: string; | ||
description?: string; | ||
prefixIcon?: React.ReactNode; | ||
hasDivider?: boolean; | ||
footer?: React.ReactNode; | ||
} | ||
|
||
function Drawer({ children, title, description, prefixIcon, hasDivider, footer, ...props }: DrawerProps) { | ||
return ( | ||
<Dialog.Root {...props}> | ||
<Dialog.Portal> | ||
<Dialog.Overlay className=" z-50 fixed inset-0 bg-[#00000011] w-screen h-screen backdrop-blur-[1px] animate-fade-in" /> | ||
<Dialog.Content className="z-50 flex flex-col bg-[#FFFFFF] shadow-drawer rounded-xl w-[400px] border border-border-card fixed right-6 top-6 h-[calc(100vh-48px)] data-[state=open]:animate-drawer-slide-in data-[state=closed]:animate-drawer-slide-out"> | ||
<div className="flex justify-between gap-2 p-6 pb-4"> | ||
<div className="flex gap-2"> | ||
{prefixIcon && <div className="h-fit">{prefixIcon}</div>} | ||
<div className="flex flex-col gap-1"> | ||
<Dialog.Title className="text-lg font-bold leading-6 text-primary-foreground">{title}</Dialog.Title> | ||
{description && ( | ||
<Dialog.Description className="text-xs font-normal text-primary-foreground opacity-65">{description}</Dialog.Description> | ||
)} | ||
</div> | ||
</div> | ||
<Dialog.Close className="h-fit"> | ||
<TimesIcon /> | ||
</Dialog.Close> | ||
</div> | ||
{hasDivider && <div className="h-px w-full bg-[#0000000A]" data-testid="divider" />} | ||
<div className="flex flex-col flex-1 overflow-y-auto p-6">{children}</div> | ||
{footer && <div className="p-6 border-t border-[#0000000A]">{footer}</div>} | ||
</Dialog.Content> | ||
</Dialog.Portal> | ||
</Dialog.Root> | ||
); | ||
} | ||
|
||
export { Drawer }; |
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 @@ | ||
export * from './drawer'; |
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
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
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