@nswds/ui 7.0.1 → 7.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,12 +1,40 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* A data table inside a horizontally scrolling container.
|
|
5
|
+
*
|
|
6
|
+
* The container scrolls whenever the table is wider than the space it has —
|
|
7
|
+
* TableHead and TableCell are `whitespace-nowrap`, so a wide table cannot
|
|
8
|
+
* shrink to fit. A region that scrolls but cannot take focus is unreachable
|
|
9
|
+
* from the keyboard (WCAG 2.1.1; axe reports `scrollable-region-focusable`),
|
|
10
|
+
* so **while the table overflows** the container becomes a named, focusable
|
|
11
|
+
* region: `tabIndex={0}` so the arrow keys scroll it, `role="region"` so the
|
|
12
|
+
* stop is announced, and an accessible name taken from the table's own
|
|
13
|
+
* `aria-labelledby` / `aria-label`, else the TableCaption (linked by id), else
|
|
14
|
+
* "Scrollable table". The `aria-label` / `aria-labelledby` stay on the
|
|
15
|
+
* `<table>` too — the region borrows the name, it does not take it.
|
|
16
|
+
*
|
|
17
|
+
* **While the table fits, the container is a plain `<div>`** — no tab stop,
|
|
18
|
+
* no landmark — so a page of fitting tables gains nothing to tab past. Both
|
|
19
|
+
* states follow the layout live: a ResizeObserver watches the container (the
|
|
20
|
+
* column changing width) and the table (its content changing width, which a
|
|
21
|
+
* `w-full` container does not itself report). The measurement happens on the
|
|
22
|
+
* client, so a server render has no tab stop until hydration; that is the
|
|
23
|
+
* cost of not giving every table one.
|
|
24
|
+
*/
|
|
25
|
+
declare function Table({ className, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, ...props }: React.ComponentProps<'table'>): React.JSX.Element;
|
|
4
26
|
declare function TableHeader({ className, ...props }: React.ComponentProps<'thead'>): React.JSX.Element;
|
|
5
27
|
declare function TableBody({ className, ...props }: React.ComponentProps<'tbody'>): React.JSX.Element;
|
|
6
28
|
declare function TableFooter({ className, ...props }: React.ComponentProps<'tfoot'>): React.JSX.Element;
|
|
7
29
|
declare function TableRow({ className, ...props }: React.ComponentProps<'tr'>): React.JSX.Element;
|
|
8
30
|
declare function TableHead({ className, ...props }: React.ComponentProps<'th'>): React.JSX.Element;
|
|
9
31
|
declare function TableCell({ className, ...props }: React.ComponentProps<'td'>): React.JSX.Element;
|
|
10
|
-
|
|
32
|
+
/**
|
|
33
|
+
* The table's caption. Besides naming the table for everyone, it names the
|
|
34
|
+
* scroll region when no explicit ARIA name is supplied: the caption takes an
|
|
35
|
+
* id (the one given, else a generated one) and reports it to the Table, which
|
|
36
|
+
* points `aria-labelledby` at it.
|
|
37
|
+
*/
|
|
38
|
+
declare function TableCaption({ className, id: idProp, ...props }: React.ComponentProps<'caption'>): React.JSX.Element;
|
|
11
39
|
|
|
12
40
|
export { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow };
|
package/dist/components/table.js
CHANGED
|
@@ -1,14 +1,62 @@
|
|
|
1
1
|
"use client"
|
|
2
2
|
import { jsx } from 'react/jsx-runtime';
|
|
3
|
+
import * as React from 'react';
|
|
3
4
|
import { cn } from '../lib/utils.js';
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
const DEFAULT_SCROLL_REGION_LABEL = "Scrollable table";
|
|
7
|
+
const TableContext = React.createContext(null);
|
|
8
|
+
function Table({
|
|
9
|
+
className,
|
|
10
|
+
"aria-label": ariaLabel,
|
|
11
|
+
"aria-labelledby": ariaLabelledBy,
|
|
12
|
+
...props
|
|
13
|
+
}) {
|
|
14
|
+
const containerRef = React.useRef(null);
|
|
15
|
+
const generatedCaptionId = React.useId();
|
|
16
|
+
const [captionId, setCaptionId] = React.useState(null);
|
|
17
|
+
const [overflows, setOverflows] = React.useState(false);
|
|
18
|
+
React.useEffect(() => {
|
|
19
|
+
const container = containerRef.current;
|
|
20
|
+
if (!container) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const observer = new ResizeObserver(() => {
|
|
24
|
+
setOverflows(container.scrollWidth > container.clientWidth);
|
|
25
|
+
});
|
|
26
|
+
observer.observe(container);
|
|
27
|
+
const table = container.firstElementChild;
|
|
28
|
+
if (table) {
|
|
29
|
+
observer.observe(table);
|
|
30
|
+
}
|
|
31
|
+
return () => observer.disconnect();
|
|
32
|
+
}, []);
|
|
33
|
+
const context = React.useMemo(
|
|
34
|
+
() => ({ captionId: generatedCaptionId, registerCaption: setCaptionId }),
|
|
35
|
+
[generatedCaptionId]
|
|
36
|
+
);
|
|
37
|
+
const explicitLabel = ariaLabel?.trim() ? ariaLabel : void 0;
|
|
38
|
+
const regionLabelledBy = ariaLabelledBy?.trim() ? ariaLabelledBy : explicitLabel ? void 0 : captionId ?? void 0;
|
|
39
|
+
const regionLabel = regionLabelledBy ? void 0 : explicitLabel ?? DEFAULT_SCROLL_REGION_LABEL;
|
|
40
|
+
return /* @__PURE__ */ jsx(TableContext.Provider, { value: context, children: /* @__PURE__ */ jsx(
|
|
41
|
+
"div",
|
|
8
42
|
{
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
43
|
+
ref: containerRef,
|
|
44
|
+
"data-slot": "table-container",
|
|
45
|
+
className: "relative w-full overflow-x-auto focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring",
|
|
46
|
+
role: overflows ? "region" : void 0,
|
|
47
|
+
tabIndex: overflows ? 0 : void 0,
|
|
48
|
+
"aria-labelledby": overflows ? regionLabelledBy : void 0,
|
|
49
|
+
"aria-label": overflows ? regionLabel : void 0,
|
|
50
|
+
children: /* @__PURE__ */ jsx(
|
|
51
|
+
"table",
|
|
52
|
+
{
|
|
53
|
+
"data-slot": "table",
|
|
54
|
+
className: cn("w-full caption-bottom text-xs", className),
|
|
55
|
+
"aria-label": ariaLabel,
|
|
56
|
+
"aria-labelledby": ariaLabelledBy,
|
|
57
|
+
...props
|
|
58
|
+
}
|
|
59
|
+
)
|
|
12
60
|
}
|
|
13
61
|
) });
|
|
14
62
|
}
|
|
@@ -71,11 +119,21 @@ function TableCell({ className, ...props }) {
|
|
|
71
119
|
}
|
|
72
120
|
);
|
|
73
121
|
}
|
|
74
|
-
function TableCaption({ className, ...props }) {
|
|
122
|
+
function TableCaption({ className, id: idProp, ...props }) {
|
|
123
|
+
const context = React.useContext(TableContext);
|
|
124
|
+
const id = idProp ?? context?.captionId;
|
|
125
|
+
React.useEffect(() => {
|
|
126
|
+
if (!context || !id) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
context.registerCaption(id);
|
|
130
|
+
return () => context.registerCaption(null);
|
|
131
|
+
}, [context, id]);
|
|
75
132
|
return /* @__PURE__ */ jsx(
|
|
76
133
|
"caption",
|
|
77
134
|
{
|
|
78
135
|
"data-slot": "table-caption",
|
|
136
|
+
id,
|
|
79
137
|
className: cn("mt-4 text-xs text-muted-foreground", className),
|
|
80
138
|
...props
|
|
81
139
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/components/table.tsx"],"names":[],"mappings":";;;AAMA,SAAS,KAAA,CAAM,EAAE,SAAA,EAAW,GAAG,OAAM,EAAkC;AACrE,EAAA,uBACE,GAAA,CAAC,KAAA,EAAA,EAAI,WAAA,EAAU,iBAAA,EAAkB,WAAU,iCAAA,EACzC,QAAA,kBAAA,GAAA;AAAA,IAAC,OAAA;AAAA,IAAA;AAAA,MACC,WAAA,EAAU,OAAA;AAAA,MACV,SAAA,EAAW,EAAA,CAAG,+BAAA,EAAiC,SAAS,CAAA;AAAA,MACvD,GAAG;AAAA;AAAA,GACN,EACF,CAAA;AAEJ;AAEA,SAAS,WAAA,CAAY,EAAE,SAAA,EAAW,GAAG,OAAM,EAAkC;AAC3E,EAAA,uBAAO,GAAA,CAAC,OAAA,EAAA,EAAM,WAAA,EAAU,cAAA,EAAe,SAAA,EAAW,GAAG,iBAAA,EAAmB,SAAS,CAAA,EAAI,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S,CAAA;AAAA,MAC1F,GAAG;AAAA;AAAA,GACN;AAEJ;AAEA,SAAS,YAAA,CAAa,EAAE,SAAA,EAAW,GAAG,OAAM,EAAoC;AAC9E,EAAA,uBACE,GAAA;AAAA,IAAC,SAAA;AAAA,IAAA;AAAA,MACC,WAAA,EAAU,eAAA;AAAA,MACV,SAAA,EAAW,EAAA,CAAG,oCAAA,EAAsC,SAAS,CAAA;AAAA,MAC5D,GAAG;AAAA;AAAA,GACN;AAEJ","file":"table.js","sourcesContent":["'use client'\n\nimport * as React from 'react'\n\nimport { cn } from '../lib/utils.js'\n\nfunction Table({ className, ...props }: React.ComponentProps<'table'>) {\n return (\n <div data-slot='table-container' className='relative w-full overflow-x-auto'>\n <table\n data-slot='table'\n className={cn('w-full caption-bottom text-xs', className)}\n {...props}\n />\n </div>\n )\n}\n\nfunction TableHeader({ className, ...props }: React.ComponentProps<'thead'>) {\n return <thead data-slot='table-header' className={cn('[&_tr]:border-b', className)} {...props} />\n}\n\nfunction TableBody({ className, ...props }: React.ComponentProps<'tbody'>) {\n return (\n <tbody\n data-slot='table-body'\n className={cn('[&_tr:last-child]:border-0', className)}\n {...props}\n />\n )\n}\n\nfunction TableFooter({ className, ...props }: React.ComponentProps<'tfoot'>) {\n return (\n <tfoot\n data-slot='table-footer'\n className={cn('border-t bg-muted/50 font-medium [&>tr]:last:border-b-0', className)}\n {...props}\n />\n )\n}\n\nfunction TableRow({ className, ...props }: React.ComponentProps<'tr'>) {\n return (\n <tr\n data-slot='table-row'\n className={cn(\n 'border-b transition-colors hover:bg-muted/50 has-aria-expanded:bg-muted/50 data-[state=selected]:bg-muted',\n className,\n )}\n {...props}\n />\n )\n}\n\nfunction TableHead({ className, ...props }: React.ComponentProps<'th'>) {\n return (\n <th\n data-slot='table-head'\n className={cn(\n 'h-10 px-2 text-start align-middle font-medium whitespace-nowrap text-foreground [&:has([role=checkbox])]:pe-0',\n className,\n )}\n {...props}\n />\n )\n}\n\nfunction TableCell({ className, ...props }: React.ComponentProps<'td'>) {\n return (\n <td\n data-slot='table-cell'\n className={cn('p-2 align-middle whitespace-nowrap [&:has([role=checkbox])]:pe-0', className)}\n {...props}\n />\n )\n}\n\nfunction TableCaption({ className, ...props }: React.ComponentProps<'caption'>) {\n return (\n <caption\n data-slot='table-caption'\n className={cn('mt-4 text-xs text-muted-foreground', className)}\n {...props}\n />\n )\n}\n\nexport { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow }\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/components/table.tsx"],"names":[],"mappings":";;;;AAUA,MAAM,2BAAA,GAA8B,kBAAA;AASpC,MAAM,YAAA,GAAe,KAAA,CAAM,aAAA,CAAwC,IAAI,CAAA;AAwBvE,SAAS,KAAA,CAAM;AAAA,EACb,SAAA;AAAA,EACA,YAAA,EAAc,SAAA;AAAA,EACd,iBAAA,EAAmB,cAAA;AAAA,EACnB,GAAG;AACL,CAAA,EAAkC;AAChC,EAAA,MAAM,YAAA,GAAe,KAAA,CAAM,MAAA,CAAuB,IAAI,CAAA;AACtD,EAAA,MAAM,kBAAA,GAAqB,MAAM,KAAA,EAAM;AACvC,EAAA,MAAM,CAAC,SAAA,EAAW,YAAY,CAAA,GAAI,KAAA,CAAM,SAAwB,IAAI,CAAA;AACpE,EAAA,MAAM,CAAC,SAAA,EAAW,YAAY,CAAA,GAAI,KAAA,CAAM,SAAS,KAAK,CAAA;AAEtD,EAAA,KAAA,CAAM,UAAU,MAAM;AACpB,IAAA,MAAM,YAAY,YAAA,CAAa,OAAA;AAC/B,IAAA,IAAI,CAAC,SAAA,EAAW;AACd,MAAA;AAAA,IACF;AAGA,IAAA,MAAM,QAAA,GAAW,IAAI,cAAA,CAAe,MAAM;AACxC,MAAA,YAAA,CAAa,SAAA,CAAU,WAAA,GAAc,SAAA,CAAU,WAAW,CAAA;AAAA,IAC5D,CAAC,CAAA;AACD,IAAA,QAAA,CAAS,QAAQ,SAAS,CAAA;AAG1B,IAAA,MAAM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;AAAA,OACF;AAAA,MACC,GAAG;AAAA;AAAA,GACN;AAEJ;AAEA,SAAS,SAAA,CAAU,EAAE,SAAA,EAAW,GAAG,OAAM,EAA+B;AACtE,EAAA,uBACE,GAAA;AAAA,IAAC,IAAA;AAAA,IAAA;AAAA,MACC,WAAA,EAAU,YAAA;AAAA,MACV,SAAA,EAAW,EAAA,CAAG,kEAAA,EAAoE,SAAS,CAAA;AAAA,MAC1F,GAAG;AAAA;AAAA,GACN;AAEJ;AAQA,SAAS,aAAa,EAAE,SAAA,EAAW,IAAI,MAAA,EAAQ,GAAG,OAAM,EAAoC;AAC1F,EAAA,MAAM,OAAA,GAAU,KAAA,CAAM,UAAA,CAAW,YAAY,CAAA;AAC7C,EAAA,MAAM,EAAA,GAAK,UAAU,OAAA,EAAS,SAAA;AAE9B,EAAA,KAAA,CAAM,UAAU,MAAM;AACpB,IAAA,IAAI,CAAC,OAAA,IAAW,CAAC,EAAA,EAAI;AACnB,MAAA;AAAA,IACF;AACA,IAAA,OAAA,CAAQ,gBAAgB,EAAE,CAAA;AAC1B,IAAA,OAAO,MAAM,OAAA,CAAQ,eAAA,CAAgB,IAAI,CAAA;AAAA,EAC3C,CAAA,EAAG,CAAC,OAAA,EAAS,EAAE,CAAC,CAAA;AAEhB,EAAA,uBACE,GAAA;AAAA,IAAC,SAAA;AAAA,IAAA;AAAA,MACC,WAAA,EAAU,eAAA;AAAA,MACV,EAAA;AAAA,MACA,SAAA,EAAW,EAAA,CAAG,oCAAA,EAAsC,SAAS,CAAA;AAAA,MAC5D,GAAG;AAAA;AAAA,GACN;AAEJ","file":"table.js","sourcesContent":["'use client'\n\nimport * as React from 'react'\n\nimport { cn } from '../lib/utils.js'\n\n/**\n * Accessible name of the scroll region when nothing else names the table —\n * no TableCaption, and no `aria-label` / `aria-labelledby` on the table.\n */\nconst DEFAULT_SCROLL_REGION_LABEL = 'Scrollable table'\n\ntype TableContextValue = {\n /** The id a TableCaption takes when the consumer has not given it one. */\n captionId: string\n /** Called by TableCaption with its live id on mount, and `null` on unmount. */\n registerCaption: (id: string | null) => void\n}\n\nconst TableContext = React.createContext<TableContextValue | null>(null)\n\n/**\n * A data table inside a horizontally scrolling container.\n *\n * The container scrolls whenever the table is wider than the space it has —\n * TableHead and TableCell are `whitespace-nowrap`, so a wide table cannot\n * shrink to fit. A region that scrolls but cannot take focus is unreachable\n * from the keyboard (WCAG 2.1.1; axe reports `scrollable-region-focusable`),\n * so **while the table overflows** the container becomes a named, focusable\n * region: `tabIndex={0}` so the arrow keys scroll it, `role=\"region\"` so the\n * stop is announced, and an accessible name taken from the table's own\n * `aria-labelledby` / `aria-label`, else the TableCaption (linked by id), else\n * \"Scrollable table\". The `aria-label` / `aria-labelledby` stay on the\n * `<table>` too — the region borrows the name, it does not take it.\n *\n * **While the table fits, the container is a plain `<div>`** — no tab stop,\n * no landmark — so a page of fitting tables gains nothing to tab past. Both\n * states follow the layout live: a ResizeObserver watches the container (the\n * column changing width) and the table (its content changing width, which a\n * `w-full` container does not itself report). The measurement happens on the\n * client, so a server render has no tab stop until hydration; that is the\n * cost of not giving every table one.\n */\nfunction Table({\n className,\n 'aria-label': ariaLabel,\n 'aria-labelledby': ariaLabelledBy,\n ...props\n}: React.ComponentProps<'table'>) {\n const containerRef = React.useRef<HTMLDivElement>(null)\n const generatedCaptionId = React.useId()\n const [captionId, setCaptionId] = React.useState<string | null>(null)\n const [overflows, setOverflows] = React.useState(false)\n\n React.useEffect(() => {\n const container = containerRef.current\n if (!container) {\n return\n }\n // The observer delivers an initial entry after `observe()`, so the first\n // measurement needs no synchronous read here.\n const observer = new ResizeObserver(() => {\n setOverflows(container.scrollWidth > container.clientWidth)\n })\n observer.observe(container)\n // The container is `w-full`, so it does not resize when its table grows\n // past it — observe the table as well to catch content changes.\n const table = container.firstElementChild\n if (table) {\n observer.observe(table)\n }\n return () => observer.disconnect()\n }, [])\n\n const context = React.useMemo<TableContextValue>(\n () => ({ captionId: generatedCaptionId, registerCaption: setCaptionId }),\n [generatedCaptionId],\n )\n\n // The name is only applied together with the role: `aria-label` on a plain\n // <div> is a prohibited attribute (the generic role cannot be named).\n const explicitLabel = ariaLabel?.trim() ? ariaLabel : undefined\n const regionLabelledBy = ariaLabelledBy?.trim()\n ? ariaLabelledBy\n : explicitLabel\n ? undefined\n : (captionId ?? undefined)\n const regionLabel = regionLabelledBy ? undefined : (explicitLabel ?? DEFAULT_SCROLL_REGION_LABEL)\n\n return (\n <TableContext.Provider value={context}>\n <div\n ref={containerRef}\n data-slot='table-container'\n className='relative w-full overflow-x-auto focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring'\n role={overflows ? 'region' : undefined}\n tabIndex={overflows ? 0 : undefined}\n aria-labelledby={overflows ? regionLabelledBy : undefined}\n aria-label={overflows ? regionLabel : undefined}\n >\n <table\n data-slot='table'\n className={cn('w-full caption-bottom text-xs', className)}\n aria-label={ariaLabel}\n aria-labelledby={ariaLabelledBy}\n {...props}\n />\n </div>\n </TableContext.Provider>\n )\n}\n\nfunction TableHeader({ className, ...props }: React.ComponentProps<'thead'>) {\n return <thead data-slot='table-header' className={cn('[&_tr]:border-b', className)} {...props} />\n}\n\nfunction TableBody({ className, ...props }: React.ComponentProps<'tbody'>) {\n return (\n <tbody\n data-slot='table-body'\n className={cn('[&_tr:last-child]:border-0', className)}\n {...props}\n />\n )\n}\n\nfunction TableFooter({ className, ...props }: React.ComponentProps<'tfoot'>) {\n return (\n <tfoot\n data-slot='table-footer'\n className={cn('border-t bg-muted/50 font-medium [&>tr]:last:border-b-0', className)}\n {...props}\n />\n )\n}\n\nfunction TableRow({ className, ...props }: React.ComponentProps<'tr'>) {\n return (\n <tr\n data-slot='table-row'\n className={cn(\n 'border-b transition-colors hover:bg-muted/50 has-aria-expanded:bg-muted/50 data-[state=selected]:bg-muted',\n className,\n )}\n {...props}\n />\n )\n}\n\nfunction TableHead({ className, ...props }: React.ComponentProps<'th'>) {\n return (\n <th\n data-slot='table-head'\n className={cn(\n 'h-10 px-2 text-start align-middle font-medium whitespace-nowrap text-foreground [&:has([role=checkbox])]:pe-0',\n className,\n )}\n {...props}\n />\n )\n}\n\nfunction TableCell({ className, ...props }: React.ComponentProps<'td'>) {\n return (\n <td\n data-slot='table-cell'\n className={cn('p-2 align-middle whitespace-nowrap [&:has([role=checkbox])]:pe-0', className)}\n {...props}\n />\n )\n}\n\n/**\n * The table's caption. Besides naming the table for everyone, it names the\n * scroll region when no explicit ARIA name is supplied: the caption takes an\n * id (the one given, else a generated one) and reports it to the Table, which\n * points `aria-labelledby` at it.\n */\nfunction TableCaption({ className, id: idProp, ...props }: React.ComponentProps<'caption'>) {\n const context = React.useContext(TableContext)\n const id = idProp ?? context?.captionId\n\n React.useEffect(() => {\n if (!context || !id) {\n return\n }\n context.registerCaption(id)\n return () => context.registerCaption(null)\n }, [context, id])\n\n return (\n <caption\n data-slot='table-caption'\n id={id}\n className={cn('mt-4 text-xs text-muted-foreground', className)}\n {...props}\n />\n )\n}\n\nexport { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow }\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nswds/ui",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.2",
|
|
4
4
|
"description": "UI components for NSW Government digital products, providing reusable design elements and patterns for consistent user interfaces.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://github.com/digitalnsw/nswds-ui#readme",
|