@behavioral-agentic-design-system/core 0.27.0 → 0.29.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/package.json +5 -5
- package/scripts/a11y-lint.cjs +78 -9
- package/scripts/build-tokens.cjs +273 -15
- package/scripts/wcag-audit.cjs +37 -7
- package/src/api/layers/file.ts +1 -0
- package/src/app/font-ready.tsx +6 -5
- package/src/app/layout.tsx +54 -3
- package/src/app/preview/components/page.tsx +5735 -2500
- package/src/app/preview/layout.tsx +4 -1
- package/src/app/preview/showcase-filter.tsx +66 -0
- package/src/app/preview/tab-replay.tsx +63 -0
- package/src/app/shell.tsx +91 -18
- package/src/components/customize/save-bar.tsx +19 -6
- package/src/components/docs-tab.tsx +36 -24
- package/src/components/layers-tab.tsx +37 -5
- package/src/components/ui/ai-badge.tsx +74 -0
- package/src/components/ui/alert.tsx +12 -0
- package/src/components/ui/badge.tsx +19 -0
- package/src/components/ui/chat-bubble.tsx +298 -0
- package/src/components/ui/chat-marker.tsx +120 -0
- package/src/components/ui/checkbox.tsx +9 -1
- package/src/components/ui/clipboard.tsx +291 -0
- package/src/components/ui/close-button.tsx +71 -0
- package/src/components/ui/combobox.tsx +62 -3
- package/src/components/ui/container.tsx +65 -0
- package/src/components/ui/copy-button.tsx +5 -1
- package/src/components/ui/data-table.tsx +388 -0
- package/src/components/ui/date-field.tsx +368 -0
- package/src/components/ui/drawer.tsx +334 -0
- package/src/components/ui/file-button.tsx +145 -0
- package/src/components/ui/floating-indicator.tsx +217 -0
- package/src/components/ui/focus-trap.tsx +184 -0
- package/src/components/ui/format-byte.tsx +113 -0
- package/src/components/ui/index.ts +28 -0
- package/src/components/ui/input.tsx +38 -3
- package/src/components/ui/keyboard-shortcut.tsx +146 -0
- package/src/components/ui/loading-dots.tsx +99 -0
- package/src/components/ui/mark.tsx +69 -0
- package/src/components/ui/month-picker.tsx +296 -0
- package/src/components/ui/multi-select.tsx +15 -13
- package/src/components/ui/nav-link.tsx +200 -0
- package/src/components/ui/navigation-rail.tsx +256 -0
- package/src/components/ui/notification.tsx +20 -0
- package/src/components/ui/password-input.tsx +34 -1
- package/src/components/ui/progress.tsx +15 -1
- package/src/components/ui/prose.tsx +62 -0
- package/src/components/ui/result.tsx +151 -0
- package/src/components/ui/rolling-number.tsx +201 -0
- package/src/components/ui/simple-grid.tsx +110 -0
- package/src/components/ui/skeleton.tsx +52 -3
- package/src/components/ui/squircle.tsx +274 -0
- package/src/components/ui/stack.tsx +105 -0
- package/src/components/ui/status-badge.tsx +93 -0
- package/src/components/ui/tabs.tsx +34 -7
- package/src/components/ui/tag.tsx +46 -5
- package/src/components/ui/theme-icon.tsx +138 -0
- package/src/components/ui/timeline.tsx +27 -10
- package/src/components/ui/tooltip.tsx +12 -1
- package/src/components/ui/tree-select.tsx +386 -0
- package/src/components/ui/typing-indicator.tsx +2 -2
- package/src/components/ui/word-rotate.tsx +6 -1
- package/src/components/ui/year-picker.tsx +307 -0
- package/src/data/docs-index.ts +10 -0
- package/src/hooks/use-global-primary-color.ts +57 -6
- package/src/stories/AiBadge.stories.tsx +67 -0
- package/src/stories/Alert.stories.tsx +31 -1
- package/src/stories/Badge.stories.tsx +63 -6
- package/src/stories/ChatBubble.stories.tsx +222 -0
- package/src/stories/ChatMarker.stories.tsx +59 -0
- package/src/stories/Checkbox.stories.tsx +5 -0
- package/src/stories/Clipboard.stories.tsx +136 -0
- package/src/stories/CloseButton.stories.tsx +65 -0
- package/src/stories/Combobox.stories.tsx +74 -3
- package/src/stories/Container.stories.tsx +134 -0
- package/src/stories/CopyButton.stories.tsx +2 -0
- package/src/stories/DataTable.stories.tsx +271 -0
- package/src/stories/DateField.stories.tsx +114 -0
- package/src/stories/Drawer.stories.tsx +211 -0
- package/src/stories/FileButton.stories.tsx +117 -0
- package/src/stories/FloatingIndicator.stories.tsx +343 -0
- package/src/stories/FocusTrap.stories.tsx +218 -0
- package/src/stories/FormatByte.stories.tsx +142 -0
- package/src/stories/Input.stories.tsx +91 -0
- package/src/stories/KeyboardShortcut.stories.tsx +87 -0
- package/src/stories/LoadingDots.stories.tsx +73 -0
- package/src/stories/Mark.stories.tsx +84 -0
- package/src/stories/MonthPicker.stories.tsx +99 -0
- package/src/stories/MultiSelect.stories.tsx +3 -1
- package/src/stories/NavLink.stories.tsx +213 -0
- package/src/stories/NavigationRail.stories.tsx +413 -0
- package/src/stories/Notification.stories.tsx +53 -0
- package/src/stories/PasswordInput.stories.tsx +21 -0
- package/src/stories/Progress.stories.tsx +38 -0
- package/src/stories/Prose.stories.tsx +232 -0
- package/src/stories/Result.stories.tsx +145 -0
- package/src/stories/RollingNumber.stories.tsx +226 -0
- package/src/stories/SimpleGrid.stories.tsx +130 -0
- package/src/stories/Skeleton.stories.tsx +62 -1
- package/src/stories/Squircle.stories.tsx +127 -0
- package/src/stories/Stack.stories.tsx +139 -0
- package/src/stories/StatusBadge.stories.tsx +125 -0
- package/src/stories/Tabs.stories.tsx +56 -1
- package/src/stories/Tag.stories.tsx +31 -0
- package/src/stories/ThemeIcon.stories.tsx +97 -0
- package/src/stories/Timeline.stories.tsx +69 -0
- package/src/stories/Tooltip.stories.tsx +18 -0
- package/src/stories/TreeSelect.stories.tsx +222 -0
- package/src/stories/TypingIndicator.stories.tsx +1 -0
- package/src/stories/WordRotate.stories.tsx +1 -0
- package/src/stories/YearPicker.stories.tsx +100 -0
- package/src/styles.css +289 -15
- package/src/tokens.json +96 -1
- package/LICENSE +0 -21
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
import { Button } from "./button";
|
|
6
|
+
|
|
7
|
+
// ─── Types ────────────────────────────────────────────────────────────────────
|
|
8
|
+
|
|
9
|
+
export interface FileButtonProps
|
|
10
|
+
extends Omit<
|
|
11
|
+
React.ComponentProps<"button">,
|
|
12
|
+
"children" | "onClick" | "onChange"
|
|
13
|
+
> {
|
|
14
|
+
/** Called with the selected File[] (or null on cancel / empty selection). */
|
|
15
|
+
onChange?: (files: File[] | null) => void;
|
|
16
|
+
/**
|
|
17
|
+
* Accept string passed to <input type="file">. e.g. "image/*", ".pdf,.docx".
|
|
18
|
+
* Omit to accept all file types.
|
|
19
|
+
*/
|
|
20
|
+
accept?: string;
|
|
21
|
+
/** Allow multiple file selection. Default false. */
|
|
22
|
+
multiple?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Visible label / trigger children. Defaults to "Upload file" so the button
|
|
25
|
+
* always has an accessible name. Icon-only usage MUST pass aria-label.
|
|
26
|
+
*/
|
|
27
|
+
children?: React.ReactNode;
|
|
28
|
+
/**
|
|
29
|
+
* Forward ref to the hidden input, for programmatic reset via
|
|
30
|
+
* `inputRef.current.value = ""` (the component already resets after every
|
|
31
|
+
* selection so the same file can be re-picked).
|
|
32
|
+
*/
|
|
33
|
+
inputRef?: React.RefObject<HTMLInputElement | null>;
|
|
34
|
+
/** Button size (forwarded to Button). Default "default". */
|
|
35
|
+
size?: "sm" | "default" | "lg" | "xl" | "2xl" | "icon";
|
|
36
|
+
/** Button variant (forwarded to Button). Default "outline". */
|
|
37
|
+
variant?:
|
|
38
|
+
| "default"
|
|
39
|
+
| "destructive"
|
|
40
|
+
| "outline"
|
|
41
|
+
| "secondary"
|
|
42
|
+
| "ghost"
|
|
43
|
+
| "link";
|
|
44
|
+
/** Disabled forwarded to both the button and the hidden input. */
|
|
45
|
+
disabled?: boolean;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ─── FileButton ───────────────────────────────────────────────────────────────
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Click-to-browse file picker trigger. Renders as a BADS Button that
|
|
52
|
+
* programmatically clicks a visually-hidden <input type="file">.
|
|
53
|
+
*
|
|
54
|
+
* Distinct from FileUpload (drag-and-drop dropzone with file list, reject
|
|
55
|
+
* state, and size validation) — FileButton is the pure button affordance:
|
|
56
|
+
* profile photo change, "Import CSV", document attachment in a composer.
|
|
57
|
+
*
|
|
58
|
+
* Does NOT upload anything. The consumer's onChange handler owns any network
|
|
59
|
+
* call and its own consent/retention obligations.
|
|
60
|
+
*
|
|
61
|
+
* Motion: none of its own — hover/focus/active transitions inherit from
|
|
62
|
+
* Button's existing token-driven contract (transition-colors + focus ring).
|
|
63
|
+
*/
|
|
64
|
+
function FileButton({
|
|
65
|
+
onChange,
|
|
66
|
+
accept,
|
|
67
|
+
multiple = false,
|
|
68
|
+
children = "Upload file",
|
|
69
|
+
inputRef,
|
|
70
|
+
size = "default",
|
|
71
|
+
variant = "outline",
|
|
72
|
+
disabled,
|
|
73
|
+
...props
|
|
74
|
+
}: FileButtonProps) {
|
|
75
|
+
const internalRef = React.useRef<HTMLInputElement | null>(null);
|
|
76
|
+
|
|
77
|
+
// One callback ref feeds both the internal ref (used for .click()) and the
|
|
78
|
+
// consumer's optional inputRef (used for programmatic reset).
|
|
79
|
+
const setInputRef = React.useCallback(
|
|
80
|
+
(node: HTMLInputElement | null) => {
|
|
81
|
+
internalRef.current = node;
|
|
82
|
+
if (inputRef) inputRef.current = node;
|
|
83
|
+
},
|
|
84
|
+
[inputRef]
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
88
|
+
const list = e.target.files;
|
|
89
|
+
onChange?.(list && list.length > 0 ? Array.from(list) : null);
|
|
90
|
+
// Reset so selecting the same file again re-fires change.
|
|
91
|
+
e.target.value = "";
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// Native `cancel` event (Chrome 113+/Safari 16.4+/Firefox 91+): user opened
|
|
95
|
+
// the picker and dismissed it. Attached natively — React's typed props don't
|
|
96
|
+
// include onCancel on <input>. Older browsers just never fire this —
|
|
97
|
+
// degraded (no null callback), not broken.
|
|
98
|
+
const onChangeRef = React.useRef(onChange);
|
|
99
|
+
React.useLayoutEffect(() => {
|
|
100
|
+
onChangeRef.current = onChange;
|
|
101
|
+
});
|
|
102
|
+
React.useEffect(() => {
|
|
103
|
+
const node = internalRef.current;
|
|
104
|
+
if (!node) return;
|
|
105
|
+
const handleCancel = () => onChangeRef.current?.(null);
|
|
106
|
+
node.addEventListener("cancel", handleCancel);
|
|
107
|
+
return () => node.removeEventListener("cancel", handleCancel);
|
|
108
|
+
}, []);
|
|
109
|
+
|
|
110
|
+
return (
|
|
111
|
+
<>
|
|
112
|
+
<Button
|
|
113
|
+
type="button"
|
|
114
|
+
data-slot="file-button"
|
|
115
|
+
variant={variant}
|
|
116
|
+
size={size}
|
|
117
|
+
disabled={disabled}
|
|
118
|
+
onClick={() => internalRef.current?.click()}
|
|
119
|
+
{...props}
|
|
120
|
+
>
|
|
121
|
+
{children}
|
|
122
|
+
</Button>
|
|
123
|
+
{/*
|
|
124
|
+
Hidden picker input. aria-hidden + tabIndex=-1: the Button is the single
|
|
125
|
+
interactive/announced stop — the input exists only to reach the OS
|
|
126
|
+
dialog. sr-only keeps it out of layout without display:none (which
|
|
127
|
+
would break .click() in some browsers).
|
|
128
|
+
*/}
|
|
129
|
+
<input
|
|
130
|
+
ref={setInputRef}
|
|
131
|
+
type="file"
|
|
132
|
+
data-slot="file-button-input"
|
|
133
|
+
accept={accept}
|
|
134
|
+
multiple={multiple}
|
|
135
|
+
disabled={disabled}
|
|
136
|
+
aria-hidden="true"
|
|
137
|
+
tabIndex={-1}
|
|
138
|
+
className="sr-only"
|
|
139
|
+
onChange={handleChange}
|
|
140
|
+
/>
|
|
141
|
+
</>
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export { FileButton };
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { cn } from "../../lib/utils"
|
|
5
|
+
|
|
6
|
+
// ─── Types ────────────────────────────────────────────────────────────────────
|
|
7
|
+
|
|
8
|
+
interface FloatingIndicatorProps {
|
|
9
|
+
/**
|
|
10
|
+
* The currently active element to track. Typically a ref.current for a nav
|
|
11
|
+
* item or tab trigger. When null, the indicator fades out.
|
|
12
|
+
*
|
|
13
|
+
* The element must be a descendant of `parent`.
|
|
14
|
+
*/
|
|
15
|
+
target: Element | null
|
|
16
|
+
/**
|
|
17
|
+
* The container element that wraps all nav items.
|
|
18
|
+
*
|
|
19
|
+
* **This element must have `position: relative` applied** so the indicator's
|
|
20
|
+
* `position: absolute` resolves against it. Without it the indicator will
|
|
21
|
+
* position against the nearest positioned ancestor, which is almost always wrong.
|
|
22
|
+
*/
|
|
23
|
+
parent: Element | null
|
|
24
|
+
/** Applied to the indicator div */
|
|
25
|
+
className?: string
|
|
26
|
+
/**
|
|
27
|
+
* CSS duration string, defaults to `var(--duration-base)`.
|
|
28
|
+
* Pass a CSS duration (`"150ms"`, `"var(--duration-fast)"`) to override per-instance.
|
|
29
|
+
* Prefer token vars over literal values so token changes propagate automatically.
|
|
30
|
+
*/
|
|
31
|
+
transitionDuration?: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface IndicatorRect {
|
|
35
|
+
left: number
|
|
36
|
+
top: number
|
|
37
|
+
width: number
|
|
38
|
+
height: number
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ─── Component ────────────────────────────────────────────────────────────────
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Animated highlight that slides between nav/tab items as selection changes.
|
|
45
|
+
*
|
|
46
|
+
* Renders an `aria-hidden` absolutely-positioned div that measures the active
|
|
47
|
+
* item's bounding rect relative to the parent container and smoothly translates
|
|
48
|
+
* to follow it. Motion uses token-driven `var(--duration-base)` + `var(--ease-out)`.
|
|
49
|
+
*
|
|
50
|
+
* **Usage requirements:**
|
|
51
|
+
* - The `parent` container must have `position: relative` (or any non-static position).
|
|
52
|
+
* - `target` must be a descendant of `parent`.
|
|
53
|
+
* - The indicator is purely decorative — all active-state ARIA (`aria-selected`,
|
|
54
|
+
* `aria-current`, `data-state="active"`) belongs on the nav items themselves.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```tsx
|
|
58
|
+
* const containerRef = useRef<HTMLDivElement>(null)
|
|
59
|
+
* const [target, setTarget] = useState<HTMLElement | null>(null)
|
|
60
|
+
* const itemRefs = useRef<(HTMLButtonElement | null)[]>([])
|
|
61
|
+
*
|
|
62
|
+
* useEffect(() => {
|
|
63
|
+
* setTarget(itemRefs.current[activeIndex])
|
|
64
|
+
* }, [activeIndex])
|
|
65
|
+
*
|
|
66
|
+
* <div ref={containerRef} className="relative inline-flex bg-muted p-1 rounded-lg">
|
|
67
|
+
* <FloatingIndicator target={target} parent={containerRef.current} />
|
|
68
|
+
* {items.map((item, i) => (
|
|
69
|
+
* <button key={item} ref={el => { itemRefs.current[i] = el }} onClick={() => setActiveIndex(i)}>
|
|
70
|
+
* {item}
|
|
71
|
+
* </button>
|
|
72
|
+
* ))}
|
|
73
|
+
* </div>
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
function FloatingIndicator({
|
|
77
|
+
target,
|
|
78
|
+
parent,
|
|
79
|
+
className,
|
|
80
|
+
transitionDuration = "var(--duration-base)",
|
|
81
|
+
}: FloatingIndicatorProps) {
|
|
82
|
+
const [rect, setRect] = React.useState<IndicatorRect | null>(null)
|
|
83
|
+
// Track whether we've ever had a rect — used to skip the initial transition
|
|
84
|
+
// so the indicator appears in place on first render rather than flying in
|
|
85
|
+
// from the top-left corner.
|
|
86
|
+
const [hasInitialized, setHasInitialized] = React.useState(false)
|
|
87
|
+
|
|
88
|
+
React.useEffect(() => {
|
|
89
|
+
if (!target || !parent) {
|
|
90
|
+
setRect(null)
|
|
91
|
+
setHasInitialized(false)
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const measure = () => {
|
|
96
|
+
const parentRect = parent.getBoundingClientRect()
|
|
97
|
+
const targetRect = target.getBoundingClientRect()
|
|
98
|
+
setRect({
|
|
99
|
+
left: targetRect.left - parentRect.left + parent.scrollLeft,
|
|
100
|
+
top: targetRect.top - parentRect.top + parent.scrollTop,
|
|
101
|
+
width: targetRect.width,
|
|
102
|
+
height: targetRect.height,
|
|
103
|
+
})
|
|
104
|
+
setHasInitialized(true)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
measure()
|
|
108
|
+
|
|
109
|
+
// Resolve the correct window from the target's own document — required for
|
|
110
|
+
// cross-window safety (preview iframes, Radix portals). The bare `window`
|
|
111
|
+
// global is the wrong reference inside an iframe (component-build-spec §2).
|
|
112
|
+
const win =
|
|
113
|
+
(target as HTMLElement).ownerDocument?.defaultView ?? window
|
|
114
|
+
|
|
115
|
+
const ro = new win.ResizeObserver(measure)
|
|
116
|
+
ro.observe(parent)
|
|
117
|
+
ro.observe(target)
|
|
118
|
+
|
|
119
|
+
return () => ro.disconnect()
|
|
120
|
+
}, [target, parent])
|
|
121
|
+
|
|
122
|
+
const isVisible = !!rect
|
|
123
|
+
|
|
124
|
+
return (
|
|
125
|
+
<div
|
|
126
|
+
aria-hidden="true"
|
|
127
|
+
data-slot="floating-indicator"
|
|
128
|
+
className={cn(
|
|
129
|
+
"pointer-events-none absolute rounded-md bg-background shadow-sm",
|
|
130
|
+
// Explicit property list — never transition-all (animates focus-ring
|
|
131
|
+
// box-shadow, layout props, and every other property, including ones
|
|
132
|
+
// that should be instant). See CLAUDE.md § microanimation rule.
|
|
133
|
+
"transition-[left,top,width,height]",
|
|
134
|
+
!isVisible && "opacity-0",
|
|
135
|
+
className
|
|
136
|
+
)}
|
|
137
|
+
style={{
|
|
138
|
+
left: rect?.left ?? 0,
|
|
139
|
+
top: rect?.top ?? 0,
|
|
140
|
+
width: rect?.width ?? 0,
|
|
141
|
+
height: rect?.height ?? 0,
|
|
142
|
+
// Token-driven duration — var() resolves at runtime from styles.css.
|
|
143
|
+
// Using "var(--duration-base)" not a literal "200ms" so token changes
|
|
144
|
+
// in tokens.json → pnpm build:tokens → propagate here automatically.
|
|
145
|
+
transitionDuration: hasInitialized ? transitionDuration : "0ms",
|
|
146
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
147
|
+
}}
|
|
148
|
+
/>
|
|
149
|
+
)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// ─── Hook ─────────────────────────────────────────────────────────────────────
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Convenience hook that manages the target element state for a `FloatingIndicator`.
|
|
156
|
+
*
|
|
157
|
+
* Returns two complementary setters:
|
|
158
|
+
* - `setTarget` — call imperatively on item click / keyboard navigation
|
|
159
|
+
* - `targetRef` — a React callback ref; conditionally pass to the active item's `ref`
|
|
160
|
+
* so the indicator auto-positions when the active item mounts
|
|
161
|
+
*
|
|
162
|
+
* The `target` state value is ready to pass directly to `<FloatingIndicator target={...} />`.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```tsx
|
|
166
|
+
* const containerRef = useRef<HTMLDivElement>(null)
|
|
167
|
+
* const [activeIndex, setActiveIndex] = useState(0)
|
|
168
|
+
* const { setTarget, targetRef, target } = useFloatingIndicator(containerRef)
|
|
169
|
+
*
|
|
170
|
+
* <div ref={containerRef} className="relative inline-flex bg-muted p-1 rounded-lg">
|
|
171
|
+
* <FloatingIndicator target={target} parent={containerRef.current} />
|
|
172
|
+
* {items.map((item, i) => (
|
|
173
|
+
* <button
|
|
174
|
+
* key={item.value}
|
|
175
|
+
* ref={activeIndex === i ? targetRef : null}
|
|
176
|
+
* onClick={() => { setActiveIndex(i); setTarget(buttonEl) }}
|
|
177
|
+
* >
|
|
178
|
+
* {item.label}
|
|
179
|
+
* </button>
|
|
180
|
+
* ))}
|
|
181
|
+
* </div>
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
function useFloatingIndicator(
|
|
185
|
+
containerRef: React.RefObject<HTMLElement | null>
|
|
186
|
+
) {
|
|
187
|
+
const [target, setTargetState] = React.useState<HTMLElement | null>(null)
|
|
188
|
+
|
|
189
|
+
/** Imperatively set the active target element. Call on click or keyboard navigation. */
|
|
190
|
+
const setTarget = React.useCallback((el: HTMLElement | null) => {
|
|
191
|
+
setTargetState(el)
|
|
192
|
+
}, [])
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Callback ref — pass as `ref` to the currently active item.
|
|
196
|
+
* When the active item mounts (or the ref is conditionally applied to a
|
|
197
|
+
* different item), the indicator auto-snaps to that element.
|
|
198
|
+
* Fires on mount only — for active-index changes on stable DOM, use `setTarget`.
|
|
199
|
+
*/
|
|
200
|
+
const targetRef = React.useCallback((el: HTMLElement | null) => {
|
|
201
|
+
if (el !== null) setTargetState(el)
|
|
202
|
+
}, [])
|
|
203
|
+
|
|
204
|
+
return {
|
|
205
|
+
/** The current target element — pass to `<FloatingIndicator target={...} />` */
|
|
206
|
+
target,
|
|
207
|
+
/** Imperative setter — call on click/keyboard */
|
|
208
|
+
setTarget,
|
|
209
|
+
/** Callback ref — conditionally pass to the active item's `ref` prop */
|
|
210
|
+
targetRef,
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// ─── Exports ──────────────────────────────────────────────────────────────────
|
|
215
|
+
|
|
216
|
+
export { FloatingIndicator, useFloatingIndicator }
|
|
217
|
+
export type { FloatingIndicatorProps }
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
import { cn } from "../../lib/utils";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Standalone focus-containment utility. Traps Tab/Shift+Tab cycling within
|
|
9
|
+
* its children while `active` is true, and calls `onEscapeKeyDown` when
|
|
10
|
+
* Escape is pressed.
|
|
11
|
+
*
|
|
12
|
+
* Use this when a custom overlay, inline drawer, or panel needs focus
|
|
13
|
+
* containment but doesn't warrant the full Dialog or Sheet weight. All
|
|
14
|
+
* BADS Radix-based components (Dialog, Sheet) trap focus internally — this
|
|
15
|
+
* is for the cases that fall between.
|
|
16
|
+
*
|
|
17
|
+
* WCAG 2.1.2 No Keyboard Trap: trapping focus alone is compliant only when
|
|
18
|
+
* an escape mechanism exists. Wire `onEscapeKeyDown` to close the container
|
|
19
|
+
* so keyboard users can always leave.
|
|
20
|
+
*
|
|
21
|
+
* <FocusTrap active={open} onEscapeKeyDown={() => setOpen(false)}>
|
|
22
|
+
* <div role="dialog" aria-modal="true" aria-labelledby="dlg-title">
|
|
23
|
+
* <FocusTrapInitialFocus />
|
|
24
|
+
* <h2 id="dlg-title">Confirm</h2>
|
|
25
|
+
* <button>OK</button>
|
|
26
|
+
* <button onClick={() => setOpen(false)}>Cancel</button>
|
|
27
|
+
* </div>
|
|
28
|
+
* </FocusTrap>
|
|
29
|
+
*
|
|
30
|
+
* Focus cycling: on Tab from the last focusable element, cycles back to the
|
|
31
|
+
* first; on Shift+Tab from the first, cycles to the last. The cycle includes
|
|
32
|
+
* all focusable elements: <button>, <a href>, <input>, <select>, <textarea>,
|
|
33
|
+
* and any element with tabIndex >= 0.
|
|
34
|
+
*
|
|
35
|
+
* FocusTrapInitialFocus: place anywhere inside FocusTrap. When the trap
|
|
36
|
+
* activates, focus moves to the nearest [data-focus-trap-initial] element
|
|
37
|
+
* instead of the first focusable element in DOM order. Useful when the
|
|
38
|
+
* visually primary action isn't the first tabbable in the DOM.
|
|
39
|
+
*
|
|
40
|
+
* Zero dependencies. Reduced-motion safety: no animation — the focus ring
|
|
41
|
+
* uses the existing CSS base layer (SC 2.4.7 + 2.4.13 enforced).
|
|
42
|
+
* Microanimation: no interactive state changes on this wrapper; rule
|
|
43
|
+
* vacuously satisfied (same exemption as VisuallyHidden, SkipLink).
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
const FOCUSABLE_SELECTORS = [
|
|
47
|
+
"button:not([disabled])",
|
|
48
|
+
"a[href]",
|
|
49
|
+
"input:not([disabled])",
|
|
50
|
+
"select:not([disabled])",
|
|
51
|
+
"textarea:not([disabled])",
|
|
52
|
+
"[tabindex]:not([tabindex='-1'])",
|
|
53
|
+
"details > summary:not([disabled])",
|
|
54
|
+
].join(",");
|
|
55
|
+
|
|
56
|
+
export interface FocusTrapProps extends React.ComponentProps<"div"> {
|
|
57
|
+
/** Whether the trap is active. When false the component renders its
|
|
58
|
+
* children unchanged with no keyboard interception. */
|
|
59
|
+
active?: boolean;
|
|
60
|
+
/** Whether Tab cycles back to the first focusable after reaching the last
|
|
61
|
+
* (and vice versa for Shift+Tab). Default: true. */
|
|
62
|
+
loop?: boolean;
|
|
63
|
+
/** Called when Escape is pressed while the trap is active.
|
|
64
|
+
* Required for WCAG 2.1.2 compliance: always wire this to close the
|
|
65
|
+
* containing overlay so keyboard users can leave. */
|
|
66
|
+
onEscapeKeyDown?: (event: KeyboardEvent) => void;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function FocusTrap({
|
|
70
|
+
active = true,
|
|
71
|
+
loop = true,
|
|
72
|
+
onEscapeKeyDown,
|
|
73
|
+
className,
|
|
74
|
+
children,
|
|
75
|
+
...props
|
|
76
|
+
}: FocusTrapProps) {
|
|
77
|
+
const containerRef = React.useRef<HTMLDivElement>(null);
|
|
78
|
+
|
|
79
|
+
// On activation, move focus to the first focusable element (or the
|
|
80
|
+
// FocusTrapInitialFocus marker if one is present).
|
|
81
|
+
React.useEffect(() => {
|
|
82
|
+
if (!active || !containerRef.current) return;
|
|
83
|
+
|
|
84
|
+
const container = containerRef.current;
|
|
85
|
+
|
|
86
|
+
// Prefer the explicit initial-focus marker if present.
|
|
87
|
+
const initialTarget = container.querySelector<HTMLElement>(
|
|
88
|
+
"[data-focus-trap-initial]"
|
|
89
|
+
);
|
|
90
|
+
const firstFocusable = container.querySelectorAll<HTMLElement>(
|
|
91
|
+
FOCUSABLE_SELECTORS
|
|
92
|
+
)[0];
|
|
93
|
+
|
|
94
|
+
const target = initialTarget ?? firstFocusable;
|
|
95
|
+
// Defer by one tick so the container is in the DOM before focusing.
|
|
96
|
+
const id = setTimeout(() => target?.focus(), 0);
|
|
97
|
+
return () => clearTimeout(id);
|
|
98
|
+
}, [active]);
|
|
99
|
+
|
|
100
|
+
// Handle Tab and Escape keydown events.
|
|
101
|
+
React.useEffect(() => {
|
|
102
|
+
if (!active || !containerRef.current) return;
|
|
103
|
+
|
|
104
|
+
const container = containerRef.current;
|
|
105
|
+
|
|
106
|
+
function handleKeyDown(event: KeyboardEvent) {
|
|
107
|
+
if (event.key === "Escape") {
|
|
108
|
+
onEscapeKeyDown?.(event);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (event.key !== "Tab") return;
|
|
113
|
+
|
|
114
|
+
const focusable = Array.from(
|
|
115
|
+
container.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTORS)
|
|
116
|
+
).filter((el) => !el.closest("[inert]") && !el.closest("[hidden]"));
|
|
117
|
+
|
|
118
|
+
if (focusable.length === 0) {
|
|
119
|
+
event.preventDefault();
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const first = focusable[0];
|
|
124
|
+
const last = focusable[focusable.length - 1];
|
|
125
|
+
const active = document.activeElement as HTMLElement | null;
|
|
126
|
+
|
|
127
|
+
if (event.shiftKey) {
|
|
128
|
+
// Shift+Tab: if focus is on the first element, cycle to the last.
|
|
129
|
+
if (loop && active === first) {
|
|
130
|
+
event.preventDefault();
|
|
131
|
+
last.focus();
|
|
132
|
+
}
|
|
133
|
+
} else {
|
|
134
|
+
// Tab: if focus is on the last element, cycle to the first.
|
|
135
|
+
if (loop && active === last) {
|
|
136
|
+
event.preventDefault();
|
|
137
|
+
first.focus();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
container.addEventListener("keydown", handleKeyDown);
|
|
143
|
+
return () => container.removeEventListener("keydown", handleKeyDown);
|
|
144
|
+
}, [active, loop, onEscapeKeyDown]);
|
|
145
|
+
|
|
146
|
+
return (
|
|
147
|
+
<div
|
|
148
|
+
data-slot="focus-trap"
|
|
149
|
+
data-state={active ? "active" : "inactive"}
|
|
150
|
+
className={cn(className)}
|
|
151
|
+
ref={containerRef}
|
|
152
|
+
{...props}
|
|
153
|
+
>
|
|
154
|
+
{children}
|
|
155
|
+
</div>
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Place this element anywhere inside a FocusTrap. When the trap activates,
|
|
161
|
+
* focus moves here instead of to the first focusable in DOM order.
|
|
162
|
+
*
|
|
163
|
+
* The element itself is not focusable (tabIndex=-1, not in the Tab cycle)
|
|
164
|
+
* — it is only a focus-target marker that FocusTrap finds at activation.
|
|
165
|
+
* Its purpose is to ensure the visually primary element gets initial focus
|
|
166
|
+
* regardless of its DOM position.
|
|
167
|
+
*/
|
|
168
|
+
function FocusTrapInitialFocus({
|
|
169
|
+
className,
|
|
170
|
+
...props
|
|
171
|
+
}: React.ComponentProps<"span">) {
|
|
172
|
+
return (
|
|
173
|
+
<span
|
|
174
|
+
data-slot="focus-trap-initial-focus"
|
|
175
|
+
data-focus-trap-initial
|
|
176
|
+
tabIndex={-1}
|
|
177
|
+
className={cn("sr-only", className)}
|
|
178
|
+
aria-hidden
|
|
179
|
+
{...props}
|
|
180
|
+
/>
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export { FocusTrap, FocusTrapInitialFocus };
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
|
|
3
|
+
import { cn } from "../../lib/utils"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Explicit default locale. FormatByte never reads the runtime locale
|
|
7
|
+
* (navigator.language): an implicit locale formats differently on the
|
|
8
|
+
* server and the client, which is the classic hydration-mismatch bug.
|
|
9
|
+
* Consumers localizing for real pass `locale` from their own i18n layer.
|
|
10
|
+
* Same contract as FormatNumber.
|
|
11
|
+
*/
|
|
12
|
+
const FORMAT_BYTE_DEFAULT_LOCALE = "en-US"
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* SI (base-1000) byte scale, matching Mantine's `formatBytes` and Chakra's
|
|
16
|
+
* byte format utilities — and what OS file managers (Finder, Nautilus)
|
|
17
|
+
* report. NOT the IEC base-1024 scale (KiB/MiB): a 1,048,576-byte file
|
|
18
|
+
* reads "1.0 MB" here, not "1.0 MiB".
|
|
19
|
+
*/
|
|
20
|
+
const BYTE_SCALE = [
|
|
21
|
+
{ threshold: 1_000_000_000_000, suffix: "TB" },
|
|
22
|
+
{ threshold: 1_000_000_000, suffix: "GB" },
|
|
23
|
+
{ threshold: 1_000_000, suffix: "MB" },
|
|
24
|
+
{ threshold: 1_000, suffix: "KB" },
|
|
25
|
+
] as const
|
|
26
|
+
|
|
27
|
+
export interface FormatByteProps
|
|
28
|
+
extends Omit<React.ComponentProps<"span">, "children"> {
|
|
29
|
+
/** The byte count to format (integer or float; negatives allowed). */
|
|
30
|
+
value: number
|
|
31
|
+
/**
|
|
32
|
+
* Decimal places on scaled values (KB and up). Defaults to `1`
|
|
33
|
+
* ("1.5 MB"). Byte-scale values (< 1000) always render as integers
|
|
34
|
+
* ("500 B") — fractional bytes aren't a thing users read.
|
|
35
|
+
*/
|
|
36
|
+
precision?: number
|
|
37
|
+
/**
|
|
38
|
+
* BCP 47 locale tag for the numeric part. Defaults to "en-US"
|
|
39
|
+
* (explicit, SSR-safe) — pass your i18n layer's locale to localize.
|
|
40
|
+
*/
|
|
41
|
+
locale?: string
|
|
42
|
+
/**
|
|
43
|
+
* `font-variant-numeric: tabular-nums` so digits occupy equal widths.
|
|
44
|
+
* Defaults to `true`, same as FormatNumber: file sizes overwhelmingly
|
|
45
|
+
* land in table columns and meta rows where alignment matters, and
|
|
46
|
+
* tabular figures sidestep the Hyperlegible Sans proportional-figure
|
|
47
|
+
* kerning collision. Pass `tabular={false}` for proportional figures.
|
|
48
|
+
*/
|
|
49
|
+
tabular?: boolean
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Locale-aware byte-value display primitive. Converts a raw byte count to
|
|
54
|
+
* the appropriate SI scale (B / KB / MB / GB / TB) and formats the number
|
|
55
|
+
* via `Intl.NumberFormat`. Display-only: for editable file-size inputs use
|
|
56
|
+
* NumberInput.
|
|
57
|
+
*
|
|
58
|
+
* Distinct from FormatNumber, which passes `Intl.NumberFormatOptions`
|
|
59
|
+
* straight through — `style="unit" unit="byte"` formats the raw numeric
|
|
60
|
+
* ("1,536,000 byte") and never picks the KB/MB/GB scale. FormatByte owns
|
|
61
|
+
* that scale conversion.
|
|
62
|
+
*
|
|
63
|
+
* Scaled output rounds, so the exact byte count rides the native `title`
|
|
64
|
+
* attribute ("1,536,000 bytes") for hover/long-press recovery — the same
|
|
65
|
+
* recoverability pattern FormatNumber uses for compact notation. A
|
|
66
|
+
* consumer-supplied `title` wins. Hook-free (no "use client"), so it works
|
|
67
|
+
* as a Server Component and formats identically on both sides.
|
|
68
|
+
*/
|
|
69
|
+
function FormatByte({
|
|
70
|
+
value,
|
|
71
|
+
precision = 1,
|
|
72
|
+
locale = FORMAT_BYTE_DEFAULT_LOCALE,
|
|
73
|
+
tabular = true,
|
|
74
|
+
className,
|
|
75
|
+
title,
|
|
76
|
+
...props
|
|
77
|
+
}: FormatByteProps) {
|
|
78
|
+
const abs = Math.abs(value)
|
|
79
|
+
const scale = BYTE_SCALE.find((s) => abs >= s.threshold)
|
|
80
|
+
|
|
81
|
+
let formatted: string
|
|
82
|
+
let fullValue: string | undefined
|
|
83
|
+
|
|
84
|
+
if (scale) {
|
|
85
|
+
const number = new Intl.NumberFormat(locale, {
|
|
86
|
+
minimumFractionDigits: precision,
|
|
87
|
+
maximumFractionDigits: precision,
|
|
88
|
+
}).format(value / scale.threshold)
|
|
89
|
+
formatted = `${number} ${scale.suffix}`
|
|
90
|
+
// Scaled output rounds — keep the exact count recoverable on title.
|
|
91
|
+
fullValue = `${new Intl.NumberFormat(locale, {
|
|
92
|
+
maximumFractionDigits: 0,
|
|
93
|
+
}).format(value)} bytes`
|
|
94
|
+
} else {
|
|
95
|
+
const number = new Intl.NumberFormat(locale, {
|
|
96
|
+
maximumFractionDigits: 0,
|
|
97
|
+
}).format(value)
|
|
98
|
+
formatted = `${number} B`
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return (
|
|
102
|
+
<span
|
|
103
|
+
data-slot="format-byte"
|
|
104
|
+
className={cn(tabular && "tabular-nums", className)}
|
|
105
|
+
title={title ?? fullValue}
|
|
106
|
+
{...props}
|
|
107
|
+
>
|
|
108
|
+
{formatted}
|
|
109
|
+
</span>
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export { FormatByte, FORMAT_BYTE_DEFAULT_LOCALE }
|