Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions apps/web/content/docs/ui/user-format.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ description: Show a member's name in their role color, with the role's icon or e
icon: UserCheck
---

import { ImgDocs } from "@/components/fumadocs/img";
import previewCard from "./user-format-preview.webp";
import previewSheet from "./user-format-preview-sheet.webp";

`UserFormat` prints a member's name the way the rest of the site does. Use it
anywhere a name shows up: article bylines, tables, comments, notifications.

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

## Profile preview

A linked name opens a profile preview when someone rests the pointer on it or
tabs to it. The card shows the member's cover, avatar with an online dot, name,
`@nameCode` and primary role, then their reputation, how long ago they joined
and when they were last here, with a **View profile** button at the bottom.

<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} />

Phones and tablets have no hover, so there a tap opens the same preview as a
bottom sheet. Swipe it down to close it, or tap **View profile** to go on to
the profile.

<ImgDocs alt="The same profile preview as a bottom sheet on a phone, with a drag handle over the cover" src={previewSheet} />

Moderators also see the member's warnings in the preview: their active warning
points, how many warnings are active and in total, and a **Review** link to the
member's warnings in the ModCP. Everyone else never loads them.

You don't pass the preview any data. When it opens, it loads the member's
public profile from `GET /users/profile/{nameCode}` and shows a skeleton until
the answer arrives. The preview shares its cache with the profile page, so
opening it and then going to the profile renders the page right away. Nothing
is fetched for names nobody opens.

The preview is on by default for every `UserFormat` with `link`. Turn it off
with `preview={false}` when the name already sits inside another hover card:

```tsx
<UserFormat format link preview={false} user={user} />
```

### Preview on an avatar

Wrap any element in `UserPreview` to give it the same preview. It renders a
link to the profile (a button that opens the sheet on touch screens), so only
pass the `nameCode`:

```tsx
import { Avatar } from '@vitnode/core/components/avatar'
import { UserPreview } from '@vitnode/core/components/user-preview'

<UserPreview decorative nameCode={user.nameCode}>
<Avatar alt="" size={32} user={user} />
</UserPreview>
```

Add `decorative` when a linked name sits right next to the avatar. It keeps the
duplicate link out of the tab order and away from screen readers, while mouse
and touch users still get the preview.

## Names inside a sentence

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

Other `<span>` props go to the outer `<span>`. With `link`, only `className`,
Expand Down
101 changes: 94 additions & 7 deletions packages/vitnode/src/components/ui/drawer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Drawer as DrawerPrimitive } from "@base-ui/react/drawer";
import { useRender } from "@base-ui/react/use-render";
import { cn } from "cn";
import React from "react";

