-
Notifications
You must be signed in to change notification settings - Fork 0
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
et-501: create drawer component #156
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 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
30 changes: 30 additions & 0 deletions
30
src/components/ui/drawer/__snapshots__/drawer.spec.tsx.snap
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,30 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`Drawer > matches snapshot 1`] = ` | ||
<div> | ||
<div | ||
class="absolute top-0 h-full right-0" | ||
style="width: 460px;" | ||
> | ||
<div | ||
class="absolute inset-0 overflow-hidden" | ||
> | ||
<div | ||
class="absolute h-full w-full top-0 right-0 bg-white transition-transform duration-300 ease-in-out translate-x-0" | ||
> | ||
<div | ||
class="w-full h-full relative" | ||
> | ||
<div | ||
class="h-full" | ||
> | ||
<div> | ||
Drawer Content | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
`; |
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,55 @@ | ||
import { expect, it, describe } from 'vitest'; | ||
import { render } from '@testing-library/react'; | ||
|
||
import { Drawer } from './drawer'; | ||
import React from 'react'; | ||
|
||
describe('Drawer', () => { | ||
const subject = (props = {}) => { | ||
const defaultProps = { | ||
isOpen: true, | ||
children: <div>Drawer Content</div>, | ||
drawerWidth: 460, | ||
side: 'right' as const | ||
}; | ||
return render(<Drawer {...defaultProps} {...props} />); | ||
}; | ||
|
||
it('matches snapshot', () => { | ||
expect(subject().container).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders with correct width', () => { | ||
const { container } = subject({ drawerWidth: 500 }); | ||
const drawer = container.firstChild as HTMLElement; | ||
expect(drawer).toHaveStyle({ width: '500px' }); | ||
}); | ||
|
||
it('positions on the right by default', () => { | ||
const { container } = subject(); | ||
const drawer = container.firstChild as HTMLElement; | ||
expect(drawer).toHaveClass('right-0'); | ||
expect(drawer).not.toHaveClass('left-0'); | ||
}); | ||
|
||
it('positions on the left when specified', () => { | ||
const { container } = subject({ side: 'left' }); | ||
const drawer = container.firstChild as HTMLElement; | ||
expect(drawer).toHaveClass('left-0'); | ||
expect(drawer).not.toHaveClass('right-0'); | ||
}); | ||
|
||
it('applies correct transform class when open', () => { | ||
const { container } = subject({ isOpen: true }); | ||
const slidePanel = container.querySelector('[class*="translate"]'); | ||
expect(slidePanel).toHaveClass('translate-x-0'); | ||
expect(slidePanel).not.toHaveClass('translate-x-full'); | ||
}); | ||
|
||
it('applies correct transform class when closed', () => { | ||
const { container } = subject({ isOpen: false }); | ||
const slidePanel = container.querySelector('[class*="translate"]'); | ||
expect(slidePanel).toHaveClass('translate-x-full'); | ||
expect(slidePanel).not.toHaveClass('translate-x-0'); | ||
}); | ||
}); |
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,45 @@ | ||
import { ReactNode, type FC } from 'react'; | ||
import clsx from 'clsx'; | ||
|
||
interface DrawerProps { | ||
isOpen: boolean | ||
children: ReactNode | ||
drawerWidth?: number | ||
side?: 'left' | 'right' | ||
} | ||
|
||
|
||
export const Drawer: FC<DrawerProps> = ({ isOpen, children, drawerWidth = 460, side = 'right' }) => { | ||
|
||
return ( | ||
<div className={ | ||
clsx( | ||
'absolute top-0 h-full', | ||
side === 'left' ? 'left-0' : 'right-0' | ||
) | ||
} | ||
style={{ width: `${drawerWidth}px` }} | ||
> | ||
<div className="absolute inset-0 overflow-hidden"> | ||
<div | ||
className={clsx( | ||
'absolute h-full w-full top-0', | ||
side === 'left' ? 'left-0' : 'right-0', | ||
'bg-white', | ||
'transition-transform duration-300 ease-in-out', | ||
{ | ||
'translate-x-0': isOpen && side === 'right', | ||
'translate-x-full': !isOpen && side === 'right', | ||
'-translate-x-0': isOpen && side === 'left', | ||
'-translate-x-full': !isOpen && side === 'left', | ||
} | ||
)} | ||
> | ||
<div className='w-full h-full relative'> | ||
<div className='h-full'>{children}</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; |
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,2 @@ | ||
|
||
export { Drawer } 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { Drawer } from '../components'; | ||
|
||
const meta = { | ||
component: Drawer | ||
} satisfies Meta<typeof Drawer>; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof Drawer>; | ||
export const ControlledDrawer = { | ||
args: { | ||
children: <div>hello</div>, | ||
isOpen: false, | ||
side: 'right', | ||
drawerWidth: 460 | ||
}, | ||
render: (args) => ( | ||
<div className='grid grid-rows-[4rem_1fr] h-screen'> | ||
<div className='bg-blue-100'> | ||
the app header | ||
</div> | ||
<main className='relative overflow-hidden'> | ||
<div className='bg-red-100 w-full h-full'> | ||
this is the main content | ||
</div> | ||
<Drawer {...args} /> | ||
</main> | ||
</div> | ||
) | ||
} satisfies Story; |
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
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.
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.
Suggestion
What if I want to pass width in '%' or 'rem' ? 🤔 Maybe it make sense to change this to be more generic and allow to pass tailwind class instead of absolute value.