@snack-uikit/accordion 0.7.2-preview-d74371e8.0 → 0.7.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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,18 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## 0.7.2 (2024-05-23)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * **FF-0000:** optimize render children non expanded collapse block ([2b67700](https://github.com/cloud-ru-tech/snack-uikit/commit/2b677004e3d04cce8ab972011962a7fa2b0cda52))
12
+ * **FF-0000:** stopPropagation on actions click in CollapseBlock.Header ([157241c](https://github.com/cloud-ru-tech/snack-uikit/commit/157241c718646ae14763f733df914eb8ad4cf9f2))
13
+
14
+
15
+
16
+
17
+
6
18
  ## 0.7.1 (2024-05-08)
7
19
 
8
20
  ### Only dependencies have been changed
@@ -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
  }
@@ -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
  };
@@ -4,6 +4,7 @@ import { ButtonFunction } from '@snack-uikit/button';
4
4
  import { ChevronDownSVG } from '@snack-uikit/icons';
5
5
  import { TEST_IDS } from '../../testIds';
6
6
  import styles from './styles.module.css';
7
+ import { stopPropagationClick } from './utils';
7
8
  export function CollapseBlockHeaderContainer({ children, toggleExpanded, expanded, actions, }) {
8
9
  const functionLayoutRef = useRef(null);
9
10
  const [width, setWidth] = useState('0px');
@@ -15,7 +16,8 @@ export function CollapseBlockHeaderContainer({ children, toggleExpanded, expande
15
16
  return '0px';
16
17
  });
17
18
  }, [functionLayoutRef]);
19
+ const handleToggleExpanded = () => toggleExpanded(!expanded);
18
20
  return (
19
21
  // 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 })] })] }));
22
+ _jsxs("div", { className: styles.header, "data-test-id": TEST_IDS.header, onClick: handleToggleExpanded, children: [_jsx("div", { className: styles.text, style: { '--width': width }, children: children }), _jsxs("div", { className: styles.functionLayout, ref: functionLayoutRef, onClick: stopPropagationClick, children: [actions, _jsx(ButtonFunction, { className: styles.button, "data-expanded": expanded || undefined, icon: _jsx(ChevronDownSVG, {}), onClick: handleToggleExpanded, size: 'xs', "data-test-id": TEST_IDS.chevron })] })] }));
21
23
  }
@@ -0,0 +1,2 @@
1
+ import { MouseEvent } from 'react';
2
+ export declare const stopPropagationClick: (e: MouseEvent<HTMLDivElement>) => void;
@@ -0,0 +1,3 @@
1
+ export const stopPropagationClick = (e) => {
2
+ e.stopPropagation();
3
+ };
@@ -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.2-preview-d74371e8.0",
7
+ "version": "0.7.2",
8
8
  "sideEffects": [
9
9
  "*.css",
10
10
  "*.woff",
@@ -36,10 +36,14 @@
36
36
  "@snack-uikit/icons": "0.21.0",
37
37
  "@snack-uikit/toggles": "0.9.9",
38
38
  "@snack-uikit/tooltip": "0.13.3",
39
- "@snack-uikit/truncate-string": "0.4.15-preview-d74371e8.0",
39
+ "@snack-uikit/truncate-string": "0.4.15",
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": "126a04bf797d30c47577b4b47a6447aa43fddb4d"
45
+ "devDependencies": {
46
+ "@types/lodash.debounce": "4.0.8"
47
+ },
48
+ "gitHead": "4ed0075faec0375fee125764be942849a20100a4"
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}
@@ -5,10 +5,11 @@ import { ChevronDownSVG } from '@snack-uikit/icons';
5
5
 
6
6
  import { TEST_IDS } from '../../testIds';
7
7
  import styles from './styles.module.scss';
8
+ import { stopPropagationClick } from './utils';
8
9
 
9
10
  export type CollapseBlockHeaderContainerProps = {
10
11
  children: ReactNode;
11
- toggleExpanded(): void;
12
+ toggleExpanded(expanded: boolean): void;
12
13
  expanded: boolean;
13
14
  actions?: ReactNode;
14
15
  };
@@ -33,20 +34,23 @@ export function CollapseBlockHeaderContainer({
33
34
  });
34
35
  }, [functionLayoutRef]);
35
36
 
37
+ const handleToggleExpanded = () => toggleExpanded(!expanded);
38
+
36
39
  return (
37
40
  // eslint-disable-next-line jsx-a11y/no-static-element-interactions
38
- <div className={styles.header} data-test-id={TEST_IDS.header} onClick={toggleExpanded}>
41
+ <div className={styles.header} data-test-id={TEST_IDS.header} onClick={handleToggleExpanded}>
39
42
  <div className={styles.text} style={{ '--width': width }}>
40
43
  {children}
41
44
  </div>
42
-
43
- <div className={styles.functionLayout} ref={functionLayoutRef}>
45
+ {/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */}
46
+ <div className={styles.functionLayout} ref={functionLayoutRef} onClick={stopPropagationClick}>
44
47
  {actions}
45
48
 
46
49
  <ButtonFunction
47
50
  className={styles.button}
48
51
  data-expanded={expanded || undefined}
49
52
  icon={<ChevronDownSVG />}
53
+ onClick={handleToggleExpanded}
50
54
  size='xs'
51
55
  data-test-id={TEST_IDS.chevron}
52
56
  />
@@ -0,0 +1,5 @@
1
+ import { MouseEvent } from 'react';
2
+
3
+ export const stopPropagationClick = (e: MouseEvent<HTMLDivElement>) => {
4
+ e.stopPropagation();
5
+ };
@@ -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 (
@@ -28,7 +30,6 @@ export function CollapseBlockPrivate({
28
30
  // eslint-disable-next-line jsx-a11y/aria-role
29
31
  role='accordion'
30
32
  data-shape={shape}
31
- // data-outline={outline || undefined}
32
33
  aria-expanded={expanded}
33
34
  {...extractSupportProps(rest)}
34
35
  >
@@ -36,7 +37,7 @@ export function CollapseBlockPrivate({
36
37
 
37
38
  <div className={styles.contentWrapper} aria-hidden={!expanded} data-test-id={TEST_IDS.content}>
38
39
  <div className={styles.content} data-content>
39
- {children}
40
+ {(expanded || expandedDebounced) && children}
40
41
  </div>
41
42
  </div>
42
43
  </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
+ }