Skip to content

Commit e400d64

Browse files
committed
feat(drawer): ✨ enhance IndentedPage with useRender for improved rendering
1 parent edfd121 commit e400d64

6 files changed

Lines changed: 60 additions & 68 deletions

File tree

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

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Drawer as DrawerPrimitive } from "@base-ui/react/drawer";
2+
import { useRender } from "@base-ui/react/use-render";
23
import { cn } from "cn";
34
import React from "react";
45

@@ -56,50 +57,40 @@ function Drawer({
5657
);
5758
}
5859

59-
interface IndentViewport {
60-
bottom: number;
61-
top: number;
62-
}
63-
6460
const IndentedPage = ({
6561
active,
6662
children,
67-
style,
63+
ref,
6864
...props
6965
}: React.ComponentProps<"div"> & { active: boolean }) => {
70-
const [wasActive, setWasActive] = React.useState(active);
71-
const [viewport, setViewport] = React.useState<IndentViewport | null>(null);
66+
const pageRef = React.useRef<HTMLDivElement>(null);
7267

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-
}
68+
React.useLayoutEffect(() => {
69+
const page = pageRef.current;
70+
if (!active || !page) return;
8271

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-
);
72+
page.style.setProperty(
73+
"--page-indent-bottom",
74+
`${window.scrollY + window.innerHeight}px`,
75+
);
76+
page.style.setProperty("--page-indent-top", `${window.scrollY}px`);
77+
}, [active]);
78+
79+
return useRender({
80+
props: {
81+
...props,
82+
children: (
83+
<>
84+
<span
85+
aria-hidden="true"
86+
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"
87+
/>
88+
{children}
89+
</>
90+
),
91+
},
92+
ref: ref ? [ref, pageRef] : pageRef,
93+
});
10394
};
10495

10596
/**

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ import { userProfileHref } from "./user-profile-href";
4949
const USER_PREVIEW_NAMESPACES = ["core.global", "core.profile"] as const;
5050
const AVATAR_SIZE = 56;
5151
const PRESENCE_DOT_SIZE = 12;
52+
const CIRCLE_EDGE_INSET_AT_45_DEG = (1 - Math.SQRT1_2) / 2;
5253

53-
// Eased stops: a two-stop fade bands and leaves a visible edge mid-cover.
54-
const COVER_FADE =
54+
const EASED_COVER_FADE =
5555
"linear-gradient(to bottom, black 0%, rgb(0 0 0 / 0.85) 30%, rgb(0 0 0 / 0.45) 60%, rgb(0 0 0 / 0.12) 85%, transparent 100%)";
5656

5757
/** Where the preview renders: a hover card, or a bottom sheet on touch screens. */
@@ -65,7 +65,6 @@ const ELAPSED_UNITS = [
6565
["minute", 60],
6666
] as const;
6767

