forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

table-row-actions.tsx82 lines, 3.5 KiBHistoryRawDownload
1import type { JSX } from "solid-js";
2import { For, Show } from "solid-js";
3import {
4 DropdownMenu,
5 DropdownMenuContent,
6 DropdownMenuItem,
7 DropdownMenuTrigger,
8} from "@/components/ui/dropdown-menu";
9import { IconDotsVertical } from "@/components/ui/icons";
10import { useT } from "@/stores/preferences-context";
11
12type TableRowAction = {
13 label: string;
14 icon: JSX.Element;
15 onSelect: () => void;
16 destructive?: boolean;
17 disabled?: boolean;
18};
19
20export function TableRowActions(props: { label: string; actions: TableRowAction[]; compact?: boolean; triggerLabel?: string }) {
21 const t = useT();
22 const singleAction = () => props.actions.length === 1 ? props.actions[0] : undefined;
23 const run = (action: TableRowAction) => {
24 if (action.disabled) return;
25 setTimeout(action.onSelect, 0);
26 };
27
28 return (
29 <div class="flex justify-center">
30 <Show
31 when={singleAction()}
32 fallback={<DropdownMenu placement="bottom-end" gutter={6}>
33 <DropdownMenuTrigger
34 class={props.compact
35 ? "inline-flex h-7 w-7 cursor-pointer items-center justify-center rounded-md border-0 bg-transparent text-muted-foreground outline-hidden transition-colors hover:bg-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring data-expanded:bg-muted data-expanded:text-foreground"
36 : "inline-flex h-8 min-w-[78px] cursor-pointer items-center justify-center gap-1.5 rounded-lg border border-border/80 bg-muted/45 px-2 text-xs font-semibold text-foreground outline-hidden transition-colors hover:border-primary/30 hover:bg-primary/8 hover:text-primary-text focus-visible:ring-2 focus-visible:ring-ring data-expanded:border-primary/30 data-expanded:bg-primary/10 data-expanded:text-primary-text"}
37 aria-label={props.label}
38 title={props.label}
39 >
40 {!props.compact && <span>{props.triggerLabel ?? t("common.action")}</span>}
41 <IconDotsVertical class="h-3.5 w-3.5" />
42 </DropdownMenuTrigger>
43 <DropdownMenuContent class="w-48">
44 <For each={props.actions}>
45 {(action) => (
46 <DropdownMenuItem
47 class="flex items-center gap-2.5 text-xs"
48 destructive={action.destructive}
49 disabled={action.disabled}
50 // Defer to the next macrotask so the menu fully closes (and
51 // restores focus to its trigger) before the action opens a
52 // panel/dialog — otherwise the non-modal SidePanel reads that
53 // focus-restore as an outside interaction and instantly closes.
54 onSelect={() => setTimeout(action.onSelect, 0)}
55 >
56 {action.icon}
57 <span>{action.label}</span>
58 </DropdownMenuItem>
59 )}
60 </For>
61 </DropdownMenuContent>
62 </DropdownMenu>}
63 >
64 {(action) => (
65 <button
66 type="button"
67 class="inline-flex h-8 w-8 items-center justify-center rounded-lg border border-transparent text-muted-foreground outline-hidden transition-colors hover:border-primary/20 hover:bg-primary/8 hover:text-primary-text focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50"
68 aria-label={action().label}
69 title={action().label}
70 disabled={action().disabled}
71 onClick={(event) => {
72 event.stopPropagation();
73 run(action());
74 }}
75 >
76 {action().icon}
77 </button>
78 )}
79 </Show>
80 </div>
81 );
82}