Skip to content

Commit edfd121

Browse files
aXenDeveloperclaude
andcommitted
feat(profile): ✨ add a profile preview card for usernames and avatars
Hovering or focusing a linked username or avatar opens a card with the member's cover fading into the card, avatar with an online dot, name, handle and primary role, Reputation / Joined / Last visited stats and a View profile button. Moderators also see the member's warning summary with a link to the ModCP. The card only receives the nameCode and loads the public profile when it opens (shared cache with the profile page), with a skeleton while loading and an empty state for missing or failed profiles. On touch screens a tap opens the same content in a swipeable bottom drawer. The drawer's built-in handle row can now be turned off, and the new `DrawerIndent` scales the page behind an open bottom drawer. All copy lives under `core.profile.preview` keys, and the user format docs page documents the preview with screenshots. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5c545c2 commit edfd121

21 files changed

Lines changed: 1086 additions & 45 deletions
43 KB
Loading
48 KB
Loading

‎apps/web/content/docs/ui/user-format.mdx‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ description: Show a member's name in their role color, with the role's icon or e
44
icon: UserCheck
55
---
66

7+
import { ImgDocs } from "@/components/fumadocs/img";
8+
import previewCard from "./user-format-preview.webp";
9+
import previewSheet from "./user-format-preview-sheet.webp";
10+
711
`UserFormat` prints a member's name the way the rest of the site does. Use it
812
anywhere a name shows up: article bylines, tables, comments, notifications.
913

@@ -89,6 +93,57 @@ Leave `link` off when the name sits inside another link or a button, such as a
8993
table row that already opens something. Nested links are invalid HTML and
9094
confuse screen readers.
9195

96+
## Profile preview
97+
98+
A linked name opens a profile preview when someone rests the pointer on it or
99+
tabs to it. The card shows the member's cover, avatar with an online dot, name,
100+
`@nameCode` and primary role, then their reputation, how long ago they joined
101+
and when they were last here, with a **View profile** button at the bottom.
102+
103+
<ImgDocs alt="A profile preview card under a linked username, with cover, avatar with an online dot, name, role, reputation, join time, last visit and a View profile button" src={previewCard} />
104+
105+
Phones and tablets have no hover, so there a tap opens the same preview as a
106+
bottom sheet. Swipe it down to close it, or tap **View profile** to go on to
107+
the profile.
108+
109+
<ImgDocs alt="The same profile preview as a bottom sheet on a phone, with a drag handle over the cover" src={previewSheet} />
110+
111+
Moderators also see the member's warnings in the preview: their active warning
112+
points, how many warnings are active and in total, and a **Review** link to the
113+
member's warnings in the ModCP. Everyone else never loads them.
114+
115+
You don't pass the preview any data. When it opens, it loads the member's
116+
public profile from `GET /users/profile/{nameCode}` and shows a skeleton until
117+
the answer arrives. The preview shares its cache with the profile page, so
118+
opening it and then going to the profile renders the page right away. Nothing
119+
is fetched for names nobody opens.
120+
121+
The preview is on by default for every `UserFormat` with `link`. Turn it off
122+
with `preview={false}` when the name already sits inside another hover card:
123+
124+
```tsx
125+
<UserFormat format link preview={false} user={user} />
126+
```
127+
128+
### Preview on an avatar
129+
130+
Wrap any element in `UserPreview` to give it the same preview. It renders a
131+
link to the profile (a button that opens the sheet on touch screens), so only
132+
pass the `nameCode`:
133+
134+
```tsx
135+
import { Avatar } from '@vitnode/core/components/avatar'
136+
import { UserPreview } from '@vitnode/core/components/user-preview'
137+
138+
<UserPreview decorative nameCode={user.nameCode}>
139+
<Avatar alt="" size={32} user={user} />
140+
</UserPreview>
141+
```
142+
143+
Add `decorative` when a linked name sits right next to the avatar. It keeps the
144+
duplicate link out of the tab order and away from screen readers, while mouse
145+
and touch users still get the preview.
146+
92147
## Names inside a sentence
93148

94149
`UserFormat` is an `inline-flex` row, which keeps the icon centered on the
@@ -111,6 +166,7 @@ words around it:
111166
| `avatarSize` | `number` | No | `20` | The avatar's width and height in pixels |
112167
| `format` | `boolean` | No | `false` | Adds the role color and prefix |
113168
| `link` | `boolean` | No | `false` | Links to `/users/<nameCode>` when it is set |
169+
| `preview` | `boolean` | No | `true` | With `link`, opens the profile preview (hover card, or bottom sheet on touch) |
114170
| `className` | `string` | No | - | Extra classes on the outer element |
115171

