@snack-uikit/accordion 0.7.1 → 0.7.2-preview-529bd544.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.
@@ -11,12 +11,12 @@ var __rest = (this && this.__rest) || function (s, e) {
11
11
  };
12
12
  import { jsx as _jsx } from "react/jsx-runtime";
13
13
  import cn from 'classnames';
14
- import { useToggleGroup } from '@snack-uikit/toggles';
15
14
  import { extractSupportProps } from '@snack-uikit/utils';
16
15
  import { CollapseBlockHeaderContainer, CollapseBlockPrivate } from '../../helperComponents';
16
+ import { useExpanded } from '../../hooks';
17
17
  import styles from './styles.module.css';
18
18
  export function CollapseBlockPrimary(_a) {
19
19
  var { id, header, children, actions, className, outline, shape = 'round' } = _a, rest = __rest(_a, ["id", "header", "children", "actions", "className", "outline", "shape"]);
20
- const { isChecked: isExpanded, handleClick: handleExpandedChange } = useToggleGroup({ value: id });
21
- return (_jsx(CollapseBlockPrivate, Object.assign({ className: cn(styles.containerBase, { [styles.container]: !outline, [styles.containerOutline]: outline }, className), shape: shape, header: _jsx(CollapseBlockHeaderContainer, { expanded: isExpanded, toggleExpanded: handleExpandedChange, actions: actions, children: header }), expanded: isExpanded }, extractSupportProps(rest), { children: children })));
20
+ const { isExpanded, isExpandedDebounced, handleToggleExpanded } = useExpanded(id);
21
+ return (_jsx(CollapseBlockPrivate, Object.assign({ className: cn(styles.containerBase, { [styles.container]: !outline, [styles.containerOutline]: outline }, className), shape: shape, header: _jsx(CollapseBlockHeaderContainer, { expanded: isExpanded, toggleExpanded: handleToggleExpanded, actions: actions, children: header }), expanded: isExpanded, expandedDebounced: isExpandedDebounced }, extractSupportProps(rest), { children: children })));
22
22
  }
@@ -11,12 +11,12 @@ var __rest = (this && this.__rest) || function (s, e) {
11
11
  };
12
12
  import { jsx as _jsx } from "react/jsx-runtime";
13
13
  import cn from 'classnames';
14
- import { useToggleGroup } from '@snack-uikit/toggles';
15
14
  import { extractSupportProps } from '@snack-uikit/utils';
16
15
  import { CollapseBlockHeaderContainer, CollapseBlockPrivate } from '../../helperComponents';
16
+ import { useExpanded } from '../../hooks';
17
17
  import styles from './styles.module.css';
18
18
  export function CollapseBlockSecondary(_a) {
19
19
  var { header, children, id, actions, className } = _a, rest = __rest(_a, ["header", "children", "id", "actions", "className"]);
20
- const { isChecked: isExpanded, handleClick: handleExpandedChange } = useToggleGroup({ value: id });
21
- return (_jsx(CollapseBlockPrivate, Object.assign({ className: cn(styles.container, className), header: _jsx(CollapseBlockHeaderContainer, { expanded: isExpanded, toggleExpanded: handleExpandedChange, actions: actions, children: header }), expanded: isExpanded }, extractSupportProps(rest), { children: children })));
20
+ const { isExpanded, isExpandedDebounced, handleToggleExpanded } = useExpanded(id);
21
+ return (_jsx(CollapseBlockPrivate, Object.assign({ className: cn(styles.container, className), header: _jsx(CollapseBlockHeaderContainer, { expanded: isExpanded, toggleExpanded: handleToggleExpanded, actions: actions, children: header }), expanded: isExpanded, expandedDebounced: isExpandedDebounced }, extractSupportProps(rest), { children: children })));
22
22
  }
@@ -4,7 +4,7 @@
4
4
  padding-left:var(--space-accordion-collapse-block-secondary-horizontal-padding, 16px);
5
5
  padding-right:var(--space-accordion-collapse-block-secondary-horizontal-padding, 16px);
6
6
  gap:var(--space-accordion-collapse-block-secondary-gap, 16px);
7
- border-bottom-width:var(--border-width-general-xs, 1px);
7
+ border-bottom-width:var(--border-width-accordion-collapse-block-secondary, 1px);
8
8
  box-sizing:border-box;
