@homebound/beam 2.210.0 → 2.210.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -15,7 +15,8 @@ function Accordion(props) {
|
|
|
15
15
|
const [expanded, setExpanded] = (0, react_1.useState)(defaultExpanded && !disabled);
|
|
16
16
|
const { isFocusVisible, focusProps } = (0, react_aria_1.useFocusRing)();
|
|
17
17
|
const contentRef = (0, react_1.useRef)(null);
|
|
18
|
-
|
|
18
|
+
// On initial render, if the accordion is expanded, then set `height` to auto to avoid unnecessary animation on render.
|
|
19
|
+
const [contentHeight, setContentHeight] = (0, react_1.useState)(expanded ? "auto" : "0");
|
|
19
20
|
(0, react_1.useEffect)(() => {
|
|
20
21
|
setExpanded(defaultExpanded && !disabled);
|
|
21
22
|
}, [defaultExpanded, disabled]);
|
|
@@ -23,8 +24,18 @@ function Accordion(props) {
|
|
|
23
24
|
// When the `expanded` value changes - If true, it means the Accordion's content has been rendered, Otherwise, it's been hidden
|
|
24
25
|
// Then when the content is displayed, the calculate its height so we can give this value to the container to animate height smoothly.
|
|
25
26
|
// When content is removed, simply set the height back to 0
|
|
26
|
-
setContentHeight(expanded && contentRef.current ? contentRef.current.scrollHeight : 0);
|
|
27
|
+
setContentHeight(expanded && contentRef.current ? `${contentRef.current.scrollHeight}px` : "0");
|
|
27
28
|
}, [expanded]);
|
|
29
|
+
// Using a resizing observer to check if the content of the accordion changes (i.e. lazy loaded image, auto-sizing textarea, etc..),
|
|
30
|
+
// If it does change, then we need to update the container's height accordingly. Only update the height if the accordion is expanded.
|
|
31
|
+
// Note - This may result in two `setContentHeight` calls when the accordion opens: (1) via the above `useEffect` and (2) in `onResize`
|
|
32
|
+
// Both `setContentHeight` calls _should_ set the same value, so no unnecessary re-renders would be triggered, making this a harmless additional set call.
|
|
33
|
+
const onResize = (0, react_1.useCallback)(() => {
|
|
34
|
+
if (contentRef.current && expanded) {
|
|
35
|
+
setContentHeight(`${contentRef.current.scrollHeight}px`);
|
|
36
|
+
}
|
|
37
|
+
}, [expanded, setContentHeight]);
|
|
38
|
+
(0, utils_1.useResizeObserver)({ ref: contentRef, onResize });
|
|
28
39
|
return ((0, jsx_runtime_1.jsxs)("div", Object.assign({}, testIds.container, { css: {
|
|
29
40
|
...Css_1.Css.bGray300.if(topBorder).bt.if(bottomBorder).bb.$,
|
|
30
41
|
...(size ? Css_1.Css.wPx(accordionSizes[size]).$ : {}),
|
|
@@ -39,7 +50,7 @@ function Accordion(props) {
|
|
|
39
50
|
} }, { children: [(0, jsx_runtime_1.jsx)("span", { children: title }, void 0), (0, jsx_runtime_1.jsx)("span", Object.assign({ css: {
|
|
40
51
|
transition: "transform 250ms linear",
|
|
41
52
|
transform: expanded ? "rotate(180deg)" : "rotate(0deg)",
|
|
42
|
-
} }, { children: (0, jsx_runtime_1.jsx)(Icon_1.Icon, { icon: "chevronDown" }, void 0) }), void 0)] }), void 0), (0, jsx_runtime_1.jsx)("div", Object.assign({}, testIds.details, { id: id, "aria-hidden": !expanded, css: Css_1.Css.overflowHidden.
|
|
53
|
+
} }, { children: (0, jsx_runtime_1.jsx)(Icon_1.Icon, { icon: "chevronDown" }, void 0) }), void 0)] }), void 0), (0, jsx_runtime_1.jsx)("div", Object.assign({}, testIds.details, { id: id, "aria-hidden": !expanded, css: Css_1.Css.overflowHidden.h(contentHeight).add("transition", "height 250ms ease-in-out").$ }, { children: expanded && ((0, jsx_runtime_1.jsx)("div", Object.assign({ css: Css_1.Css.px2.pb2.pt1.$, ref: contentRef }, testIds.content, { children: children }), void 0)) }), void 0)] }), void 0));
|
|
43
54
|
}
|
|
44
55
|
exports.Accordion = Accordion;
|
|
45
56
|
const accordionSizes = {
|
|
@@ -8,7 +8,7 @@ const __1 = require("..");
|
|
|
8
8
|
const Accordion_1 = require("./Accordion");
|
|
9
9
|
function AccordionList(props) {
|
|
10
10
|
const { accordions, size, allowMultipleExpanded = true } = props;
|
|
11
|
-
const [expandedIndex, setExpandedIndex] = (0, react_2.useState)();
|
|
11
|
+
const [expandedIndex, setExpandedIndex] = (0, react_2.useState)(accordions.findIndex((a) => a.defaultExpanded));
|
|
12
12
|
const tid = (0, __1.useTestIds)(props, "accordionList");
|
|
13
13
|
return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: accordions.map((accordionProps, index, arr) => ((0, react_1.createElement)(Accordion_1.Accordion, { ...accordionProps, ...tid, key: index, size: size, bottomBorder: index === arr.length - 1, defaultExpanded: !allowMultipleExpanded && expandedIndex === index, index: index, setExpandedIndex: setExpandedIndex }))) }, void 0));
|
|
14
14
|
}
|