Expand Down Expand Up @@ -30,16 +31,91 @@ const asChildProps = (
*/
function Drawer({
direction = "bottom",
indent = direction === "bottom",
...props
}: Omit<DrawerPrimitive.Root.Props, "swipeDirection"> & {
direction?: DrawerDirection;
/**
* Shrinks the page behind the drawer while it is open, following the
* swipe, like an iOS sheet. Needs a `DrawerIndent` around the page (the
* public theme layout has one); without it nothing changes. Defaults to
* `true` for bottom drawers and `false` for the other directions.
*/
indent?: boolean;
}) {
return (
const root = (
<DrawerPrimitive.Root
swipeDirection={SWIPE_DIRECTION[direction]}
{...props}
/>
);

return indent ? (
root
) : (
<DrawerPrimitive.Provider>{root}</DrawerPrimitive.Provider>
);
}

const IndentedPage = ({
active,
children,
ref,
...props
}: React.ComponentProps<"div"> & { active: boolean }) => {
const pageRef = React.useRef<HTMLDivElement>(null);

React.useLayoutEffect(() => {
const page = pageRef.current;
if (!active || !page) return;

page.style.setProperty(
"--page-indent-bottom",
`${window.scrollY + window.innerHeight}px`,
);
page.style.setProperty("--page-indent-top", `${window.scrollY}px`);
}, [active]);

return useRender({
props: {
...props,
children: (
<>
<span
aria-hidden="true"
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"
/>
{children}
</>
),
},
ref: ref ? [ref, pageRef] : pageRef,
});
};

/**
* Wraps the page so bottom drawers (and any drawer with `indent`) shrink it behind
* them: the page scales down around the visible screen, gets rounded top
* corners over a black backdrop and grows back as the drawer is swiped away.
* Fixed elements inside the page are positioned against the scaled page
* while it is indented; pin them to `--page-indent-bottom` /
* `--page-indent-top` under `group-data-active/indent:` (see the mobile tab
* bar). Skipped under reduced motion. Universal.
*/
function DrawerIndent({ children }: { children: React.ReactNode }) {
return (
<DrawerPrimitive.Provider>
<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" />
<DrawerPrimitive.Indent
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"
render={(props, state) => (
<IndentedPage {...props} active={state.active} />
)}
>
{children}
</DrawerPrimitive.Indent>
</DrawerPrimitive.Provider>
);
}

/**
Expand Down Expand Up @@ -101,13 +177,21 @@ function DrawerOverlay({

/**
* The drawer panel, rendered in a portal over a scrim, with a decorative drag
* handle.
* handle on bottom drawers.
*/
function DrawerContent({
className,
children,
handle = true,
...props
}: DrawerPrimitive.Popup.Props) {
}: DrawerPrimitive.Popup.Props & {
/**
* Renders the drag handle row above `children` on bottom drawers. Turn it
* off to let content such as a cover image reach the top edge and draw your
* own handle over it. Defaults to `true`.
*/
handle?: boolean;
}) {
return (
<DrawerPortal>
<DrawerOverlay />
Expand All @@ -124,10 +208,12 @@ function DrawerContent({
data-slot="drawer-content"
{...props}
>
<div
aria-hidden="true"
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"
/>
{handle ? (
<div
aria-hidden="true"
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"
/>
) : null}
{children}
</DrawerPrimitive.Popup>
</DrawerPrimitive.Viewport>
Expand Down Expand Up @@ -192,6 +278,7 @@ export {
DrawerDescription,
DrawerFooter,
DrawerHeader,
DrawerIndent,
DrawerOverlay,
DrawerPortal,
DrawerTitle,
Expand Down
2 changes: 1 addition & 1 deletion packages/vitnode/src/components/ui/hover-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function HoverCardContent({
>
<PreviewCardPrimitive.Popup
className={cn(
"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",
"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",
className,
)}
data-slot="hover-card-content"
Expand Down
33 changes: 28 additions & 5 deletions packages/vitnode/src/components/user-format.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@ import { cn } from "cn";

import { Avatar } from "./avatar";
import { EmojiIcon } from "./ui/emoji-icon";
import { UserPreview } from "./user-preview";
import { userProfileHref } from "./user-profile-href";

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

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

/**
* A user's name, optionally styled by their role, led by their avatar and
* linked to their profile. Shows `user.fullName` in place of the nickname when
* the API sends one.
* the API sends one. A linked name previews the member's profile - a hover
* card with a mouse, a bottom sheet on touch screens - which fetches its own
* data.
*/
export const UserFormat = ({
user,
avatar = false,
avatarSize = 20,
format,
link,
preview = true,
className,
style,
...props
Expand All @@ -38,6 +41,13 @@ export const UserFormat = ({
format?: boolean;
/** Links to the profile page; needs `user.nameCode`. */
link?: boolean;
/**
* With `link`, previews the member's profile: a hover card when the link is
* hovered or focused, a bottom sheet when it is tapped on a touch screen
* (the tap then opens the sheet instead of the profile). Turn it off inside
* another hover card. Defaults to `true`.
*/
preview?: boolean;
user: {
/** The letter avatar's background, a hex color without `#`. */
avatarColor?: string;
Expand Down Expand Up @@ -83,6 +93,19 @@ export const UserFormat = ({
</>
);

if (link && user.nameCode && preview) {
return (
<UserPreview
className={cn(classes, "underline-offset-4 hover:underline")}
nameCode={user.nameCode}
style={colorStyle}
title={props.title}
>
{content}
</UserPreview>
);
}

if (link && user.nameCode) {
return (
<Link
Expand Down
Loading