9
9
  max-width:100%;
10
10
  border-color:var(--sys-neutral-decor-default, #dfe2ec);
@@ -1,7 +1,7 @@
1
1
  import { ReactNode } from 'react';
2
2
  export type CollapseBlockHeaderContainerProps = {
3
3
  children: ReactNode;
4
- toggleExpanded(): void;
4
+ toggleExpanded(expanded: boolean): void;
5
5
  expanded: boolean;
6
6
  actions?: ReactNode;
7
7
  };
@@ -17,5 +17,5 @@ export function CollapseBlockHeaderContainer({ children, toggleExpanded, expande
17
17
  }, [functionLayoutRef]);
18
18
  return (
19
19
  // eslint-disable-next-line jsx-a11y/no-static-element-interactions
20
- _jsxs("div", { className: styles.header, "data-test-id": TEST_IDS.header, onClick: toggleExpanded, children: [_jsx("div", { className: styles.text, style: { '--width': width }, children: children }), _jsxs("div", { className: styles.functionLayout, ref: functionLayoutRef, children: [actions, _jsx(ButtonFunction, { className: styles.button, "data-expanded": expanded || undefined, icon: _jsx(ChevronDownSVG, {}), size: 'xs', "data-test-id": TEST_IDS.chevron })] })] }));
20
+ _jsxs("div", { className: styles.header, "data-test-id": TEST_IDS.header, onClick: () => toggleExpanded(!expanded), children: [_jsx("div", { className: styles.text, style: { '--width': width }, children: children }), _jsxs("div", { className: styles.functionLayout, ref: functionLayoutRef, children: [actions, _jsx(ButtonFunction, { className: styles.button, "data-expanded": expanded || undefined, icon: _jsx(ChevronDownSVG, {}), size: 'xs', "data-test-id": TEST_IDS.chevron })] })] }));
21
21
  }
@@ -4,7 +4,8 @@ export type CollapseBlockPrivateProps = WithSupportProps<{
4
4
  children: ReactNode;
5
5
  header: ReactNode;
6
6
  expanded: boolean;
7
+ expandedDebounced: boolean;
7
8
  className?: string;
8
9
  shape?: 'round' | 'square';
9
10
  }>;
10
- export declare function CollapseBlockPrivate({ children, expanded, className, header, shape, ...rest }: CollapseBlockPrivateProps): import("react/jsx-runtime").JSX.Element;
11
+ export declare function CollapseBlockPrivate({ children, expanded, className, header, shape, expandedDebounced, ...rest }: CollapseBlockPrivateProps): import("react/jsx-runtime").JSX.Element;
@@ -15,8 +15,8 @@ import { extractSupportProps } from '@snack-uikit/utils';
15
15
  import { TEST_IDS } from '../../testIds';
16
16
  import styles from './styles.module.css';
17
17
  export function CollapseBlockPrivate(_a) {
18
- var { children, expanded, className, header, shape } = _a, rest = __rest(_a, ["children", "expanded", "className", "header", "shape"]);
18
+ var { children, expanded, className, header, shape, expandedDebounced } = _a, rest = __rest(_a, ["children", "expanded", "className", "header", "shape", "expandedDebounced"]);
19
19
  return (_jsxs("div", Object.assign({ className: cn(styles.accordion, className),
20
20
  // eslint-disable-next-line jsx-a11y/aria-role
21
- role: 'accordion', "data-shape": shape, "aria-expanded": expanded }, extractSupportProps(rest), { children: [header, _jsx("div", { className: styles.contentWrapper, "aria-hidden": !expanded, "data-test-id": TEST_IDS.content, children: _jsx("div", { className: styles.content, "data-content": true, children: children }) })] })));
21
+ role: 'accordion', "data-shape": shape, "aria-expanded": expanded }, extractSupportProps(rest), { children: [header, _jsx("div", { className: styles.contentWrapper, "aria-hidden": !expanded, "data-test-id": TEST_IDS.content, children: _jsx("div", { className: styles.content, "data-content": true, children: (expanded || expandedDebounced) && children }) })] })));
22
22
  }
