@behavioral-agentic-design-system/core 0.39.1 → 0.40.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 +8 -3
- package/scripts/a11y-lint.cjs +47 -6
- package/scripts/bads-components.mjs +118 -0
- package/scripts/build-tokens.cjs +133 -12
- package/scripts/check-stories.mjs +1 -1
- package/scripts/lint-component-source.mjs +100 -0
- package/scripts/lint-microanimation.mjs +267 -0
- package/scripts/size-audit.cjs +79 -0
- package/scripts/source-of-truth.cjs +58 -0
- package/scripts/ux-smoke.mjs +438 -0
- package/scripts/wcag-audit.cjs +49 -1
- package/src/api/layers/file.ts +1 -0
- package/src/app/preview/components/page.tsx +968 -0
- package/src/app/shell.tsx +11 -13
- package/src/components/customize/brand-mark-section.tsx +4 -2
- package/src/components/customize/design-controls.tsx +1 -1
- package/src/components/customize/discipline-section.tsx +3 -6
- package/src/components/customize/font-pickers.tsx +1 -1
- package/src/components/customize/letter-spacing.tsx +1 -1
- package/src/components/customize/save-bar.tsx +2 -2
- package/src/components/customize/theme-io.tsx +7 -7
- package/src/components/customize/type-scale.tsx +1 -1
- package/src/components/customize-tab.tsx +7 -4
- package/src/components/home-tab.tsx +3 -0
- package/src/components/ui/accordion.tsx +7 -0
- package/src/components/ui/alert.tsx +1 -0
- package/src/components/ui/announcement-bar.tsx +256 -0
- package/src/components/ui/badge.tsx +1 -0
- package/src/components/ui/callout.tsx +218 -0
- package/src/components/ui/chart.tsx +2 -14
- package/src/components/ui/color-picker.tsx +1 -1
- package/src/components/ui/format-relative-time.tsx +139 -0
- package/src/components/ui/grid.tsx +255 -0
- package/src/components/ui/icon.tsx +108 -0
- package/src/components/ui/index.ts +8 -0
- package/src/components/ui/page-header.tsx +63 -0
- package/src/components/ui/portal.tsx +111 -0
- package/src/components/ui/presence.tsx +172 -0
- package/src/components/ui/progress-stack.tsx +258 -0
- package/src/components/ui/select.tsx +89 -4
- package/src/components/ui/separator.tsx +1 -0
- package/src/components/ui/slider.tsx +83 -3
- package/src/components/ui/sparkline.tsx +2 -14
- package/src/components/ui/textarea.tsx +28 -1
- package/src/data/docs-index.ts +1 -0
- package/src/hooks/use-reduced-motion.ts +6 -0
- package/src/stories/Accordion.stories.tsx +59 -0
- package/src/stories/Alert.stories.tsx +3 -1
- package/src/stories/AnnouncementBar.stories.tsx +261 -0
- package/src/stories/Badge.stories.tsx +3 -0
- package/src/stories/Calendar.stories.tsx +1 -1
- package/src/stories/Callout.stories.tsx +219 -0
- package/src/stories/Chart.stories.tsx +4 -0
- package/src/stories/ColorPicker.stories.tsx +23 -0
- package/src/stories/FormatRelativeTime.stories.tsx +116 -0
- package/src/stories/Grid.stories.tsx +197 -0
- package/src/stories/Icon.stories.tsx +175 -0
- package/src/stories/MultiSelect.stories.tsx +1 -1
- package/src/stories/PageHeader.stories.tsx +102 -0
- package/src/stories/Portal.stories.tsx +189 -0
- package/src/stories/Presence.stories.tsx +228 -0
- package/src/stories/ProgressStack.stories.tsx +287 -0
- package/src/stories/Select.stories.tsx +25 -0
- package/src/stories/Separator.stories.tsx +3 -0
- package/src/stories/Slider.stories.tsx +54 -5
- package/src/stories/Sparkline.stories.tsx +3 -0
- package/src/stories/Textarea.stories.tsx +18 -0
- package/src/stories/story-groups.json +14 -5
- package/src/styles.css +133 -12
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { cva, type VariantProps } from "class-variance-authority"
|
|
5
|
+
|
|
6
|
+
import { cn } from "../../lib/utils"
|
|
7
|
+
import { getIconColor, type IconRole } from "../../lib/get-icon-color"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Icon — the system wrapper for any Lucide (or SVG-prop-spreading) icon.
|
|
11
|
+
* Three rules from CLAUDE.md → Icons become unforgettable here:
|
|
12
|
+
*
|
|
13
|
+
* 1. **Stroke width is 1.5, always.** Lucide defaults to 2; this wrapper pins
|
|
14
|
+
* 1.5 and deliberately omits `strokeWidth` from its props. If an icon
|
|
15
|
+
* looks too thin or heavy at 1.5, swap the icon — never the stroke.
|
|
16
|
+
* 2. **Size is a token ladder** (`sm` 14 / `default` 16 / `lg` 20 / `xl` 24),
|
|
17
|
+
* not a per-call pixel number. `default` matches the `size-4` convention
|
|
18
|
+
* every BADS button and row already uses.
|
|
19
|
+
* 3. **Color is a semantic role**, not a class. `color="error"` resolves
|
|
20
|
+
* through `getIconColor()` to `text-icon-error` — the `icon-*` token
|
|
21
|
+
* family that aliases already-AAA-audited text roles. No `color` means
|
|
22
|
+
* `currentColor`, which is the right default for an icon beside text.
|
|
23
|
+
*
|
|
24
|
+
* Accessibility: decorative by default (`aria-hidden="true"`, the Lucide
|
|
25
|
+
* default). Pass `label` when the icon carries meaning on its own — it then
|
|
26
|
+
* renders `role="img"` + `aria-label`. Name the meaning ("Passing"), not the
|
|
27
|
+
* glyph ("check mark").
|
|
28
|
+
*
|
|
29
|
+
* Peer parity: Chakra UI Icon, MUI SvgIcon / Icon, Ant Design Icon,
|
|
30
|
+
* Component Gallery "Icon".
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
const iconVariants = cva("inline-block shrink-0 transition-colors", {
|
|
34
|
+
variants: {
|
|
35
|
+
size: {
|
|
36
|
+
sm: "size-3.5",
|
|
37
|
+
default: "size-4",
|
|
38
|
+
lg: "size-5",
|
|
39
|
+
xl: "size-6",
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
defaultVariants: {
|
|
43
|
+
size: "default",
|
|
44
|
+
},
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
type IconComponent = React.ComponentType<React.SVGProps<SVGSVGElement>>
|
|
48
|
+
|
|
49
|
+
interface IconProps
|
|
50
|
+
extends Omit<
|
|
51
|
+
React.ComponentPropsWithoutRef<"svg">,
|
|
52
|
+
"strokeWidth" | "color" | "children"
|
|
53
|
+
>,
|
|
54
|
+
VariantProps<typeof iconVariants> {
|
|
55
|
+
/** The icon component to render — any Lucide icon, or any component that
|
|
56
|
+
* spreads SVG props (`stroke="currentColor" fill="none"`). */
|
|
57
|
+
as: IconComponent
|
|
58
|
+
/**
|
|
59
|
+
* Semantic color role from the `icon-*` token family. Omit for
|
|
60
|
+
* `currentColor` (inherit from surrounding text). Never pass a token name
|
|
61
|
+
* or a Tailwind class here — `pnpm lint:icon-color` flags it.
|
|
62
|
+
*/
|
|
63
|
+
color?: IconRole
|
|
64
|
+
/**
|
|
65
|
+
* Accessible name. When set, the icon is announced as an image with this
|
|
66
|
+
* label; when omitted the icon is decorative and hidden from assistive
|
|
67
|
+
* tech. Describe the meaning, not the shape.
|
|
68
|
+
*/
|
|
69
|
+
label?: string
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Pattern-c motion: a parent-driven color change (group-hover, a role swap)
|
|
73
|
+
// eases on the fast token instead of snapping. The CSS vars resolve at
|
|
74
|
+
// runtime against :root in the generated styles.css.
|
|
75
|
+
const motionStyle: React.CSSProperties = {
|
|
76
|
+
transitionDuration: "var(--duration-fast)",
|
|
77
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const Icon = React.forwardRef<SVGSVGElement, IconProps>(
|
|
81
|
+
({ as: Component, size, color, label, className, style, ...props }, ref) => {
|
|
82
|
+
const labelled = typeof label === "string" && label.length > 0
|
|
83
|
+
return (
|
|
84
|
+
<Component
|
|
85
|
+
ref={ref}
|
|
86
|
+
data-slot="icon"
|
|
87
|
+
data-size={size ?? "default"}
|
|
88
|
+
data-color={color}
|
|
89
|
+
strokeWidth={1.5}
|
|
90
|
+
focusable="false"
|
|
91
|
+
role={labelled ? "img" : undefined}
|
|
92
|
+
aria-label={labelled ? label : undefined}
|
|
93
|
+
aria-hidden={labelled ? undefined : true}
|
|
94
|
+
className={cn(
|
|
95
|
+
iconVariants({ size }),
|
|
96
|
+
color ? getIconColor(color) : undefined,
|
|
97
|
+
className
|
|
98
|
+
)}
|
|
99
|
+
style={{ ...motionStyle, ...style }}
|
|
100
|
+
{...props}
|
|
101
|
+
/>
|
|
102
|
+
)
|
|
103
|
+
}
|
|
104
|
+
)
|
|
105
|
+
Icon.displayName = "Icon"
|
|
106
|
+
|
|
107
|
+
export { Icon, iconVariants }
|
|
108
|
+
export type { IconProps, IconComponent }
|
|
@@ -78,6 +78,7 @@ export * from "./editable";
|
|
|
78
78
|
export * from "./qr-code";
|
|
79
79
|
export * from "./highlight";
|
|
80
80
|
export * from "./format-number";
|
|
81
|
+
export * from "./format-relative-time";
|
|
81
82
|
export * from "./autocomplete";
|
|
82
83
|
export * from "./button-group";
|
|
83
84
|
export * from "./checkbox-group";
|
|
@@ -142,5 +143,12 @@ export * from "./date-field";
|
|
|
142
143
|
export * from "./stack";
|
|
143
144
|
export * from "./container";
|
|
144
145
|
export * from "./simple-grid";
|
|
146
|
+
export * from "./grid";
|
|
145
147
|
export * from "./close-button";
|
|
146
148
|
export * from "./toggle-tip";
|
|
149
|
+
export * from "./presence";
|
|
150
|
+
export * from "./portal";
|
|
151
|
+
export * from "./icon";
|
|
152
|
+
export * from "./progress-stack";
|
|
153
|
+
export * from "./callout";
|
|
154
|
+
export * from "./announcement-bar";
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
import { cn } from "../../lib/utils"
|
|
3
|
+
|
|
4
|
+
export interface PageHeaderProps {
|
|
5
|
+
/** Primary heading. Rendered as <h1> by default; use `asChild` on the heading slot if you need a different level. */
|
|
6
|
+
title: React.ReactNode
|
|
7
|
+
/** Supporting description rendered below the title at text-sm / muted-foreground. */
|
|
8
|
+
description?: React.ReactNode
|
|
9
|
+
/** Uppercase eyebrow kicker above the title — used for section category or breadcrumb-equivalent labels. */
|
|
10
|
+
eyebrow?: string
|
|
11
|
+
/** Right-aligned action slot: a Button, ButtonGroup, or any control. */
|
|
12
|
+
action?: React.ReactNode
|
|
13
|
+
/** Adds a bottom border that separates the header from its content region. */
|
|
14
|
+
bordered?: boolean
|
|
15
|
+
/** Pins the header to the top of its scroll container. */
|
|
16
|
+
sticky?: boolean
|
|
17
|
+
className?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function PageHeader({
|
|
21
|
+
title,
|
|
22
|
+
description,
|
|
23
|
+
eyebrow,
|
|
24
|
+
action,
|
|
25
|
+
bordered = false,
|
|
26
|
+
sticky = false,
|
|
27
|
+
className,
|
|
28
|
+
}: PageHeaderProps) {
|
|
29
|
+
return (
|
|
30
|
+
<div
|
|
31
|
+
data-slot="page-header"
|
|
32
|
+
className={cn(
|
|
33
|
+
"flex items-start justify-between gap-4",
|
|
34
|
+
bordered && "border-b border-border pb-6",
|
|
35
|
+
sticky && "sticky top-0 z-10 bg-background py-4",
|
|
36
|
+
className
|
|
37
|
+
)}
|
|
38
|
+
>
|
|
39
|
+
<div className="min-w-0 flex-1 space-y-1">
|
|
40
|
+
{eyebrow && (
|
|
41
|
+
<p className="text-xs font-medium uppercase tracking-widest text-muted-foreground">
|
|
42
|
+
{eyebrow}
|
|
43
|
+
</p>
|
|
44
|
+
)}
|
|
45
|
+
<h1 className="text-2xl font-semibold leading-tight tracking-tight text-foreground truncate">
|
|
46
|
+
{title}
|
|
47
|
+
</h1>
|
|
48
|
+
{description && (
|
|
49
|
+
<p className="text-sm text-muted-foreground leading-relaxed">
|
|
50
|
+
{description}
|
|
51
|
+
</p>
|
|
52
|
+
)}
|
|
53
|
+
</div>
|
|
54
|
+
{action && (
|
|
55
|
+
<div className="shrink-0 flex items-center gap-2">
|
|
56
|
+
{action}
|
|
57
|
+
</div>
|
|
58
|
+
)}
|
|
59
|
+
</div>
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export { PageHeader }
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { createPortal } from "react-dom"
|
|
5
|
+
|
|
6
|
+
import { cn } from "../../lib/utils"
|
|
7
|
+
import { usePortalThemeClass } from "../../hooks/use-portal-theme-class"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Portal — render children into a different DOM container (by default the
|
|
11
|
+
* `<body>` of the component's own document) while keeping them in the React
|
|
12
|
+
* tree, so context, events, and state all still flow from the parent.
|
|
13
|
+
*
|
|
14
|
+
* Peer parity: Radix Portal, Chakra UI Portal, Mantine Portal, Ark UI Portal,
|
|
15
|
+
* MUI Portal. Zero dependencies beyond react-dom's `createPortal`.
|
|
16
|
+
*
|
|
17
|
+
* Why not just call createPortal?
|
|
18
|
+
* --------------------------------
|
|
19
|
+
* A raw portal lands OUTSIDE any theme/brand class a consumer applied to a
|
|
20
|
+
* page wrapper, so theme-scoped custom properties (`--shell-chrome-bg-rgb`,
|
|
21
|
+
* `--brand-*`) go invalid at computed-value time inside it and the surface
|
|
22
|
+
* renders unthemed — the failure documented in hooks/use-portal-theme-class
|
|
23
|
+
* and CLAUDE.md → "Portal components and CSS scope". Every BADS-shipped Radix
|
|
24
|
+
* portal merges `usePortalThemeClass()` onto its content for exactly this
|
|
25
|
+
* reason; this primitive does the same for arbitrary content, so a fork's
|
|
26
|
+
* own floating bar or toast gets the scope for free.
|
|
27
|
+
*
|
|
28
|
+
* The portal root is a `<div class="contents">`: it stays in the DOM tree
|
|
29
|
+
* (so the scope class's custom properties inherit into the children) but
|
|
30
|
+
* generates no box, so it never affects the layout of what it wraps. A plain
|
|
31
|
+
* div carries no semantics, so the historical `display: contents` a11y bug
|
|
32
|
+
* (dropped semantics on buttons/lists) does not apply.
|
|
33
|
+
*
|
|
34
|
+
* Cross-window safety
|
|
35
|
+
* -------------------
|
|
36
|
+
* The default container is read from an inline hidden anchor's
|
|
37
|
+
* `ownerDocument`, never the bare `document` global — a component inside an
|
|
38
|
+
* iframe (the BADS preview tabs) or another window portals into ITS body,
|
|
39
|
+
* not the top-level one (spec §2). Pass `container` to target any element.
|
|
40
|
+
*
|
|
41
|
+
* SSR: the anchor renders on the server; the portal mounts after hydration
|
|
42
|
+
* (same mount gate Radix uses).
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
const useIsoLayoutEffect =
|
|
46
|
+
typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect
|
|
47
|
+
|
|
48
|
+
interface PortalProps extends React.ComponentPropsWithoutRef<"div"> {
|
|
49
|
+
/**
|
|
50
|
+
* Element to render into. Defaults to the `<body>` of the component's own
|
|
51
|
+
* document. `null` is treated as "not ready" and renders nothing until a
|
|
52
|
+
* container resolves — handy with a ref that populates after mount.
|
|
53
|
+
*/
|
|
54
|
+
container?: Element | null
|
|
55
|
+
/** When true, children render in place (no portal). Useful for tests,
|
|
56
|
+
* print stylesheets, or when an ancestor already portals. */
|
|
57
|
+
disabled?: boolean
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function Portal({
|
|
61
|
+
container,
|
|
62
|
+
disabled = false,
|
|
63
|
+
className,
|
|
64
|
+
children,
|
|
65
|
+
...props
|
|
66
|
+
}: PortalProps) {
|
|
67
|
+
const anchorRef = React.useRef<HTMLSpanElement | null>(null)
|
|
68
|
+
const [ownBody, setOwnBody] = React.useState<Element | null>(null)
|
|
69
|
+
const portalThemeClass = usePortalThemeClass()
|
|
70
|
+
|
|
71
|
+
useIsoLayoutEffect(() => {
|
|
72
|
+
setOwnBody(anchorRef.current?.ownerDocument.body ?? null)
|
|
73
|
+
}, [])
|
|
74
|
+
|
|
75
|
+
if (disabled) {
|
|
76
|
+
return (
|
|
77
|
+
<div
|
|
78
|
+
data-slot="portal"
|
|
79
|
+
data-disabled="true"
|
|
80
|
+
className={cn("contents", className)}
|
|
81
|
+
{...props}
|
|
82
|
+
>
|
|
83
|
+
{children}
|
|
84
|
+
</div>
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// `container === undefined` means "use my own body"; `null` means "wait".
|
|
89
|
+
const target = container === undefined ? ownBody : container
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
<>
|
|
93
|
+
<span ref={anchorRef} data-slot="portal-anchor" hidden aria-hidden="true" />
|
|
94
|
+
{target
|
|
95
|
+
? createPortal(
|
|
96
|
+
<div
|
|
97
|
+
data-slot="portal"
|
|
98
|
+
className={cn("contents", portalThemeClass, className)}
|
|
99
|
+
{...props}
|
|
100
|
+
>
|
|
101
|
+
{children}
|
|
102
|
+
</div>,
|
|
103
|
+
target
|
|
104
|
+
)
|
|
105
|
+
: null}
|
|
106
|
+
</>
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export { Portal }
|
|
111
|
+
export type { PortalProps }
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { Slot } from "@radix-ui/react-slot"
|
|
5
|
+
|
|
6
|
+
import { cn } from "../../lib/utils"
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Presence — mount/unmount wrapper with a token-driven enter *and exit*
|
|
10
|
+
* animation. `present={true}` mounts the child and plays the enter keyframe;
|
|
11
|
+
* `present={false}` plays the exit keyframe and only THEN unmounts (or hides)
|
|
12
|
+
* the child. Conditional rendering (`{open && <X />}`) can never animate out
|
|
13
|
+
* because the node is gone before the browser paints a frame — that exit
|
|
14
|
+
* frame is the whole reason this primitive exists.
|
|
15
|
+
*
|
|
16
|
+
* Peer parity: Ark UI Presence, Chakra UI v3 Presence, Mantine Transition,
|
|
17
|
+
* MUI Fade / Grow / Slide / Zoom, Radix's internal Presence.
|
|
18
|
+
*
|
|
19
|
+
* How the exit wait works
|
|
20
|
+
* -----------------------
|
|
21
|
+
* On `present=false` the wrapper flips to `data-state="closed"`, which the
|
|
22
|
+
* design-system CSS (keyed off `[data-slot="presence"][data-transition=…]
|
|
23
|
+
* [data-state=…]` in build-tokens.cjs — spec §4 pattern b) maps to the
|
|
24
|
+
* matching `presence-<transition>-out` keyframe. The effect then reads
|
|
25
|
+
* `element.getAnimations()`: zero running animations → unmount immediately
|
|
26
|
+
* (a consumer overrode `animation: none`, or the browser lacks the API);
|
|
27
|
+
* otherwise wait for every `finished` promise. A monotonically increasing
|
|
28
|
+
* token guards the wait, so a `present=true` that arrives mid-exit cancels
|
|
29
|
+
* the pending unmount and the enter keyframe simply replays.
|
|
30
|
+
*
|
|
31
|
+
* `animation-fill-mode: both` on the CSS side is load-bearing — without it
|
|
32
|
+
* the exit keyframe snaps back to its start frame (fully visible) for the
|
|
33
|
+
* one frame between `animationend` and React's commit.
|
|
34
|
+
*
|
|
35
|
+
* Reduced motion: the base layer forces `animation-duration: 0.01ms`, the
|
|
36
|
+
* `finished` promise still resolves, and the child unmounts effectively
|
|
37
|
+
* immediately. No per-component guard needed.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
const PRESENCE_TRANSITIONS = [
|
|
41
|
+
"fade",
|
|
42
|
+
"scale",
|
|
43
|
+
"slide-up",
|
|
44
|
+
"slide-down",
|
|
45
|
+
"slide-left",
|
|
46
|
+
"slide-right",
|
|
47
|
+
] as const
|
|
48
|
+
|
|
49
|
+
type PresenceTransition = (typeof PRESENCE_TRANSITIONS)[number]
|
|
50
|
+
|
|
51
|
+
type Phase = "mounted" | "exiting" | "unmounted"
|
|
52
|
+
|
|
53
|
+
interface PresenceProps extends React.ComponentPropsWithoutRef<"div"> {
|
|
54
|
+
/** Whether the child should be shown. Flipping to false plays the exit
|
|
55
|
+
* animation before the child leaves the DOM. */
|
|
56
|
+
present: boolean
|
|
57
|
+
/**
|
|
58
|
+
* Which enter/exit keyframe pair to play. Written to `data-transition`;
|
|
59
|
+
* the keyframes live in the generated styles.css. Default: fade.
|
|
60
|
+
*/
|
|
61
|
+
transition?: PresenceTransition
|
|
62
|
+
/**
|
|
63
|
+
* When true (default) the child is removed from the DOM after the exit
|
|
64
|
+
* animation. When false it stays mounted with the `hidden` attribute —
|
|
65
|
+
* useful for expensive subtrees (iframes, charts) that should keep their
|
|
66
|
+
* state between shows.
|
|
67
|
+
*/
|
|
68
|
+
unmountOnExit?: boolean
|
|
69
|
+
/**
|
|
70
|
+
* When true, renders as the single child element instead of a wrapper div —
|
|
71
|
+
* `data-slot`, `data-state`, `data-transition`, and `hidden` are merged
|
|
72
|
+
* onto the child (via Radix Slot). The child must forward its ref and must
|
|
73
|
+
* not set its own `animation` shorthand, which would override the keyframe.
|
|
74
|
+
*/
|
|
75
|
+
asChild?: boolean
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const useIsoLayoutEffect =
|
|
79
|
+
typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect
|
|
80
|
+
|
|
81
|
+
const Presence = React.forwardRef<HTMLDivElement, PresenceProps>(
|
|
82
|
+
(
|
|
83
|
+
{
|
|
84
|
+
present,
|
|
85
|
+
transition = "fade",
|
|
86
|
+
unmountOnExit = true,
|
|
87
|
+
asChild = false,
|
|
88
|
+
className,
|
|
89
|
+
children,
|
|
90
|
+
...props
|
|
91
|
+
},
|
|
92
|
+
forwardedRef
|
|
93
|
+
) => {
|
|
94
|
+
const nodeRef = React.useRef<HTMLDivElement | null>(null)
|
|
95
|
+
const [phase, setPhase] = React.useState<Phase>(
|
|
96
|
+
present ? "mounted" : "unmounted"
|
|
97
|
+
)
|
|
98
|
+
// Bumped on every present flip; a pending exit wait only commits its
|
|
99
|
+
// unmount if the token it captured is still current.
|
|
100
|
+
const tokenRef = React.useRef(0)
|
|
101
|
+
|
|
102
|
+
const setRefs = React.useCallback(
|
|
103
|
+
(node: HTMLDivElement | null) => {
|
|
104
|
+
nodeRef.current = node
|
|
105
|
+
if (typeof forwardedRef === "function") forwardedRef(node)
|
|
106
|
+
else if (forwardedRef) forwardedRef.current = node
|
|
107
|
+
},
|
|
108
|
+
[forwardedRef]
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
// Layout effect so the data-state flip lands before the browser paints
|
|
112
|
+
// the frame in which the exit keyframe must already be running.
|
|
113
|
+
useIsoLayoutEffect(() => {
|
|
114
|
+
tokenRef.current += 1
|
|
115
|
+
if (present) {
|
|
116
|
+
setPhase("mounted")
|
|
117
|
+
} else {
|
|
118
|
+
setPhase((p) => (p === "unmounted" ? p : "exiting"))
|
|
119
|
+
}
|
|
120
|
+
}, [present])
|
|
121
|
+
|
|
122
|
+
React.useEffect(() => {
|
|
123
|
+
if (phase !== "exiting") return
|
|
124
|
+
const token = tokenRef.current
|
|
125
|
+
const finish = () => {
|
|
126
|
+
if (tokenRef.current === token) setPhase("unmounted")
|
|
127
|
+
}
|
|
128
|
+
const el = nodeRef.current
|
|
129
|
+
if (!el || typeof el.getAnimations !== "function") {
|
|
130
|
+
finish()
|
|
131
|
+
return
|
|
132
|
+
}
|
|
133
|
+
const animations = el.getAnimations()
|
|
134
|
+
if (animations.length === 0) {
|
|
135
|
+
finish()
|
|
136
|
+
return
|
|
137
|
+
}
|
|
138
|
+
let cancelled = false
|
|
139
|
+
Promise.allSettled(animations.map((a) => a.finished)).then(() => {
|
|
140
|
+
if (!cancelled) finish()
|
|
141
|
+
})
|
|
142
|
+
return () => {
|
|
143
|
+
cancelled = true
|
|
144
|
+
}
|
|
145
|
+
}, [phase])
|
|
146
|
+
|
|
147
|
+
const isOpen = phase === "mounted"
|
|
148
|
+
const isGone = phase === "unmounted"
|
|
149
|
+
|
|
150
|
+
if (isGone && unmountOnExit) return null
|
|
151
|
+
|
|
152
|
+
const Comp = asChild ? Slot : "div"
|
|
153
|
+
|
|
154
|
+
return (
|
|
155
|
+
<Comp
|
|
156
|
+
ref={setRefs}
|
|
157
|
+
data-slot="presence"
|
|
158
|
+
data-state={isOpen ? "open" : "closed"}
|
|
159
|
+
data-transition={transition}
|
|
160
|
+
hidden={isGone || undefined}
|
|
161
|
+
className={cn(className)}
|
|
162
|
+
{...props}
|
|
163
|
+
>
|
|
164
|
+
{children}
|
|
165
|
+
</Comp>
|
|
166
|
+
)
|
|
167
|
+
}
|
|
168
|
+
)
|
|
169
|
+
Presence.displayName = "Presence"
|
|
170
|
+
|
|
171
|
+
export { Presence, PRESENCE_TRANSITIONS }
|
|
172
|
+
export type { PresenceProps, PresenceTransition }
|