drawer.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. 'use client';
  2. import * as React from 'react';
  3. import { Drawer as DrawerPrimitive } from '@base-ui/react/drawer';
  4. import { cn } from '@/lib/utils';
  5. type DrawerContextProps = {
  6. hasSnapPoints: boolean;
  7. modal: DrawerPrimitive.Root.Props['modal'];
  8. showSwipeHandle: boolean;
  9. swipeDirection: NonNullable<DrawerPrimitive.Root.Props['swipeDirection']>;
  10. };
  11. const DrawerContext = React.createContext<DrawerContextProps | null>(null);
  12. function useDrawer() {
  13. const context = React.useContext(DrawerContext);
  14. if (!context) {
  15. throw new Error('useDrawer must be used within a Drawer.');
  16. }
  17. return context;
  18. }
  19. function Drawer({
  20. modal = true,
  21. showSwipeHandle = false,
  22. snapPoints,
  23. swipeDirection = 'down',
  24. ...props
  25. }: DrawerPrimitive.Root.Props & {
  26. showSwipeHandle?: boolean;
  27. }) {
  28. const hasSnapPoints = snapPoints != null && snapPoints.length > 0;
  29. const contextValue = React.useMemo(
  30. () => ({ hasSnapPoints, modal, showSwipeHandle, swipeDirection }),
  31. [hasSnapPoints, modal, showSwipeHandle, swipeDirection],
  32. );
  33. return (
  34. <DrawerContext.Provider value={contextValue}>
  35. <DrawerPrimitive.Root
  36. data-slot="drawer"
  37. modal={modal}
  38. snapPoints={snapPoints}
  39. swipeDirection={swipeDirection}
  40. {...props}
  41. />
  42. </DrawerContext.Provider>
  43. );
  44. }
  45. function DrawerTrigger({ ...props }: DrawerPrimitive.Trigger.Props) {
  46. return <DrawerPrimitive.Trigger data-slot="drawer-trigger" {...props} />;
  47. }
  48. function DrawerPortal({ ...props }: DrawerPrimitive.Portal.Props) {
  49. return <DrawerPrimitive.Portal data-slot="drawer-portal" {...props} />;
  50. }
  51. function DrawerClose({ ...props }: DrawerPrimitive.Close.Props) {
  52. return <DrawerPrimitive.Close data-slot="drawer-close" {...props} />;
  53. }
  54. function DrawerOverlay({
  55. className,
  56. ...props
  57. }: DrawerPrimitive.Backdrop.Props) {
  58. return (
  59. <DrawerPrimitive.Backdrop
  60. data-slot="drawer-overlay"
  61. className={cn(
  62. 'bg-black/10 supports-backdrop-filter:backdrop-blur-xs fixed inset-0 z-50 min-h-dvh opacity-[max(var(--drawer-overlay-min-opacity,0),calc(1-var(--drawer-swipe-progress)))] transition-opacity duration-450 ease-[cubic-bezier(0.32,0.72,0,1)] select-none data-ending-style:pointer-events-none data-ending-style:opacity-0 data-ending-style:duration-[calc(var(--drawer-swipe-strength)*400ms)] data-snap-points:[--drawer-overlay-min-opacity:0.5] data-starting-style:opacity-0 data-swiping:duration-0 supports-[-webkit-touch-callout:none]:absolute',
  63. className,
  64. )}
  65. {...props}
  66. />
  67. );
  68. }
  69. function DrawerSwipeHandle({
  70. className,
  71. ...props
  72. }: React.ComponentProps<'div'>) {
  73. return (
  74. <div
  75. data-slot="drawer-swipe-handle"
  76. aria-hidden="true"
  77. className={cn(
  78. 'after:block after:shrink-0 after:rounded-full after:bg-muted group-data-[swipe-axis=y]/drawer-popup:h-3 group-data-[swipe-axis=y]/drawer-popup:w-full group-data-[swipe-axis=y]/drawer-popup:justify-center group-data-[swipe-axis=y]/drawer-popup:after:h-1 group-data-[swipe-axis=y]/drawer-popup:after:w-24 group-data-[swipe-direction=down]/drawer-popup:items-end group-data-[swipe-direction=up]/drawer-popup:items-start group-data-[swipe-axis=x]/drawer-popup:h-full group-data-[swipe-axis=x]/drawer-popup:w-3 group-data-[swipe-axis=x]/drawer-popup:items-center group-data-[swipe-axis=x]/drawer-popup:after:h-24 group-data-[swipe-axis=x]/drawer-popup:after:w-1 group-data-[swipe-direction=left]/drawer-popup:justify-start group-data-[swipe-direction=right]/drawer-popup:justify-end relative z-10 flex shrink-0 cursor-grab transition-opacity duration-200 group-data-nested-drawer-open/drawer-popup:opacity-0 group-data-nested-drawer-swiping/drawer-popup:opacity-100 group-data-[swipe-direction=left]/drawer-popup:order-last group-data-[swipe-direction=up]/drawer-popup:order-last active:cursor-grabbing',
  79. className,
  80. )}
  81. {...props}
  82. />
  83. );
  84. }
  85. function DrawerContent({
  86. className,
  87. children,
  88. ...props
  89. }: DrawerPrimitive.Popup.Props) {
  90. const { hasSnapPoints, modal, showSwipeHandle, swipeDirection } = useDrawer();
  91. const swipeAxis =
  92. swipeDirection === 'down' || swipeDirection === 'up' ? 'y' : 'x';
  93. return (
  94. <DrawerPortal data-slot="drawer-portal">
  95. {modal === true && (
  96. <DrawerOverlay data-snap-points={hasSnapPoints ? '' : undefined} />
  97. )}
  98. <DrawerPrimitive.Viewport
  99. data-slot="drawer-viewport"
  100. data-modal={modal}
  101. className="pointer-events-none fixed inset-0 z-50 select-none data-[modal=true]:pointer-events-auto"
  102. >
  103. <DrawerPrimitive.Popup
  104. data-slot="drawer-popup"
  105. data-swipe-axis={swipeAxis}
  106. data-snap-points={hasSnapPoints ? '' : undefined}
  107. className={cn(
  108. // Base.
  109. 'bg-popover text-popover-foreground text-sm data-[swipe-direction=down]:rounded-t-xl data-[swipe-direction=down]:border-t data-[swipe-direction=left]:rounded-r-xl data-[swipe-direction=left]:border-r data-[swipe-direction=right]:rounded-l-xl data-[swipe-direction=right]:border-l data-[swipe-direction=up]:rounded-b-xl data-[swipe-direction=up]:border-b group/drawer-popup pointer-events-auto fixed z-50 m-(--drawer-inset,0px) flex h-(--drawer-content-height) max-h-(--drawer-content-max-height,none) min-h-0 w-(--drawer-content-width,auto) transform-[translate3d(var(--translate-x,0px),var(--translate-y,0px),0)_scale(var(--stack-scale))] flex-col transition-[transform,height,opacity,filter] duration-450 ease-[cubic-bezier(0.22,1,0.36,1)] will-change-transform outline-none select-none [interpolate-size:allow-keywords]',
  110. // Nested.
  111. 'data-nested-drawer-open:overflow-hidden data-nested-drawer-open:brightness-95',
  112. // Bleed.
  113. 'after:pointer-events-none after:absolute after:bg-(--drawer-bleed-background,var(--color-popover)) data-[swipe-axis=x]:after:inset-y-0 data-[swipe-axis=x]:after:w-(--bleed) data-[swipe-axis=y]:after:inset-x-0 data-[swipe-axis=y]:after:h-(--bleed) data-[swipe-direction=down]:after:top-full data-[swipe-direction=left]:after:right-full data-[swipe-direction=right]:after:left-full data-[swipe-direction=up]:after:bottom-full',
  114. // Sizing.
  115. '[--drawer-content-height:var(--drawer-height,auto)] data-[swipe-axis=x]:[--drawer-content-width:75%] data-[swipe-axis=y]:[--drawer-content-max-height:calc(100dvh-6rem)] data-[swipe-axis=y]:data-snap-points:[--drawer-content-height:100dvh] data-[swipe-axis=x]:sm:[--drawer-content-width:24rem]',
  116. // Stack.
  117. '[--bleed:3rem] [--peek:1rem] [--stack-height:var(--drawer-frontmost-height,var(--drawer-height,0px))] [--stack-peek-offset:max(0px,calc((var(--nested-drawers)-var(--stack-progress))*var(--peek)))] [--stack-progress:clamp(0,var(--drawer-swipe-progress),1)] [--stack-scale-base:max(0,calc(1-(var(--nested-drawers)*var(--stack-step))))] [--stack-scale:clamp(0,calc(var(--stack-scale-base)+(var(--stack-step)*var(--stack-progress))),1)] [--stack-shrink:calc(1-var(--stack-scale))] [--stack-step:0.05]',
  118. // Transitions.
  119. 'data-ending-style:transform-(--closed-transform) data-ending-style:opacity-[0.9999] data-ending-style:duration-[calc(var(--drawer-swipe-strength)*400ms)] data-nested-drawer-swiping:duration-0 data-ending-style:data-nested-drawer-swiping:duration-[calc(var(--drawer-swipe-strength)*400ms)] data-starting-style:transform-(--closed-transform) data-swiping:duration-0 data-ending-style:data-swiping:duration-[calc(var(--drawer-swipe-strength)*400ms)]',
  120. // Axis: y.
  121. 'data-[swipe-axis=y]:inset-x-0 data-[swipe-axis=y]:data-nested-drawer-open:h-(--stack-height)',
  122. // Axis: x.
  123. 'data-[swipe-axis=x]:inset-y-0 data-[swipe-axis=x]:flex-row',
  124. // Direction: down.
  125. 'data-[swipe-direction=down]:bottom-0 data-[swipe-direction=down]:origin-bottom data-[swipe-direction=down]:[--closed-transform:translate3d(0,calc(100%+var(--drawer-inset,0px)+2px),0)] data-[swipe-direction=down]:[--translate-y:calc(var(--drawer-snap-point-offset,0px)+var(--drawer-swipe-movement-y)-var(--stack-peek-offset)-(var(--stack-shrink)*var(--stack-height)))]',
  126. // Direction: up.
  127. 'data-[swipe-direction=up]:top-0 data-[swipe-direction=up]:origin-top data-[swipe-direction=up]:[--closed-transform:translate3d(0,calc(-100%-var(--drawer-inset,0px)-2px),0)] data-[swipe-direction=up]:[--translate-y:calc(var(--drawer-snap-point-offset,0px)+var(--drawer-swipe-movement-y)+var(--stack-peek-offset)+(var(--stack-shrink)*var(--stack-height)))]',
  128. // Direction: left.
  129. 'data-[swipe-direction=left]:left-0 data-[swipe-direction=left]:origin-left data-[swipe-direction=left]:[--closed-transform:translate3d(calc(-100%-var(--drawer-inset,0px)-2px),0,0)] data-[swipe-direction=left]:[--translate-x:calc(var(--drawer-swipe-movement-x)+var(--stack-peek-offset)+(var(--stack-shrink)*100%))]',
  130. // Direction: right.
  131. 'data-[swipe-direction=right]:right-0 data-[swipe-direction=right]:origin-right data-[swipe-direction=right]:[--closed-transform:translate3d(calc(100%+var(--drawer-inset,0px)+2px),0,0)] data-[swipe-direction=right]:[--translate-x:calc(var(--drawer-swipe-movement-x)-var(--stack-peek-offset)-(var(--stack-shrink)*100%))]',
  132. className,
  133. )}
  134. {...props}
  135. >
  136. {showSwipeHandle && <DrawerSwipeHandle />}
  137. <DrawerPrimitive.Content
  138. data-slot="drawer-content"
  139. className={cn(
  140. 'flex min-h-0 flex-1 flex-col overflow-hidden overscroll-contain rounded-[inherit] transition-opacity duration-300 ease-[cubic-bezier(0.45,1.005,0,1.005)] select-text group-data-nested-drawer-open/drawer-popup:opacity-0 group-data-nested-drawer-swiping/drawer-popup:opacity-100 group-data-swiping/drawer-popup:select-none',
  141. )}
  142. >
  143. {children}
  144. </DrawerPrimitive.Content>
  145. </DrawerPrimitive.Popup>
  146. </DrawerPrimitive.Viewport>
  147. </DrawerPortal>
  148. );
  149. }
  150. function DrawerHeader({ className, ...props }: React.ComponentProps<'div'>) {
  151. return (
  152. <div
  153. data-slot="drawer-header"
  154. className={cn(
  155. 'gap-0.5 p-4 pb-0 md:gap-0.5 md:text-left flex shrink-0 flex-col group-data-[swipe-axis=y]/drawer-popup:text-center',
  156. className,
  157. )}
  158. {...props}
  159. />
  160. );
  161. }
  162. function DrawerFooter({ className, ...props }: React.ComponentProps<'div'>) {
  163. return (
  164. <div
  165. data-slot="drawer-footer"
  166. className={cn('gap-2 p-4 pt-0 mt-auto flex shrink-0 flex-col', className)}
  167. {...props}
  168. />
  169. );
  170. }
  171. function DrawerTitle({ className, ...props }: DrawerPrimitive.Title.Props) {
  172. return (
  173. <DrawerPrimitive.Title
  174. data-slot="drawer-title"
  175. className={cn(
  176. 'text-foreground text-base font-medium cn-font-heading',
  177. className,
  178. )}
  179. {...props}
  180. />
  181. );
  182. }
  183. function DrawerDescription({
  184. className,
  185. ...props
  186. }: DrawerPrimitive.Description.Props) {
  187. return (
  188. <DrawerPrimitive.Description
  189. data-slot="drawer-description"
  190. className={cn('text-muted-foreground text-sm text-balance', className)}
  191. {...props}
  192. />
  193. );
  194. }
  195. export {
  196. Drawer,
  197. DrawerPortal,
  198. DrawerOverlay,
  199. DrawerSwipeHandle,
  200. DrawerTrigger,
  201. DrawerClose,
  202. DrawerContent,
  203. DrawerHeader,
  204. DrawerFooter,
  205. DrawerTitle,
  206. DrawerDescription,
  207. };