colibri-ui 0.8.88 → 0.8.89

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.
@@ -10,6 +10,10 @@
10
10
  transition: block-size 0.2s ease-in-out;
11
11
  }
12
12
 
13
+ .inner {
14
+ display: flow-root;
15
+ }
16
+
13
17
  .button {
14
18
  align-items: center;
15
19
  background: none;
@@ -18,15 +18,29 @@ const Accordion = ({
18
18
  boldTitle = false,
19
19
  children,
20
20
  className,
21
- tabIndex
21
+ tabIndex,
22
+ defaultOpen = false
22
23
  }) => {
23
- const [isOpen, setIsOpen] = React.useState(false);
24
+ const [isOpen, setIsOpen] = React.useState(defaultOpen);
24
25
  const contentRef = React.useRef(null);
26
+ const innerRef = React.useRef(null);
25
27
  const toggleAccordion = () => setIsOpen(!isOpen);
26
- React.useEffect(() => {
27
- if (contentRef.current) {
28
- contentRef.current.style.blockSize = isOpen ? `${contentRef.current.scrollHeight}px` : "0";
28
+ React.useLayoutEffect(() => {
29
+ const content = contentRef.current;
30
+ const inner = innerRef.current;
31
+ if (!content || !inner) return;
32
+ if (!isOpen) {
33
+ content.style.blockSize = "0";
34
+ return;
29
35
  }
36
+ const updateBlockSize = () => {
37
+ content.style.blockSize = `${inner.scrollHeight}px`;
38
+ };
39
+ updateBlockSize();
40
+ if (typeof ResizeObserver === "undefined") return;
41
+ const observer = new ResizeObserver(updateBlockSize);
42
+ observer.observe(inner);
43
+ return () => observer.disconnect();
30
44
  }, [isOpen]);
31
45
  return /*#__PURE__*/React__default.default.createElement("div", {
32
46
  className: clsx__default.default(styles__default.default.root, className)
@@ -53,7 +67,10 @@ const Accordion = ({
53
67
  }))), /*#__PURE__*/React__default.default.createElement("div", {
54
68
  ref: contentRef,
55
69
  className: styles__default.default.content
56
- }, children));
70
+ }, /*#__PURE__*/React__default.default.createElement("div", {
71
+ ref: innerRef,
72
+ className: styles__default.default.inner
73
+ }, children)));
57
74
  };
58
75
 
59
76
  exports.Accordion = Accordion;
@@ -4,4 +4,6 @@ export type AccordionProps = PropsWithChildren<{
4
4
  className?: string;
5
5
  boldTitle?: boolean;
6
6
  tabIndex?: number;
7
+ /** Initial expanded state. Defaults to `false`. */
8
+ defaultOpen?: boolean;
7
9
  }>;
@@ -10,6 +10,10 @@
10
10
  transition: block-size 0.2s ease-in-out;
11
11
  }
12
12
 
13
+ .inner {
14
+ display: flow-root;
15
+ }
16
+
13
17
  .button {
14
18
  align-items: center;
15
19
  background: none;
@@ -1,4 +1,4 @@
1
- import React, { useState, useRef, useEffect } from 'react';
1
+ import React, { useState, useRef, useLayoutEffect } from 'react';
2
2
  import clsx from 'clsx';
3
3
  import 'tslib';
4
4
  import { Chevron } from '../Icons/Chevron.js';
@@ -10,15 +10,29 @@ const Accordion = ({
10
10
  boldTitle = false,
11
11
  children,
12
12
  className,
13
- tabIndex
13
+ tabIndex,
14
+ defaultOpen = false
14
15
  }) => {
15
- const [isOpen, setIsOpen] = useState(false);
16
+ const [isOpen, setIsOpen] = useState(defaultOpen);
16
17
  const contentRef = useRef(null);
18
+ const innerRef = useRef(null);
17
19
  const toggleAccordion = () => setIsOpen(!isOpen);
18
- useEffect(() => {
19
- if (contentRef.current) {
20
- contentRef.current.style.blockSize = isOpen ? `${contentRef.current.scrollHeight}px` : "0";
20
+ useLayoutEffect(() => {
21
+ const content = contentRef.current;
22
+ const inner = innerRef.current;
23
+ if (!content || !inner) return;
24
+ if (!isOpen) {
25
+ content.style.blockSize = "0";
26
+ return;
21
27
  }
28
+ const updateBlockSize = () => {
29
+ content.style.blockSize = `${inner.scrollHeight}px`;
30
+ };
31
+ updateBlockSize();
32
+ if (typeof ResizeObserver === "undefined") return;
33
+ const observer = new ResizeObserver(updateBlockSize);
34
+ observer.observe(inner);
35
+ return () => observer.disconnect();
22
36
  }, [isOpen]);
23
37
  return /*#__PURE__*/React.createElement("div", {
24
38
  className: clsx(styles.root, className)
@@ -45,7 +59,10 @@ const Accordion = ({
45
59
  }))), /*#__PURE__*/React.createElement("div", {
46
60
  ref: contentRef,
47
61
  className: styles.content
48
- }, children));
62
+ }, /*#__PURE__*/React.createElement("div", {
63
+ ref: innerRef,
64
+ className: styles.inner
65
+ }, children)));
49
66
  };
50
67
 
51
68
  export { Accordion };
@@ -4,4 +4,6 @@ export type AccordionProps = PropsWithChildren<{
4
4
  className?: string;
5
5
  boldTitle?: boolean;
6
6
  tabIndex?: number;
7
+ /** Initial expanded state. Defaults to `false`. */
8
+ defaultOpen?: boolean;
7
9
  }>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "colibri-ui",
3
- "version": "0.8.88",
3
+ "version": "0.8.89",
4
4
  "description": "React UI components library",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",