@brightlocal/ui-components 3.4.0 → 3.5.0
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.
- package/AI_USAGE.md +22 -3
- package/README.md +177 -109
- package/component-meta.json +1061 -102
- package/dist/alert-dialog.cjs +1 -1
- package/dist/alert-dialog.d.ts +13 -2
- package/dist/alert-dialog.d.ts.map +1 -1
- package/dist/alert-dialog.js +4 -3
- package/dist/banner.cjs +1 -1
- package/dist/banner.d.ts +47 -11
- package/dist/banner.d.ts.map +1 -1
- package/dist/banner.js +156 -79
- package/dist/blocks/empty-state.cjs +1 -1
- package/dist/blocks/empty-state.d.ts +40 -5
- package/dist/blocks/empty-state.d.ts.map +1 -1
- package/dist/blocks/empty-state.js +59 -52
- package/dist/blocks/input-list.d.ts +1 -0
- package/dist/blocks/input-list.d.ts.map +1 -1
- package/dist/blocks/sidebar.cjs +1 -1
- package/dist/blocks/sidebar.d.ts +12 -6
- package/dist/blocks/sidebar.d.ts.map +1 -1
- package/dist/blocks/sidebar.js +64 -63
- package/dist/button.cjs +1 -1
- package/dist/button.d.ts.map +1 -1
- package/dist/button.js +47 -43
- package/dist/card.cjs +1 -1
- package/dist/card.d.ts +12 -0
- package/dist/card.d.ts.map +1 -1
- package/dist/card.js +115 -97
- package/dist/chart.cjs +6 -6
- package/dist/chart.d.ts +6 -10
- package/dist/chart.d.ts.map +1 -1
- package/dist/chart.js +281 -296
- package/dist/combobox.cjs +3 -3
- package/dist/combobox.d.ts.map +1 -1
- package/dist/combobox.js +430 -343
- package/dist/command.cjs +1 -1
- package/dist/command.d.ts +23 -2
- package/dist/command.d.ts.map +1 -1
- package/dist/command.js +212 -207
- package/dist/count-indicator.cjs +1 -0
- package/dist/count-indicator.d.ts +85 -0
- package/dist/count-indicator.d.ts.map +1 -0
- package/dist/count-indicator.js +72 -0
- package/dist/data-table.cjs +1 -1
- package/dist/data-table.d.ts +101 -11
- package/dist/data-table.d.ts.map +1 -1
- package/dist/data-table.js +593 -523
- package/dist/dialog.cjs +1 -1
- package/dist/dialog.d.ts +26 -3
- package/dist/dialog.d.ts.map +1 -1
- package/dist/dialog.js +62 -47
- package/dist/drawer.cjs +1 -1
- package/dist/drawer.d.ts +8 -4
- package/dist/drawer.d.ts.map +1 -1
- package/dist/drawer.js +37 -38
- package/dist/file-upload.cjs +1 -0
- package/dist/file-upload.d.ts +546 -0
- package/dist/file-upload.d.ts.map +1 -0
- package/dist/file-upload.js +1103 -0
- package/dist/global-layout.cjs +1 -1
- package/dist/global-layout.d.ts +7 -2
- package/dist/global-layout.d.ts.map +1 -1
- package/dist/global-layout.js +52 -52
- package/dist/hooks/use-file-dropzone.cjs +1 -0
- package/dist/hooks/use-file-dropzone.d.ts +75 -0
- package/dist/hooks/use-file-dropzone.d.ts.map +1 -0
- package/dist/hooks/use-file-dropzone.js +133 -0
- package/dist/hover-card.cjs +1 -1
- package/dist/hover-card.d.ts +29 -16
- package/dist/hover-card.d.ts.map +1 -1
- package/dist/hover-card.js +153 -36
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +676 -640
- package/dist/input-chip.cjs +1 -1
- package/dist/input-chip.d.ts +30 -1
- package/dist/input-chip.d.ts.map +1 -1
- package/dist/input-chip.js +131 -115
- package/dist/input-group.cjs +1 -1
- package/dist/input-group.d.ts +37 -2
- package/dist/input-group.d.ts.map +1 -1
- package/dist/input-group.js +131 -114
- package/dist/input-password.cjs +1 -1
- package/dist/input-password.js +6 -6
- package/dist/lib/empty-canvas.cjs +1 -1
- package/dist/lib/empty-canvas.d.ts +27 -1
- package/dist/lib/empty-canvas.d.ts.map +1 -1
- package/dist/lib/empty-canvas.js +27 -23
- package/dist/lib/empty-chip.cjs +1 -1
- package/dist/lib/empty-chip.d.ts +19 -2
- package/dist/lib/empty-chip.d.ts.map +1 -1
- package/dist/lib/empty-chip.js +14 -9
- package/dist/lib/field-aria.cjs +1 -1
- package/dist/lib/field-aria.d.ts +4 -4
- package/dist/lib/field-aria.d.ts.map +1 -1
- package/dist/lib/field-aria.js +7 -5
- package/dist/lib/media-viewer-content.cjs +1 -1
- package/dist/lib/media-viewer-content.js +1 -1
- package/dist/lib/table-internal.cjs +1 -1
- package/dist/lib/table-internal.d.ts +19 -0
- package/dist/lib/table-internal.d.ts.map +1 -1
- package/dist/lib/table-internal.js +22 -7
- package/dist/list.cjs +1 -1
- package/dist/list.d.ts +13 -0
- package/dist/list.d.ts.map +1 -1
- package/dist/list.js +103 -82
- package/dist/logo-mark.cjs +1 -1
- package/dist/logo-mark.d.ts +18 -2
- package/dist/logo-mark.d.ts.map +1 -1
- package/dist/logo-mark.js +159 -79
- package/dist/media-empty.cjs +1 -1
- package/dist/media-empty.d.ts +50 -7
- package/dist/media-empty.d.ts.map +1 -1
- package/dist/media-empty.js +14 -13
- package/dist/media-viewer.cjs +1 -1
- package/dist/media-viewer.js +6 -6
- package/dist/multi-select-filter.cjs +1 -1
- package/dist/multi-select-filter.d.ts.map +1 -1
- package/dist/multi-select-filter.js +92 -101
- package/dist/pagination.cjs +1 -1
- package/dist/pagination.d.ts +11 -0
- package/dist/pagination.d.ts.map +1 -1
- package/dist/pagination.js +17 -17
- package/dist/relative-date.d.ts +6 -0
- package/dist/relative-date.d.ts.map +1 -1
- package/dist/resizable.d.ts +1 -1
- package/dist/result-count.cjs +1 -1
- package/dist/result-count.d.ts +13 -16
- package/dist/result-count.d.ts.map +1 -1
- package/dist/result-count.js +24 -33
- package/dist/sheet.cjs +1 -1
- package/dist/sheet.d.ts +22 -3
- package/dist/sheet.d.ts.map +1 -1
- package/dist/sheet.js +152 -125
- package/dist/single-select-filter.cjs +1 -1
- package/dist/single-select-filter.d.ts.map +1 -1
- package/dist/single-select-filter.js +67 -76
- package/dist/table.cjs +1 -1
- package/dist/table.d.ts.map +1 -1
- package/dist/table.js +77 -76
- package/dist/tabs.cjs +1 -1
- package/dist/tabs.d.ts +39 -3
- package/dist/tabs.d.ts.map +1 -1
- package/dist/tabs.js +127 -76
- package/dist/textarea.cjs +1 -1
- package/dist/textarea.d.ts +34 -0
- package/dist/textarea.d.ts.map +1 -1
- package/dist/textarea.js +115 -19
- package/dist/tooltip.cjs +1 -1
- package/dist/tooltip.d.ts.map +1 -1
- package/dist/tooltip.js +137 -131
- package/dist/typography-text.cjs +1 -1
- package/dist/typography-text.d.ts +6 -2
- package/dist/typography-text.d.ts.map +1 -1
- package/dist/typography-text.js +14 -13
- package/package.json +3 -4
- package/src/blocks/empty-state.tsx +108 -48
- package/src/blocks/input-list.tsx +1 -0
- package/src/blocks/sidebar.tsx +39 -16
- package/src/components/alert-dialog.tsx +18 -2
- package/src/components/banner.tsx +247 -68
- package/src/components/button.tsx +27 -10
- package/src/components/card.tsx +89 -7
- package/src/components/chart.tsx +47 -67
- package/src/components/combobox.tsx +221 -12
- package/src/components/command.tsx +83 -37
- package/src/components/count-indicator.tsx +188 -0
- package/src/components/data-table.tsx +506 -159
- package/src/components/dialog.tsx +74 -5
- package/src/components/drawer.tsx +52 -32
- package/src/components/file-upload.tsx +2574 -0
- package/src/components/global-layout.tsx +18 -5
- package/src/components/hover-card.tsx +395 -65
- package/src/components/input-chip.tsx +67 -2
- package/src/components/input-group.tsx +89 -6
- package/src/components/list.tsx +40 -3
- package/src/components/logo-mark.tsx +109 -25
- package/src/components/media-empty.tsx +62 -7
- package/src/components/multi-select-filter.tsx +9 -16
- package/src/components/pagination.tsx +15 -2
- package/src/components/relative-date.tsx +6 -0
- package/src/components/result-count.tsx +27 -54
- package/src/components/sheet.tsx +86 -8
- package/src/components/single-select-filter.tsx +9 -16
- package/src/components/table.tsx +16 -1
- package/src/components/tabs.tsx +223 -41
- package/src/components/textarea.tsx +271 -4
- package/src/components/tooltip.tsx +29 -2
- package/src/components/typography-text.tsx +7 -2
- package/src/hooks/use-file-dropzone.ts +325 -0
- package/src/index.ts +3 -0
- package/src/lib/empty-canvas.tsx +47 -20
- package/src/lib/empty-chip.tsx +38 -3
- package/src/lib/field-aria.tsx +11 -6
- package/src/lib/table-internal.ts +38 -0
|
@@ -10,7 +10,8 @@ import { Button } from "@brightlocal/ui-components/button";
|
|
|
10
10
|
import { cn } from "@brightlocal/ui-components/lib/utils";
|
|
11
11
|
import { useDevWarning } from "@brightlocal/ui-components/lib/use-dev-warning";
|
|
12
12
|
|
|
13
|
-
import {
|
|
13
|
+
import { EmptyState } from "../blocks/empty-state";
|
|
14
|
+
import { hasEmptyActions } from "../lib/empty-chip";
|
|
14
15
|
|
|
15
16
|
/* -------------------------------------------------------------------------------------------------
|
|
16
17
|
* Command Context — shared state between CommandInput and CommandVirtualList
|
|
@@ -197,12 +198,13 @@ const CommandInput = React.forwardRef<
|
|
|
197
198
|
|
|
198
199
|
// cmdk sets aria-controls/aria-activedescendant internally after
|
|
199
200
|
// spreading props, so we override via DOM to point at our virtual listbox.
|
|
201
|
+
// Only while a CommandVirtualList is mounted (it sets listboxId): with
|
|
202
|
+
// regular cmdk items the attributes are cmdk's, and removing them here
|
|
203
|
+
// after every render left the input without an active descendant.
|
|
200
204
|
React.useEffect(() => {
|
|
201
205
|
const el = inputRef.current;
|
|
202
|
-
if (!el) return;
|
|
203
|
-
|
|
204
|
-
el.setAttribute("aria-controls", listboxId);
|
|
205
|
-
}
|
|
206
|
+
if (!el || !listboxId) return;
|
|
207
|
+
el.setAttribute("aria-controls", listboxId);
|
|
206
208
|
if (activeDescendantId) {
|
|
207
209
|
el.setAttribute("aria-activedescendant", activeDescendantId);
|
|
208
210
|
} else {
|
|
@@ -323,8 +325,9 @@ CommandList.displayName = "CommandList";
|
|
|
323
325
|
/**
|
|
324
326
|
* Props for the CommandEmpty component
|
|
325
327
|
*/
|
|
326
|
-
export interface CommandEmptyProps extends
|
|
327
|
-
typeof CommandPrimitive.Empty
|
|
328
|
+
export interface CommandEmptyProps extends Omit<
|
|
329
|
+
React.ComponentProps<typeof CommandPrimitive.Empty>,
|
|
330
|
+
"title"
|
|
328
331
|
> {
|
|
329
332
|
/**
|
|
330
333
|
* Data hook for automated testing
|
|
@@ -336,6 +339,14 @@ export interface CommandEmptyProps extends React.ComponentProps<
|
|
|
336
339
|
* (Patterns/Empty States). Decorative; the message carries the state.
|
|
337
340
|
*/
|
|
338
341
|
icon?: React.ReactNode;
|
|
342
|
+
/**
|
|
343
|
+
* The message, e.g. “No locations match “{search}””. Pass it here when the
|
|
344
|
+
* empty state carries actions: `children` then become the actions row under
|
|
345
|
+
* the description, the same split as EmptyState. The line keeps the compact
|
|
346
|
+
* body-sm size; it is never a heading. Without `title`, `children` are the
|
|
347
|
+
* message.
|
|
348
|
+
*/
|
|
349
|
+
title?: React.ReactNode;
|
|
339
350
|
/**
|
|
340
351
|
* One quiet muted line under the message, e.g. "Try a different search."
|
|
341
352
|
*/
|
|
@@ -345,7 +356,9 @@ export interface CommandEmptyProps extends React.ComponentProps<
|
|
|
345
356
|
/**
|
|
346
357
|
* Empty state displayed when no results are found. Plain `children` render
|
|
347
358
|
* the one-line form; pass `icon` and/or `description` for the compact form
|
|
348
|
-
* the Empty States pattern uses at popover scope — never hand-roll it.
|
|
359
|
+
* the Empty States pattern uses at popover scope — never hand-roll it. To
|
|
360
|
+
* offer an action (Add location), pass the message as `title` and the
|
|
361
|
+
* buttons as `children`, as in EmptyState.
|
|
349
362
|
*
|
|
350
363
|
* @example
|
|
351
364
|
* ```tsx
|
|
@@ -355,57 +368,90 @@ export interface CommandEmptyProps extends React.ComponentProps<
|
|
|
355
368
|
* >
|
|
356
369
|
* No locations match “{search}”
|
|
357
370
|
* </CommandEmpty>
|
|
371
|
+
*
|
|
372
|
+
* <CommandEmpty
|
|
373
|
+
* description="Check the spelling, or add it as a new location."
|
|
374
|
+
* icon={<SearchX />}
|
|
375
|
+
* title={`No locations match “${search}”`}
|
|
376
|
+
* >
|
|
377
|
+
* <Button dataHook="add-location" size="sm" variant="secondary">
|
|
378
|
+
* <Plus />
|
|
379
|
+
* Add location
|
|
380
|
+
* </Button>
|
|
381
|
+
* </CommandEmpty>
|
|
358
382
|
* ```
|
|
359
383
|
*/
|
|
360
384
|
const CommandEmpty = React.forwardRef<
|
|
361
385
|
React.ComponentRef<typeof CommandPrimitive.Empty>,
|
|
362
386
|
CommandEmptyProps
|
|
363
|
-
>(({ children, dataHook, description, icon, ...props }, ref) => {
|
|
387
|
+
>(({ children, dataHook, description, icon, title, ...props }, ref) => {
|
|
388
|
+
const isCompact = Boolean(icon || description) || title !== undefined;
|
|
389
|
+
// With `title`, children are the actions; without it, they are the message.
|
|
390
|
+
const hasActions = title !== undefined && hasEmptyActions(children);
|
|
391
|
+
const fallbackTitleId = React.useId();
|
|
392
|
+
|
|
364
393
|
// cmdk hardcodes role="presentation" after spreading props, so we
|
|
365
394
|
// override it via a callback ref to satisfy the listbox a11y contract
|
|
366
|
-
// (role="listbox" requires option or group children).
|
|
395
|
+
// (role="listbox" requires option or group children). An option's content
|
|
396
|
+
// is presentational and cannot hold a button, so with actions the empty
|
|
397
|
+
// state is a group, labelled by EmptyState's title line.
|
|
367
398
|
const setRef = React.useCallback(
|
|
368
399
|
(node: HTMLDivElement | null) => {
|
|
369
|
-
node
|
|
400
|
+
if (node) {
|
|
401
|
+
node.setAttribute("role", hasActions ? "group" : "option");
|
|
402
|
+
const titleNode = hasActions
|
|
403
|
+
? node.querySelector<HTMLElement>('[data-slot="empty-state-title"]')
|
|
404
|
+
: null;
|
|
405
|
+
if (titleNode) {
|
|
406
|
+
if (!titleNode.id) titleNode.id = fallbackTitleId;
|
|
407
|
+
node.setAttribute("aria-labelledby", titleNode.id);
|
|
408
|
+
} else {
|
|
409
|
+
node.removeAttribute("aria-labelledby");
|
|
410
|
+
}
|
|
411
|
+
}
|
|
370
412
|
if (typeof ref === "function") ref(node);
|
|
371
413
|
else if (ref) ref.current = node;
|
|
372
414
|
},
|
|
373
|
-
[ref]
|
|
415
|
+
[ref, hasActions, fallbackTitleId]
|
|
374
416
|
);
|
|
375
417
|
|
|
376
418
|
return (
|
|
377
419
|
<CommandPrimitive.Empty
|
|
378
420
|
ref={setRef}
|
|
379
|
-
|
|
380
|
-
|
|
421
|
+
{...(hasActions
|
|
422
|
+
? {}
|
|
423
|
+
: { "aria-disabled": "true", "aria-selected": "false" })}
|
|
381
424
|
className="text-body-sm py-6 text-center"
|
|
382
425
|
data-hook={dataHook}
|
|
383
426
|
data-slot="command-empty"
|
|
427
|
+
/* cmdk prevents the default of every Enter that reaches the Command
|
|
428
|
+
root, which would swallow an action button's keyboard click, so
|
|
429
|
+
Enter stops here. Only the actions inside can take focus. */
|
|
430
|
+
onKeyDown={
|
|
431
|
+
hasActions
|
|
432
|
+
? (event) => {
|
|
433
|
+
if (event.key === "Enter") event.stopPropagation();
|
|
434
|
+
}
|
|
435
|
+
: undefined
|
|
436
|
+
}
|
|
384
437
|
{...props}
|
|
385
438
|
>
|
|
386
|
-
{
|
|
387
|
-
/* The compact form
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
{
|
|
401
|
-
|
|
402
|
-
className="text-muted-foreground"
|
|
403
|
-
data-slot="command-empty-description"
|
|
404
|
-
>
|
|
405
|
-
{description}
|
|
406
|
-
</p>
|
|
407
|
-
) : null}
|
|
408
|
-
</div>
|
|
439
|
+
{isCompact ? (
|
|
440
|
+
/* The compact form is EmptyState at size sm — one empty block
|
|
441
|
+
across the DS (Patterns/Empty States). */
|
|
442
|
+
<EmptyState
|
|
443
|
+
className="px-4"
|
|
444
|
+
/* The parts derive their hooks from it (`-icon`, `-title`,
|
|
445
|
+
`-message`); the root's own hook stays on the cmdk wrapper. */
|
|
446
|
+
data-hook={undefined}
|
|
447
|
+
dataHook={dataHook ?? "command-empty"}
|
|
448
|
+
description={description}
|
|
449
|
+
icon={icon}
|
|
450
|
+
size="sm"
|
|
451
|
+
title={title === undefined ? children : title}
|
|
452
|
+
>
|
|
453
|
+
{hasActions ? children : null}
|
|
454
|
+
</EmptyState>
|
|
409
455
|
) : (
|
|
410
456
|
children
|
|
411
457
|
)}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
import { cn } from "@brightlocal/ui-components/lib/utils";
|
|
6
|
+
import { useFieldAriaRegistration } from "../lib/field-aria";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Whether the count is within its limit.
|
|
10
|
+
* - `neutral`: at or under the limit. Exactly at the limit is the count the
|
|
11
|
+
* user is allowed to reach, so it is not highlighted.
|
|
12
|
+
* - `over`: above the limit
|
|
13
|
+
*/
|
|
14
|
+
type CountIndicatorState = "neutral" | "over";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Screen reader copy for the count indicator. Every string is optional;
|
|
18
|
+
* pass translations through `labels`.
|
|
19
|
+
*/
|
|
20
|
+
interface CountIndicatorLabels {
|
|
21
|
+
/**
|
|
22
|
+
* Spoken value, read together with the field when it gets focus.
|
|
23
|
+
* @default (count, limit) => `${count} of ${limit}`
|
|
24
|
+
*/
|
|
25
|
+
value: (count: number, limit: number) => string;
|
|
26
|
+
/**
|
|
27
|
+
* Announced once when the count goes over the limit.
|
|
28
|
+
* @default (excess) => `${excess} over the limit`
|
|
29
|
+
*/
|
|
30
|
+
over: (excess: number) => string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const DEFAULT_LABELS: CountIndicatorLabels = {
|
|
34
|
+
value: (count, limit) => `${count} of ${limit}`,
|
|
35
|
+
over: (excess) => `${excess} over the limit`,
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** Resolves the state for a count against a limit. */
|
|
39
|
+
function getCountState(count: number, limit: number): CountIndicatorState {
|
|
40
|
+
return count > limit ? "over" : "neutral";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface CountIndicatorProps extends Omit<
|
|
44
|
+
React.HTMLAttributes<HTMLSpanElement>,
|
|
45
|
+
"children"
|
|
46
|
+
> {
|
|
47
|
+
/**
|
|
48
|
+
* Data hook for automated testing (REQUIRED)
|
|
49
|
+
* @example "description-count"
|
|
50
|
+
*/
|
|
51
|
+
dataHook: string;
|
|
52
|
+
/**
|
|
53
|
+
* Current count: characters typed, or items added.
|
|
54
|
+
*/
|
|
55
|
+
count: number;
|
|
56
|
+
/**
|
|
57
|
+
* Largest allowed count.
|
|
58
|
+
*/
|
|
59
|
+
limit: number;
|
|
60
|
+
/**
|
|
61
|
+
* Screen reader copy. Pass translations here; omitted entries use English.
|
|
62
|
+
*/
|
|
63
|
+
labels?: Partial<CountIndicatorLabels>;
|
|
64
|
+
/**
|
|
65
|
+
* Set to `false` while the count is not known yet (for example before a
|
|
66
|
+
* field has been read from the DOM). The first count after that becomes the
|
|
67
|
+
* silent starting point, so a saved value that is already over the limit is
|
|
68
|
+
* not announced.
|
|
69
|
+
* @default true
|
|
70
|
+
*/
|
|
71
|
+
ready?: boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Optional tracking prop for analytics tracking element identifier
|
|
74
|
+
*/
|
|
75
|
+
trackingEl?: string;
|
|
76
|
+
/**
|
|
77
|
+
* Optional tracking prop for analytics tracking label
|
|
78
|
+
*/
|
|
79
|
+
trackingLabel?: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* A small count against a limit, such as `120 / 750`.
|
|
84
|
+
*
|
|
85
|
+
* It is the shared count for text fields: `Textarea` shows it with `limit`,
|
|
86
|
+
* `InputGroupCount` and `InputChipCount` place it inside an input group or a
|
|
87
|
+
* chip field. Use it directly only to build another field that needs it.
|
|
88
|
+
*
|
|
89
|
+
* The count never blocks typing. Over the limit it turns red, and the field
|
|
90
|
+
* around it gets the error border; the consumer shows the error message and
|
|
91
|
+
* stops the save.
|
|
92
|
+
*
|
|
93
|
+
* Screen readers hear the value with the field (`aria-describedby`, wired
|
|
94
|
+
* automatically inside a `Field`). A polite live region speaks only when the
|
|
95
|
+
* state changes, never on each keystroke.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```tsx
|
|
99
|
+
* <CountIndicator dataHook="alt-count" count={alt.length} limit={125} />
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
const CountIndicator = React.forwardRef<HTMLSpanElement, CountIndicatorProps>(
|
|
103
|
+
(
|
|
104
|
+
{
|
|
105
|
+
className,
|
|
106
|
+
dataHook,
|
|
107
|
+
count,
|
|
108
|
+
limit,
|
|
109
|
+
labels,
|
|
110
|
+
ready = true,
|
|
111
|
+
id,
|
|
112
|
+
trackingEl,
|
|
113
|
+
trackingLabel,
|
|
114
|
+
...props
|
|
115
|
+
},
|
|
116
|
+
ref
|
|
117
|
+
) => {
|
|
118
|
+
const generatedId = React.useId();
|
|
119
|
+
const indicatorId = id ?? generatedId;
|
|
120
|
+
const copy = { ...DEFAULT_LABELS, ...labels };
|
|
121
|
+
const state = getCountState(count, limit);
|
|
122
|
+
|
|
123
|
+
// Inside a Field, the control's aria-describedby picks this id up.
|
|
124
|
+
useFieldAriaRegistration("count", indicatorId, true);
|
|
125
|
+
|
|
126
|
+
// Speak only when the state changes. The first render stays silent, so a
|
|
127
|
+
// saved value that is already over the limit is not announced on load.
|
|
128
|
+
const [announced, setAnnounced] = React.useState({
|
|
129
|
+
state,
|
|
130
|
+
text: "",
|
|
131
|
+
seeded: ready,
|
|
132
|
+
});
|
|
133
|
+
if (!ready) {
|
|
134
|
+
if (announced.seeded) setAnnounced({ state, text: "", seeded: false });
|
|
135
|
+
} else if (!announced.seeded) {
|
|
136
|
+
// First known count: the starting point, never an announcement
|
|
137
|
+
setAnnounced({ state, text: "", seeded: true });
|
|
138
|
+
} else if (announced.state !== state) {
|
|
139
|
+
setAnnounced({
|
|
140
|
+
state,
|
|
141
|
+
text: state === "over" ? copy.over(count - limit) : "",
|
|
142
|
+
seeded: true,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
const announcement = announced.text;
|
|
146
|
+
|
|
147
|
+
return (
|
|
148
|
+
<>
|
|
149
|
+
<span
|
|
150
|
+
ref={ref}
|
|
151
|
+
className={cn(
|
|
152
|
+
"text-body-xs shrink-0 tabular-nums",
|
|
153
|
+
state === "neutral" && "text-muted-foreground",
|
|
154
|
+
state === "over" && "text-destructive-foreground",
|
|
155
|
+
className
|
|
156
|
+
)}
|
|
157
|
+
data-hook={dataHook}
|
|
158
|
+
data-slot="count-indicator"
|
|
159
|
+
data-state={state}
|
|
160
|
+
data-tracking-el={trackingEl}
|
|
161
|
+
data-tracking-label={trackingLabel}
|
|
162
|
+
id={indicatorId}
|
|
163
|
+
{...props}
|
|
164
|
+
>
|
|
165
|
+
<span aria-hidden="true">
|
|
166
|
+
{count} / {limit}
|
|
167
|
+
</span>
|
|
168
|
+
<span className="sr-only">{copy.value(count, limit)}</span>
|
|
169
|
+
</span>
|
|
170
|
+
<span
|
|
171
|
+
aria-atomic="true"
|
|
172
|
+
aria-live="polite"
|
|
173
|
+
className="sr-only"
|
|
174
|
+
data-slot="count-indicator-announcer"
|
|
175
|
+
role="status"
|
|
176
|
+
>
|
|
177
|
+
{announcement}
|
|
178
|
+
</span>
|
|
179
|
+
</>
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
CountIndicator.displayName = "CountIndicator";
|
|
185
|
+
|
|
186
|
+
export { CountIndicator, getCountState };
|
|
187
|
+
|
|
188
|
+
export type { CountIndicatorLabels, CountIndicatorProps, CountIndicatorState };
|