@@ -0,0 +1,5 @@
1
+ export declare function useExpanded(id: string): {
2
+ isExpanded: boolean;
3
+ isExpandedDebounced: boolean;
4
+ handleToggleExpanded: (expanded: boolean) => void;
5
+ };
package/dist/hooks.js ADDED
@@ -0,0 +1,13 @@
1
+ import debounce from 'lodash.debounce';
2
+ import { useCallback, useMemo, useState } from 'react';
3
+ import { useToggleGroup } from '@snack-uikit/toggles';
4
+ export function useExpanded(id) {
5
+ const { isChecked: isExpanded, handleClick: handleExpandedChange } = useToggleGroup({ value: id });
6
+ const [isExpandedDebounced, setIsExpandedDebounced] = useState(isExpanded);
7
+ const debouncedSetIsExpandedDebounced = useMemo(() => debounce(setIsExpandedDebounced, 300), []);
8
+ const handleToggleExpanded = useCallback((expanded) => {
9
+ handleExpandedChange();
10
+ debouncedSetIsExpandedDebounced(expanded);
11
+ }, [debouncedSetIsExpandedDebounced, handleExpandedChange]);
12
+ return { isExpanded, isExpandedDebounced, handleToggleExpanded };
13
+ }
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "access": "public"
5
5
  },
6
6
  "title": "Accordion",
7
- "version": "0.7.1",
7
+ "version": "0.7.2-preview-529bd544.0",
8
8
  "sideEffects": [
9
9
  "*.css",
10
10
  "*.woff",
@@ -39,7 +39,11 @@
39
39
  "@snack-uikit/truncate-string": "0.4.14",
40
40
  "@snack-uikit/typography": "0.6.2",
41
41
  "@snack-uikit/utils": "3.3.0",
42
- "classnames": "2.3.2"
42
+ "classnames": "2.3.2",
43
+ "lodash.debounce": "4.0.8"
43
44
  },
44
- "gitHead": "59bd2ac46a341357d543ada864a34e09745b3e42"
45
+ "devDependencies": {
46
+ "@types/lodash.debounce": "4.0.8"
47
+ },
48
+ "gitHead": "47ac11742202c7ebd27e9c001d2e9bf9783f1632"
45
49
  }
@@ -1,9 +1,9 @@
1
1
  import cn from 'classnames';
2
2
 
3
- import { useToggleGroup } from '@snack-uikit/toggles';
4
3
  import { extractSupportProps } from '@snack-uikit/utils';
5
4
 
6
5
  import { CollapseBlockHeaderContainer, CollapseBlockPrivate } from '../../helperComponents';
6
+ import { useExpanded } from '../../hooks';
7
7
  import { CollapseBlockPrimaryProps } from '../../types';
8
8
  import styles from './styles.module.scss';
9
9
 
