Repository navigation
Expand file tree
/
Copy pathtable.tsx
More file actions
175 lines (163 loc) · 4.15 KB
/
Copy pathtable.tsx
File metadata and controls
175 lines (163 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"use client";
import clsx from "clsx";
import React from "react";
import { createContext, useContext, useState } from "react";
import { Link } from "./link";
const TableContext = createContext<{
bleed: boolean;
dense: boolean;
grid: boolean;
striped: boolean;
}>({
bleed: false,
dense: false,
grid: false,
striped: false,
});
export function Table({
bleed = false,
dense = false,
grid = false,
striped = false,
className,
children,
...props
}: {
bleed?: boolean;
dense?: boolean;
grid?: boolean;
striped?: boolean;
} & React.ComponentPropsWithoutRef<"div">) {
return (
<TableContext.Provider
value={
{ bleed, dense, grid, striped } as React.ContextType<
typeof TableContext
>
}
>
<div className="flow-root">
<div
{...props}
className={clsx(
className,
"-mx-[--gutter] overflow-x-auto whitespace-nowrap",
)}
>
<div
className={clsx(
"inline-block min-w-full align-middle",
!bleed && "sm:px-[--gutter]",
)}
>
<table className="min-w-full text-left text-sm/6 text-fg">
{children}
</table>
</div>
</div>
</div>
</TableContext.Provider>
);
}
export function TableHead({
className,
...props
}: React.ComponentPropsWithoutRef<"thead">) {
return <thead {...props} className={clsx(className, "text-muted")} />;
}
export function TableBody(props: React.ComponentPropsWithoutRef<"tbody">) {
return <tbody {...props} />;
}
const TableRowContext = createContext<{
href?: string;
target?: string;
title?: string;
}>({
href: undefined,
target: undefined,
title: undefined,
});
export function TableRow({
href,
target,
title,
className,
...props
}: {
href?: string;
target?: string;
title?: string;
} & React.ComponentPropsWithoutRef<"tr">) {
const { striped } = useContext(TableContext);
return (
<TableRowContext.Provider
value={
{ href, target, title } as React.ContextType<typeof TableRowContext>
}
>
<tr
{...props}
className={clsx(
className,
href &&
"focus-within:bg-hover/50 has-[[data-row-link][data-focus]]:outline has-[[data-row-link][data-focus]]:outline-2 has-[[data-row-link][data-focus]]:-outline-offset-2 has-[[data-row-link][data-focus]]:outline-accent",
striped && "even:bg-hover/50",
href && striped && "hover:bg-hover",
href && !striped && "hover:bg-hover/50",
)}
/>
</TableRowContext.Provider>
);
}
export function TableHeader({
className,
...props
}: React.ComponentPropsWithoutRef<"th">) {
const { bleed, grid } = useContext(TableContext);
return (
<th
{...props}
className={clsx(
className,
"border-b border-b-hairline px-4 py-2 font-medium first:pl-[var(--gutter,theme(spacing.2))] last:pr-[var(--gutter,theme(spacing.2))]",
grid && "border-l border-l-hairline first:border-l-0",
!bleed && "sm:first:pl-1 sm:last:pr-1",
)}
/>
);
}
export function TableCell({
className,
children,
...props
}: React.ComponentPropsWithoutRef<"td">) {
const { bleed, dense, grid, striped } = useContext(TableContext);
const { href, target, title } = useContext(TableRowContext);
const [cellRef, setCellRef] = useState<HTMLElement | null>(null);
return (
<td
ref={href ? setCellRef : undefined}
{...props}
className={clsx(
className,
"relative px-4 first:pl-[var(--gutter,theme(spacing.2))] last:pr-[var(--gutter,theme(spacing.2))]",
!striped && "border-b border-hairline",
grid && "border-l border-l-hairline first:border-l-0",
dense ? "py-2.5" : "py-4",
!bleed && "sm:first:pl-1 sm:last:pr-1",
)}
>
{href && (
<Link
data-row-link
href={href}
target={target}
aria-label={title}
tabIndex={cellRef?.previousElementSibling === null ? 0 : -1}
className="absolute inset-0 focus:outline-none"
/>
)}
{children}
</td>
);
}