@@ -30,16 +30,101 @@ const asChildProps = (
3030 */
3131function Drawer ( {
3232 direction = "bottom" ,
33+ indent = direction === "bottom" ,
3334 ...props
3435} : Omit < DrawerPrimitive . Root . Props , "swipeDirection" > & {
3536 direction ?: DrawerDirection ;
37+ /**
38+ * Shrinks the page behind the drawer while it is open, following the
39+ * swipe, like an iOS sheet. Needs a `DrawerIndent` around the page (the
40+ * public theme layout has one); without it nothing changes. Defaults to
41+ * `true` for bottom drawers and `false` for the other directions.
42+ */
43+ indent ?: boolean ;
3644} ) {
37- return (
45+ const root = (
3846 < DrawerPrimitive . Root
3947 swipeDirection = { SWIPE_DIRECTION [ direction ] }
4048 { ...props }
4149 />
4250 ) ;
51+
52+ return indent ? (
53+ root
54+ ) : (
55+ < DrawerPrimitive . Provider > { root } </ DrawerPrimitive . Provider >
56+ ) ;
57+ }
58+
59+ interface IndentViewport {
60+ bottom : number ;
61+ top : number ;
62+ }
63+
64+ const IndentedPage = ( {
65+ active,
66+ children,
67+ style,
68+ ...props
69+ } : React . ComponentProps < "div" > & { active : boolean } ) => {
70+ const [ wasActive , setWasActive ] = React . useState ( active ) ;
71+ const [ viewport , setViewport ] = React . useState < IndentViewport | null > ( null ) ;
72+
73+ if ( active !== wasActive ) {
74+ setWasActive ( active ) ;
75+ if ( active ) {
76+ setViewport ( {
77+ bottom : window . scrollY + window . innerHeight ,
78+ top : window . scrollY ,
79+ } ) ;
80+ }
81+ }
82+
83+ return (
84+ < div
85+ { ...props }
86+ style = {
87+ viewport
88+ ? ( {
89+ ...style ,
90+ "--page-indent-bottom" : `${ viewport . bottom } px` ,
91+ "--page-indent-top" : `${ viewport . top } px` ,
92+ } as React . CSSProperties )
93+ : style
94+ }
95+ >
96+ < span
97+ aria-hidden = "true"
98+ className = "ease-fluid pointer-events-none absolute inset-x-0 top-[var(--page-indent-top,0px)] z-40 h-3 bg-[radial-gradient(circle_at_100%_100%,transparent_12px,black_12.5px),radial-gradient(circle_at_0_100%,transparent_12px,black_12.5px)] bg-size-[12px_12px] bg-position-[left_top,right_top] bg-no-repeat opacity-0 transition-opacity duration-200 group-data-active/indent:duration-300 motion-safe:group-data-active/indent:opacity-100"
99+ />
100+ { children }
101+ </ div >
102+ ) ;
103+ } ;
104+
105+ /**
106+ * Wraps the page so bottom drawers (and any drawer with `indent`) shrink it behind
107+ * them: the page scales down around the visible screen, gets rounded top
108+ * corners over a black backdrop and grows back as the drawer is swiped away.
109+ * Fixed elements inside the page are positioned against the scaled page
110+ * while it is indented; pin them to `--page-indent-bottom` /
111+ * `--page-indent-top` under `group-data-active/indent:` (see the mobile tab
112+ * bar). Skipped under reduced motion. Universal.
113+ */
114+ function DrawerIndent ( { children } : { children : React . ReactNode } ) {
115+ return (
116+ < DrawerPrimitive . Provider >
117+ < DrawerPrimitive . IndentBackground className = "ease-fluid pointer-events-none fixed inset-0 bg-black opacity-0 transition-opacity duration-200 data-active:duration-300 motion-safe:data-active:opacity-100" />
118+ < DrawerPrimitive . Indent
119+ className = "group/indent ease-fluid bg-background relative min-h-dvh origin-[50%_var(--page-indent-top,0px)] transition-[scale,translate] duration-200 data-active:duration-300 motion-safe:data-active:translate-y-[calc((1-var(--drawer-swipe-progress))*(env(safe-area-inset-top)+12px))] motion-safe:data-active:scale-[calc(0.94+0.06*var(--drawer-swipe-progress))] [:root:has([data-swiping])_&]:duration-0"
120+ render = { ( props , state ) => (
121+ < IndentedPage { ...props } active = { state . active } />
122+ ) }
123+ >
124+ { children }
125+ </ DrawerPrimitive . Indent >
126+ </ DrawerPrimitive . Provider >
127+ ) ;
43128}
44129
45130/**
@@ -202,6 +287,7 @@ export {
202287 DrawerDescription ,
203288 DrawerFooter ,
204289 DrawerHeader ,
290+ DrawerIndent ,
205291 DrawerOverlay ,
206292 DrawerPortal ,
207293 DrawerTitle ,
0 commit comments