68-
/** Short elapsed time since `date`, like "7 yrs" or "14 hr". */
6968
const formatElapsed = (date: Date | string, now: Date, locale: string) => {
7069
const seconds = Math.max(
7170
0,
@@ -103,8 +102,9 @@ const PresenceAvatar = ({
103102
})
104103
: t("offline");
105104
}
106-
// The dot's centre sits on the circle's edge at 45°, ~14.6% in from the box.
107-
const offset = Math.round(AVATAR_SIZE * 0.146 - PRESENCE_DOT_SIZE / 2);
105+
const offset = Math.round(
106+
AVATAR_SIZE * CIRCLE_EDGE_INSET_AT_45_DEG - PRESENCE_DOT_SIZE / 2,
107+
);
108108

109109
return (
110110
<span className="relative inline-flex size-fit shrink-0">
@@ -169,7 +169,6 @@ const LastVisited = ({ presence }: { presence: undefined | UserPresence }) => {
169169
);
170170
};
171171

172-
/** Moderators only; the API answers 403 to everyone else anyway. */
173172
const ModeratorWarnings = ({ userId }: { userId: number }) => {
174173
const t = useTranslations("core.profile.preview.warnings");
175174
const tPreview = useTranslations("core.profile.preview");
@@ -184,7 +183,6 @@ const ModeratorWarnings = ({ userId }: { userId: number }) => {
184183
}
185184

186185
if (query.isError) {
187-
// A moderator without the warnings permission: nothing to show or retry.
188186
if (
189187
query.error instanceof ModcpRequestError &&
190188
query.error.status === 403
@@ -278,6 +276,7 @@ const PreviewProfile = ({
278276
enabled: !isSessionPending,
279277
});
280278
const href = userProfileHref(user.nameCode);
279+
const reputation = user.reputation ?? 0;
281280

282281
return (
283282
<div
@@ -294,8 +293,8 @@ const PreviewProfile = ({
294293
data-slot="user-preview-cover"
295294
style={{
296295
...userCoverStyle(user.avatarColor),
297-
maskImage: COVER_FADE,
298-
WebkitMaskImage: COVER_FADE,
296+
maskImage: EASED_COVER_FADE,
297+
WebkitMaskImage: EASED_COVER_FADE,
299298
}}
300299
>
301300
<UserCoverImage url={user.coverUrl} />
@@ -319,11 +318,11 @@ const PreviewProfile = ({
319318
<Stat label={t("reputation")}>
320319
<ShortNumber
321320
className={cn(
322-
(user.reputation ?? 0) > 0 && FEEDBACK_TONE_TEXT.success,
323-
(user.reputation ?? 0) < 0 && FEEDBACK_TONE_TEXT.destructive,
321+
reputation > 0 && FEEDBACK_TONE_TEXT.success,
322+
reputation < 0 && FEEDBACK_TONE_TEXT.destructive,
324323
)}
325324
formatOptions={{ signDisplay: "exceptZero" }}
326-
value={user.reputation ?? 0}
325+
value={reputation}
327326
/>
328327
</Stat>
329328
<Stat label={t("joined")}>
@@ -439,9 +438,7 @@ export const UserPreviewCard = ({
439438
nameCode: string;
440439
surface?: UserPreviewSurface;
441440
}) => (
442-
<React.Suspense fallback={<UserPreviewSkeleton />}>
443-
<RouteMessages namespaces={USER_PREVIEW_NAMESPACES}>
444-
<PreviewBody nameCode={nameCode} surface={surface} />
445-
</RouteMessages>
446-
</React.Suspense>
441+
<RouteMessages namespaces={USER_PREVIEW_NAMESPACES}>
442+
<PreviewBody nameCode={nameCode} surface={surface} />
443+
</RouteMessages>
447444
);

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ const UserPreviewSheet = ({
115115
const warmPreview = useWarmPreview(nameCode);
116116
const [open, setOpen] = React.useState(false);
117117

118-
// Following a link inside the sheet - "View profile", the name - closes it.
119118
React.useEffect(
120119
() =>
121120
router.subscribe("onBeforeNavigate", () => {
@@ -124,14 +123,13 @@ const UserPreviewSheet = ({
124123
[router],
125124
);
126125

127-
// Kept after the first open, so the sheet still has content while it slides away.
128-
const [opened, setOpened] = React.useState(false);
126+
const [hasOpened, setHasOpened] = React.useState(false);
129127

130128
return (
131129
<Drawer
132130
onOpenChange={next => {
133131
setOpen(next);
134-
if (next) setOpened(true);
132+
if (next) setHasOpened(true);
135133
}}
136134
open={open}
137135
>
@@ -159,13 +157,12 @@ const UserPreviewSheet = ({
159157
handle={false}
160158
style={{ paddingBottom: "env(safe-area-inset-bottom)" }}
161159
>
162-
{/* Drawn over the cover, which runs to the sheet's top edge. */}
163160
<div
164161
aria-hidden="true"
165162
className="pointer-events-none absolute inset-x-0 top-2.5 z-10 mx-auto h-1.5 w-12 rounded-full bg-black/25 dark:bg-white/40"
166163
/>
167164
<div className="mx-auto w-full max-w-md overflow-y-auto">
168-
{opened ? (
165+
{hasOpened ? (
169166
<PreviewContent nameCode={nameCode} surface="sheet" />
170167
) : null}
171168
</div>

‎packages/vitnode/src/views/admin/views/core/users/detail/user-account-panels.test.tsx‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ import {
77
within,
88
} from "@testing-library/react";
99
import { IntlProvider } from "use-intl";
10-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
10+
import {
11+
afterEach,
12+
beforeAll,
13+
beforeEach,
14+
describe,
15+
expect,
16+
it,
17+
vi,
18+
} from "vitest";
1119

1220
import type { SsoConnectionsApi } from "@/views/auth/settings/sso/sso-connections-query";
1321
import type { NotificationPreferenceTypeView } from "@/views/notifications/notifications-query";
@@ -127,6 +135,10 @@ const requestOf = (index: number) => {
127135
};
128136
};
129137

138+
beforeAll(async () => {
139+
await import("./user-devices-sheet");
140+
});
141+
130142
beforeEach(() => {
131143
fetchMock.mockReset();
132144
vi.stubGlobal("fetch", fetchMock);

‎plugins/blog/src/pages/post-comment-thread.tsx‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,7 @@ const CommentAvatar = ({ node }: { node: CommentTreeNode }) =>
145145
decorative
146146
nameCode={node.record.author.nameCode}
147147
>
148-
<Avatar
149-
alt=""
150-
className="size-8 shrink-0"
151-
size={32}
152-
user={node.record.author}
153-
/>
148+
<Avatar alt="" className="size-8" size={32} user={node.record.author} />
154149
</UserPreview>
155150
) : (
156151
<span

‎plugins/forum/src/pages/forum-ui.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export const PersonAvatar = ({
117117
>
118118
<Avatar
119119
alt=""
120-
className={cn("shrink-0", AVATAR_SIZE_CLASS[size])}
120+
className={AVATAR_SIZE_CLASS[size]}
121121
size={size}
122122
user={person}
123123
/>

0 commit comments

Comments
 (0)