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
2 changes: 1 addition & 1 deletion src/components/community-sections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function CommunitySections({ section, children, actions, disabled = false
const navigate = useNavigate();
return <Tabs value={section} onValueChange={value => { onChange?.(); navigate(value === "events" ? "/events" : value === "submissions" ? "/community?tab=questions&mine=1" : `/community?tab=${value}`); }}>
<div className="flex min-w-0 flex-wrap items-center justify-between gap-3">
<TabsList aria-label="Community sections" className="h-auto max-w-full flex-wrap"><TabsTrigger className="min-h-11 focus-visible:ring-2 focus-visible:ring-foreground/40" disabled={disabled} value="discussions">Discussions</TabsTrigger><TabsTrigger className="min-h-11 focus-visible:ring-2 focus-visible:ring-foreground/40" disabled={disabled} value="questions">Questions</TabsTrigger><TabsTrigger className="min-h-11 focus-visible:ring-2 focus-visible:ring-foreground/40" disabled={disabled} value="events">Events</TabsTrigger><TabsTrigger className="min-h-11 focus-visible:ring-2 focus-visible:ring-foreground/40" disabled={disabled} value="submissions">My submissions</TabsTrigger></TabsList>
<TabsList aria-label="Community sections"><TabsTrigger className="min-h-11 focus-visible:ring-2 focus-visible:ring-foreground/40" disabled={disabled} value="discussions">Discussions</TabsTrigger><TabsTrigger className="min-h-11 focus-visible:ring-2 focus-visible:ring-foreground/40" disabled={disabled} value="questions">Questions</TabsTrigger><TabsTrigger className="min-h-11 focus-visible:ring-2 focus-visible:ring-foreground/40" disabled={disabled} value="events">Events</TabsTrigger><TabsTrigger className="min-h-11 focus-visible:ring-2 focus-visible:ring-foreground/40" disabled={disabled} value="submissions">My submissions</TabsTrigger></TabsList>
{actions && <div className="ml-auto shrink-0">{actions}</div>}
</div>
<TabsContent value={section}>{children}</TabsContent>
Expand Down
6 changes: 1 addition & 5 deletions src/components/library/code-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { toast } from "sonner";
import { highlightLines, TOKEN_CLASS } from "./highlight";

export async function copyText(text: string, done: string) {

Check warning on line 6 in src/components/library/code-view.tsx

View workflow job for this annotation

GitHub Actions / validate

Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components

Check warning on line 6 in src/components/library/code-view.tsx

View workflow job for this annotation

GitHub Actions / validate

Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components
try {
await navigator.clipboard.writeText(text);
toast.success(done);
Expand All @@ -18,15 +18,11 @@
export function CodeView({ code, label, maxHeight = "32rem" }: { code: string; label: string; maxHeight?: string }) {
const [copied, setCopied] = useState(false);
const lines = useMemo(() => highlightLines(code.trimEnd()), [code]);
const extension = /\.(\w+)$/.exec(label)?.[1];

return (
<figure className="overflow-hidden rounded-[22px] bg-[hsl(var(--foreground)/0.04)]">
<figcaption className="flex items-center justify-between gap-3 bg-foreground/[0.03] py-2 pl-4 pr-2 text-xs text-t2">
<span className="flex min-w-0 items-center gap-2">
{extension && <span className="rounded-md bg-[hsl(var(--syn-tag)/0.14)] px-1.5 py-0.5 font-mono text-[10px] font-medium uppercase text-[hsl(var(--syn-tag))]" aria-hidden>{extension}</span>}
<span className="truncate font-mono">{label}</span>
</span>
<span className="truncate font-mono">{label}</span>
<button
type="button"
aria-label={`Copy ${label}`}
Expand Down
91 changes: 91 additions & 0 deletions src/components/library/copy-menu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { useEffect, useRef, useState } from "react";
import { AnimatePresence, motion } from "motion/react";
import { AltArrowDownIcon } from "@solar-icons/react/linear";
import { MetalButton } from "@/components/metal-button";
import { cn } from "@/lib/utils";
import { copyText } from "./code-view";

export interface CopyOption { id: string; label: string; text: () => string; done: string }

/**
* One metal button for copying: the label copies the current choice (the first
* option until another is picked), the arrow opens the other choices.
*/
export function CopyMenu({ options }: { options: CopyOption[] }) {
const [current, setCurrent] = useState(options[0].id);
const [open, setOpen] = useState(false);
const root = useRef<HTMLDivElement>(null);
const trigger = useRef<HTMLButtonElement>(null);
const menu = useRef<HTMLDivElement>(null);
const active = options.find((option) => option.id === current) ?? options[0];

useEffect(() => {
if (!open) return;
menu.current?.querySelector<HTMLButtonElement>("[role=menuitem]")?.focus();
const close = (event: PointerEvent) => { if (!root.current?.contains(event.target as Node)) setOpen(false); };
const key = (event: KeyboardEvent) => {
if (event.key === "Escape") { setOpen(false); trigger.current?.focus(); }
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
event.preventDefault();
const items = [...menu.current?.querySelectorAll<HTMLButtonElement>("[role=menuitem]") ?? []];
const at = items.indexOf(document.activeElement as HTMLButtonElement);
items[(at + (event.key === "ArrowDown" ? 1 : -1) + items.length) % items.length]?.focus();
}
};
document.addEventListener("pointerdown", close);
document.addEventListener("keydown", key);
return () => { document.removeEventListener("pointerdown", close); document.removeEventListener("keydown", key); };
}, [open]);

const copy = (option: CopyOption) => void copyText(option.text(), option.done);

return (
<div ref={root} className="relative">
<MetalButton asChild className="h-10" innerClassName="gap-0 p-0">
<div role="group" aria-label="Copy">
<button type="button" onClick={() => copy(active)} className="flex h-full items-center whitespace-nowrap pl-4 pr-3 text-sm [outline-style:none] focus-visible:underline focus-visible:underline-offset-4">
{active.label}
</button>
<span aria-hidden className="h-4 w-px shrink-0 bg-current opacity-20" />
<button
ref={trigger}
type="button"
aria-label="More copy options"
aria-haspopup="menu"
aria-expanded={open}
onClick={() => setOpen((value) => !value)}
className="flex h-full items-center pl-2 pr-3 [outline-style:none] focus-visible:opacity-70"
>
<AltArrowDownIcon className={cn("size-4 transition-transform duration-200", open && "rotate-180")} aria-hidden />
</button>
</div>
</MetalButton>
<AnimatePresence>
{open && (
<motion.div
ref={menu}
role="menu"
aria-label="Copy"
initial={{ opacity: 0, y: -4, scale: 0.98 }}
animate={{ opacity: 1, y: 0, scale: 1 }}
exit={{ opacity: 0, y: -4, scale: 0.98 }}
transition={{ duration: 0.14 }}
className="absolute right-0 top-full z-30 mt-2 min-w-44 origin-top-right rounded-2xl bg-[hsl(var(--alva-card))] p-1.5"
>
{options.map((option) => (
<button
key={option.id}
type="button"
role="menuitem"
onClick={() => { setCurrent(option.id); setOpen(false); copy(option); trigger.current?.focus(); }}
className={cn("flex min-h-10 w-full items-center rounded-xl px-3 text-left text-sm [outline-style:none] transition-colors hover:bg-foreground/[0.08] focus-visible:bg-foreground/[0.08]", option.id === current ? "text-foreground" : "text-t2")}
>
{option.label}
</button>
))}
</motion.div>
)}
</AnimatePresence>
</div>
);
}
10 changes: 7 additions & 3 deletions src/components/library/scaled-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@ import { useLayoutEffect, useRef, useState, type ReactNode } from "react";
/**
* Renders children at a fixed design width and scales them down to fit, so a
* full-width component keeps its real layout in a smaller frame. As a card
* thumbnail it's purely visual; with `interactive` it stays usable and the
* frame takes the scaled height of its content.
* thumbnail it's purely visual and the content gets the frame's full (scaled)
* height to fill; with `interactive` it stays usable and the frame takes the
* scaled height of its content.
*/
export function ScaledPreview({ width, children, className, interactive = false }: { width: number; children: ReactNode; className?: string; interactive?: boolean }) {
const frame = useRef<HTMLDivElement>(null);
const content = useRef<HTMLDivElement>(null);
const [scale, setScale] = useState(0.4);
const [height, setHeight] = useState<number | undefined>(undefined);
const [frameHeight, setFrameHeight] = useState<number | undefined>(undefined);

useLayoutEffect(() => {
const element = frame.current;
const inner = content.current;
if (!element || !inner) return;
const update = () => {
const next = Math.min(1, element.clientWidth / width);
if (next <= 0) return;
setScale(next);
if (interactive) setHeight(inner.offsetHeight * next);
else setFrameHeight(element.clientHeight / next);
};
update();
const observer = new ResizeObserver(update);
Expand All @@ -35,7 +39,7 @@ export function ScaledPreview({ width, children, className, interactive = false
style={interactive ? { height } : undefined}
{...(interactive ? {} : { "aria-hidden": true, inert: true })}
>
<div ref={content} className="absolute left-0 top-0 origin-top-left" style={{ width, transform: `scale(${scale})` }}>
<div ref={content} className="absolute left-0 top-0 origin-top-left" style={{ width, height: interactive ? undefined : frameHeight, transform: `scale(${scale})` }}>
{children}
</div>
</div>
Expand Down
5 changes: 3 additions & 2 deletions src/components/ui/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { cn } from "@/lib/utils"

const Tabs = TabsPrimitive.Root

/** Tab rows stay on one line on every screen; when they don't fit, they slide sideways instead of wrapping. */
const TabsList = React.forwardRef<
React.ElementRef<typeof TabsPrimitive.List>,
React.ComponentPropsWithoutRef<typeof TabsPrimitive.List>
>(({ className, ...props }, ref) => (
<TabsPrimitive.List
ref={ref}
className={cn(
"relative inline-flex h-auto items-center justify-center gap-1 rounded-full bg-[hsl(var(--field-tone)/0.06)] p-1 text-t3",
"relative inline-flex h-auto min-w-0 max-w-full flex-nowrap items-center justify-start gap-1 overflow-x-auto overscroll-x-contain rounded-full bg-[hsl(var(--field-tone)/0.06)] p-1 text-t3 [scrollbar-width:none] [&::-webkit-scrollbar]:hidden",
className
)}
{...props}
Expand All @@ -27,7 +28,7 @@ const TabsTrigger = React.forwardRef<
<TabsPrimitive.Trigger
ref={ref}
className={cn(
"relative inline-flex h-8 items-center justify-center whitespace-nowrap rounded-full px-3.5 text-[13px] sm:h-9 sm:px-4 sm:text-sm font-medium outline-none transition-colors duration-300 hover:text-t2 focus-visible:outline-none focus-visible:text-foreground disabled:pointer-events-none disabled:opacity-50 data-[state=active]:text-foreground",
"relative inline-flex h-8 shrink-0 items-center justify-center whitespace-nowrap rounded-full px-3.5 text-[13px] sm:h-9 sm:px-4 sm:text-sm font-medium outline-none transition-colors duration-300 hover:text-t2 focus-visible:outline-none focus-visible:text-foreground disabled:pointer-events-none disabled:opacity-50 data-[state=active]:text-foreground",
className
)}
{...props}
Expand Down
Loading
Loading