@cloud-ru/ds-uikit-product-widget 1.0.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.
Files changed (131) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/LICENSE +201 -0
  3. package/README.md +374 -0
  4. package/dist/cjs/components/Widget/Widget.d.ts +2 -0
  5. package/dist/cjs/components/Widget/Widget.js +42 -0
  6. package/dist/cjs/components/Widget/index.d.ts +1 -0
  7. package/dist/cjs/components/Widget/index.js +17 -0
  8. package/dist/cjs/components/Widget/styles.module.css +284 -0
  9. package/dist/cjs/components/index.d.ts +1 -0
  10. package/dist/cjs/components/index.js +17 -0
  11. package/dist/cjs/constants.d.ts +25 -0
  12. package/dist/cjs/constants.js +28 -0
  13. package/dist/cjs/helperComponents/Actions/ActionView.d.ts +10 -0
  14. package/dist/cjs/helperComponents/Actions/ActionView.js +40 -0
  15. package/dist/cjs/helperComponents/Actions/Actions.d.ts +12 -0
  16. package/dist/cjs/helperComponents/Actions/Actions.js +43 -0
  17. package/dist/cjs/helperComponents/Actions/helpers.d.ts +21 -0
  18. package/dist/cjs/helperComponents/Actions/helpers.js +57 -0
  19. package/dist/cjs/helperComponents/Actions/index.d.ts +3 -0
  20. package/dist/cjs/helperComponents/Actions/index.js +21 -0
  21. package/dist/cjs/helperComponents/Actions/styles.module.css +46 -0
  22. package/dist/cjs/helperComponents/ButtonDroplist/ActionList.d.ts +9 -0
  23. package/dist/cjs/helperComponents/ButtonDroplist/ActionList.js +39 -0
  24. package/dist/cjs/helperComponents/ButtonDroplist/ButtonDroplist.d.ts +2 -0
  25. package/dist/cjs/helperComponents/ButtonDroplist/ButtonDroplist.js +34 -0
  26. package/dist/cjs/helperComponents/ButtonDroplist/index.d.ts +1 -0
  27. package/dist/cjs/helperComponents/ButtonDroplist/index.js +17 -0
  28. package/dist/cjs/helperComponents/ButtonDroplist/styles.module.css +5 -0
  29. package/dist/cjs/helperComponents/ButtonKebab/ButtonKebab.d.ts +2 -0
  30. package/dist/cjs/helperComponents/ButtonKebab/ButtonKebab.js +18 -0
  31. package/dist/cjs/helperComponents/ButtonKebab/index.d.ts +1 -0
  32. package/dist/cjs/helperComponents/ButtonKebab/index.js +17 -0
  33. package/dist/cjs/helperComponents/ButtonKebab/styles.module.css +5 -0
  34. package/dist/cjs/helperComponents/Content/Content.d.ts +11 -0
  35. package/dist/cjs/helperComponents/Content/Content.js +36 -0
  36. package/dist/cjs/helperComponents/Content/index.d.ts +1 -0
  37. package/dist/cjs/helperComponents/Content/index.js +17 -0
  38. package/dist/cjs/helperComponents/Content/styles.module.css +22 -0
  39. package/dist/cjs/helperComponents/ControlBlock/ControlBlock.d.ts +12 -0
  40. package/dist/cjs/helperComponents/ControlBlock/ControlBlock.js +22 -0
  41. package/dist/cjs/helperComponents/ControlBlock/index.d.ts +1 -0
  42. package/dist/cjs/helperComponents/ControlBlock/index.js +17 -0
  43. package/dist/cjs/helperComponents/ControlBlock/styles.module.css +12 -0
  44. package/dist/cjs/helperComponents/index.d.ts +5 -0
  45. package/dist/cjs/helperComponents/index.js +21 -0
  46. package/dist/cjs/index.d.ts +4 -0
  47. package/dist/cjs/index.js +20 -0
  48. package/dist/cjs/locale/index.d.ts +27 -0
  49. package/dist/cjs/locale/index.js +18 -0
  50. package/dist/cjs/package.json +3 -0
  51. package/dist/cjs/style.css +374 -0
  52. package/dist/cjs/types.d.ts +116 -0
  53. package/dist/cjs/types.js +2 -0
  54. package/dist/esm/components/Widget/Widget.d.ts +2 -0
  55. package/dist/esm/components/Widget/Widget.js +36 -0
  56. package/dist/esm/components/Widget/index.d.ts +1 -0
  57. package/dist/esm/components/Widget/index.js +1 -0
  58. package/dist/esm/components/Widget/styles.module.css +284 -0
  59. package/dist/esm/components/index.d.ts +1 -0
  60. package/dist/esm/components/index.js +1 -0
  61. package/dist/esm/constants.d.ts +25 -0
  62. package/dist/esm/constants.js +25 -0
  63. package/dist/esm/helperComponents/Actions/ActionView.d.ts +10 -0
  64. package/dist/esm/helperComponents/Actions/ActionView.js +37 -0
  65. package/dist/esm/helperComponents/Actions/Actions.d.ts +12 -0
  66. package/dist/esm/helperComponents/Actions/Actions.js +37 -0
  67. package/dist/esm/helperComponents/Actions/helpers.d.ts +21 -0
  68. package/dist/esm/helperComponents/Actions/helpers.js +50 -0
  69. package/dist/esm/helperComponents/Actions/index.d.ts +3 -0
  70. package/dist/esm/helperComponents/Actions/index.js +3 -0
  71. package/dist/esm/helperComponents/Actions/styles.module.css +46 -0
  72. package/dist/esm/helperComponents/ButtonDroplist/ActionList.d.ts +9 -0
  73. package/dist/esm/helperComponents/ButtonDroplist/ActionList.js +33 -0
  74. package/dist/esm/helperComponents/ButtonDroplist/ButtonDroplist.d.ts +2 -0
  75. package/dist/esm/helperComponents/ButtonDroplist/ButtonDroplist.js +28 -0
  76. package/dist/esm/helperComponents/ButtonDroplist/index.d.ts +1 -0
  77. package/dist/esm/helperComponents/ButtonDroplist/index.js +1 -0
  78. package/dist/esm/helperComponents/ButtonDroplist/styles.module.css +5 -0
  79. package/dist/esm/helperComponents/ButtonKebab/ButtonKebab.d.ts +2 -0
  80. package/dist/esm/helperComponents/ButtonKebab/ButtonKebab.js +12 -0
  81. package/dist/esm/helperComponents/ButtonKebab/index.d.ts +1 -0
  82. package/dist/esm/helperComponents/ButtonKebab/index.js +1 -0
  83. package/dist/esm/helperComponents/ButtonKebab/styles.module.css +5 -0
  84. package/dist/esm/helperComponents/Content/Content.d.ts +11 -0
  85. package/dist/esm/helperComponents/Content/Content.js +30 -0
  86. package/dist/esm/helperComponents/Content/index.d.ts +1 -0
  87. package/dist/esm/helperComponents/Content/index.js +1 -0
  88. package/dist/esm/helperComponents/Content/styles.module.css +22 -0
  89. package/dist/esm/helperComponents/ControlBlock/ControlBlock.d.ts +12 -0
  90. package/dist/esm/helperComponents/ControlBlock/ControlBlock.js +16 -0
  91. package/dist/esm/helperComponents/ControlBlock/index.d.ts +1 -0
  92. package/dist/esm/helperComponents/ControlBlock/index.js +1 -0
  93. package/dist/esm/helperComponents/ControlBlock/styles.module.css +12 -0
  94. package/dist/esm/helperComponents/index.d.ts +5 -0
  95. package/dist/esm/helperComponents/index.js +5 -0
  96. package/dist/esm/index.d.ts +4 -0
  97. package/dist/esm/index.js +4 -0
  98. package/dist/esm/locale/index.d.ts +27 -0
  99. package/dist/esm/locale/index.js +15 -0
  100. package/dist/esm/style.css +374 -0
  101. package/dist/esm/types.d.ts +116 -0
  102. package/dist/esm/types.js +1 -0
  103. package/dist/tsconfig.cjs.tsbuildinfo +1 -0
  104. package/dist/tsconfig.esm.tsbuildinfo +1 -0
  105. package/package.json +59 -0
  106. package/src/components/Widget/Widget.tsx +113 -0
  107. package/src/components/Widget/index.ts +1 -0
  108. package/src/components/Widget/styles.module.scss +93 -0
  109. package/src/components/index.ts +1 -0
  110. package/src/constants.ts +27 -0
  111. package/src/helperComponents/Actions/ActionView.tsx +56 -0
  112. package/src/helperComponents/Actions/Actions.tsx +84 -0
  113. package/src/helperComponents/Actions/helpers.ts +84 -0
  114. package/src/helperComponents/Actions/index.ts +3 -0
  115. package/src/helperComponents/Actions/styles.module.scss +54 -0
  116. package/src/helperComponents/ButtonDroplist/ButtonDroplist.tsx +50 -0
  117. package/src/helperComponents/ButtonDroplist/index.ts +1 -0
  118. package/src/helperComponents/ButtonDroplist/styles.module.scss +5 -0
  119. package/src/helperComponents/ButtonKebab/ButtonKebab.tsx +36 -0
  120. package/src/helperComponents/ButtonKebab/index.ts +1 -0
  121. package/src/helperComponents/ButtonKebab/styles.module.scss +5 -0
  122. package/src/helperComponents/Content/Content.tsx +72 -0
  123. package/src/helperComponents/Content/index.ts +1 -0
  124. package/src/helperComponents/Content/styles.module.scss +26 -0
  125. package/src/helperComponents/ControlBlock/ControlBlock.tsx +40 -0
  126. package/src/helperComponents/ControlBlock/index.ts +1 -0
  127. package/src/helperComponents/ControlBlock/styles.module.scss +16 -0
  128. package/src/helperComponents/index.ts +5 -0
  129. package/src/index.ts +4 -0
  130. package/src/locale/index.ts +19 -0
  131. package/src/types.ts +151 -0
