-
-
Notifications
You must be signed in to change notification settings - Fork 99
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 #943 from tszhong0411/pack-19-add-drawer
Add drawer
- Loading branch information
Showing
11 changed files
with
570 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@tszhong0411/ui': patch | ||
--- | ||
|
||
Add drawer component |
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 |
---|---|---|
|
@@ -39,5 +39,6 @@ tsup | |
turbopack | ||
unifiedjs | ||
usehooks | ||
vaul | ||
vfile | ||
zustand |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
title: Drawer | ||
description: A drawer component for React. | ||
--- | ||
|
||
<ComponentPreview name='drawer/drawer' /> | ||
|
||
## Usage | ||
|
||
```tsx | ||
import { | ||
Drawer, | ||
DrawerClose, | ||
DrawerContent, | ||
DrawerDescription, | ||
DrawerFooter, | ||
DrawerHeader, | ||
DrawerTitle, | ||
DrawerTrigger | ||
} from '@tszhong0411/ui' | ||
``` | ||
|
||
```tsx | ||
<Drawer> | ||
<DrawerTrigger>Open</DrawerTrigger> | ||
<DrawerContent> | ||
<DrawerHeader> | ||
<DrawerTitle>Are you absolutely sure?</DrawerTitle> | ||
<DrawerDescription>This action cannot be undone.</DrawerDescription> | ||
</DrawerHeader> | ||
<DrawerFooter> | ||
<Button>Submit</Button> | ||
<DrawerClose> | ||
<Button variant='outline'>Cancel</Button> | ||
</DrawerClose> | ||
</DrawerFooter> | ||
</DrawerContent> | ||
</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,143 @@ | ||
'use client' | ||
|
||
import { | ||
Button, | ||
Drawer, | ||
DrawerClose, | ||
DrawerContent, | ||
DrawerDescription, | ||
DrawerFooter, | ||
DrawerHeader, | ||
DrawerTitle, | ||
DrawerTrigger | ||
} from '@tszhong0411/ui' | ||
import { MinusIcon, PlusIcon } from 'lucide-react' | ||
import { useState } from 'react' | ||
import { Bar, BarChart, ResponsiveContainer } from 'recharts' | ||
|
||
const data = [ | ||
{ | ||
goal: 400 | ||
}, | ||
{ | ||
goal: 300 | ||
}, | ||
{ | ||
goal: 200 | ||
}, | ||
{ | ||
goal: 300 | ||
}, | ||
{ | ||
goal: 200 | ||
}, | ||
{ | ||
goal: 278 | ||
}, | ||
{ | ||
goal: 189 | ||
}, | ||
{ | ||
goal: 239 | ||
}, | ||
{ | ||
goal: 300 | ||
}, | ||
{ | ||
goal: 200 | ||
}, | ||
{ | ||
goal: 278 | ||
}, | ||
{ | ||
goal: 189 | ||
}, | ||
{ | ||
goal: 349 | ||
} | ||
] | ||
|
||
const DrawerDemo = () => { | ||
const [goal, setGoal] = useState(350) | ||
|
||
const onClick = (adjustment: number) => { | ||
setGoal(Math.max(200, Math.min(400, goal + adjustment))) | ||
} | ||
|
||
return ( | ||
<Drawer> | ||
<DrawerTrigger asChild> | ||
<Button variant='outline' type='button'> | ||
Open Drawer | ||
</Button> | ||
</DrawerTrigger> | ||
<DrawerContent> | ||
<div className='mx-auto w-full max-w-sm'> | ||
<DrawerHeader> | ||
<DrawerTitle>Move Goal</DrawerTitle> | ||
<DrawerDescription>Set your daily activity goal.</DrawerDescription> | ||
</DrawerHeader> | ||
<div className='p-4 pb-0'> | ||
<div className='flex items-center justify-center space-x-2'> | ||
<Button | ||
variant='outline' | ||
size='icon' | ||
className='size-8 shrink-0 rounded-full' | ||
onClick={() => { | ||
onClick(-10) | ||
}} | ||
disabled={goal <= 200} | ||
type='button' | ||
> | ||
<MinusIcon /> | ||
<span className='sr-only'>Decrease</span> | ||
</Button> | ||
<div className='flex-1 text-center'> | ||
<div className='text-7xl font-bold tracking-tighter'>{goal}</div> | ||
<div className='text-muted-foreground text-[0.70rem] uppercase'>Calories/day</div> | ||
</div> | ||
<Button | ||
variant='outline' | ||
size='icon' | ||
className='size-8 shrink-0 rounded-full' | ||
onClick={() => { | ||
onClick(10) | ||
}} | ||
disabled={goal >= 400} | ||
type='button' | ||
> | ||
<PlusIcon /> | ||
<span className='sr-only'>Increase</span> | ||
</Button> | ||
</div> | ||
<div className='mt-3 h-[120px]'> | ||
<ResponsiveContainer width='100%' height='100%'> | ||
<BarChart data={data}> | ||
<Bar | ||
dataKey='goal' | ||
style={ | ||
{ | ||
fill: 'hsl(var(--foreground))', | ||
opacity: 0.9 | ||
} as React.CSSProperties | ||
} | ||
/> | ||
</BarChart> | ||
</ResponsiveContainer> | ||
</div> | ||
</div> | ||
<DrawerFooter> | ||
<Button type='button'>Submit</Button> | ||
<DrawerClose asChild> | ||
<Button variant='outline' type='button'> | ||
Cancel | ||
</Button> | ||
</DrawerClose> | ||
</DrawerFooter> | ||
</div> | ||
</DrawerContent> | ||
</Drawer> | ||
) | ||
} | ||
|
||
export default DrawerDemo |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
'use client' | ||
|
||
import { cn } from '@tszhong0411/utils' | ||
import { Drawer as DrawerPrimitive } from 'vaul' | ||
|
||
type DrawerProps = React.ComponentProps<typeof DrawerPrimitive.Root> | ||
|
||
export const Drawer = (props: DrawerProps) => { | ||
const { shouldScaleBackground = true, ...rest } = props | ||
|
||
return <DrawerPrimitive.Root shouldScaleBackground={shouldScaleBackground} {...rest} /> | ||
} | ||
|
||
export const DrawerTrigger = DrawerPrimitive.Trigger | ||
export const DrawerPortal = DrawerPrimitive.Portal | ||
export const DrawerClose = DrawerPrimitive.Close | ||
|
||
type DrawerOverlayProps = React.ComponentProps<typeof DrawerPrimitive.Overlay> | ||
|
||
export const DrawerOverlay = (props: DrawerOverlayProps) => { | ||
const { className, ...rest } = props | ||
|
||
return ( | ||
<DrawerPrimitive.Overlay | ||
className={cn('fixed inset-0 z-50 bg-black/80', className)} | ||
{...rest} | ||
/> | ||
) | ||
} | ||
|
||
type DrawerContentProps = React.ComponentProps<typeof DrawerPrimitive.Content> | ||
|
||
export const DrawerContent = (props: DrawerContentProps) => { | ||
const { className, children, ...rest } = props | ||
|
||
return ( | ||
<DrawerPortal> | ||
<DrawerOverlay /> | ||
<DrawerPrimitive.Content | ||
className={cn( | ||
'bg-background fixed inset-x-0 bottom-0 z-50 mt-24 flex h-auto flex-col rounded-t-[10px] border', | ||
className | ||
)} | ||
{...rest} | ||
> | ||
<div className='bg-muted mx-auto mt-4 h-2 w-[100px] rounded-full' /> | ||
{children} | ||
</DrawerPrimitive.Content> | ||
</DrawerPortal> | ||
) | ||
} | ||
|
||
type DrawerHeaderProps = React.ComponentProps<'div'> | ||
|
||
export const DrawerHeader = (props: DrawerHeaderProps) => { | ||
const { className, ...rest } = props | ||
|
||
return <div className={cn('grid gap-1.5 p-4 text-center sm:text-left', className)} {...rest} /> | ||
} | ||
|
||
type DrawerFooterProps = React.ComponentProps<'div'> | ||
|
||
export const DrawerFooter = (props: DrawerFooterProps) => { | ||
const { className, ...rest } = props | ||
|
||
return <div className={cn('mt-auto flex flex-col gap-2 p-4', className)} {...rest} /> | ||
} | ||
|
||
type DrawerTitleProps = React.ComponentProps<typeof DrawerPrimitive.Title> | ||
|
||
export const DrawerTitle = (props: DrawerTitleProps) => { | ||
const { className, ...rest } = props | ||
|
||
return ( | ||
<DrawerPrimitive.Title | ||
className={cn('text-lg font-semibold leading-none tracking-tight', className)} | ||
{...rest} | ||
/> | ||
) | ||
} | ||
|
||
type DrawerDescriptionProps = React.ComponentProps<typeof DrawerPrimitive.Description> | ||
|
||
export const DrawerDescription = (props: DrawerDescriptionProps) => { | ||
const { className, ...rest } = props | ||
|
||
return ( | ||
<DrawerPrimitive.Description | ||
className={cn('text-muted-foreground text-sm', className)} | ||
{...rest} | ||
/> | ||
) | ||
} |
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
Oops, something went wrong.