Skip to content
Source
Sign inSign up
fuko/trytanstackdb-v2
Open History to see the last commit

editor-toolbar.tsx

146 lines4.6 KB

1import {
2 ChevronsRight,
3 Ellipsis,
4 Link as LinkIcon,
5 ListChecks,
6 Paperclip,
7 PenTool,
8 Search,
9 Share,
10 SquarePen,
11 Table,
12 UserRoundPlus,
13} from "lucide-react";
14import type { ReactNode } from "react";
15​
16import {
17 DropdownMenu,
18 DropdownMenuContent,
19 DropdownMenuItem,
20 DropdownMenuTrigger,
21} from "@/components/ui/dropdown-menu";
22import { useElementWidth } from "@/hooks/use-element-width";
23import { cn } from "@/lib/utils";
24​
25import { MacCircleButton, MacPill, MacPillButton } from "./controls";
26import { FORMAT_ITEMS, planToolbar, TOOLBAR_METRICS } from "./toolbar-plan";
27import type { ToolbarItemId } from "./toolbar-plan";
28​
29const ITEMS: Record<ToolbarItemId, { label: string; icon: ReactNode }> = {
30 format: {
31 label: "Format",
32 icon: <span className="text-[15px] font-medium tracking-tight">Aa</span>,
33 },
34 checklist: { label: "Checklist", icon: <ListChecks /> },
35 table: { label: "Table", icon: <Table /> },
36 attachment: { label: "Attach file", icon: <Paperclip /> },
37 markup: { label: "Markup", icon: <PenTool /> },
38 link: { label: "Add link", icon: <LinkIcon /> },
39 collaborate: { label: "Collaborate", icon: <UserRoundPlus /> },
40 share: { label: "Share", icon: <Share /> },
41 more: { label: "More", icon: <Ellipsis /> },
42};
43​
44/**
45 * The editor half of the window toolbar. It measures its own width (the app
46 * pane is resizable) and collapses items into "»" when they don't fit.
47 */
48export function EditorToolbar() {
49 const [ref, width] = useElementWidth<HTMLDivElement>();
50 const plan = planToolbar(width ?? Number.POSITIVE_INFINITY);
51 const shown = (id: ToolbarItemId) => plan.visible.has(id);
52 const formatItems = FORMAT_ITEMS.filter(shown);
53 const trailingPill = (["share", "more"] as const).filter(shown);
54​
55 return (
56 <div
57 ref={ref}
58 role="toolbar"
59 aria-label="Note"
60 className="grid h-[52px] w-full min-w-0 shrink-0 grid-cols-[minmax(auto,1fr)_auto_minmax(auto,1fr)] items-center"
61 style={{ paddingInline: TOOLBAR_METRICS.padding, columnGap: TOOLBAR_METRICS.gap }}
62 >
63 <div className="flex items-center">
64 <MacCircleButton aria-label="New note" title="New note">
65 <SquarePen />
66 </MacCircleButton>
67 </div>
68​
69 <div className="flex justify-center">
70 {formatItems.length > 0 && (
71 <MacPill>
72 {formatItems.map((id) => (
73 <MacPillButton key={id} aria-label={ITEMS[id].label} title={ITEMS[id].label}>
74 {ITEMS[id].icon}
75 </MacPillButton>
76 ))}
77 </MacPill>
78 )}
79 </div>
80​
81 <div className="flex items-center justify-end" style={{ gap: TOOLBAR_METRICS.gap }}>
82 {shown("collaborate") && (
83 <MacCircleButton aria-label={ITEMS.collaborate.label} title={ITEMS.collaborate.label}>
84 {ITEMS.collaborate.icon}
85 </MacCircleButton>
86 )}
87 {trailingPill.length > 0 && (
88 <MacPill>
89 {trailingPill.map((id) => (
90 <MacPillButton key={id} aria-label={ITEMS[id].label} title={ITEMS[id].label}>
91 {ITEMS[id].icon}
92 </MacPillButton>
93 ))}
94 </MacPill>
95 )}
96 {plan.overflow.length > 0 && <OverflowMenu items={plan.overflow} />}
97 <SearchControl field={plan.searchField} />
98 </div>
99 </div>
100 );
102​
103function OverflowMenu({ items }: { items: ToolbarItemId[] }) {
104 return (
105 <DropdownMenu>
106 <DropdownMenuTrigger asChild>
107 <MacCircleButton aria-label="More toolbar items" title="More toolbar items">
108 <ChevronsRight />
109 </MacCircleButton>
110 </DropdownMenuTrigger>
111 <DropdownMenuContent align="end" className="min-w-44">
112 {items.map((id) => (
113 <DropdownMenuItem key={id} className="[&_svg]:text-muted-foreground">
114 <span className="grid w-5 place-items-center [&>span]:text-xs">{ITEMS[id].icon}</span>
115 {ITEMS[id].label}
116 </DropdownMenuItem>
117 ))}
118 </DropdownMenuContent>
119 </DropdownMenu>
120 );
122​
123function SearchControl({ field }: { field: boolean }) {
124 if (!field) {
125 return (
126 <MacCircleButton aria-label="Search" title="Search">
127 <Search />
128 </MacCircleButton>
129 );
130 }
131 return (
132 <label
133 className={cn(
134 "flex h-[34px] items-center gap-1.5 rounded-full border border-mac-control-border bg-mac-control px-3 text-mac-icon",
135 )}
136 style={{ width: TOOLBAR_METRICS.searchField }}
137 >
138 <Search className="size-[15px] shrink-0" />
139 <input
140 type="search"
141 placeholder="Search"
142 className="min-w-0 flex-1 bg-transparent text-[13px] text-mac-text outline-none placeholder:text-mac-text-secondary"
143 />
144 </label>
145 );