@trackunit/react-components 7.0.10 → 8.0.1
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/index.cjs.js +297 -128
- package/index.esm.js +297 -128
- package/migrations/assets/react-router-components/package.json +6 -6
- package/migrations/utils/jsx-utils.js +7 -1
- package/migrations/v7-0-0/collapse-native-details.js +593 -0
- package/migrations.json +5 -0
- package/package.json +6 -6
- package/src/components/Collapse/Collapse.d.ts +76 -8
- package/src/components/Collapse/Collapse.variants.d.ts +121 -87
- package/src/components/Collapse/utils/getInteractiveDescendant/getInteractiveDescendant.d.ts +5 -0
- package/src/components/Collapse/utils/isNativelyActivatable/isNativelyActivatable.d.ts +5 -0
- package/src/components/Tag/Tag.d.ts +9 -1
- package/src/components/buttons/Button/Button.d.ts +1 -1
- package/src/components/buttons/shared/ButtonProps.d.ts +11 -0
package/index.cjs.js
CHANGED
|
@@ -1901,13 +1901,19 @@ const cvaIconButton = cssClassVarianceUtilities.cva({
|
|
|
1901
1901
|
* @param {ButtonProps} props - The props for the Button component
|
|
1902
1902
|
* @returns {ReactElement} Button component
|
|
1903
1903
|
*/
|
|
1904
|
-
const Button = ({ onClick, children, loading = false, disabled = false, className, fullWidth = false, prefix, suffix, variant = "primary", type = "button", size = "medium", square = false, "data-testid": dataTestId, title, ariaLabel, ariaLabelledBy, ariaDescribedBy, ariaHidden = false, role, tabIndex, asChild = false, ref, ...rest }) => {
|
|
1904
|
+
const Button = ({ onClick, children, loading = false, disabled = false, className, fullWidth = false, prefix, suffix, variant = "primary", type = "button", size = "medium", square = false, "data-testid": dataTestId, title, ariaLabel, ariaLabelledBy, ariaDescribedBy, ariaHidden = false, role, tabIndex, asChild = false, keepFocus = false, onMouseDown, ref, ...rest }) => {
|
|
1905
1905
|
const Comp = asChild ? reactSlot.Slot : "button";
|
|
1906
1906
|
const sharedCompProps = {
|
|
1907
1907
|
ref,
|
|
1908
1908
|
title,
|
|
1909
1909
|
role,
|
|
1910
1910
|
onClick,
|
|
1911
|
+
onMouseDown: keepFocus
|
|
1912
|
+
? (event) => {
|
|
1913
|
+
onMouseDown?.(event);
|
|
1914
|
+
event.preventDefault();
|
|
1915
|
+
}
|
|
1916
|
+
: onMouseDown,
|
|
1911
1917
|
tabIndex,
|
|
1912
1918
|
className: cvaButton({ disabled, size, square, variant, loading, fullWidth, className }),
|
|
1913
1919
|
"aria-describedby": ariaDescribedBy,
|
|
@@ -2377,7 +2383,7 @@ const TAG_TEXT_MIN_WIDTH_PX = sharedUtils.parseTailwindArbitraryValue(TAG_TEXT_M
|
|
|
2377
2383
|
* @param {TagProps} props - The props for the Tag component.
|
|
2378
2384
|
* @returns {ReactElement} The rendered Tag component.
|
|
2379
2385
|
*/
|
|
2380
|
-
const Tag = ({ className, "data-testid": dataTestId, children, size = "medium", onClickClose, color = "info", disabled = false, ref, icon, onMouseEnter, style, removeTagLabel, removeButtonInTabOrder = true, }) => {
|
|
2386
|
+
const Tag = ({ className, "data-testid": dataTestId, children, size = "medium", onClickClose, color = "info", disabled = false, ref, icon, onMouseEnter, style, removeTagLabel, removeButtonInTabOrder = true, keepFocus = false, }) => {
|
|
2381
2387
|
const textRef = react.useRef(null);
|
|
2382
2388
|
const [shouldTruncate, setShouldTruncate] = react.useState(false);
|
|
2383
2389
|
const textId = react.useId();
|
|
@@ -2408,7 +2414,7 @@ const Tag = ({ className, "data-testid": dataTestId, children, size = "medium",
|
|
|
2408
2414
|
color,
|
|
2409
2415
|
layout,
|
|
2410
2416
|
border: color === "white" ? "default" : "none",
|
|
2411
|
-
}), "data-testid": dataTestId, onMouseEnter: onMouseEnter, ref: ref, style: style, children: [icon !== null && icon !== undefined && size === "medium" ? (jsxRuntime.jsx("div", { className: cvaTagIconContainer(), "data-slot": "icon", children: icon })) : null, jsxRuntime.jsx("span", { className: cvaTagText({ truncate: shouldTruncate }), id: textId, ref: textRef, children: children }), Boolean(onClickClose) && size === "medium" && !disabled ? (jsxRuntime.jsx("div", { className: cvaTagIconContainer(), "data-slot": "dismiss", onClick: onClickClose, children: jsxRuntime.jsx(Tooltip, { disabled: disabled, label: removeTagLabel, placement: "bottom", children: jsxRuntime.jsx("button", { "aria-label": removeTagLabel, "aria-labelledby": `${removeButtonId} ${textId}`, className: cvaTagDismissButton(), id: removeButtonId, tabIndex: removeButtonInTabOrder ? undefined : -1, type: "button", children: jsxRuntime.jsx(Icon, { ariaHidden: true, className: cvaTagIcon(), "data-testid": `${dataTestId}Icon`, name: "XCircle", size: "small", style: { WebkitTransition: "-webkit-transform 0.150s" }, type: "solid" }) }) }) })) : null] }));
|
|
2417
|
+
}), "data-testid": dataTestId, onMouseEnter: onMouseEnter, ref: ref, style: style, children: [icon !== null && icon !== undefined && size === "medium" ? (jsxRuntime.jsx("div", { className: cvaTagIconContainer(), "data-slot": "icon", children: icon })) : null, jsxRuntime.jsx("span", { className: cvaTagText({ truncate: shouldTruncate }), id: textId, ref: textRef, children: children }), Boolean(onClickClose) && size === "medium" && !disabled ? (jsxRuntime.jsx("div", { className: cvaTagIconContainer(), "data-slot": "dismiss", onClick: onClickClose, children: jsxRuntime.jsx(Tooltip, { disabled: disabled, label: removeTagLabel, placement: "bottom", children: jsxRuntime.jsx("button", { "aria-label": removeTagLabel, "aria-labelledby": `${removeButtonId} ${textId}`, className: cvaTagDismissButton(), id: removeButtonId, onMouseDown: keepFocus ? event => event.preventDefault() : undefined, tabIndex: removeButtonInTabOrder ? undefined : -1, type: "button", children: jsxRuntime.jsx(Icon, { ariaHidden: true, className: cvaTagIcon(), "data-testid": `${dataTestId}Icon`, name: "XCircle", size: "small", style: { WebkitTransition: "-webkit-transform 0.150s" }, type: "solid" }) }) }) })) : null] }));
|
|
2412
2418
|
};
|
|
2413
2419
|
|
|
2414
2420
|
// Mirrors the slug logic in @trackunit/storybook-docs-components SectionName so anchors line up
|
|
@@ -4206,112 +4212,82 @@ const cvaClickable = cssClassVarianceUtilities.cva({
|
|
|
4206
4212
|
});
|
|
4207
4213
|
|
|
4208
4214
|
/**
|
|
4209
|
-
*
|
|
4210
|
-
*
|
|
4211
|
-
*
|
|
4212
|
-
*
|
|
4213
|
-
* @returns {UseMeasureResult<HTMLElement>} An object containing `geometry`, `ref` callback, and `element`.
|
|
4214
|
-
* @example
|
|
4215
|
-
* ```tsx
|
|
4216
|
-
* const { geometry, ref } = useMeasure();
|
|
4217
|
-
* return <div ref={ref}>Width: {geometry.width}</div>;
|
|
4218
|
-
* ```
|
|
4215
|
+
* The container doesn't clip its content: the header's focus ring is the browser's own, drawn outside the
|
|
4216
|
+
* header like a button's, and clipping would cut it off. The header and the content round their own corners
|
|
4217
|
+
* to the container's inner radius instead: `rounded-md` minus the 1px border, taken from the theme's
|
|
4218
|
+
* `--radius-md` so it follows the token.
|
|
4219
4219
|
*/
|
|
4220
|
-
const useMeasure = ({ skip = false, onChange, } = {}) => {
|
|
4221
|
-
const [element, setElement] = react.useState(null);
|
|
4222
|
-
const [geometry, setGeometry] = react.useState(undefined);
|
|
4223
|
-
const observerRef = react.useRef(null);
|
|
4224
|
-
const onChangeRef = react.useRef(onChange);
|
|
4225
|
-
const isInitialRender = react.useRef(true);
|
|
4226
|
-
// Callback ref to track the element
|
|
4227
|
-
const ref = react.useCallback((node) => {
|
|
4228
|
-
setElement(node);
|
|
4229
|
-
}, []);
|
|
4230
|
-
const disconnectObserver = () => {
|
|
4231
|
-
if (observerRef.current !== null) {
|
|
4232
|
-
observerRef.current.disconnect();
|
|
4233
|
-
observerRef.current = null;
|
|
4234
|
-
}
|
|
4235
|
-
};
|
|
4236
|
-
react.useEffect(() => {
|
|
4237
|
-
onChangeRef.current = onChange;
|
|
4238
|
-
}, [onChange]);
|
|
4239
|
-
react.useEffect(() => {
|
|
4240
|
-
if (skip || element === null) {
|
|
4241
|
-
return;
|
|
4242
|
-
}
|
|
4243
|
-
const updateGeometry = (rect) => {
|
|
4244
|
-
const newGeometry = {
|
|
4245
|
-
width: rect.width,
|
|
4246
|
-
height: rect.height,
|
|
4247
|
-
top: rect.top,
|
|
4248
|
-
bottom: rect.bottom,
|
|
4249
|
-
left: rect.left,
|
|
4250
|
-
right: rect.right,
|
|
4251
|
-
x: rect.x,
|
|
4252
|
-
y: rect.y,
|
|
4253
|
-
};
|
|
4254
|
-
setGeometry(prevGeometry => {
|
|
4255
|
-
if (esToolkit.isEqual(prevGeometry, newGeometry)) {
|
|
4256
|
-
return prevGeometry;
|
|
4257
|
-
}
|
|
4258
|
-
if (isInitialRender.current === false) {
|
|
4259
|
-
onChangeRef.current?.(newGeometry);
|
|
4260
|
-
}
|
|
4261
|
-
return newGeometry;
|
|
4262
|
-
});
|
|
4263
|
-
isInitialRender.current = false;
|
|
4264
|
-
};
|
|
4265
|
-
const handleResize = (entries) => {
|
|
4266
|
-
for (const entry of entries) {
|
|
4267
|
-
const rect = entry.target.getBoundingClientRect();
|
|
4268
|
-
updateGeometry(rect);
|
|
4269
|
-
}
|
|
4270
|
-
};
|
|
4271
|
-
disconnectObserver();
|
|
4272
|
-
const observer = new ResizeObserver(handleResize);
|
|
4273
|
-
observer.observe(element);
|
|
4274
|
-
observerRef.current = observer;
|
|
4275
|
-
const initialRect = element.getBoundingClientRect();
|
|
4276
|
-
updateGeometry(initialRect);
|
|
4277
|
-
return disconnectObserver;
|
|
4278
|
-
}, [element, skip]);
|
|
4279
|
-
return react.useMemo(() => ({ geometry, ref, element }), [geometry, ref, element]);
|
|
4280
|
-
};
|
|
4281
|
-
|
|
4282
4220
|
const cvaCollapse = cssClassVarianceUtilities.cva({
|
|
4283
|
-
base: ["flex", "flex-col"
|
|
4221
|
+
base: ["flex", "flex-col"],
|
|
4284
4222
|
variants: {
|
|
4285
4223
|
variant: {
|
|
4286
4224
|
primary: ["border", "border-solid", "border-neutral-300", "rounded-md", "bg-white"],
|
|
4287
|
-
secondary:
|
|
4225
|
+
secondary: [],
|
|
4288
4226
|
},
|
|
4289
4227
|
},
|
|
4290
4228
|
});
|
|
4229
|
+
/**
|
|
4230
|
+
* The `::details-content` box is a one-row grid whose row animates from `0fr` to `1fr`, so the content
|
|
4231
|
+
* height is never measured in JavaScript. `content-visibility` is transitioned discretely so the content
|
|
4232
|
+
* stays visible until the closing animation ends. Browsers without `::details-content` support, users
|
|
4233
|
+
* who prefer reduced motion, and `animate={false}` get an instant open and close.
|
|
4234
|
+
*/
|
|
4235
|
+
const cvaCollapseDetails = cssClassVarianceUtilities.cva({
|
|
4236
|
+
variants: {
|
|
4237
|
+
animate: {
|
|
4238
|
+
true: [
|
|
4239
|
+
"details-content:grid",
|
|
4240
|
+
"details-content:grid-rows-[0fr]",
|
|
4241
|
+
"open:details-content:grid-rows-[1fr]",
|
|
4242
|
+
"motion-safe:details-content:transition-[grid-template-rows,content-visibility]",
|
|
4243
|
+
"motion-safe:details-content:transition-discrete",
|
|
4244
|
+
"motion-safe:details-content:duration-200",
|
|
4245
|
+
"motion-safe:details-content:ease-in-out",
|
|
4246
|
+
],
|
|
4247
|
+
false: [],
|
|
4248
|
+
},
|
|
4249
|
+
},
|
|
4250
|
+
defaultVariants: {
|
|
4251
|
+
animate: true,
|
|
4252
|
+
},
|
|
4253
|
+
});
|
|
4254
|
+
/**
|
|
4255
|
+
* A closed primary header rounds its bottom corners. The `open` attribute is removed as soon as closing
|
|
4256
|
+
* starts, so when the closing is animated the rounding waits for the animation to end: until then the
|
|
4257
|
+
* content is still visible below a header that should stay square.
|
|
4258
|
+
*/
|
|
4291
4259
|
const cvaCollapseHeader = cssClassVarianceUtilities.cva({
|
|
4292
|
-
base: [
|
|
4260
|
+
base: [
|
|
4261
|
+
"flex",
|
|
4262
|
+
"items-center",
|
|
4263
|
+
"cursor-pointer",
|
|
4264
|
+
"transition-all",
|
|
4265
|
+
"group/collapse",
|
|
4266
|
+
"list-none",
|
|
4267
|
+
"[&::-webkit-details-marker]:hidden",
|
|
4268
|
+
],
|
|
4293
4269
|
variants: {
|
|
4294
|
-
expanded: { true: [], false: [] },
|
|
4295
4270
|
variant: {
|
|
4296
|
-
primary: [
|
|
4271
|
+
primary: [
|
|
4272
|
+
"gap-4",
|
|
4273
|
+
"justify-between",
|
|
4274
|
+
"bg-white",
|
|
4275
|
+
"hover:bg-neutral-100",
|
|
4276
|
+
"rounded-t-[calc(var(--radius-md)-1px)]",
|
|
4277
|
+
"[details:not([open])>&]:rounded-b-[calc(var(--radius-md)-1px)]",
|
|
4278
|
+
],
|
|
4297
4279
|
secondary: ["w-fit"],
|
|
4298
4280
|
},
|
|
4299
4281
|
extraPadding: {
|
|
4300
4282
|
true: [],
|
|
4301
4283
|
false: [],
|
|
4302
4284
|
},
|
|
4285
|
+
animate: {
|
|
4286
|
+
true: [],
|
|
4287
|
+
false: [],
|
|
4288
|
+
},
|
|
4303
4289
|
},
|
|
4304
4290
|
compoundVariants: [
|
|
4305
|
-
{
|
|
4306
|
-
variant: "primary",
|
|
4307
|
-
expanded: true,
|
|
4308
|
-
class: ["border-neutral-200", "border-b", "border-solid"],
|
|
4309
|
-
},
|
|
4310
|
-
{
|
|
4311
|
-
variant: "primary",
|
|
4312
|
-
expanded: false,
|
|
4313
|
-
class: [],
|
|
4314
|
-
},
|
|
4315
4291
|
{
|
|
4316
4292
|
variant: "primary",
|
|
4317
4293
|
extraPadding: true,
|
|
@@ -4332,7 +4308,17 @@ const cvaCollapseHeader = cssClassVarianceUtilities.cva({
|
|
|
4332
4308
|
extraPadding: false,
|
|
4333
4309
|
class: [],
|
|
4334
4310
|
},
|
|
4311
|
+
{
|
|
4312
|
+
variant: "primary",
|
|
4313
|
+
animate: true,
|
|
4314
|
+
class: [
|
|
4315
|
+
"motion-safe:supports-[selector(::details-content)]:[details:not([open])>&]:[transition:background-color_150ms,border-radius_0s_200ms]",
|
|
4316
|
+
],
|
|
4317
|
+
},
|
|
4335
4318
|
],
|
|
4319
|
+
defaultVariants: {
|
|
4320
|
+
animate: true,
|
|
4321
|
+
},
|
|
4336
4322
|
});
|
|
4337
4323
|
const cvaCollapseLabelContainer = cssClassVarianceUtilities.cva({
|
|
4338
4324
|
base: ["flex", "items-center"],
|
|
@@ -4352,13 +4338,45 @@ const cvaCollapseLabel = cssClassVarianceUtilities.cva({
|
|
|
4352
4338
|
},
|
|
4353
4339
|
},
|
|
4354
4340
|
});
|
|
4355
|
-
|
|
4356
|
-
|
|
4341
|
+
/**
|
|
4342
|
+
* The single grid item inside `::details-content`. It must not carry padding, because padding would
|
|
4343
|
+
* stay visible while the row is collapsed to `0fr`. `min-w-0` keeps wide content (a code block, a table)
|
|
4344
|
+
* from stretching the grid column past the Collapse: a grid item is otherwise at least as wide as its content.
|
|
4345
|
+
*/
|
|
4346
|
+
const cvaCollapseContent = cssClassVarianceUtilities.cva({
|
|
4347
|
+
base: ["min-h-0", "min-w-0", "overflow-clip"],
|
|
4357
4348
|
variants: {
|
|
4358
4349
|
variant: {
|
|
4359
|
-
primary: [],
|
|
4350
|
+
primary: ["rounded-b-[calc(var(--radius-md)-1px)]"],
|
|
4360
4351
|
secondary: [],
|
|
4361
4352
|
},
|
|
4353
|
+
},
|
|
4354
|
+
});
|
|
4355
|
+
/**
|
|
4356
|
+
* Hidden while it has no children, so its padding doesn't show as an empty box. A `lazy` Collapse renders
|
|
4357
|
+
* its children a moment after the section opens, when the browser's `toggle` event arrives.
|
|
4358
|
+
*
|
|
4359
|
+
* The line between the header and the content is this element's top border, not the header's bottom border.
|
|
4360
|
+
* It is clipped away with the content, and it fades out over the closing animation so it doesn't end in a
|
|
4361
|
+
* hard cut when the content reaches zero height.
|
|
4362
|
+
*/
|
|
4363
|
+
const cvaCollapsible = cssClassVarianceUtilities.cva({
|
|
4364
|
+
base: ["block", "relative", "empty:hidden"],
|
|
4365
|
+
variants: {
|
|
4366
|
+
variant: {
|
|
4367
|
+
primary: [
|
|
4368
|
+
"border-t",
|
|
4369
|
+
"border-solid",
|
|
4370
|
+
"border-neutral-200",
|
|
4371
|
+
"transition-colors",
|
|
4372
|
+
"duration-200",
|
|
4373
|
+
"ease-in-out",
|
|
4374
|
+
"motion-reduce:transition-none",
|
|
4375
|
+
"[details:not([open])>*>&]:border-transparent",
|
|
4376
|
+
],
|
|
4377
|
+
// Replaces the gap between header and content that the outer flex container used to provide.
|
|
4378
|
+
secondary: ["mt-3"],
|
|
4379
|
+
},
|
|
4362
4380
|
extraPadding: {
|
|
4363
4381
|
true: [],
|
|
4364
4382
|
false: [],
|
|
@@ -4387,34 +4405,49 @@ const cvaCollapsible = cssClassVarianceUtilities.cva({
|
|
|
4387
4405
|
},
|
|
4388
4406
|
],
|
|
4389
4407
|
});
|
|
4390
|
-
|
|
4391
|
-
|
|
4392
|
-
|
|
4393
|
-
|
|
4394
|
-
true: "block",
|
|
4395
|
-
false: "hidden",
|
|
4396
|
-
},
|
|
4397
|
-
},
|
|
4398
|
-
defaultVariants: {
|
|
4399
|
-
expanded: false,
|
|
4400
|
-
},
|
|
4401
|
-
});
|
|
4408
|
+
/**
|
|
4409
|
+
* Rotates when this Collapse's own `<details>` is open, with the same timing as the height animation.
|
|
4410
|
+
* The child combinator keeps a nested Collapse's chevron from rotating when only its parent is open.
|
|
4411
|
+
*/
|
|
4402
4412
|
const cvaChevronIcon = cssClassVarianceUtilities.cva({
|
|
4403
|
-
base: [
|
|
4404
|
-
|
|
4405
|
-
|
|
4406
|
-
|
|
4407
|
-
|
|
4408
|
-
|
|
4409
|
-
|
|
4410
|
-
defaultVariants: {
|
|
4411
|
-
expanded: false,
|
|
4412
|
-
},
|
|
4413
|
+
base: [
|
|
4414
|
+
"transition-transform",
|
|
4415
|
+
"duration-200",
|
|
4416
|
+
"ease-in-out",
|
|
4417
|
+
"motion-reduce:transition-none",
|
|
4418
|
+
"[details[open]>summary_&]:rotate-180",
|
|
4419
|
+
],
|
|
4413
4420
|
});
|
|
4414
4421
|
|
|
4422
|
+
const INTERACTIVE_SELECTOR = "a,button,input,select,textarea,label,[role=button]";
|
|
4423
|
+
/**
|
|
4424
|
+
* The interactive element inside the summary that a click came from, such as a button in `headerAddon`.
|
|
4425
|
+
*/
|
|
4426
|
+
const getInteractiveDescendant = (event) => {
|
|
4427
|
+
const { target, currentTarget } = event;
|
|
4428
|
+
if (!(target instanceof Element)) {
|
|
4429
|
+
return null;
|
|
4430
|
+
}
|
|
4431
|
+
const interactive = target.closest(INTERACTIVE_SELECTOR);
|
|
4432
|
+
return interactive !== null && interactive !== currentTarget && currentTarget.contains(interactive)
|
|
4433
|
+
? interactive
|
|
4434
|
+
: null;
|
|
4435
|
+
};
|
|
4436
|
+
|
|
4437
|
+
const NATIVELY_ACTIVATABLE_SELECTOR = "a[href],button,input,label";
|
|
4438
|
+
/**
|
|
4439
|
+
* Whether an interactive element's own activation behaviour takes the click, so a `<summary>` around it doesn't
|
|
4440
|
+
* toggle natively. Calling preventDefault on such an element would cancel its action (e.g. a checkbox change).
|
|
4441
|
+
*/
|
|
4442
|
+
const isNativelyActivatable = (element) => element.matches(NATIVELY_ACTIVATABLE_SELECTOR);
|
|
4443
|
+
|
|
4415
4444
|
/**
|
|
4416
4445
|
* The Collapse component is a container for additional information that adds context to various flows. It is used for displaying non-essential information that can be hidden away.
|
|
4417
4446
|
*
|
|
4447
|
+
* It is built on the native `<details>` and `<summary>` elements, so keyboard support, screen reader state and
|
|
4448
|
+
* find-in-page come from the browser. Opening and closing animate in CSS; in Firefox closing doesn't animate, and
|
|
4449
|
+
* nothing animates when the user prefers reduced motion.
|
|
4450
|
+
*
|
|
4418
4451
|
* ### When to use
|
|
4419
4452
|
* - To keep content off the screen to shorten pages and reduce scrolling, while it's still only a tap away. Remember that the users might choose not to interact with the component and won't see the information within.
|
|
4420
4453
|
* - To display additional details that are not essential or necessary in the main flow/page.
|
|
@@ -4455,23 +4488,85 @@ const cvaChevronIcon = cssClassVarianceUtilities.cva({
|
|
|
4455
4488
|
* </Collapse>
|
|
4456
4489
|
* );
|
|
4457
4490
|
* ```
|
|
4491
|
+
* @example Accordion group where opening one section closes the others
|
|
4492
|
+
* ```tsx
|
|
4493
|
+
* import { Collapse } from "@trackunit/react-components";
|
|
4494
|
+
*
|
|
4495
|
+
* const Faq = () => (
|
|
4496
|
+
* <>
|
|
4497
|
+
* <Collapse id="faq-billing" label="Billing" name="faq">Billing answers</Collapse>
|
|
4498
|
+
* <Collapse id="faq-access" label="Access" name="faq">Access answers</Collapse>
|
|
4499
|
+
* </>
|
|
4500
|
+
* );
|
|
4501
|
+
* ```
|
|
4502
|
+
* @example Controlled collapse
|
|
4503
|
+
* ```tsx
|
|
4504
|
+
* import { Collapse } from "@trackunit/react-components";
|
|
4505
|
+
* import { useState } from "react";
|
|
4506
|
+
*
|
|
4507
|
+
* const ControlledSection = () => {
|
|
4508
|
+
* const [expanded, setExpanded] = useState(false);
|
|
4509
|
+
* return (
|
|
4510
|
+
* <Collapse expanded={expanded} id="controlled" label="Details" onToggle={(_e, next) => setExpanded(next)}>
|
|
4511
|
+
* Details content
|
|
4512
|
+
* </Collapse>
|
|
4513
|
+
* );
|
|
4514
|
+
* };
|
|
4515
|
+
* ```
|
|
4458
4516
|
* @param {CollapseProps} props - The props for the Collapse component
|
|
4459
4517
|
* @returns {ReactElement} Collapse component
|
|
4460
4518
|
*/
|
|
4461
|
-
const Collapse = ({ id, variant = "primary", initialExpanded = false, onToggle, label, children, className, headerClassName, headerAddon, "data-testid": dataTestId, animate = true, extraPadding = true, style, ref, }) => {
|
|
4462
|
-
const
|
|
4463
|
-
const
|
|
4464
|
-
const
|
|
4465
|
-
|
|
4466
|
-
|
|
4519
|
+
const Collapse = ({ id, variant = "primary", initialExpanded = false, onToggle, label, children, className, headerClassName, headerAddon, "data-testid": dataTestId, animate = true, lazy = false, extraPadding = true, style, ref, name, expanded = undefined, }) => {
|
|
4520
|
+
const labelId = react.useId();
|
|
4521
|
+
const isControlled = expanded !== undefined;
|
|
4522
|
+
const [uncontrolledOpen, setUncontrolledOpen] = react.useState(initialExpanded);
|
|
4523
|
+
const isOpen = isControlled ? expanded : uncontrolledOpen;
|
|
4524
|
+
const handleToggle = react.useCallback((event) => {
|
|
4525
|
+
// The native toggle event doesn't bubble, but React dispatches it to ancestor handlers as well.
|
|
4526
|
+
// Toggles from a nested Collapse or a popover in the content aren't this section's.
|
|
4527
|
+
if (event.target !== event.currentTarget) {
|
|
4528
|
+
return;
|
|
4467
4529
|
}
|
|
4468
|
-
|
|
4469
|
-
|
|
4470
|
-
|
|
4471
|
-
|
|
4472
|
-
|
|
4473
|
-
|
|
4474
|
-
|
|
4530
|
+
// Read the live attribute: jsdom fires a plain Event, and queued toggle events can be merged.
|
|
4531
|
+
const nextOpen = event.currentTarget.open;
|
|
4532
|
+
if (nextOpen === isOpen) {
|
|
4533
|
+
// The DOM matches what React rendered: the toggle came from mounting open or from a prop update, or
|
|
4534
|
+
// the section was toggled twice before the browser sent the event and is back where it started.
|
|
4535
|
+
return;
|
|
4536
|
+
}
|
|
4537
|
+
if (!isControlled) {
|
|
4538
|
+
setUncontrolledOpen(nextOpen);
|
|
4539
|
+
}
|
|
4540
|
+
onToggle?.(event, nextOpen);
|
|
4541
|
+
}, [isControlled, isOpen, onToggle]);
|
|
4542
|
+
// Runs in the capture phase, so an addon calling stopPropagation can't skip it.
|
|
4543
|
+
const handleSummaryClickCapture = react.useCallback((event) => {
|
|
4544
|
+
const interactive = getInteractiveDescendant(event);
|
|
4545
|
+
if (interactive !== null) {
|
|
4546
|
+
// Interactive descendants (e.g. a headerAddon button) keep their own click and don't toggle.
|
|
4547
|
+
// Elements without native activation behaviour would otherwise let the summary toggle.
|
|
4548
|
+
if (!isNativelyActivatable(interactive)) {
|
|
4549
|
+
event.preventDefault();
|
|
4550
|
+
}
|
|
4551
|
+
return;
|
|
4552
|
+
}
|
|
4553
|
+
if (!isControlled) {
|
|
4554
|
+
// The browser toggles and handleToggle reports it.
|
|
4555
|
+
return;
|
|
4556
|
+
}
|
|
4557
|
+
const parent = event.currentTarget.parentElement;
|
|
4558
|
+
const details = parent instanceof HTMLDetailsElement ? parent : null;
|
|
4559
|
+
event.preventDefault();
|
|
4560
|
+
// Toggle from what the user sees. The browser can have moved the section since `expanded` last changed
|
|
4561
|
+
// (find-in-page opening it, a `name` group closing it) without the parent following.
|
|
4562
|
+
const requestedOpen = details !== null ? !details.open : !isOpen;
|
|
4563
|
+
onToggle?.(event, requestedOpen);
|
|
4564
|
+
if (requestedOpen === isOpen && details !== null) {
|
|
4565
|
+
// The parent already holds the requested state, so no render will bring the section back in line.
|
|
4566
|
+
details.open = isOpen;
|
|
4567
|
+
}
|
|
4568
|
+
}, [isControlled, isOpen, onToggle]);
|
|
4569
|
+
return (jsxRuntime.jsx("div", { className: cvaCollapse({ variant, className }), "data-testid": dataTestId, ref: ref, style: style, children: jsxRuntime.jsxs("details", { className: cvaCollapseDetails({ animate }), "data-testid": dataTestId ? `${dataTestId}-details` : undefined, name: name, onToggle: handleToggle, open: isOpen, children: [jsxRuntime.jsx("summary", { className: cvaCollapseHeader({ variant, extraPadding, animate, className: headerClassName }), "data-testid": dataTestId ? `${dataTestId}-header` : undefined, onClickCapture: handleSummaryClickCapture, children: jsxRuntime.jsxs("div", { className: cvaCollapseLabelContainer({ variant }), children: [jsxRuntime.jsx(Text, { className: cvaCollapseLabel({ variant }), id: labelId, size: variant === "secondary" ? "small" : "medium", type: "span", weight: "bold", children: label }), jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [headerAddon !== null && headerAddon !== undefined && variant !== "secondary" ? headerAddon : null, jsxRuntime.jsx(Icon, { ariaHidden: true, className: cvaChevronIcon(), name: "ChevronDown", size: variant === "secondary" ? "small" : "medium" })] })] }) }), jsxRuntime.jsx("div", { className: cvaCollapseContent({ variant }), "data-testid": dataTestId ? `${dataTestId}-content` : undefined, id: id, children: jsxRuntime.jsx("div", { className: cvaCollapsible({ variant, extraPadding }), children: !lazy || isOpen ? children : null }) })] }) }));
|
|
4475
4570
|
};
|
|
4476
4571
|
|
|
4477
4572
|
/**
|
|
@@ -9421,6 +9516,80 @@ const PopoverTitle = ({ children, action, divider = false, className, "data-test
|
|
|
9421
9516
|
return (jsxRuntime.jsxs("div", { className: cvaPopoverTitleContainer({ divider, className }), "data-testid": dataTestId, ref: ref, style: style, children: [jsxRuntime.jsx(Text, { className: cvaPopoverTitleText(), size: "small", subtle: true, type: "div", uppercase: true, weight: "bold", children: children }), action] }));
|
|
9422
9517
|
};
|
|
9423
9518
|
|
|
9519
|
+
/**
|
|
9520
|
+
* Custom hook to measure the geometry of an element using a callback ref.
|
|
9521
|
+
* Measures the size and position of the element relative to the viewport.
|
|
9522
|
+
*
|
|
9523
|
+
* @template TElement extends HTMLElement
|
|
9524
|
+
* @returns {UseMeasureResult<HTMLElement>} An object containing `geometry`, `ref` callback, and `element`.
|
|
9525
|
+
* @example
|
|
9526
|
+
* ```tsx
|
|
9527
|
+
* const { geometry, ref } = useMeasure();
|
|
9528
|
+
* return <div ref={ref}>Width: {geometry.width}</div>;
|
|
9529
|
+
* ```
|
|
9530
|
+
*/
|
|
9531
|
+
const useMeasure = ({ skip = false, onChange, } = {}) => {
|
|
9532
|
+
const [element, setElement] = react.useState(null);
|
|
9533
|
+
const [geometry, setGeometry] = react.useState(undefined);
|
|
9534
|
+
const observerRef = react.useRef(null);
|
|
9535
|
+
const onChangeRef = react.useRef(onChange);
|
|
9536
|
+
const isInitialRender = react.useRef(true);
|
|
9537
|
+
// Callback ref to track the element
|
|
9538
|
+
const ref = react.useCallback((node) => {
|
|
9539
|
+
setElement(node);
|
|
9540
|
+
}, []);
|
|
9541
|
+
const disconnectObserver = () => {
|
|
9542
|
+
if (observerRef.current !== null) {
|
|
9543
|
+
observerRef.current.disconnect();
|
|
9544
|
+
observerRef.current = null;
|
|
9545
|
+
}
|
|
9546
|
+
};
|
|
9547
|
+
react.useEffect(() => {
|
|
9548
|
+
onChangeRef.current = onChange;
|
|
9549
|
+
}, [onChange]);
|
|
9550
|
+
react.useEffect(() => {
|
|
9551
|
+
if (skip || element === null) {
|
|
9552
|
+
return;
|
|
9553
|
+
}
|
|
9554
|
+
const updateGeometry = (rect) => {
|
|
9555
|
+
const newGeometry = {
|
|
9556
|
+
width: rect.width,
|
|
9557
|
+
height: rect.height,
|
|
9558
|
+
top: rect.top,
|
|
9559
|
+
bottom: rect.bottom,
|
|
9560
|
+
left: rect.left,
|
|
9561
|
+
right: rect.right,
|
|
9562
|
+
x: rect.x,
|
|
9563
|
+
y: rect.y,
|
|
9564
|
+
};
|
|
9565
|
+
setGeometry(prevGeometry => {
|
|
9566
|
+
if (esToolkit.isEqual(prevGeometry, newGeometry)) {
|
|
9567
|
+
return prevGeometry;
|
|
9568
|
+
}
|
|
9569
|
+
if (isInitialRender.current === false) {
|
|
9570
|
+
onChangeRef.current?.(newGeometry);
|
|
9571
|
+
}
|
|
9572
|
+
return newGeometry;
|
|
9573
|
+
});
|
|
9574
|
+
isInitialRender.current = false;
|
|
9575
|
+
};
|
|
9576
|
+
const handleResize = (entries) => {
|
|
9577
|
+
for (const entry of entries) {
|
|
9578
|
+
const rect = entry.target.getBoundingClientRect();
|
|
9579
|
+
updateGeometry(rect);
|
|
9580
|
+
}
|
|
9581
|
+
};
|
|
9582
|
+
disconnectObserver();
|
|
9583
|
+
const observer = new ResizeObserver(handleResize);
|
|
9584
|
+
observer.observe(element);
|
|
9585
|
+
observerRef.current = observer;
|
|
9586
|
+
const initialRect = element.getBoundingClientRect();
|
|
9587
|
+
updateGeometry(initialRect);
|
|
9588
|
+
return disconnectObserver;
|
|
9589
|
+
}, [element, skip]);
|
|
9590
|
+
return react.useMemo(() => ({ geometry, ref, element }), [geometry, ref, element]);
|
|
9591
|
+
};
|
|
9592
|
+
|
|
9424
9593
|
const cvaPreferenceCard = cssClassVarianceUtilities.cva({
|
|
9425
9594
|
base: [
|
|
9426
9595
|
"rounded-lg",
|