@scaleflex/ui-tw 0.0.151 → 0.0.153
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/button/button.types.d.ts +4 -4
- package/button/components/copy-button.js +15 -54
- package/button/components/copy-to-clipboard-button.d.ts +1 -0
- package/button/components/copy-to-clipboard-button.js +17 -53
- package/button/components/end-icon.js +3 -0
- package/button/components/start-icon.js +12 -6
- package/color-picker/color-picker.component.js +6 -3
- package/color-picker/color-picker.types.d.ts +4 -4
- package/combobox/combobox-multi-checkbox/combobox-multi-checkbox.component.js +8 -4
- package/combobox/combobox-multi-inline/combobox-multi-inline.component.js +6 -3
- package/combobox/combobox-multi-tag/combobox-multi-tag.component.js +1 -1
- package/combobox/combobox-single/combobox-single.component.js +3 -3
- package/combobox/combobox-users/combobox-users.component.js +9 -8
- package/combobox/combobox.component.js +30 -18
- package/combobox/combobox.types.d.ts +9 -8
- package/command/command.component.d.ts +2 -2
- package/command/command.component.js +28 -13
- package/command/command.utils.d.ts +4 -2
- package/command/command.utils.js +6 -1
- package/copyable-text/copyable-text.component.js +17 -56
- package/dialog/dialog.component.d.ts +1 -1
- package/dialog/dialog.component.js +4 -2
- package/dialog/dialog.types.d.ts +2 -0
- package/form/components/form-checkbox-field.component.js +4 -2
- package/form/components/form-checkbox-group-field.component.js +7 -5
- package/form/components/form-field-group.component.js +7 -5
- package/form/components/form-radio-group-field.component.js +7 -5
- package/form/components/form-switch-field.component.js +6 -4
- package/form/form.types.d.ts +24 -24
- package/input-tags/input-tags.component.js +18 -15
- package/label/components/label-icon.js +8 -3
- package/label/label.types.d.ts +4 -4
- package/package.json +2 -2
- package/select/components/select-icon.js +3 -0
- package/select/components/selector.js +13 -9
- package/select/select.types.d.ts +5 -5
- package/textarea/components/textarea-with-actions.js +7 -5
- package/textarea/textarea.types.d.ts +2 -2
- package/tooltip/tooltip.component.js +3 -2
- package/tooltip/tooltip.types.d.ts +1 -1
- package/utils/copy-to-clipboard.d.ts +10 -0
- package/utils/copy-to-clipboard.js +65 -0
- package/utils/use-copy-to-clipboard.d.ts +13 -0
- package/utils/use-copy-to-clipboard.js +68 -0
|
@@ -20,6 +20,12 @@ import { cva } from 'class-variance-authority';
|
|
|
20
20
|
import { ChevronsUpDownIcon } from 'lucide-react';
|
|
21
21
|
import * as React from 'react';
|
|
22
22
|
import { useMemo, useState } from 'react';
|
|
23
|
+
var normalizeLabel = function normalizeLabel(label) {
|
|
24
|
+
return label.trim().replace(/\s+/g, ' ');
|
|
25
|
+
};
|
|
26
|
+
var normalizeValue = function normalizeValue(label) {
|
|
27
|
+
return normalizeLabel(label).replace(/\s+/g, '-');
|
|
28
|
+
};
|
|
23
29
|
var inputTagsVariants = cva('', {
|
|
24
30
|
variants: {
|
|
25
31
|
size: textareaSizeOptions
|
|
@@ -68,7 +74,9 @@ export function InputTags(_ref) {
|
|
|
68
74
|
_useState4 = _slicedToArray(_useState3, 2),
|
|
69
75
|
search = _useState4[0],
|
|
70
76
|
setSearch = _useState4[1];
|
|
71
|
-
var tagsList =
|
|
77
|
+
var tagsList = useMemo(function () {
|
|
78
|
+
return [].concat(_toConsumableArray(suggestedTags), _toConsumableArray(allTags));
|
|
79
|
+
}, [suggestedTags, allTags]);
|
|
72
80
|
var _getToolbarSizes = getToolbarSizes(size),
|
|
73
81
|
buttonSize = _getToolbarSizes.buttonSize;
|
|
74
82
|
var searchTrimmed = search.trim().toLowerCase();
|
|
@@ -80,18 +88,11 @@ export function InputTags(_ref) {
|
|
|
80
88
|
return isSameTag(t, tag);
|
|
81
89
|
});
|
|
82
90
|
};
|
|
83
|
-
var
|
|
84
|
-
return label.trim().replace(/\s+/g, ' ');
|
|
85
|
-
};
|
|
86
|
-
var normalizeValue = function normalizeValue(label) {
|
|
87
|
-
return normalizeLabel(label).replace(/\s+/g, '-');
|
|
88
|
-
};
|
|
89
|
-
var createTag = function createTag(label) {
|
|
91
|
+
var newTag = useMemo(function () {
|
|
90
92
|
return _defineProperty({
|
|
91
|
-
label: normalizeLabel(
|
|
92
|
-
}, valueProp, normalizeValue(
|
|
93
|
-
};
|
|
94
|
-
var newTag = createTag(search);
|
|
93
|
+
label: normalizeLabel(search)
|
|
94
|
+
}, valueProp, normalizeValue(search));
|
|
95
|
+
}, [search, valueProp]);
|
|
95
96
|
var canCreate = useMemo(function () {
|
|
96
97
|
if (!allowCustomTags || !searchTrimmed) return false;
|
|
97
98
|
return !value.some(function (v) {
|
|
@@ -130,7 +131,7 @@ export function InputTags(_ref) {
|
|
|
130
131
|
asChild: true
|
|
131
132
|
}, /*#__PURE__*/React.createElement("button", {
|
|
132
133
|
id: formItemId,
|
|
133
|
-
role: "combobox
|
|
134
|
+
role: "combobox",
|
|
134
135
|
type: "button",
|
|
135
136
|
"aria-expanded": open,
|
|
136
137
|
"aria-invalid": rest['aria-invalid'],
|
|
@@ -202,7 +203,8 @@ export function InputTags(_ref) {
|
|
|
202
203
|
onSelect: function onSelect() {
|
|
203
204
|
return handleAddTag(tag);
|
|
204
205
|
},
|
|
205
|
-
isGroup: true
|
|
206
|
+
isGroup: true,
|
|
207
|
+
labelTooltip: tag.label
|
|
206
208
|
}, /*#__PURE__*/React.createElement("span", {
|
|
207
209
|
className: "block w-full truncate overflow-hidden text-ellipsis whitespace-nowrap"
|
|
208
210
|
}, tag.label));
|
|
@@ -216,7 +218,8 @@ export function InputTags(_ref) {
|
|
|
216
218
|
onSelect: function onSelect() {
|
|
217
219
|
return handleAddTag(tag);
|
|
218
220
|
},
|
|
219
|
-
isGroup: true
|
|
221
|
+
isGroup: true,
|
|
222
|
+
labelTooltip: tag.label
|
|
220
223
|
}, /*#__PURE__*/React.createElement("span", {
|
|
221
224
|
className: "block w-full truncate overflow-hidden text-ellipsis whitespace-nowrap"
|
|
222
225
|
}, tag.label));
|
|
@@ -22,7 +22,12 @@ var LabelIcon = function LabelIcon(props) {
|
|
|
22
22
|
return getLabelIconSizeInRem(size);
|
|
23
23
|
}, [size]);
|
|
24
24
|
if (!icon && !tooltip) return null;
|
|
25
|
-
var
|
|
25
|
+
var resolvedTooltip = typeof tooltip === 'function' ? tooltip() : tooltip;
|
|
26
|
+
var iconElement = typeof icon === 'function' ? /*#__PURE__*/React.createElement("span", _extends({
|
|
27
|
+
className: cn('text-muted-foreground', labelIconVariants({
|
|
28
|
+
size: size
|
|
29
|
+
}))
|
|
30
|
+
}, rest), icon()) : /*#__PURE__*/React.createElement("span", _extends({
|
|
26
31
|
className: cn('text-muted-foreground', labelIconVariants({
|
|
27
32
|
size: size
|
|
28
33
|
}))
|
|
@@ -33,9 +38,9 @@ var LabelIcon = function LabelIcon(props) {
|
|
|
33
38
|
height: sizeInRem
|
|
34
39
|
}
|
|
35
40
|
}));
|
|
36
|
-
if (!
|
|
41
|
+
if (!resolvedTooltip) return iconElement;
|
|
37
42
|
return /*#__PURE__*/React.createElement(Tooltip, null, /*#__PURE__*/React.createElement(TooltipTrigger, {
|
|
38
43
|
asChild: true
|
|
39
|
-
}, iconElement), /*#__PURE__*/React.createElement(TooltipContent, null,
|
|
44
|
+
}, iconElement), /*#__PURE__*/React.createElement(TooltipContent, null, resolvedTooltip));
|
|
40
45
|
};
|
|
41
46
|
export { LabelIcon };
|
package/label/label.types.d.ts
CHANGED
|
@@ -3,12 +3,12 @@ import { FormSizeType } from '@scaleflex/ui-tw/types/form-size';
|
|
|
3
3
|
import type { ComponentProps, ReactElement } from 'react';
|
|
4
4
|
export interface LabelProps extends Omit<ComponentProps<typeof LabelPrimitive.Root>, 'size'> {
|
|
5
5
|
size?: FormSizeType;
|
|
6
|
-
icon?: ReactElement;
|
|
7
|
-
tooltip?: ReactElement | string;
|
|
6
|
+
icon?: ReactElement | (() => ReactElement);
|
|
7
|
+
tooltip?: ReactElement | string | (() => ReactElement);
|
|
8
8
|
disabled?: boolean;
|
|
9
|
-
disabledTooltip?: ReactElement | string;
|
|
9
|
+
disabledTooltip?: ReactElement | string | (() => ReactElement);
|
|
10
10
|
noTruncate?: boolean;
|
|
11
11
|
}
|
|
12
12
|
export interface LabelIconProps extends ComponentProps<'span'>, Pick<LabelProps, 'size' | 'tooltip'> {
|
|
13
|
-
icon?: ReactElement;
|
|
13
|
+
icon?: ReactElement | (() => ReactElement);
|
|
14
14
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scaleflex/ui-tw",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.153",
|
|
4
4
|
"author": "scaleflex",
|
|
5
5
|
"repository": "github:scaleflex/ui",
|
|
6
6
|
"homepage": "https://github.com/scaleflex/ui/blob/master/README.md",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"@radix-ui/react-switch": "^1.0.1",
|
|
30
30
|
"@radix-ui/react-tabs": "^1.1.13",
|
|
31
31
|
"@radix-ui/react-tooltip": "^1.2.6",
|
|
32
|
-
"@scaleflex/icons-tw": "^0.0.
|
|
32
|
+
"@scaleflex/icons-tw": "^0.0.153",
|
|
33
33
|
"@tanstack/react-table": "^8.21.3",
|
|
34
34
|
"@types/lodash.merge": "^4.6.9",
|
|
35
35
|
"class-variance-authority": "^0.7.1",
|
|
@@ -12,6 +12,9 @@ export var SelectIcon = function SelectIcon(props) {
|
|
|
12
12
|
return getIconMarginRightInRem(size);
|
|
13
13
|
}, [size]);
|
|
14
14
|
if (!icon) return null;
|
|
15
|
+
if (typeof icon === 'function') {
|
|
16
|
+
return icon();
|
|
17
|
+
}
|
|
15
18
|
return /*#__PURE__*/cloneElement(icon, {
|
|
16
19
|
size: sizeInRem,
|
|
17
20
|
style: {
|
|
@@ -46,13 +46,13 @@ export function Selector(_ref) {
|
|
|
46
46
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
47
47
|
var option = _step.value;
|
|
48
48
|
if (isOptionGroup(option)) {
|
|
49
|
-
if (option.value === val) return option.label;
|
|
49
|
+
if (option.value === val) return typeof option.label === 'function' ? option.label() : option.label;
|
|
50
50
|
var found = option.options.find(function (o) {
|
|
51
51
|
return o.value === val;
|
|
52
52
|
});
|
|
53
|
-
if (found) return found.label;
|
|
53
|
+
if (found) return typeof found.label === 'function' ? found.label() : found.label;
|
|
54
54
|
} else if (option.value === val) {
|
|
55
|
-
return option.label;
|
|
55
|
+
return typeof option.label === 'function' ? option.label() : option.label;
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
} catch (err) {
|
|
@@ -79,8 +79,8 @@ export function Selector(_ref) {
|
|
|
79
79
|
}, value ? getLabelByValue(value) : '')), /*#__PURE__*/React.createElement(SelectContent, _extends({
|
|
80
80
|
className: popoverClassName
|
|
81
81
|
}, selectorContentProps), options.map(function (option, groupIndex) {
|
|
82
|
-
var _option$optionLabel;
|
|
83
82
|
if (isOptionGroup(option)) {
|
|
83
|
+
var resolvedGroupLabel = typeof option.label === 'function' ? option.label() : option.label;
|
|
84
84
|
return /*#__PURE__*/React.createElement(SelectGroup, {
|
|
85
85
|
key: groupIndex
|
|
86
86
|
}, option.value ? /*#__PURE__*/React.createElement(SelectItem, {
|
|
@@ -95,9 +95,9 @@ export function Selector(_ref) {
|
|
|
95
95
|
className: cn('text-secondary-foreground', selectLabelVariants({
|
|
96
96
|
size: size
|
|
97
97
|
}))
|
|
98
|
-
},
|
|
98
|
+
}, resolvedGroupLabel) : resolvedGroupLabel && /*#__PURE__*/React.createElement(SelectLabel, {
|
|
99
99
|
size: size
|
|
100
|
-
},
|
|
100
|
+
}, resolvedGroupLabel), option.options.map(function (_ref2) {
|
|
101
101
|
var label = _ref2.label,
|
|
102
102
|
optionLabel = _ref2.optionLabel,
|
|
103
103
|
value = _ref2.value,
|
|
@@ -105,6 +105,8 @@ export function Selector(_ref) {
|
|
|
105
105
|
icon = _ref2.icon,
|
|
106
106
|
tooltip = _ref2.tooltip,
|
|
107
107
|
disabledTooltip = _ref2.disabledTooltip;
|
|
108
|
+
var resolvedLabel = typeof label === 'function' ? label() : label;
|
|
109
|
+
var resolvedOptionLabel = typeof optionLabel === 'function' ? optionLabel() : optionLabel;
|
|
108
110
|
return /*#__PURE__*/React.createElement(SelectItem, {
|
|
109
111
|
key: value,
|
|
110
112
|
value: value,
|
|
@@ -113,10 +115,12 @@ export function Selector(_ref) {
|
|
|
113
115
|
icon: icon,
|
|
114
116
|
tooltip: tooltip,
|
|
115
117
|
disabledTooltip: disabledTooltip,
|
|
116
|
-
isGroup: !!
|
|
117
|
-
},
|
|
118
|
+
isGroup: !!resolvedGroupLabel
|
|
119
|
+
}, resolvedOptionLabel !== null && resolvedOptionLabel !== void 0 ? resolvedOptionLabel : resolvedLabel);
|
|
118
120
|
}), showGroupSeparator && groupIndex !== options.length - 1 && /*#__PURE__*/React.createElement(SelectSeparator, null));
|
|
119
121
|
}
|
|
122
|
+
var resolvedLabel = typeof option.label === 'function' ? option.label() : option.label;
|
|
123
|
+
var resolvedOptionLabel = typeof option.optionLabel === 'function' ? option.optionLabel() : option.optionLabel;
|
|
120
124
|
return /*#__PURE__*/React.createElement(SelectGroup, {
|
|
121
125
|
key: groupIndex
|
|
122
126
|
}, /*#__PURE__*/React.createElement(SelectItem, {
|
|
@@ -126,7 +130,7 @@ export function Selector(_ref) {
|
|
|
126
130
|
icon: option.icon,
|
|
127
131
|
tooltip: option.tooltip,
|
|
128
132
|
disabledTooltip: option.disabledTooltip
|
|
129
|
-
},
|
|
133
|
+
}, resolvedOptionLabel !== null && resolvedOptionLabel !== void 0 ? resolvedOptionLabel : resolvedLabel));
|
|
130
134
|
}), /*#__PURE__*/React.createElement(SelectActions, {
|
|
131
135
|
actions: actions
|
|
132
136
|
})));
|
package/select/select.types.d.ts
CHANGED
|
@@ -6,15 +6,15 @@ export interface SelectProps extends ComponentProps<typeof SelectPrimitive.Root>
|
|
|
6
6
|
}
|
|
7
7
|
export interface SelectTriggerProps extends ComponentProps<typeof SelectPrimitive.Trigger> {
|
|
8
8
|
size?: FormSizeType;
|
|
9
|
-
icon?: ReactElement;
|
|
9
|
+
icon?: ReactElement | (() => ReactElement);
|
|
10
10
|
readOnly?: boolean;
|
|
11
11
|
'aria-invalid'?: boolean;
|
|
12
12
|
}
|
|
13
13
|
export interface SelectItemProps extends ComponentProps<typeof SelectPrimitive.Item> {
|
|
14
|
-
icon?: ReactElement;
|
|
14
|
+
icon?: ReactElement | (() => ReactElement);
|
|
15
15
|
size?: FormSizeType;
|
|
16
|
-
tooltip?: ReactElement | string;
|
|
17
|
-
disabledTooltip?: ReactElement | string;
|
|
16
|
+
tooltip?: ReactElement | string | (() => ReactElement);
|
|
17
|
+
disabledTooltip?: ReactElement | string | (() => ReactElement);
|
|
18
18
|
isGroup?: boolean;
|
|
19
19
|
}
|
|
20
20
|
export interface SelectLabelProps extends ComponentProps<typeof SelectPrimitive.Label> {
|
|
@@ -34,7 +34,7 @@ export interface SelectorProps extends SelectProps {
|
|
|
34
34
|
readOnly?: boolean;
|
|
35
35
|
size?: FormSizeType;
|
|
36
36
|
className?: string;
|
|
37
|
-
icon?: ReactElement;
|
|
37
|
+
icon?: ReactElement | (() => ReactElement);
|
|
38
38
|
defaultOpen?: boolean;
|
|
39
39
|
showGroupSeparator?: boolean;
|
|
40
40
|
formItemId?: string;
|
|
@@ -55,7 +55,9 @@ function TextareaWithActions(_ref) {
|
|
|
55
55
|
}
|
|
56
56
|
onBlur === null || onBlur === void 0 || onBlur(e);
|
|
57
57
|
};
|
|
58
|
-
var
|
|
58
|
+
var resolvedLeftToolbar = typeof leftToolbar === 'function' ? leftToolbar() : leftToolbar;
|
|
59
|
+
var resolvedRightToolbar = typeof rightToolbar === 'function' ? rightToolbar() : rightToolbar;
|
|
60
|
+
var hasBottomArea = resolvedLeftToolbar || resolvedRightToolbar || showCharCount;
|
|
59
61
|
return /*#__PURE__*/React.createElement("div", {
|
|
60
62
|
className: "relative"
|
|
61
63
|
}, /*#__PURE__*/React.createElement(Textarea, _extends({}, textareaProps, {
|
|
@@ -73,12 +75,12 @@ function TextareaWithActions(_ref) {
|
|
|
73
75
|
className: cn('bg-background absolute right-2.5 bottom-0.25 left-1.5', textareaBottomActionAreaPadding[size]),
|
|
74
76
|
onClick: handleActionClick,
|
|
75
77
|
tabIndex: -1
|
|
76
|
-
},
|
|
78
|
+
}, resolvedLeftToolbar && /*#__PURE__*/React.createElement("div", {
|
|
77
79
|
className: "absolute bottom-1.5 left-0 flex items-center gap-0.5"
|
|
78
|
-
},
|
|
80
|
+
}, resolvedLeftToolbar), /*#__PURE__*/React.createElement("div", {
|
|
79
81
|
className: "absolute right-0 bottom-1.5 flex items-center gap-0.5"
|
|
80
82
|
}, showCharCount && /*#__PURE__*/React.createElement("span", {
|
|
81
|
-
className: cn(textareaCharCountTextSizeOptions[size],
|
|
82
|
-
}, currentLength, "/", maxCharCount),
|
|
83
|
+
className: cn(textareaCharCountTextSizeOptions[size], resolvedRightToolbar && 'mr-1', charCountColorClass, disabled && 'opacity-50')
|
|
84
|
+
}, currentLength, "/", maxCharCount), resolvedRightToolbar)));
|
|
83
85
|
}
|
|
84
86
|
export { TextareaWithActions };
|
|
@@ -8,8 +8,8 @@ export interface TextareaProps extends Omit<ComponentProps<'textarea'>, 'size'>
|
|
|
8
8
|
maxCharCount?: number;
|
|
9
9
|
}
|
|
10
10
|
export interface TextareaWithActionsProps extends TextareaProps {
|
|
11
|
-
leftToolbar?: ReactElement;
|
|
12
|
-
rightToolbar?: ReactElement;
|
|
11
|
+
leftToolbar?: ReactElement | (() => ReactElement);
|
|
12
|
+
rightToolbar?: ReactElement | (() => ReactElement);
|
|
13
13
|
}
|
|
14
14
|
export interface RightToolbarButtonsProps {
|
|
15
15
|
size?: FormSizeType;
|
|
@@ -64,7 +64,8 @@ function WithTooltip(_ref5) {
|
|
|
64
64
|
delayDuration = _ref5.delayDuration,
|
|
65
65
|
open = _ref5.open,
|
|
66
66
|
otherProps = _objectWithoutProperties(_ref5, _excluded3);
|
|
67
|
-
|
|
67
|
+
var resolvedContent = typeof content === 'function' ? content() : content;
|
|
68
|
+
if (!resolvedContent) return children;
|
|
68
69
|
return /*#__PURE__*/React.createElement(Tooltip, {
|
|
69
70
|
open: open,
|
|
70
71
|
delayDuration: delayDuration || 700
|
|
@@ -72,6 +73,6 @@ function WithTooltip(_ref5) {
|
|
|
72
73
|
asChild: true
|
|
73
74
|
}, children), /*#__PURE__*/React.createElement(TooltipContent, _extends({
|
|
74
75
|
variant: variant
|
|
75
|
-
}, otherProps),
|
|
76
|
+
}, otherProps), resolvedContent));
|
|
76
77
|
}
|
|
77
78
|
export { Tooltip, TooltipTrigger, TooltipContent, TooltipProvider, WithTooltip };
|
|
@@ -5,7 +5,7 @@ export type TooltipVariantType = (typeof TooltipVariant)[keyof typeof TooltipVar
|
|
|
5
5
|
export interface WithTooltipProps extends Omit<ComponentProps<typeof TooltipPrimitive.Content>, 'content'> {
|
|
6
6
|
children: ReactElement;
|
|
7
7
|
open?: boolean;
|
|
8
|
-
content?: ReactElement | undefined | string;
|
|
8
|
+
content?: ReactElement | undefined | string | (() => ReactElement);
|
|
9
9
|
variant?: TooltipVariantType;
|
|
10
10
|
delayDuration?: number;
|
|
11
11
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copies text to the clipboard with a fallback for environments where the
|
|
3
|
+
* Clipboard API is unavailable (iframes without `clipboard-write` permissions,
|
|
4
|
+
* HTTP contexts, older browsers).
|
|
5
|
+
*
|
|
6
|
+
* @returns `true` on success, `false` on failure. Note: the `execCommand`
|
|
7
|
+
* fallback path may return `true` unreliably in some browsers — do not rely
|
|
8
|
+
* on it for critical confirmations.
|
|
9
|
+
*/
|
|
10
|
+
export declare function copyToClipboard(text: string): Promise<boolean>;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
|
|
2
|
+
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
3
|
+
/**
|
|
4
|
+
* Copies text to the clipboard with a fallback for environments where the
|
|
5
|
+
* Clipboard API is unavailable (iframes without `clipboard-write` permissions,
|
|
6
|
+
* HTTP contexts, older browsers).
|
|
7
|
+
*
|
|
8
|
+
* @returns `true` on success, `false` on failure. Note: the `execCommand`
|
|
9
|
+
* fallback path may return `true` unreliably in some browsers — do not rely
|
|
10
|
+
* on it for critical confirmations.
|
|
11
|
+
*/
|
|
12
|
+
export function copyToClipboard(_x) {
|
|
13
|
+
return _copyToClipboard.apply(this, arguments);
|
|
14
|
+
}
|
|
15
|
+
function _copyToClipboard() {
|
|
16
|
+
_copyToClipboard = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(text) {
|
|
17
|
+
var _navigator$clipboard;
|
|
18
|
+
var textarea;
|
|
19
|
+
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
20
|
+
while (1) switch (_context.prev = _context.next) {
|
|
21
|
+
case 0:
|
|
22
|
+
if (!(typeof navigator !== 'undefined' && (_navigator$clipboard = navigator.clipboard) !== null && _navigator$clipboard !== void 0 && _navigator$clipboard.writeText)) {
|
|
23
|
+
_context.next = 9;
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
_context.prev = 1;
|
|
27
|
+
_context.next = 4;
|
|
28
|
+
return navigator.clipboard.writeText(text);
|
|
29
|
+
case 4:
|
|
30
|
+
return _context.abrupt("return", true);
|
|
31
|
+
case 7:
|
|
32
|
+
_context.prev = 7;
|
|
33
|
+
_context.t0 = _context["catch"](1);
|
|
34
|
+
case 9:
|
|
35
|
+
if (!(typeof document === 'undefined')) {
|
|
36
|
+
_context.next = 11;
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
return _context.abrupt("return", false);
|
|
40
|
+
case 11:
|
|
41
|
+
// Fallback for iframes, HTTP contexts, older browsers
|
|
42
|
+
textarea = document.createElement('textarea');
|
|
43
|
+
textarea.value = text;
|
|
44
|
+
textarea.style.cssText = 'position:fixed;top:0;left:0;opacity:0;pointer-events:none';
|
|
45
|
+
document.body.appendChild(textarea);
|
|
46
|
+
_context.prev = 15;
|
|
47
|
+
textarea.focus();
|
|
48
|
+
textarea.select();
|
|
49
|
+
return _context.abrupt("return", document.execCommand('copy'));
|
|
50
|
+
case 21:
|
|
51
|
+
_context.prev = 21;
|
|
52
|
+
_context.t1 = _context["catch"](15);
|
|
53
|
+
return _context.abrupt("return", false);
|
|
54
|
+
case 24:
|
|
55
|
+
_context.prev = 24;
|
|
56
|
+
document.body.removeChild(textarea);
|
|
57
|
+
return _context.finish(24);
|
|
58
|
+
case 27:
|
|
59
|
+
case "end":
|
|
60
|
+
return _context.stop();
|
|
61
|
+
}
|
|
62
|
+
}, _callee, null, [[1, 7], [15, 21, 24, 27]]);
|
|
63
|
+
}));
|
|
64
|
+
return _copyToClipboard.apply(this, arguments);
|
|
65
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
interface UseCopyToClipboardOptions {
|
|
2
|
+
copiedMessage?: string;
|
|
3
|
+
tooltipDuration?: number;
|
|
4
|
+
initialTooltipText?: string;
|
|
5
|
+
onCopy?: () => void;
|
|
6
|
+
}
|
|
7
|
+
export declare function useCopyToClipboard({ copiedMessage, tooltipDuration, initialTooltipText, onCopy, }?: UseCopyToClipboardOptions): {
|
|
8
|
+
tooltipText: string;
|
|
9
|
+
tooltipVariant: "default" | "success";
|
|
10
|
+
open: boolean;
|
|
11
|
+
handleCopy: (text: string) => Promise<void>;
|
|
12
|
+
};
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
|
|
2
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
3
|
+
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
4
|
+
import { copyToClipboard } from '@scaleflex/ui-tw/utils/copy-to-clipboard';
|
|
5
|
+
import { useState } from 'react';
|
|
6
|
+
export function useCopyToClipboard() {
|
|
7
|
+
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
8
|
+
_ref$copiedMessage = _ref.copiedMessage,
|
|
9
|
+
copiedMessage = _ref$copiedMessage === void 0 ? 'Copied!' : _ref$copiedMessage,
|
|
10
|
+
_ref$tooltipDuration = _ref.tooltipDuration,
|
|
11
|
+
tooltipDuration = _ref$tooltipDuration === void 0 ? 2000 : _ref$tooltipDuration,
|
|
12
|
+
_ref$initialTooltipTe = _ref.initialTooltipText,
|
|
13
|
+
initialTooltipText = _ref$initialTooltipTe === void 0 ? '' : _ref$initialTooltipTe,
|
|
14
|
+
onCopy = _ref.onCopy;
|
|
15
|
+
var _useState = useState(initialTooltipText),
|
|
16
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
17
|
+
tooltipText = _useState2[0],
|
|
18
|
+
setTooltipText = _useState2[1];
|
|
19
|
+
var _useState3 = useState('default'),
|
|
20
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
21
|
+
tooltipVariant = _useState4[0],
|
|
22
|
+
setTooltipVariant = _useState4[1];
|
|
23
|
+
var _useState5 = useState(false),
|
|
24
|
+
_useState6 = _slicedToArray(_useState5, 2),
|
|
25
|
+
open = _useState6[0],
|
|
26
|
+
setOpen = _useState6[1];
|
|
27
|
+
var handleCopy = /*#__PURE__*/function () {
|
|
28
|
+
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(text) {
|
|
29
|
+
var success;
|
|
30
|
+
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
31
|
+
while (1) switch (_context.prev = _context.next) {
|
|
32
|
+
case 0:
|
|
33
|
+
_context.next = 2;
|
|
34
|
+
return copyToClipboard(text);
|
|
35
|
+
case 2:
|
|
36
|
+
success = _context.sent;
|
|
37
|
+
if (success) {
|
|
38
|
+
setTooltipText(copiedMessage);
|
|
39
|
+
setTooltipVariant('success');
|
|
40
|
+
setOpen(true);
|
|
41
|
+
onCopy === null || onCopy === void 0 || onCopy();
|
|
42
|
+
setTimeout(function () {
|
|
43
|
+
setOpen(false);
|
|
44
|
+
}, tooltipDuration - 500);
|
|
45
|
+
setTimeout(function () {
|
|
46
|
+
setTooltipText(initialTooltipText);
|
|
47
|
+
setTooltipVariant('default');
|
|
48
|
+
}, tooltipDuration);
|
|
49
|
+
} else {
|
|
50
|
+
console.error('Failed to copy');
|
|
51
|
+
}
|
|
52
|
+
case 4:
|
|
53
|
+
case "end":
|
|
54
|
+
return _context.stop();
|
|
55
|
+
}
|
|
56
|
+
}, _callee);
|
|
57
|
+
}));
|
|
58
|
+
return function handleCopy(_x) {
|
|
59
|
+
return _ref2.apply(this, arguments);
|
|
60
|
+
};
|
|
61
|
+
}();
|
|
62
|
+
return {
|
|
63
|
+
tooltipText: tooltipText,
|
|
64
|
+
tooltipVariant: tooltipVariant,
|
|
65
|
+
open: open,
|
|
66
|
+
handleCopy: handleCopy
|
|
67
|
+
};
|
|
68
|
+
}
|