@@ -0,0 +1,113 @@
1
+ import { isMobileLayout, useAdaptiveLayout } from '@cloud-ru/ds-adaptive';
2
+ import { Skeleton } from '@cloud-ru/ds-skeleton';
3
+ import { TitleClickable } from '@cloud-ru/ds-uikit-product-title-clickable';
4
+ import { extractSupportProps, useDynamicList } from '@cloud-ru/ds-utils';
5
+ import cn from 'classnames';
6
+ import { memo, useMemo, useRef } from 'react';
7
+
8
+ import { BUTTON_TYPE, TEST_IDS, WIDGET_STATE } from '../../constants';
9
+ import { Actions, ActionView, Content, ControlBlock, isVisibleAction } from '../../helperComponents';
10
+ import { WidgetProps } from '../../types';
11
+ import styles from './styles.module.scss';
12
+
13
+ function WidgetComponent({
14
+ header,
15
+ children,
16
+ actions = [],
17
+ wide: wideProp = false,
18
+ state = WIDGET_STATE.Default,
19
+ loadingState,
20
+ errorState,
21
+ className,
22
+ actionsChildren,
23
+ segmentControl,
24
+ ...rest
25
+ }: WidgetProps) {
26
+ const { layoutType: contextLayoutType } = useAdaptiveLayout();
27
+ const isMobile = isMobileLayout(contextLayoutType);
28
+ const wide = wideProp && !isMobile;
29
+
30
+ const containerRef = useRef<HTMLDivElement>(null);
31
+ const visibleSource = useMemo(() => actions.filter(isVisibleAction), [actions]);
32
+ const { visibleItems, hiddenItems } = useDynamicList({
33
+ parentContainerRef: containerRef,
34
+ items: visibleSource,
35
+ maxVisibleItems: 1,
36
+ });
37
+
38
+ const [visibleItemsWithoutKebab, hiddenItemsWithKebab] = useMemo(() => {
39
+ const withoutKebab = visibleItems.filter(item => item.variant !== BUTTON_TYPE.Kebab);
40
+ const withKebab = hiddenItems.concat(visibleItems.filter(item => item.variant === BUTTON_TYPE.Kebab));
41
+
42
+ return [withoutKebab, withKebab];
43
+ }, [visibleItems, hiddenItems]);
44
+
45
+ return (
46
+ <div
47
+ className={cn(styles.widget, className)}
48
+ data-state={state}
49
+ ref={containerRef}
50
+ data-wide={wide || undefined}
51
+ {...extractSupportProps(rest)}
52
+ >
53
+ <div
54
+ className={styles.widgetHeader}
55
+ data-mobile={!wide || undefined}
56
+ data-loading={state === WIDGET_STATE.Loading || undefined}
57
+ data-test-id={TEST_IDS.header}
58
+ >
59
+ <div className={styles.headerMain}>
60
+ {state === WIDGET_STATE.Loading ? (
61
+ <div className={styles.skeletonHeader}>
62
+ <Skeleton loading width={wide ? '96px' : '100%'} height='32px' borderRadius='4px' />
63
+ </div>
64
+ ) : (
65
+ <TitleClickable {...header} className={cn(styles.titleClickable, header.className)} />
66
+ )}
67
+
68
+ {!wide && (
69
+ <Actions
70
+ actions={state === WIDGET_STATE.Error ? actions : hiddenItemsWithKebab}
71
+ actionsChildren={actionsChildren}
72
+ wide={wide}
73
+ state={state}
74
+ />
75
+ )}
76
+ </div>
77
+
78
+ <ControlBlock
79
+ actions={actions}
80
+ actionsChildren={actionsChildren}
81
+ segmentControl={segmentControl}
82
+ wide={wide}
83
+ state={state}
84
+ />
85
+ </div>
86
+
87
+ <div className={styles.widgetContent} data-test-id={TEST_IDS.content}>
88
+ <Content state={state} wide={wide} loadingState={loadingState} errorState={errorState}>
89
+ {children}
90
+ </Content>
91
+ </div>
92
+ {!wide &&
93
+ state !== WIDGET_STATE.Error &&
94
+ visibleItemsWithoutKebab.map((action, index) =>
95
+ state === WIDGET_STATE.Loading ? (
96
+ <Skeleton key={index} loading height='32px' borderRadius='8px' />
97
+ ) : (
98
+ <ActionView
99
+ {...action}
100
+ key={index}
101
+ commonProps={{
102
+ className: styles.button,
103
+ size: 'm',
104
+ fullWidth: true,
105
+ }}
106
+ />
107
+ ),
108
+ )}
109
+ </div>
110
+ );
111
+ }
112
+
113
+ export const Widget = memo<WidgetProps>(WidgetComponent);
@@ -0,0 +1 @@
1
+ export * from './Widget';
@@ -0,0 +1,93 @@
1
+ @use '@cloud-ru/ds-figma-variables/build/scss/styles/styles.module' as base;
2
+ @use '@cloud-ru/ds-figma-variables/build/scss/components/card.module' as card;
3
+ @use '@cloud-ru/ds-materials' as m;
4
+
5
+ .widget {
6
+ position: relative;
7
+
8
+ display: flex;
9
+ flex-direction: column;
10
+ gap: base.$sn-primitive-dimension-12;
11
+
12
+ box-sizing: border-box;
13
+ width: 100%;
14
+ min-width: 0;
15
+ padding: base.$sn-primitive-dimension-16;
16
+ border-radius: card.$sn-card-anatomy-radius-l-container-borderRadius;
17
+
18
+ color: base.$sn-theme-color-available-version-textMain;
19
+
20
+ background: base.$sn-theme-color-neutral-background1Level;
21
+
22
+ @include m.with-material('acrylic', #{acrylic});
23
+
24
+ &[data-state='loading'][data-wide] {
25
+ gap: base.$sn-primitive-dimension-16;
26
+ }
27
+ }
28
+
29
+ .widgetHeader {
30
+ display: flex;
31
+ gap: base.$sn-primitive-dimension-16;
32
+ align-items: center;
33
+ justify-content: space-between;
34
+
35
+ min-width: 0;
36
+
37
+ &[data-mobile] {
38
+ flex-direction: column;
39
+ align-items: flex-start;
40
+ }
41
+
42
+ &[data-loading] {
43
+ gap: base.$sn-primitive-dimension-12;
44
+ }
45
+ }
46
+
47
+ .headerMain {
48
+ display: flex;
49
+ flex: 0 1 auto;
50
+ gap: base.$sn-primitive-dimension-16;
51
+ align-items: center;
52
+ justify-content: space-between;
53
+
54
+ min-width: 0;
55
+ max-width: 100%;
56
+ }
57
+
58
+ .widgetHeader[data-mobile] .headerMain {
59
+ width: 100%;
60
+ }
61
+
62
+ .titleClickable {
63
+ flex: 0 1 auto;
64
+ min-width: 0;
65
+ max-width: 100%;
66
+ }
67
+
68
+ .widgetContent {
69
+ min-height: 0;
70
+ color: base.$sn-theme-color-available-version-textMain;
71
+
72
+ @include base.composite-var(base.$sn-regular-body-s);
73
+ }
74
+
75
+ .skeletonHeader {
76
+ flex: 1;
77
+ width: 100%;
78
+ min-width: 0;
79
+ }
80
+
81
+ .button {
82
+ white-space: nowrap;
83
+
84
+ &[data-full-width]:not([data-variant='icon-only']) {
85
+ flex-shrink: 0;
86
+ width: auto;
87
+ }
88
+
89
+ &[data-variant='icon-only'] {
90
+ flex-grow: 0;
91
+ flex-shrink: 0;
92
+ }
93
+ }
@@ -0,0 +1 @@
1
+ export * from './Widget';
@@ -0,0 +1,27 @@
1
+ export const WIDGET_STATE = {
2
+ Default: 'default',
3
+ Loading: 'loading',
4
+ Error: 'error',
5
+ } as const;
6
+
7
+ export const BUTTON_TYPE = {
8
+ Filled: 'filled',
9
+ Outline: 'outline',
10
+ Tonal: 'tonal',
11
+ Function: 'function',
12
+ Simple: 'simple',
13
+ Droplist: 'droplist',
14
+ Kebab: 'kebab',
15
+ } as const;
16
+
17
+ export const TEST_IDS = {
18
+ root: 'widget',
19
+ header: 'widget-header',
20
+ content: 'widget-content',
21
+ actions: 'widget-actions',
22
+ control: 'widget-control',
23
+ kebabButton: 'widget-kebab-button',
24
+ kebabDroplist: 'widget-kebab-droplist',
25
+ dropdown: 'widget-dropdown',
26
+ errorRetry: 'widget-error-retry',
27
+ } as const;
@@ -0,0 +1,56 @@
1
+ import { Button, VIEW } from '@cloud-ru/ds-button';
2
+ import { Tooltip } from '@cloud-ru/ds-tooltip';
3
+ import { memo, ReactNode } from 'react';
4
+
5
+ import { BUTTON_TYPE } from '../../constants';
6
+ import { WidgetAction } from '../../types';
7
+ import { ButtonDroplist } from '../ButtonDroplist';
8
+ import { ButtonKebab } from '../ButtonKebab';
9
+
10
+ type ActionViewProps = WidgetAction & {
11
+ commonProps?: {
12
+ className?: string;
13
+ size?: 's' | 'm' | 'l';
14
+ fullWidth?: boolean;
15
+ };
16
+ };
17
+
18
+ function withTooltip(children: ReactNode, tooltip?: WidgetAction['tooltip']) {
19
+ return tooltip ? <Tooltip {...tooltip}>{children}</Tooltip> : children;
20
+ }
21
+
22
+ function ActionViewComponent({
23
+ variant = BUTTON_TYPE.Filled,
24
+ tooltip,
25
+ hidden = false,
26
+ commonProps,
27
+ ...rest
28
+ }: ActionViewProps) {
29
+ if (hidden) {
30
+ return null;
31
+ }
32
+
33
+ switch (variant) {
34
+ case BUTTON_TYPE.Outline:
35
+ return withTooltip(<Button {...rest} {...commonProps} view={VIEW.Outline} />, tooltip);
36
+ case BUTTON_TYPE.Tonal:
37
+ return withTooltip(<Button {...rest} {...commonProps} view={VIEW.Tonal} />, tooltip);
38
+ case BUTTON_TYPE.Function:
39
+ return withTooltip(<Button {...rest} {...commonProps} view={VIEW.Function} />, tooltip);
40
+ case BUTTON_TYPE.Simple:
41
+ return withTooltip(<Button {...rest} {...commonProps} view={VIEW.Simple} />, tooltip);
42
+ case BUTTON_TYPE.Kebab: {
43
+ const { button, list } = rest as Extract<WidgetAction, { variant: typeof BUTTON_TYPE.Kebab }>;
44
+ return withTooltip(<ButtonKebab button={{ ...button, ...commonProps }} list={list} />, tooltip);
45
+ }
46
+ case BUTTON_TYPE.Droplist: {
47
+ const { button, list } = rest as Extract<WidgetAction, { variant: typeof BUTTON_TYPE.Droplist }>;
48
+ return withTooltip(<ButtonDroplist button={{ ...button, ...commonProps }} list={list} />, tooltip);
49
+ }
50
+ case BUTTON_TYPE.Filled:
51
+ default:
52
+ return withTooltip(<Button {...rest} {...commonProps} view={VIEW.Filled} />, tooltip);
53
+ }
54
+ }
55
+
56
+ export const ActionView = memo<ActionViewProps>(ActionViewComponent);
@@ -0,0 +1,84 @@
1
+ import { Skeleton } from '@cloud-ru/ds-skeleton';
2
+ import { memo, MouseEvent, ReactNode, useMemo } from 'react';
3
+
4
+ import { TEST_IDS } from '../../constants';
5
+ import { WidgetAction, WidgetState } from '../../types';
6
+ import { ButtonKebab } from '../ButtonKebab';
7
+ import { ActionView } from './ActionView';
8
+ import { actionToListItem, buildKebabItems, getPrimaryAction, hasVisibleActions } from './helpers';
9
+ import styles from './styles.module.scss';
10
+
11
+ type ActionsProps = {
12
+ actions?: WidgetAction[];
13
+ actionsChildren?: ReactNode;
14
+ wide?: boolean;
15
+ state?: WidgetState;
16
+ fullWidthPrimaryAction?: boolean;
17
+ showOverflowActions?: boolean;
18
+ };
19
+
20
+ function ActionsSkeleton({ wide }: Pick<ActionsProps, 'wide'>) {
21
+ return (
22
+ <div className={styles.actionsWrapper} data-wide={wide || undefined}>
23
+ <div className={styles.skeletonActions}>
24
+ {wide ? (
25
+ <>
26
+ <Skeleton loading width='96px' height='32px' borderRadius='8px' />
27
+ <Skeleton loading width='32px' height='32px' borderRadius='8px' />
28
+ </>
29
+ ) : (
30
+ <>
31
+ <div className={styles.skeletonActionFull}>
32
+ <Skeleton loading width='100%' height='32px' borderRadius='8px' />
33
+ </div>
34
+ <Skeleton loading width='32px' height='32px' borderRadius='8px' />
35
+ </>
36
+ )}
37
+ </div>
38
+ </div>
39
+ );
40
+ }
41
+
42
+ function ActionsComponent({ actions = [], actionsChildren, wide, state }: ActionsProps) {
43
+ const { action: primaryAction, index: primaryActionIndex } = useMemo(() => getPrimaryAction(actions), [actions]);
44
+
45
+ const kebabItems = useMemo(() => {
46
+ const items = buildKebabItems(actions, primaryActionIndex);
47
+ if (!wide && primaryAction) {
48
+ items.unshift(actionToListItem(primaryAction));
49
+ }
50
+ return items.map(item =>
51
+ 'type' in item
52
+ ? item
53
+ : {
54
+ ...item,
55
+ onClick: (event: MouseEvent<HTMLElement>) => item.onClick?.(event),
56
+ },
57
+ );
58
+ }, [primaryAction, wide, actions, primaryActionIndex]);
59
+
60
+ if (state === 'loading') {
61
+ return <ActionsSkeleton wide={wide} />;
62
+ }
63
+
64
+ if (!hasVisibleActions(actions) && !actionsChildren) {
65
+ return null;
66
+ }
67
+
68
+ return (
69
+ <div className={styles.actionsWrapper} data-test-id={TEST_IDS.actions}>
70
+ {actionsChildren}
71
+ {primaryAction && wide && <ActionView {...primaryAction} commonProps={{ size: 'm', fullWidth: true }} />}
72
+ {Boolean(kebabItems.length) && (
73
+ <ButtonKebab
74
+ list={{
75
+ items: kebabItems,
76
+ closeDroplistOnItemClick: true,
77
+ }}
78
+ />
79
+ )}
80
+ </div>
81
+ );
82
+ }
83
+
84
+ export const Actions = memo<ActionsProps>(ActionsComponent);
@@ -0,0 +1,84 @@
1
+ import { MouseEvent } from 'react';
2
+
3
+ import { BUTTON_TYPE } from '../../constants';
4
+ import { WidgetAction, WidgetActionListEntry } from '../../types';
5
+
6
+ export function isVisibleAction(action: WidgetAction) {
7
+ return !action.hidden;
8
+ }
9
+
10
+ export function hasVisibleActions(actions: WidgetAction[]) {
11
+ return actions.some(isVisibleAction);
12
+ }
13
+
14
+ export function actionToListItem(action: WidgetAction, close?: () => void) {
15
+ const {
16
+ label = '',
17
+ icon,
18
+ onClick,
19
+ disabled,
20
+ tooltip,
21
+ } = action as Extract<
22
+ WidgetAction,
23
+ {
24
+ variant?:
25
+ | typeof BUTTON_TYPE.Filled
26
+ | typeof BUTTON_TYPE.Outline
27
+ | typeof BUTTON_TYPE.Tonal
28
+ | typeof BUTTON_TYPE.Function
29
+ | typeof BUTTON_TYPE.Simple;
30
+ }
31
+ >;
32
+
33
+ return {
34
+ content: { label: label },
35
+ beforeContent: icon,
36
+ tooltip,
37
+ disabled,
38
+ onClick: (event: MouseEvent<HTMLButtonElement>) => {
39
+ close?.();
40
+ onClick?.(event);
41
+ },
42
+ };
43
+ }
44
+
45
+ export function getPrimaryAction(items: WidgetAction[]) {
46
+ const index = items.findIndex(
47
+ action =>
48
+ isVisibleAction(action) &&
49
+ action.variant !== BUTTON_TYPE.Function &&
50
+ action.variant !== BUTTON_TYPE.Droplist &&
51
+ action.variant !== BUTTON_TYPE.Kebab,
52
+ );
53
+
54
+ return index >= 0 ? { action: items[index], index } : { index: -1 };
55
+ }
56
+
57
+ export function buildKebabItems(items: WidgetAction[], primaryActionIndex: number): WidgetActionListEntry[] {
58
+ return items.reduce<WidgetActionListEntry[]>((acc, action, index) => {
59
+ if (!isVisibleAction(action)) {
60
+ return acc;
61
+ }
62
+
63
+ if (action.variant === BUTTON_TYPE.Function) {
64
+ return acc;
65
+ }
66
+
67
+ if (action.variant === BUTTON_TYPE.Droplist || action.variant === BUTTON_TYPE.Kebab) {
68
+ acc.push({
69
+ type: 'group',
70
+ label: action.variant === BUTTON_TYPE.Droplist ? action.button.label : undefined,
71
+ items: action.list.items.flatMap(item => ('type' in item ? item.items : [item])),
72
+ divider: acc.length > 0,
73
+ });
74
+
75
+ return acc;
76
+ }
77
+
78
+ if (index !== primaryActionIndex) {
79
+ acc.push(actionToListItem(action, () => undefined));
80
+ }
81
+
82
+ return acc;
83
+ }, []);
84
+ }
@@ -0,0 +1,3 @@
1
+ export * from './Actions';
2
+ export * from './ActionView';
3
+ export { isVisibleAction } from './helpers';
@@ -0,0 +1,54 @@
1
+ @use '@cloud-ru/ds-figma-variables/build/scss/styles/styles.module' as base;
2
+
3
+ .mobileActionsWrapper {
4
+ scrollbar-width: none;
5
+
6
+ overflow-y: auto;
7
+ display: flex;
8
+ flex-shrink: 0;
9
+ gap: base.$sn-primitive-dimension-8;
10
+ justify-content: stretch;
11
+
12
+ width: 100%;
13
+
14
+ &::-webkit-scrollbar {
15
+ display: none;
16
+ }
17
+ }
18
+
19
+ .button {
20
+ white-space: nowrap;
21
+
22
+ &[data-full-width]:not([data-variant='icon-only']) {
23
+ flex-shrink: 0;
24
+ width: auto;
25
+ }
26
+
27
+ &[data-variant='icon-only'] {
28
+ flex-grow: 0;
29
+ flex-shrink: 0;
30
+ }
31
+ }
32
+
33
+ .actionsWrapper {
34
+ display: flex;
35
+ flex-shrink: 0;
36
+ gap: base.$sn-primitive-dimension-8;
37
+ align-items: center;
38
+
39
+ &[data-wide] {
40
+ justify-content: flex-end;
41
+ }
42
+ }
43
+
44
+ .skeletonActions {
45
+ display: flex;
46
+ gap: base.$sn-primitive-dimension-8;
47
+ align-items: center;
48
+ width: 100%;
49
+ }
50
+
51
+ .skeletonActionFull {
52
+ flex: 1;
53
+ min-width: 0;
54
+ }
@@ -0,0 +1,50 @@
1
+ import { Button, ICON_POSITION, VIEW } from '@cloud-ru/ds-button';
2
+ import { ChevronDownSVG, ChevronUpSVG } from '@cloud-ru/ds-icons/interface/system';
3
+ import { Droplist } from '@cloud-ru/ds-list';
4
+ import { useUncontrolledProp } from '@cloud-ru/ds-utils';
5
+ import { memo, useMemo } from 'react';
6
+
7
+ import { TEST_IDS } from '../../constants';
8
+ import { ButtonDroplistProps } from '../../types';
9
+ import styles from './styles.module.scss';
10
+
11
+ function ButtonDroplistComponent({ button, list }: ButtonDroplistProps) {
12
+ const { buttonType: buttonTypeProp, ...buttonRest } = button;
13
+ const { items, className, closeDroplistOnItemClick = true, open: openProp, onOpenChange } = list;
14
+ // Стрелка триггера смотрит вверх на открытом списке, поэтому состояние нужно и здесь,
15
+ // а не только внутри Droplist.
16
+ const [open, setOpen] = useUncontrolledProp(openProp, false, onOpenChange);
17
+
18
+ const Icon = open ? ChevronUpSVG : ChevronDownSVG;
19
+ const buttonType = buttonTypeProp ?? 'function';
20
+ const buttonProps = useMemo(
21
+ () =>
22
+ buttonType === 'filled'
23
+ ? { ...buttonRest, view: VIEW.Filled }
24
+ : {
25
+ ...buttonRest,
26
+ view: VIEW.Function,
27
+ appearance: 'neutral' as const,
28
+ icon: <Icon />,
29
+ iconPosition: ICON_POSITION.After,
30
+ },
31
+ [Icon, buttonRest, buttonType],
32
+ );
33
+
34
+ return (
35
+ <Droplist
36
+ items={items}
37
+ className={className}
38
+ open={open}
39
+ onOpenChange={setOpen}
40
+ closeDroplistOnItemClick={closeDroplistOnItemClick}
41
+ placement='bottom-end'
42
+ triggerClassName={styles.trigger}
43
+ data-test-id={TEST_IDS.dropdown}
44
+ >
45
+ <Button {...buttonProps} />
46
+ </Droplist>
47
+ );
48
+ }
49
+
50
+ export const ButtonDroplist = memo<ButtonDroplistProps>(ButtonDroplistComponent);
@@ -0,0 +1 @@
1
+ export * from './ButtonDroplist';
@@ -0,0 +1,5 @@
1
+ .trigger {
2
+ display: inline-flex;
3
+ align-items: center;
4
+ vertical-align: top;
5
+ }
@@ -0,0 +1,36 @@
1
+ import { Button, VIEW } from '@cloud-ru/ds-button';
2
+ import { KebabSVG } from '@cloud-ru/ds-icons/interface/system';
3
+ import { Droplist } from '@cloud-ru/ds-list';
4
+ import { memo } from 'react';
5
+
6
+ import { TEST_IDS } from '../../constants';
7
+ import { ButtonKebabProps } from '../../types';
8
+ import styles from './styles.module.scss';
9
+
10
+ function ButtonKebabComponent({ button, list }: ButtonKebabProps) {
11
+ const { items, className, closeDroplistOnItemClick = true, open, onOpenChange } = list;
12
+
13
+ return (
14
+ <Droplist
15
+ items={items}
16
+ className={className}
17
+ open={open}
18
+ onOpenChange={onOpenChange}
19
+ closeDroplistOnItemClick={closeDroplistOnItemClick}
20
+ placement='bottom-end'
21
+ triggerClassName={styles.trigger}
22
+ data-test-id={TEST_IDS.kebabDroplist}
23
+ >
24
+ <Button
25
+ {...button}
26
+ icon={<KebabSVG />}
27
+ appearance='neutral'
28
+ size='m'
29
+ view={VIEW.Outline}
30
+ data-test-id={TEST_IDS.kebabButton}
31
+ />
32
+ </Droplist>
33
+ );
34
+ }
35
+
36
+ export const ButtonKebab = memo<ButtonKebabProps>(ButtonKebabComponent);
@@ -0,0 +1 @@
1
+ export * from './ButtonKebab';
@@ -0,0 +1,5 @@
1
+ .trigger {
2
+ display: inline-flex;
3
+ align-items: center;
4
+ vertical-align: top;
5
+ }