116172
Other `<span>` props go to the outer `<span>`. With `link`, only `className`,

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

Lines changed: 103 additions & 7 deletions
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
/**
@@ -101,13 +186,21 @@ function DrawerOverlay({
101186

102187
/**
103188
* The drawer panel, rendered in a portal over a scrim, with a decorative drag
104-
* handle.
189+
* handle on bottom drawers.
105190
*/
106191
function DrawerContent({
107192
className,
108193
children,
194+
handle = true,
109195
...props
110-
}: DrawerPrimitive.Popup.Props) {
196+
}: DrawerPrimitive.Popup.Props & {
197+
/**
198+
* Renders the drag handle row above `children` on bottom drawers. Turn it
199+
* off to let content such as a cover image reach the top edge and draw your
200+
* own handle over it. Defaults to `true`.
201+
*/
202+
handle?: boolean;
203+
}) {
111204
return (
112205
<DrawerPortal>
113206
<DrawerOverlay />
@@ -124,10 +217,12 @@ function DrawerContent({
124217
data-slot="drawer-content"
125218
{...props}
126219
>
127-
<div
128-
aria-hidden="true"
129-
className="bg-muted mx-auto mt-4 hidden h-1.5 w-24 shrink-0 rounded-full group-data-[swipe-direction=down]/drawer-content:block"
130-
/>
220+
{handle ? (
221+
<div
222+
aria-hidden="true"
223+
className="bg-muted mx-auto mt-4 hidden h-1.5 w-24 shrink-0 rounded-full group-data-[swipe-direction=down]/drawer-content:block"
224+
/>
225+
) : null}
131226
{children}
132227
</DrawerPrimitive.Popup>
133228
</DrawerPrimitive.Viewport>
@@ -192,6 +287,7 @@ export {
192287
DrawerDescription,
193288
DrawerFooter,
194289
DrawerHeader,
290+
DrawerIndent,
195291
DrawerOverlay,
196292
DrawerPortal,
197293
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-format.tsx‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,27 @@ import { cn } from "cn";
44

55
import { Avatar } from "./avatar";
66
import { EmojiIcon } from "./ui/emoji-icon";
7+
import { UserPreview } from "./user-preview";
8+
import { userProfileHref } from "./user-profile-href";
79

8-
const FALLBACK_AVATAR_COLOR = "64748b";
10+
export { userProfileHref } from "./user-profile-href";
911

10-
/** The path of a user's public profile page. */
11-
export const userProfileHref = (nameCode: string): string =>
12-
`/users/${encodeURIComponent(nameCode)}`;
12+
const FALLBACK_AVATAR_COLOR = "64748b";
1313

1414
/**
1515
* A user's name, optionally styled by their role, led by their avatar and
1616
* linked to their profile. Shows `user.fullName` in place of the nickname when
17-
* the API sends one.
17+
* the API sends one. A linked name previews the member's profile - a hover
18+
* card with a mouse, a bottom sheet on touch screens - which fetches its own
19+
* data.
1820
*/
1921
export const UserFormat = ({
2022
user,
2123
avatar = false,
2224
avatarSize = 20,
2325
format,
2426
link,
27+
preview = true,
2528
className,
2629
style,
2730
...props
@@ -38,6 +41,13 @@ export const UserFormat = ({
3841
format?: boolean;
3942
/** Links to the profile page; needs `user.nameCode`. */
4043
link?: boolean;
44+
/**
45+
* With `link`, previews the member's profile: a hover card when the link is
46+
* hovered or focused, a bottom sheet when it is tapped on a touch screen
47+
* (the tap then opens the sheet instead of the profile). Turn it off inside
48+
* another hover card. Defaults to `true`.
49+
*/
50+
preview?: boolean;
4151
user: {
4252
/** The letter avatar's background, a hex color without `#`. */
4353
avatarColor?: string;
@@ -83,6 +93,19 @@ export const UserFormat = ({
8393
</>
8494
);
8595

96+
if (link && user.nameCode && preview) {
97+
return (
98+
<UserPreview
99+
className={cn(classes, "underline-offset-4 hover:underline")}
100+
nameCode={user.nameCode}
101+
style={colorStyle}
102+
title={props.title}
103+
>
104+
{content}
105+
</UserPreview>
106+
);
107+
}
108+
86109
if (link && user.nameCode) {
87110
return (
88111
<Link

0 commit comments

Comments
 (0)