@@ -17,7 +17,7 @@ export function CollapseBlockPrimary({
17
17
  shape = 'round',
18
18
  ...rest
19
19
  }: CollapseBlockPrimaryProps) {
20
- const { isChecked: isExpanded, handleClick: handleExpandedChange } = useToggleGroup({ value: id });
20
+ const { isExpanded, isExpandedDebounced, handleToggleExpanded } = useExpanded(id);
21
21
 
22
22
  return (
23
23
  <CollapseBlockPrivate
@@ -28,11 +28,12 @@ export function CollapseBlockPrimary({
28
28
  )}
29
29
  shape={shape}
30
30
  header={
31
- <CollapseBlockHeaderContainer expanded={isExpanded} toggleExpanded={handleExpandedChange} actions={actions}>
31
+ <CollapseBlockHeaderContainer expanded={isExpanded} toggleExpanded={handleToggleExpanded} actions={actions}>
32
32
  {header}
33
33
  </CollapseBlockHeaderContainer>
34
34
  }
35
35
  expanded={isExpanded}
36
+ expandedDebounced={isExpandedDebounced}
36
37
  {...extractSupportProps(rest)}
37
38
  >
38
39
  {children}
@@ -1,9 +1,9 @@
1
1
  import cn from 'classnames';
2
2
 
3
- import { useToggleGroup } from '@snack-uikit/toggles';
4
3
  import { extractSupportProps } from '@snack-uikit/utils';
5
4
 
6
5
  import { CollapseBlockHeaderContainer, CollapseBlockPrivate } from '../../helperComponents';
6
+ import { useExpanded } from '../../hooks';
7
7
  import { CollapseBlockSecondaryProps } from '../../types';
8
8
  import styles from './styles.module.scss';
9
9
 
@@ -15,17 +15,18 @@ export function CollapseBlockSecondary({
15
15
  className,
16
16
  ...rest
17
17
  }: CollapseBlockSecondaryProps) {
18
- const { isChecked: isExpanded, handleClick: handleExpandedChange } = useToggleGroup({ value: id });
18
+ const { isExpanded, isExpandedDebounced, handleToggleExpanded } = useExpanded(id);
19
19
 
20
20
  return (
21
21
  <CollapseBlockPrivate
22
22
  className={cn(styles.container, className)}
23
23
  header={
24
- <CollapseBlockHeaderContainer expanded={isExpanded} toggleExpanded={handleExpandedChange} actions={actions}>
24
+ <CollapseBlockHeaderContainer expanded={isExpanded} toggleExpanded={handleToggleExpanded} actions={actions}>
25
25
  {header}
26
26
  </CollapseBlockHeaderContainer>
27
27
  }
28
28
  expanded={isExpanded}
29
+ expandedDebounced={isExpandedDebounced}
29
30
  {...extractSupportProps(rest)}
30
31
  >
31
32
  {children}
@@ -8,7 +8,7 @@ import styles from './styles.module.scss';
8
8
 
9
9
  export type CollapseBlockHeaderContainerProps = {
10
10
  children: ReactNode;
11
- toggleExpanded(): void;
11
+ toggleExpanded(expanded: boolean): void;
12
12
  expanded: boolean;
13
13
  actions?: ReactNode;
14
14
  };
@@ -35,7 +35,7 @@ export function CollapseBlockHeaderContainer({
35
35
 
36
36
  return (
37
37
  // eslint-disable-next-line jsx-a11y/no-static-element-interactions
38
- <div className={styles.header} data-test-id={TEST_IDS.header} onClick={toggleExpanded}>
38
+ <div className={styles.header} data-test-id={TEST_IDS.header} onClick={() => toggleExpanded(!expanded)}>
39
39
  <div className={styles.text} style={{ '--width': width }}>
40
40
  {children}
41
41
  </div>
@@ -10,6 +10,7 @@ export type CollapseBlockPrivateProps = WithSupportProps<{
10
10
  children: ReactNode;
11
11
  header: ReactNode;
12
12
  expanded: boolean;
13
+ expandedDebounced: boolean;
13
14
  className?: string;
14
15
  shape?: 'round' | 'square';
15
16
  }>;
@@ -20,6 +21,7 @@ export function CollapseBlockPrivate({
20
21
  className,
21
22
  header,
22
23
  shape,
24
+ expandedDebounced,
23
25
  ...rest
24
26
  }: CollapseBlockPrivateProps) {
25
27
  return (
@@ -36,7 +38,7 @@ export function CollapseBlockPrivate({
36
38
 
37
39
  <div className={styles.contentWrapper} aria-hidden={!expanded} data-test-id={TEST_IDS.content}>
38
40
  <div className={styles.content} data-content>
39
- {children}
41
+ {(expanded || expandedDebounced) && children}
40
42
  </div>
41
43
  </div>
42
44
  </div>
package/src/hooks.ts ADDED
@@ -0,0 +1,21 @@
1
+ import debounce from 'lodash.debounce';
2
+ import { useCallback, useMemo, useState } from 'react';
3
+
4
+ import { useToggleGroup } from '@snack-uikit/toggles';
5
+
6
+ export function useExpanded(id: string) {
7
+ const { isChecked: isExpanded, handleClick: handleExpandedChange } = useToggleGroup({ value: id });
8
+ const [isExpandedDebounced, setIsExpandedDebounced] = useState<boolean>(isExpanded);
9
+
10
+ const debouncedSetIsExpandedDebounced = useMemo(() => debounce(setIsExpandedDebounced, 300), []);
11
+
12
+ const handleToggleExpanded = useCallback(
13
+ (expanded: boolean) => {
14
+ handleExpandedChange();
15
+ debouncedSetIsExpandedDebounced(expanded);
16
+ },
17
+ [debouncedSetIsExpandedDebounced, handleExpandedChange],
18
+ );
19
+
20
+ return { isExpanded, isExpandedDebounced, handleToggleExpanded };
21
+ }