Skip to content

Commit bd131bf

Browse files
committed
feat(drawer): ✨ add DrawerIndent for improved page interaction with bottom drawers
1 parent 89b66a6 commit bd131bf

6 files changed

Lines changed: 132 additions & 15 deletions

File tree

‎packages/vitnode/src/components/ui/drawer.tsx‎

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,101 @@ const asChildProps = (
3030
*/
3131
function 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,

‎packages/vitnode/src/components/ui/hover-card.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function HoverCardContent({
4747
>
4848
<PreviewCardPrimitive.Popup
4949
className={cn(
50-
"bg-popover/90 text-popover-foreground ring-foreground/10 ease-fluid z-50 w-64 origin-(--transform-origin) rounded-xl p-4 text-sm shadow-lg ring-1 outline-hidden backdrop-blur-sm transition-[opacity,scale] duration-150 data-ending-style:scale-95 data-ending-style:opacity-0 data-ending-style:duration-100 data-starting-style:scale-95 data-starting-style:opacity-0 motion-reduce:transition-none",
50+
"bg-popover/90 text-popover-foreground ring-foreground/10 ease-fluid z-50 w-64 origin-(--transform-origin) rounded-xl p-4 text-sm shadow-lg ring-1 outline-hidden backdrop-blur-sm transition-[opacity,scale,translate] duration-200 will-change-[opacity,scale,translate] data-ending-style:scale-98 data-ending-style:opacity-0 data-ending-style:duration-120 data-ending-style:ease-in data-starting-style:scale-96 data-starting-style:opacity-0 data-[side=bottom]:data-ending-style:-translate-y-0.5 data-[side=bottom]:data-starting-style:-translate-y-1 data-[side=left]:data-ending-style:translate-x-0.5 data-[side=left]:data-starting-style:translate-x-1 data-[side=right]:data-ending-style:-translate-x-0.5 data-[side=right]:data-starting-style:-translate-x-1 data-[side=top]:data-ending-style:translate-y-0.5 data-[side=top]:data-starting-style:translate-y-1 motion-reduce:transition-none",
5151
className,
5252
)}
5353
data-slot="hover-card-content"

‎packages/vitnode/src/components/user-preview-card.tsx‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,13 @@ const ModeratorWarnings = ({ userId }: { userId: number }) => {
259259
);
260260
};
261261

262-
const PreviewProfile = ({ user }: { user: UserProfile }) => {
262+
const PreviewProfile = ({
263+
fadeIn,
264+
user,
265+
}: {
266+
fadeIn: boolean;
267+
user: UserProfile;
268+
}) => {
263269
const t = useTranslations("core.profile.preview");
264270
const locale = useLocale();
265271
const now = useNow({ updateInterval: 60_000 });
@@ -274,7 +280,13 @@ const PreviewProfile = ({ user }: { user: UserProfile }) => {
274280
const href = userProfileHref(user.nameCode);
275281

276282
return (
277-
<div className="flex flex-col">
283+
<div
284+
className={cn(
285+
"flex flex-col",
286+
fadeIn &&
287+
"ease-fluid transition-opacity duration-200 motion-reduce:transition-none starting:opacity-0",
288+
)}
289+
>
278290
<div className="relative isolate flex flex-col gap-3 px-4 pt-14 pb-4">
279291
<div
280292
aria-hidden="true"
@@ -350,6 +362,7 @@ const PreviewBody = ({
350362
}) => {
351363
const t = useTranslations("core.profile");
352364
const query = useQuery(userProfileQueryOptions({ nameCode }));
365+
const [replacesSkeleton] = React.useState(query.isPending);
353366
const title =
354367
surface === "sheet" ? (
355368
<DrawerTitle className="sr-only">
@@ -409,7 +422,7 @@ const PreviewBody = ({
409422
return (
410423
<>
411424
{title}
412-
<PreviewProfile user={query.data} />
425+
<PreviewProfile fadeIn={replacesSkeleton} user={query.data} />
413426
</>
414427
);
415428
};

‎packages/vitnode/src/components/user-preview.tsx‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { useQueryClient } from "@tanstack/react-query";
12
import { Link, useRouter } from "@tanstack/react-router";
23
import { cn } from "cn";
34
import React from "react";
45

56
import { useCoarsePointer } from "@/hooks/use-coarse-pointer";
7+
import { userProfileQueryOptions } from "@/views/profile/profile-query";
68

79
import type { UserPreviewSurface } from "./user-preview-card";
810

@@ -46,6 +48,17 @@ const PreviewContent = ({
4648
</React.Suspense>
4749
);
4850

51+
const useWarmPreview = (nameCode: string) => {
52+
const queryClient = useQueryClient();
53+
54+
return () => {
55+
void loadUserPreviewCard();
56+
void queryClient
57+
.query(userProfileQueryOptions({ nameCode }))
58+
.catch(() => undefined);
59+
};
60+
};
61+
4962
const UserPreviewHoverCard = ({
5063
children,
5164
className,
@@ -54,16 +67,15 @@ const UserPreviewHoverCard = ({
5467
style,
5568
title,
5669
}: UserPreviewProps) => {
57-
const [open, setOpen] = React.useState(false);
70+
const warmPreview = useWarmPreview(nameCode);
5871

5972
return (
60-
<HoverCard onOpenChange={setOpen} open={open}>
73+
<HoverCard>
6174
<HoverCardTrigger
6275
closeDelay={150}
6376
delay={400}
64-
onPointerEnter={() => {
65-
void loadUserPreviewCard();
66-
}}
77+
onFocus={warmPreview}
78+
onPointerEnter={warmPreview}
6779
render={
6880
<Link
6981
aria-hidden={decorative ? true : undefined}
@@ -85,7 +97,7 @@ const UserPreviewHoverCard = ({
8597
className="w-72 overflow-hidden p-0 sm:w-80"
8698
side="bottom"
8799
>
88-
{open ? <PreviewContent nameCode={nameCode} surface="card" /> : null}
100+
<PreviewContent nameCode={nameCode} surface="card" />
89101
</HoverCardContent>
90102
</HoverCard>
91103
);
@@ -100,6 +112,7 @@ const UserPreviewSheet = ({
100112
title,
101113
}: UserPreviewProps) => {
102114
const router = useRouter();
115+
const warmPreview = useWarmPreview(nameCode);
103116
const [open, setOpen] = React.useState(false);
104117

105118
// Following a link inside the sheet - "View profile", the name - closes it.
@@ -130,6 +143,8 @@ const UserPreviewSheet = ({
130143
"focus-visible:ring-ring/50 cursor-pointer touch-manipulation rounded-sm text-start outline-none focus-visible:ring-3",
131144
className,
132145
)}
146+
onFocus={warmPreview}
147+
onPointerDown={warmPreview}
133148
style={style}
134149
tabIndex={decorative ? -1 : undefined}
135150
title={title}

‎packages/vitnode/src/views/layouts/theme/header/mobile-nav/mobile-nav-bar.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export const MobileNavBar = ({
9292
</style>
9393
<nav
9494
aria-label={t("label")}
95-
className="bg-card text-card-foreground fixed inset-x-0 bottom-0 z-30 border-t pb-[env(safe-area-inset-bottom)] sm:hidden"
95+
className="bg-card text-card-foreground fixed inset-x-0 bottom-0 z-30 border-t pb-[env(safe-area-inset-bottom)] transition-[top,bottom,translate] transition-discrete delay-200 duration-0 group-data-active/indent:delay-0 motion-safe:group-data-active/indent:top-(--page-indent-bottom) motion-safe:group-data-active/indent:bottom-auto motion-safe:group-data-active/indent:-translate-y-full sm:hidden"
9696
data-slot="mobile-nav"
9797
>
9898
<ul className="mx-auto flex max-w-lg items-stretch">

‎packages/vitnode/src/views/layouts/theme/layout-content.tsx‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { DrawerIndent } from "@/components/ui/drawer";
2+
13
/**
24
* The public site's page frame: `listeners`, then `header`, then `breadcrumb`,
35
* then the page inside `<main>`. Each slot is optional and renders nothing
4-
* when unset. Universal.
6+
* when unset. The frame sits in a `DrawerIndent`, so a bottom drawer opened
7+
* with `indent` shrinks the page behind it. Universal.
58
*
69
* @example
710
* ```tsx
@@ -30,10 +33,10 @@ export const ThemeLayoutContent = ({
3033
/** Invisible components rendered before the header, e.g. event listeners. */
3134
listeners?: React.ReactNode;
3235
}) => (
33-
<>
36+
<DrawerIndent>
3437
{listeners}
3538
{header}
3639
{breadcrumb}
3740
<main>{children}</main>
38-
</>
41+
</DrawerIndent>
3942
);

0 commit comments

Comments
 (0)