Skip to content

Commit 7d68dd9

Browse files
Merge pull request #931 from aXenDeveloper/claude/intelligent-lamport-f88uqb
feat: Add user profile preview card on hover/focus
2 parents 5c545c2 + e400d64 commit 7d68dd9

22 files changed

Lines changed: 1079 additions & 46 deletions

File tree

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: 94 additions & 7 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

@@ -30,16 +31,91 @@ const asChildProps = (
3031
*/
3132
function Drawer({
3233
direction = "bottom",
34+
indent = direction === "bottom",
3335
...props
3436
}: Omit<DrawerPrimitive.Root.Props, "swipeDirection"> & {
3537
direction?: DrawerDirection;
38+
/**
39+
* Shrinks the page behind the drawer while it is open, following the
40+
* swipe, like an iOS sheet. Needs a `DrawerIndent` around the page (the
41+
* public theme layout has one); without it nothing changes. Defaults to
42+
* `true` for bottom drawers and `false` for the other directions.
43+
*/
44+
indent?: boolean;
3645
}) {
37-
return (
46+
const root = (
3847
<DrawerPrimitive.Root
3948
swipeDirection={SWIPE_DIRECTION[direction]}
4049
{...props}
4150
/>
4251
);
52+
53+
return indent ? (
54+
root
55+
) : (
56+
<DrawerPrimitive.Provider>{root}</DrawerPrimitive.Provider>
57+
);
58+
}
59+
60+
const IndentedPage = ({
61+
active,
62+
children,
63+
ref,
64+
...props
65+
}: React.ComponentProps<"div"> & { active: boolean }) => {
66+
const pageRef = React.useRef<HTMLDivElement>(null);
67+
68+
React.useLayoutEffect(() => {
69+
const page = pageRef.current;
70+
if (!active || !page) return;
71+
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+
});
94+
};
95+
96+
/**
97+
* Wraps the page so bottom drawers (and any drawer with `indent`) shrink it behind
98+
* them: the page scales down around the visible screen, gets rounded top
99+
* corners over a black backdrop and grows back as the drawer is swiped away.
100+
* Fixed elements inside the page are positioned against the scaled page
101+
* while it is indented; pin them to `--page-indent-bottom` /
102+
* `--page-indent-top` under `group-data-active/indent:` (see the mobile tab
103+
* bar). Skipped under reduced motion. Universal.
104+
*/
105+
function DrawerIndent({ children }: { children: React.ReactNode }) {
106+
return (
107+
<DrawerPrimitive.Provider>
108+
<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" />
109+
<DrawerPrimitive.Indent
110+
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"
111+
render={(props, state) => (
112+
<IndentedPage {...props} active={state.active} />
113+
)}
114+
>
115+
{children}
116+
</DrawerPrimitive.Indent>
117+
</DrawerPrimitive.Provider>
118+
);
43119
}
44120

45121
/**
@@ -101,13 +177,21 @@ function DrawerOverlay({
101177

102178
/**
103179
* The drawer panel, rendered in a portal over a scrim, with a decorative drag
104-
* handle.
180+
* handle on bottom drawers.
105181
*/
106182
function DrawerContent({
107183
className,
108184
children,
185+
handle = true,
109186
...props
110-
}: DrawerPrimitive.Popup.Props) {
187+
}: DrawerPrimitive.Popup.Props & {
188+
/**
189+
* Renders the drag handle row above `children` on bottom drawers. Turn it
190+
* off to let content such as a cover image reach the top edge and draw your
191+
* own handle over it. Defaults to `true`.
192+
*/
193+
handle?: boolean;
194+
}) {
111195
return (
112196
<DrawerPortal>
113197
<DrawerOverlay />
@@ -124,10 +208,12 @@ function DrawerContent({
124208
data-slot="drawer-content"
125209
{...props}
126210
>
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-
/>
211+
{handle ? (
212+
<div
213+
aria-hidden="true"
214+
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"
215+
/>
216+
) : null}
131217
{children}
132218
</DrawerPrimitive.Popup>
133219
</DrawerPrimitive.Viewport>
@@ -192,6 +278,7 @@ export {
192278
DrawerDescription,
193279
DrawerFooter,
194280
DrawerHeader,
281+
DrawerIndent,
195282
DrawerOverlay,
196283
DrawerPortal,
197284
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)