@cloud-ru/ds-uikit-product-copy 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 (62) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/LICENSE +201 -0
  3. package/README.md +253 -0
  4. package/dist/cjs/components/CopyButton/CopyButton.d.ts +16 -0
  5. package/dist/cjs/components/CopyButton/CopyButton.js +17 -0
  6. package/dist/cjs/components/CopyButton/index.d.ts +1 -0
  7. package/dist/cjs/components/CopyButton/index.js +17 -0
  8. package/dist/cjs/components/CopyLine/CopyLine.d.ts +16 -0
  9. package/dist/cjs/components/CopyLine/CopyLine.js +32 -0
  10. package/dist/cjs/components/CopyLine/constants.d.ts +4 -0
  11. package/dist/cjs/components/CopyLine/constants.js +7 -0
  12. package/dist/cjs/components/CopyLine/index.d.ts +3 -0
  13. package/dist/cjs/components/CopyLine/index.js +19 -0
  14. package/dist/cjs/components/CopyLine/styles.module.css +36 -0
  15. package/dist/cjs/components/CopyLine/types.d.ts +3 -0
  16. package/dist/cjs/components/CopyLine/types.js +2 -0
  17. package/dist/cjs/components/index.d.ts +2 -0
  18. package/dist/cjs/components/index.js +18 -0
  19. package/dist/cjs/constants.d.ts +9 -0
  20. package/dist/cjs/constants.js +12 -0
  21. package/dist/cjs/index.d.ts +2 -0
  22. package/dist/cjs/index.js +18 -0
  23. package/dist/cjs/package.json +3 -0
  24. package/dist/cjs/style.css +36 -0
  25. package/dist/cjs/utils.d.ts +2 -0
  26. package/dist/cjs/utils.js +6 -0
  27. package/dist/esm/components/CopyButton/CopyButton.d.ts +16 -0
  28. package/dist/esm/components/CopyButton/CopyButton.js +14 -0
  29. package/dist/esm/components/CopyButton/index.d.ts +1 -0
  30. package/dist/esm/components/CopyButton/index.js +1 -0
  31. package/dist/esm/components/CopyLine/CopyLine.d.ts +16 -0
  32. package/dist/esm/components/CopyLine/CopyLine.js +26 -0
  33. package/dist/esm/components/CopyLine/constants.d.ts +4 -0
  34. package/dist/esm/components/CopyLine/constants.js +4 -0
  35. package/dist/esm/components/CopyLine/index.d.ts +3 -0
  36. package/dist/esm/components/CopyLine/index.js +3 -0
  37. package/dist/esm/components/CopyLine/styles.module.css +36 -0
  38. package/dist/esm/components/CopyLine/types.d.ts +3 -0
  39. package/dist/esm/components/CopyLine/types.js +1 -0
  40. package/dist/esm/components/index.d.ts +2 -0
  41. package/dist/esm/components/index.js +2 -0
  42. package/dist/esm/constants.d.ts +9 -0
  43. package/dist/esm/constants.js +9 -0
  44. package/dist/esm/index.d.ts +2 -0
  45. package/dist/esm/index.js +2 -0
  46. package/dist/esm/style.css +36 -0
  47. package/dist/esm/utils.d.ts +2 -0
  48. package/dist/esm/utils.js +3 -0
  49. package/dist/tsconfig.cjs.tsbuildinfo +1 -0
  50. package/dist/tsconfig.esm.tsbuildinfo +1 -0
  51. package/package.json +45 -0
  52. package/src/components/CopyButton/CopyButton.tsx +43 -0
  53. package/src/components/CopyButton/index.ts +1 -0
  54. package/src/components/CopyLine/CopyLine.tsx +73 -0
  55. package/src/components/CopyLine/constants.ts +4 -0
  56. package/src/components/CopyLine/index.ts +3 -0
  57. package/src/components/CopyLine/styles.module.scss +43 -0
  58. package/src/components/CopyLine/types.ts +5 -0
  59. package/src/components/index.ts +2 -0
  60. package/src/constants.ts +9 -0
  61. package/src/index.ts +2 -0
  62. package/src/utils.ts +5 -0
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@cloud-ru/ds-uikit-product-copy",
3
+ "version": "1.0.0",
4
+ "description": "Пакет копирования значений в буфер — компоненты CopyButton и CopyLine с единой логикой clipboard и иконкой подтверждения.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/cloud-ru-tech/snack-v2.git",
8
+ "directory": "packages/uikit-product-copy"
9
+ },
10
+ "homepage": "https://github.com/cloud-ru-tech/snack-v2/tree/master/packages/uikit-product-copy",
11
+ "license": "Apache-2.0",
12
+ "author": "Nikita Ershov <niershov@cloud.ru>",
13
+ "type": "module",
14
+ "main": "./dist/cjs/index.js",
15
+ "module": "./dist/esm/index.js",
16
+ "types": "./dist/esm/index.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "source": "./src/index.ts",
20
+ "types": "./dist/esm/index.d.ts",
21
+ "import": "./dist/esm/index.js",
22
+ "require": "./dist/cjs/index.js"
23
+ }
24
+ },
25
+ "files": [
26
+ "dist",
27
+ "src",
28
+ "./CHANGELOG.md",
29
+ "./LICENSE"
30
+ ],
31
+ "sideEffects": [
32
+ "*.css",
33
+ "**/*.css"
34
+ ],
35
+ "dependencies": {
36
+ "classnames": "^2.5.1",
37
+ "@cloud-ru/ds-button": "^1.0.0",
38
+ "@cloud-ru/ds-truncate-string": "^1.0.0",
39
+ "@cloud-ru/ds-icons": "^1.0.0",
40
+ "@cloud-ru/ds-utils": "^1.0.0"
41
+ },
42
+ "publishConfig": {
43
+ "access": "public"
44
+ }
45
+ }
@@ -0,0 +1,43 @@
1
+ import { Button, ButtonProps } from '@cloud-ru/ds-button';
2
+ import { CheckSVG, CopySVG } from '@cloud-ru/ds-icons/interface/system';
3
+ import { extractSupportProps, useCopyToClipboard, WithSupportProps } from '@cloud-ru/ds-utils';
4
+ import { MouseEventHandler } from 'react';
5
+
6
+ export type CopyButtonProps = WithSupportProps<{
7
+ /** Значение для копирования в буфер. */
8
+ valueToCopy: string | number;
9
+ /** Размер кнопки (s / m / l). По-умолчанию s. */
10
+ size?: ButtonProps['size'];
11
+ /** Текст рядом с иконкой. Если не задан — кнопка отображается только с иконкой. */
12
+ label?: string;
13
+ /** Дополнительный класс. */
14
+ className?: string;
15
+ /** Доп. обработчик клика. */
16
+ onClick?: MouseEventHandler<HTMLButtonElement>;
17
+ }>;
18
+
19
+ export function CopyButton({ valueToCopy, size = 's', label, className, onClick, ...rest }: CopyButtonProps) {
20
+ const { isChecked, copy } = useCopyToClipboard();
21
+
22
+ const handleClick: MouseEventHandler<HTMLButtonElement> = event => {
23
+ event.stopPropagation();
24
+ if (valueToCopy) copy(String(valueToCopy));
25
+ onClick?.(event);
26
+ };
27
+
28
+ return (
29
+ <Button
30
+ {...extractSupportProps(rest)}
31
+ className={className}
32
+ view='function'
33
+ appearance='neutral'
34
+ size={size}
35
+ type='button'
36
+ aria-label={label ? undefined : 'Copy'}
37
+ label={label}
38
+ icon={isChecked ? <CheckSVG /> : <CopySVG />}
39
+ iconPosition='after'
40
+ onClick={handleClick}
41
+ />
42
+ );
43
+ }
@@ -0,0 +1 @@
1
+ export * from './CopyButton';
@@ -0,0 +1,73 @@
1
+ import { Button } from '@cloud-ru/ds-button';
2
+ import { CheckSVG, CopySVG } from '@cloud-ru/ds-icons/interface/system';
3
+ import { TruncateString } from '@cloud-ru/ds-truncate-string';
4
+ import { extractSupportProps, useCopyToClipboard, WithSupportProps } from '@cloud-ru/ds-utils';
5
+ import cn from 'classnames';
6
+ import { MouseEvent, MouseEventHandler, ReactNode } from 'react';
7
+
8
+ import { TEST_IDS } from '../../constants';
9
+ import { isStringOrNumber } from '../../utils';
10
+ import { COPY_BUTTON_HIDE_STRATEGY } from './constants';
11
+ import styles from './styles.module.scss';
12
+ import { CopyButtonHideStrategy } from './types';
13
+
14
+ const ICON_SIZE = 16;
15
+
16
+ export type CopyLineProps = WithSupportProps<{
17
+ /** Отображаемое содержимое. Если это строка/число — автоматически truncated и используется как значение для копирования по-умолчанию. */
18
+ content: ReactNode;
19
+ /** Значение, которое попадёт в буфер при клике. По-умолчанию равно content, если он string/number. */
20
+ valueToCopy?: string | number;
21
+ /** Дополнительный класс корневого элемента. */
22
+ className?: string;
23
+ /** Обработчик клика по строке. */
24
+ onClick?: MouseEventHandler<HTMLDivElement>;
25
+ /** Стратегия показа кнопки копирования: hover — только по наведению, never — всегда. */
26
+ copyButtonHideStrategy?: CopyButtonHideStrategy;
27
+ }>;
28
+
29
+ export function CopyLine({
30
+ content,
31
+ className,
32
+ valueToCopy: valueToCopyProp,
33
+ onClick,
34
+ copyButtonHideStrategy = COPY_BUTTON_HIDE_STRATEGY.Hover,
35
+ ...rest
36
+ }: CopyLineProps) {
37
+ const valueToCopy = valueToCopyProp ?? (isStringOrNumber(content) ? String(content) : '');
38
+ const { isChecked, copy } = useCopyToClipboard();
39
+
40
+ const handleClick: MouseEventHandler<HTMLDivElement> = event => {
41
+ event.stopPropagation();
42
+ if (valueToCopy) copy(String(valueToCopy));
43
+ onClick?.(event);
44
+ };
45
+
46
+ return (
47
+ <div
48
+ className={cn(styles.copyLine, className)}
49
+ onClick={handleClick}
50
+ role='presentation'
51
+ data-copy-button-hide-strategy={copyButtonHideStrategy}
52
+ {...extractSupportProps(rest)}
53
+ >
54
+ <span className={styles.content}>
55
+ {isStringOrNumber(content) ? <TruncateString text={String(content)} maxLines={1} /> : content}
56
+ </span>
57
+ <Button
58
+ className={styles.copyButton}
59
+ view='function'
60
+ appearance='neutral'
61
+ size='s'
62
+ type='button'
63
+ aria-label='Copy'
64
+ data-test-id={TEST_IDS.copyLine.copyButton}
65
+ icon={isChecked ? <CheckSVG size={ICON_SIZE} /> : <CopySVG size={ICON_SIZE} />}
66
+ onClick={(e: MouseEvent<HTMLButtonElement>) => {
67
+ e.stopPropagation();
68
+ if (valueToCopy) copy(String(valueToCopy));
69
+ }}
70
+ />
71
+ </div>
72
+ );
73
+ }
@@ -0,0 +1,4 @@
1
+ export const COPY_BUTTON_HIDE_STRATEGY = {
2
+ Never: 'never',
3
+ Hover: 'hover',
4
+ } as const;
@@ -0,0 +1,3 @@
1
+ export * from './CopyLine';
2
+ export * from './constants';
3
+ export * from './types';
@@ -0,0 +1,43 @@
1
+ @use '@cloud-ru/ds-figma-variables/build/scss/styles/styles.module' as base;
2
+
3
+ .copyButton {
4
+ flex-shrink: 0;
5
+ }
6
+
7
+ .content {
8
+ @include base.composite-var(base.$sn-regular-body-m);
9
+
10
+ overflow: hidden;
11
+ flex: 0 1 auto;
12
+
13
+ min-width: 0;
14
+
15
+ color: base.$sn-theme-color-available-version-textMain;
16
+ text-overflow: ellipsis;
17
+ white-space: nowrap;
18
+ }
19
+
20
+ .copyLine {
21
+ cursor: pointer;
22
+
23
+ display: inline-flex;
24
+ flex-shrink: 0;
25
+ gap: base.$sn-primitive-dimension-4;
26
+ align-items: center;
27
+
28
+ box-sizing: border-box;
29
+ min-width: 0;
30
+ max-width: 100%;
31
+ min-height: base.$sn-primitive-dimension-24;
32
+
33
+ &[data-copy-button-hide-strategy='hover'] {
34
+ .copyButton {
35
+ display: none;
36
+ }
37
+
38
+ &:hover .copyButton,
39
+ &:focus-within .copyButton {
40
+ display: inline-flex;
41
+ }
42
+ }
43
+ }
@@ -0,0 +1,5 @@
1
+ import { ValueOf } from '@cloud-ru/ds-utils';
2
+
3
+ import { COPY_BUTTON_HIDE_STRATEGY } from './constants';
4
+
5
+ export type CopyButtonHideStrategy = ValueOf<typeof COPY_BUTTON_HIDE_STRATEGY>;
@@ -0,0 +1,2 @@
1
+ export * from './CopyLine';
2
+ export * from './CopyButton';
@@ -0,0 +1,9 @@
1
+ export const TEST_IDS = {
2
+ copyButton: {
3
+ root: 'copy-button',
4
+ },
5
+ copyLine: {
6
+ root: 'copy-line',
7
+ copyButton: 'copy-line__copy-button',
8
+ },
9
+ } as const;
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './components';
2
+ export * from './constants';
package/src/utils.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { ReactNode } from 'react';
2
+
3
+ export function isStringOrNumber(content: ReactNode): content is string | number {
4
+ return typeof content === 'string' || typeof content === 'number';
5
+ }