@zag-js/clipboard 0.82.2 → 1.0.1
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/dist/index.d.mts +35 -23
- package/dist/index.d.ts +35 -23
- package/dist/index.js +91 -72
- package/dist/index.mjs +93 -74
- package/package.json +14 -9
package/dist/index.d.mts
CHANGED
|
@@ -1,27 +1,39 @@
|
|
|
1
1
|
import * as _zag_js_anatomy from '@zag-js/anatomy';
|
|
2
|
-
import { RequiredBy, PropTypes,
|
|
2
|
+
import { CommonProperties, RequiredBy, PropTypes, NormalizeProps } from '@zag-js/types';
|
|
3
3
|
import * as _zag_js_core from '@zag-js/core';
|
|
4
|
-
import {
|
|
4
|
+
import { Service, Machine } from '@zag-js/core';
|
|
5
5
|
|
|
6
6
|
declare const anatomy: _zag_js_anatomy.AnatomyInstance<"root" | "control" | "trigger" | "indicator" | "input" | "label">;
|
|
7
7
|
|
|
8
8
|
interface CopyStatusDetails {
|
|
9
9
|
copied: boolean;
|
|
10
10
|
}
|
|
11
|
+
interface ValueChangeDetails {
|
|
12
|
+
value: string;
|
|
13
|
+
}
|
|
11
14
|
type ElementIds = Partial<{
|
|
12
15
|
root: string;
|
|
13
16
|
input: string;
|
|
14
17
|
label: string;
|
|
15
18
|
}>;
|
|
16
|
-
interface
|
|
19
|
+
interface ClipboardProps extends CommonProperties {
|
|
17
20
|
/**
|
|
18
21
|
* The ids of the elements in the clipboard. Useful for composition.
|
|
19
22
|
*/
|
|
20
23
|
ids?: ElementIds | undefined;
|
|
21
24
|
/**
|
|
22
|
-
* The value
|
|
25
|
+
* The controlled value of the clipboard
|
|
23
26
|
*/
|
|
24
|
-
value
|
|
27
|
+
value?: string | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* The initial value to be copied to the clipboard when rendered.
|
|
30
|
+
* Use when you don't need to control the value of the clipboard.
|
|
31
|
+
*/
|
|
32
|
+
defaultValue?: string | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* The function to be called when the value changes
|
|
35
|
+
*/
|
|
36
|
+
onValueChange?: ((details: ValueChangeDetails) => void) | undefined;
|
|
25
37
|
/**
|
|
26
38
|
* The function to be called when the value is copied to the clipboard
|
|
27
39
|
*/
|
|
@@ -30,24 +42,24 @@ interface PublicContext extends CommonProperties {
|
|
|
30
42
|
* The timeout for the copy operation
|
|
31
43
|
* @default 3000
|
|
32
44
|
*/
|
|
33
|
-
timeout
|
|
34
|
-
}
|
|
35
|
-
interface PrivateContext {
|
|
36
|
-
}
|
|
37
|
-
type ComputedContext = Readonly<{}>;
|
|
38
|
-
type UserDefinedContext = RequiredBy<PublicContext, "id">;
|
|
39
|
-
interface MachineContext extends PublicContext, PrivateContext, ComputedContext {
|
|
45
|
+
timeout?: number | undefined;
|
|
40
46
|
}
|
|
41
|
-
interface
|
|
42
|
-
|
|
47
|
+
interface ClipboardSchema {
|
|
48
|
+
state: "idle" | "copied";
|
|
49
|
+
props: RequiredBy<ClipboardProps, "timeout">;
|
|
50
|
+
context: {
|
|
51
|
+
value: string;
|
|
52
|
+
};
|
|
53
|
+
effect: string;
|
|
54
|
+
action: string;
|
|
55
|
+
guard: string;
|
|
43
56
|
}
|
|
44
|
-
type
|
|
45
|
-
type
|
|
46
|
-
type Service = Machine<MachineContext, MachineState, StateMachine.AnyEventObject>;
|
|
57
|
+
type ClipboardService = Service<ClipboardSchema>;
|
|
58
|
+
type ClipboardMachine = Machine<ClipboardSchema>;
|
|
47
59
|
interface IndicatorProps {
|
|
48
60
|
copied: boolean;
|
|
49
61
|
}
|
|
50
|
-
interface
|
|
62
|
+
interface ClipboardApi<T extends PropTypes = PropTypes> {
|
|
51
63
|
/**
|
|
52
64
|
* Whether the value has been copied to the clipboard
|
|
53
65
|
*/
|
|
@@ -72,13 +84,13 @@ interface MachineApi<T extends PropTypes = PropTypes> {
|
|
|
72
84
|
getIndicatorProps(props: IndicatorProps): T["element"];
|
|
73
85
|
}
|
|
74
86
|
|
|
75
|
-
declare function connect<T extends PropTypes>(
|
|
87
|
+
declare function connect<T extends PropTypes>(service: ClipboardService, normalize: NormalizeProps<T>): ClipboardApi<T>;
|
|
76
88
|
|
|
77
|
-
declare
|
|
89
|
+
declare const machine: _zag_js_core.Machine<ClipboardSchema>;
|
|
78
90
|
|
|
79
|
-
declare const props: (
|
|
80
|
-
declare const contextProps: <Props extends
|
|
91
|
+
declare const props: (keyof ClipboardProps)[];
|
|
92
|
+
declare const contextProps: <Props extends ClipboardProps>(props: Props) => [ClipboardProps, Omit<Props, keyof ClipboardProps>];
|
|
81
93
|
declare const indicatorProps: "copied"[];
|
|
82
94
|
declare const splitIndicatorProps: <Props extends IndicatorProps>(props: Props) => [IndicatorProps, Omit<Props, "copied">];
|
|
83
95
|
|
|
84
|
-
export { type
|
|
96
|
+
export { type ClipboardApi as Api, type CopyStatusDetails, type ElementIds, type IndicatorProps, type ClipboardMachine as Machine, type ClipboardProps as Props, type ClipboardSchema as Schema, type ClipboardService as Service, anatomy, connect, contextProps, indicatorProps, machine, props, splitIndicatorProps };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,27 +1,39 @@
|
|
|
1
1
|
import * as _zag_js_anatomy from '@zag-js/anatomy';
|
|
2
|
-
import { RequiredBy, PropTypes,
|
|
2
|
+
import { CommonProperties, RequiredBy, PropTypes, NormalizeProps } from '@zag-js/types';
|
|
3
3
|
import * as _zag_js_core from '@zag-js/core';
|
|
4
|
-
import {
|
|
4
|
+
import { Service, Machine } from '@zag-js/core';
|
|
5
5
|
|
|
6
6
|
declare const anatomy: _zag_js_anatomy.AnatomyInstance<"root" | "control" | "trigger" | "indicator" | "input" | "label">;
|
|
7
7
|
|
|
8
8
|
interface CopyStatusDetails {
|
|
9
9
|
copied: boolean;
|
|
10
10
|
}
|
|
11
|
+
interface ValueChangeDetails {
|
|
12
|
+
value: string;
|
|
13
|
+
}
|
|
11
14
|
type ElementIds = Partial<{
|
|
12
15
|
root: string;
|
|
13
16
|
input: string;
|
|
14
17
|
label: string;
|
|
15
18
|
}>;
|
|
16
|
-
interface
|
|
19
|
+
interface ClipboardProps extends CommonProperties {
|
|
17
20
|
/**
|
|
18
21
|
* The ids of the elements in the clipboard. Useful for composition.
|
|
19
22
|
*/
|
|
20
23
|
ids?: ElementIds | undefined;
|
|
21
24
|
/**
|
|
22
|
-
* The value
|
|
25
|
+
* The controlled value of the clipboard
|
|
23
26
|
*/
|
|
24
|
-
value
|
|
27
|
+
value?: string | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* The initial value to be copied to the clipboard when rendered.
|
|
30
|
+
* Use when you don't need to control the value of the clipboard.
|
|
31
|
+
*/
|
|
32
|
+
defaultValue?: string | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* The function to be called when the value changes
|
|
35
|
+
*/
|
|
36
|
+
onValueChange?: ((details: ValueChangeDetails) => void) | undefined;
|
|
25
37
|
/**
|
|
26
38
|
* The function to be called when the value is copied to the clipboard
|
|
27
39
|
*/
|
|
@@ -30,24 +42,24 @@ interface PublicContext extends CommonProperties {
|
|
|
30
42
|
* The timeout for the copy operation
|
|
31
43
|
* @default 3000
|
|
32
44
|
*/
|
|
33
|
-
timeout
|
|
34
|
-
}
|
|
35
|
-
interface PrivateContext {
|
|
36
|
-
}
|
|
37
|
-
type ComputedContext = Readonly<{}>;
|
|
38
|
-
type UserDefinedContext = RequiredBy<PublicContext, "id">;
|
|
39
|
-
interface MachineContext extends PublicContext, PrivateContext, ComputedContext {
|
|
45
|
+
timeout?: number | undefined;
|
|
40
46
|
}
|
|
41
|
-
interface
|
|
42
|
-
|
|
47
|
+
interface ClipboardSchema {
|
|
48
|
+
state: "idle" | "copied";
|
|
49
|
+
props: RequiredBy<ClipboardProps, "timeout">;
|
|
50
|
+
context: {
|
|
51
|
+
value: string;
|
|
52
|
+
};
|
|
53
|
+
effect: string;
|
|
54
|
+
action: string;
|
|
55
|
+
guard: string;
|
|
43
56
|
}
|
|
44
|
-
type
|
|
45
|
-
type
|
|
46
|
-
type Service = Machine<MachineContext, MachineState, StateMachine.AnyEventObject>;
|
|
57
|
+
type ClipboardService = Service<ClipboardSchema>;
|
|
58
|
+
type ClipboardMachine = Machine<ClipboardSchema>;
|
|
47
59
|
interface IndicatorProps {
|
|
48
60
|
copied: boolean;
|
|
49
61
|
}
|
|
50
|
-
interface
|
|
62
|
+
interface ClipboardApi<T extends PropTypes = PropTypes> {
|
|
51
63
|
/**
|
|
52
64
|
* Whether the value has been copied to the clipboard
|
|
53
65
|
*/
|
|
@@ -72,13 +84,13 @@ interface MachineApi<T extends PropTypes = PropTypes> {
|
|
|
72
84
|
getIndicatorProps(props: IndicatorProps): T["element"];
|
|
73
85
|
}
|
|
74
86
|
|
|
75
|
-
declare function connect<T extends PropTypes>(
|
|
87
|
+
declare function connect<T extends PropTypes>(service: ClipboardService, normalize: NormalizeProps<T>): ClipboardApi<T>;
|
|
76
88
|
|
|
77
|
-
declare
|
|
89
|
+
declare const machine: _zag_js_core.Machine<ClipboardSchema>;
|
|
78
90
|
|
|
79
|
-
declare const props: (
|
|
80
|
-
declare const contextProps: <Props extends
|
|
91
|
+
declare const props: (keyof ClipboardProps)[];
|
|
92
|
+
declare const contextProps: <Props extends ClipboardProps>(props: Props) => [ClipboardProps, Omit<Props, keyof ClipboardProps>];
|
|
81
93
|
declare const indicatorProps: "copied"[];
|
|
82
94
|
declare const splitIndicatorProps: <Props extends IndicatorProps>(props: Props) => [IndicatorProps, Omit<Props, "copied">];
|
|
83
95
|
|
|
84
|
-
export { type
|
|
96
|
+
export { type ClipboardApi as Api, type CopyStatusDetails, type ElementIds, type IndicatorProps, type ClipboardMachine as Machine, type ClipboardProps as Props, type ClipboardSchema as Schema, type ClipboardService as Service, anatomy, connect, contextProps, indicatorProps, machine, props, splitIndicatorProps };
|
package/dist/index.js
CHANGED
|
@@ -9,13 +9,11 @@ var types = require('@zag-js/types');
|
|
|
9
9
|
// src/clipboard.anatomy.ts
|
|
10
10
|
var anatomy = anatomy$1.createAnatomy("clipboard").parts("root", "control", "trigger", "indicator", "input", "label");
|
|
11
11
|
var parts = anatomy.build();
|
|
12
|
-
var
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
writeToClipboard: (ctx) => copyText(dom.getDoc(ctx), ctx.value)
|
|
18
|
-
});
|
|
12
|
+
var getRootId = (ctx) => ctx.ids?.root ?? `clip:${ctx.id}`;
|
|
13
|
+
var getInputId = (ctx) => ctx.ids?.input ?? `clip:${ctx.id}:input`;
|
|
14
|
+
var getLabelId = (ctx) => ctx.ids?.label ?? `clip:${ctx.id}:label`;
|
|
15
|
+
var getInputEl = (ctx) => ctx.getById(getInputId(ctx));
|
|
16
|
+
var writeToClipboard = (ctx, value) => copyText(ctx.getDoc(), value);
|
|
19
17
|
function createNode(doc, text) {
|
|
20
18
|
const node = doc.createElement("pre");
|
|
21
19
|
Object.assign(node.style, {
|
|
@@ -58,11 +56,12 @@ function copyText(doc, text) {
|
|
|
58
56
|
}
|
|
59
57
|
|
|
60
58
|
// src/clipboard.connect.ts
|
|
61
|
-
function connect(
|
|
59
|
+
function connect(service, normalize) {
|
|
60
|
+
const { state, send, context, scope } = service;
|
|
62
61
|
const copied = state.matches("copied");
|
|
63
62
|
return {
|
|
64
63
|
copied,
|
|
65
|
-
value:
|
|
64
|
+
value: context.get("value"),
|
|
66
65
|
setValue(value) {
|
|
67
66
|
send({ type: "VALUE.SET", value });
|
|
68
67
|
},
|
|
@@ -73,15 +72,15 @@ function connect(state, send, normalize) {
|
|
|
73
72
|
return normalize.element({
|
|
74
73
|
...parts.root.attrs,
|
|
75
74
|
"data-copied": domQuery.dataAttr(copied),
|
|
76
|
-
id:
|
|
75
|
+
id: getRootId(scope)
|
|
77
76
|
});
|
|
78
77
|
},
|
|
79
78
|
getLabelProps() {
|
|
80
79
|
return normalize.label({
|
|
81
80
|
...parts.label.attrs,
|
|
82
|
-
htmlFor:
|
|
81
|
+
htmlFor: getInputId(scope),
|
|
83
82
|
"data-copied": domQuery.dataAttr(copied),
|
|
84
|
-
id:
|
|
83
|
+
id: getLabelId(scope)
|
|
85
84
|
});
|
|
86
85
|
},
|
|
87
86
|
getControlProps() {
|
|
@@ -93,11 +92,11 @@ function connect(state, send, normalize) {
|
|
|
93
92
|
getInputProps() {
|
|
94
93
|
return normalize.input({
|
|
95
94
|
...parts.input.attrs,
|
|
96
|
-
defaultValue:
|
|
95
|
+
defaultValue: context.get("value"),
|
|
97
96
|
"data-copied": domQuery.dataAttr(copied),
|
|
98
97
|
readOnly: true,
|
|
99
98
|
"data-readonly": "true",
|
|
100
|
-
id:
|
|
99
|
+
id: getInputId(scope),
|
|
101
100
|
onFocus(event) {
|
|
102
101
|
event.currentTarget.select();
|
|
103
102
|
},
|
|
@@ -125,82 +124,102 @@ function connect(state, send, normalize) {
|
|
|
125
124
|
}
|
|
126
125
|
};
|
|
127
126
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
127
|
+
var machine = core.createMachine({
|
|
128
|
+
props({ props: props2 }) {
|
|
129
|
+
return {
|
|
130
|
+
timeout: 3e3,
|
|
131
|
+
defaultValue: "",
|
|
132
|
+
...props2
|
|
133
|
+
};
|
|
134
|
+
},
|
|
135
|
+
initialState() {
|
|
136
|
+
return "idle";
|
|
137
|
+
},
|
|
138
|
+
context({ prop, bindable }) {
|
|
139
|
+
return {
|
|
140
|
+
value: bindable(() => ({
|
|
141
|
+
defaultValue: prop("defaultValue"),
|
|
142
|
+
value: prop("value"),
|
|
143
|
+
onChange(value) {
|
|
144
|
+
prop("onValueChange")?.({ value });
|
|
145
|
+
}
|
|
146
|
+
}))
|
|
147
|
+
};
|
|
148
|
+
},
|
|
149
|
+
watch({ track, context, action }) {
|
|
150
|
+
track([() => context.get("value")], () => {
|
|
151
|
+
action(["syncInputElement"]);
|
|
152
|
+
});
|
|
153
|
+
},
|
|
154
|
+
on: {
|
|
155
|
+
"VALUE.SET": {
|
|
156
|
+
actions: ["setValue"]
|
|
157
|
+
},
|
|
158
|
+
COPY: {
|
|
159
|
+
target: "copied",
|
|
160
|
+
actions: ["copyToClipboard", "invokeOnCopy"]
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
states: {
|
|
164
|
+
idle: {
|
|
142
165
|
on: {
|
|
143
|
-
"
|
|
144
|
-
|
|
166
|
+
"INPUT.COPY": {
|
|
167
|
+
target: "copied",
|
|
168
|
+
actions: ["invokeOnCopy"]
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
copied: {
|
|
173
|
+
effects: ["waitForTimeout"],
|
|
174
|
+
on: {
|
|
175
|
+
"COPY.DONE": {
|
|
176
|
+
target: "idle"
|
|
145
177
|
},
|
|
146
178
|
COPY: {
|
|
147
179
|
target: "copied",
|
|
148
180
|
actions: ["copyToClipboard", "invokeOnCopy"]
|
|
149
|
-
}
|
|
150
|
-
},
|
|
151
|
-
states: {
|
|
152
|
-
idle: {
|
|
153
|
-
on: {
|
|
154
|
-
"INPUT.COPY": {
|
|
155
|
-
target: "copied",
|
|
156
|
-
actions: ["invokeOnCopy"]
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
181
|
},
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
COPY_TIMEOUT: "idle"
|
|
163
|
-
},
|
|
164
|
-
on: {
|
|
165
|
-
COPY: {
|
|
166
|
-
target: "copied",
|
|
167
|
-
actions: ["copyToClipboard", "invokeOnCopy"]
|
|
168
|
-
},
|
|
169
|
-
"INPUT.COPY": {
|
|
170
|
-
actions: ["invokeOnCopy"]
|
|
171
|
-
}
|
|
172
|
-
}
|
|
182
|
+
"INPUT.COPY": {
|
|
183
|
+
actions: ["invokeOnCopy"]
|
|
173
184
|
}
|
|
174
185
|
}
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
implementations: {
|
|
189
|
+
effects: {
|
|
190
|
+
waitForTimeout({ prop, send }) {
|
|
191
|
+
return utils.setRafTimeout(() => {
|
|
192
|
+
send({ type: "COPY.DONE" });
|
|
193
|
+
}, prop("timeout"));
|
|
194
|
+
}
|
|
175
195
|
},
|
|
176
|
-
{
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
ctx2.onStatusChange?.({ copied: true });
|
|
186
|
-
},
|
|
187
|
-
syncInputElement(ctx2) {
|
|
188
|
-
dom.setValue(dom.getInputEl(ctx2), ctx2.value);
|
|
189
|
-
}
|
|
196
|
+
actions: {
|
|
197
|
+
setValue({ context, event }) {
|
|
198
|
+
context.set("value", event.value);
|
|
199
|
+
},
|
|
200
|
+
copyToClipboard({ context, scope }) {
|
|
201
|
+
writeToClipboard(scope, context.get("value"));
|
|
202
|
+
},
|
|
203
|
+
invokeOnCopy({ prop }) {
|
|
204
|
+
prop("onStatusChange")?.({ copied: true });
|
|
190
205
|
},
|
|
191
|
-
|
|
192
|
-
|
|
206
|
+
syncInputElement({ context, scope }) {
|
|
207
|
+
const inputEl = getInputEl(scope);
|
|
208
|
+
if (!inputEl) return;
|
|
209
|
+
domQuery.setElementValue(inputEl, context.get("value"));
|
|
193
210
|
}
|
|
194
211
|
}
|
|
195
|
-
|
|
196
|
-
}
|
|
212
|
+
}
|
|
213
|
+
});
|
|
197
214
|
var props = types.createProps()([
|
|
198
215
|
"getRootNode",
|
|
199
216
|
"id",
|
|
200
217
|
"ids",
|
|
201
218
|
"value",
|
|
219
|
+
"defaultValue",
|
|
202
220
|
"timeout",
|
|
203
|
-
"onStatusChange"
|
|
221
|
+
"onStatusChange",
|
|
222
|
+
"onValueChange"
|
|
204
223
|
]);
|
|
205
224
|
var contextProps = utils.createSplitProps(props);
|
|
206
225
|
var indicatorProps = types.createProps()(["copied"]);
|
package/dist/index.mjs
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
import { createAnatomy } from '@zag-js/anatomy';
|
|
2
|
-
import {
|
|
2
|
+
import { setElementValue, dataAttr, getWindow } from '@zag-js/dom-query';
|
|
3
3
|
import { createMachine } from '@zag-js/core';
|
|
4
|
-
import {
|
|
4
|
+
import { setRafTimeout, createSplitProps } from '@zag-js/utils';
|
|
5
5
|
import { createProps } from '@zag-js/types';
|
|
6
6
|
|
|
7
7
|
// src/clipboard.anatomy.ts
|
|
8
8
|
var anatomy = createAnatomy("clipboard").parts("root", "control", "trigger", "indicator", "input", "label");
|
|
9
9
|
var parts = anatomy.build();
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
writeToClipboard: (ctx) => copyText(dom.getDoc(ctx), ctx.value)
|
|
16
|
-
});
|
|
10
|
+
var getRootId = (ctx) => ctx.ids?.root ?? `clip:${ctx.id}`;
|
|
11
|
+
var getInputId = (ctx) => ctx.ids?.input ?? `clip:${ctx.id}:input`;
|
|
12
|
+
var getLabelId = (ctx) => ctx.ids?.label ?? `clip:${ctx.id}:label`;
|
|
13
|
+
var getInputEl = (ctx) => ctx.getById(getInputId(ctx));
|
|
14
|
+
var writeToClipboard = (ctx, value) => copyText(ctx.getDoc(), value);
|
|
17
15
|
function createNode(doc, text) {
|
|
18
16
|
const node = doc.createElement("pre");
|
|
19
17
|
Object.assign(node.style, {
|
|
@@ -56,11 +54,12 @@ function copyText(doc, text) {
|
|
|
56
54
|
}
|
|
57
55
|
|
|
58
56
|
// src/clipboard.connect.ts
|
|
59
|
-
function connect(
|
|
57
|
+
function connect(service, normalize) {
|
|
58
|
+
const { state, send, context, scope } = service;
|
|
60
59
|
const copied = state.matches("copied");
|
|
61
60
|
return {
|
|
62
61
|
copied,
|
|
63
|
-
value:
|
|
62
|
+
value: context.get("value"),
|
|
64
63
|
setValue(value) {
|
|
65
64
|
send({ type: "VALUE.SET", value });
|
|
66
65
|
},
|
|
@@ -71,15 +70,15 @@ function connect(state, send, normalize) {
|
|
|
71
70
|
return normalize.element({
|
|
72
71
|
...parts.root.attrs,
|
|
73
72
|
"data-copied": dataAttr(copied),
|
|
74
|
-
id:
|
|
73
|
+
id: getRootId(scope)
|
|
75
74
|
});
|
|
76
75
|
},
|
|
77
76
|
getLabelProps() {
|
|
78
77
|
return normalize.label({
|
|
79
78
|
...parts.label.attrs,
|
|
80
|
-
htmlFor:
|
|
79
|
+
htmlFor: getInputId(scope),
|
|
81
80
|
"data-copied": dataAttr(copied),
|
|
82
|
-
id:
|
|
81
|
+
id: getLabelId(scope)
|
|
83
82
|
});
|
|
84
83
|
},
|
|
85
84
|
getControlProps() {
|
|
@@ -91,11 +90,11 @@ function connect(state, send, normalize) {
|
|
|
91
90
|
getInputProps() {
|
|
92
91
|
return normalize.input({
|
|
93
92
|
...parts.input.attrs,
|
|
94
|
-
defaultValue:
|
|
93
|
+
defaultValue: context.get("value"),
|
|
95
94
|
"data-copied": dataAttr(copied),
|
|
96
95
|
readOnly: true,
|
|
97
96
|
"data-readonly": "true",
|
|
98
|
-
id:
|
|
97
|
+
id: getInputId(scope),
|
|
99
98
|
onFocus(event) {
|
|
100
99
|
event.currentTarget.select();
|
|
101
100
|
},
|
|
@@ -123,82 +122,102 @@ function connect(state, send, normalize) {
|
|
|
123
122
|
}
|
|
124
123
|
};
|
|
125
124
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
125
|
+
var machine = createMachine({
|
|
126
|
+
props({ props: props2 }) {
|
|
127
|
+
return {
|
|
128
|
+
timeout: 3e3,
|
|
129
|
+
defaultValue: "",
|
|
130
|
+
...props2
|
|
131
|
+
};
|
|
132
|
+
},
|
|
133
|
+
initialState() {
|
|
134
|
+
return "idle";
|
|
135
|
+
},
|
|
136
|
+
context({ prop, bindable }) {
|
|
137
|
+
return {
|
|
138
|
+
value: bindable(() => ({
|
|
139
|
+
defaultValue: prop("defaultValue"),
|
|
140
|
+
value: prop("value"),
|
|
141
|
+
onChange(value) {
|
|
142
|
+
prop("onValueChange")?.({ value });
|
|
143
|
+
}
|
|
144
|
+
}))
|
|
145
|
+
};
|
|
146
|
+
},
|
|
147
|
+
watch({ track, context, action }) {
|
|
148
|
+
track([() => context.get("value")], () => {
|
|
149
|
+
action(["syncInputElement"]);
|
|
150
|
+
});
|
|
151
|
+
},
|
|
152
|
+
on: {
|
|
153
|
+
"VALUE.SET": {
|
|
154
|
+
actions: ["setValue"]
|
|
155
|
+
},
|
|
156
|
+
COPY: {
|
|
157
|
+
target: "copied",
|
|
158
|
+
actions: ["copyToClipboard", "invokeOnCopy"]
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
states: {
|
|
162
|
+
idle: {
|
|
140
163
|
on: {
|
|
141
|
-
"
|
|
142
|
-
|
|
164
|
+
"INPUT.COPY": {
|
|
165
|
+
target: "copied",
|
|
166
|
+
actions: ["invokeOnCopy"]
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
copied: {
|
|
171
|
+
effects: ["waitForTimeout"],
|
|
172
|
+
on: {
|
|
173
|
+
"COPY.DONE": {
|
|
174
|
+
target: "idle"
|
|
143
175
|
},
|
|
144
176
|
COPY: {
|
|
145
177
|
target: "copied",
|
|
146
178
|
actions: ["copyToClipboard", "invokeOnCopy"]
|
|
147
|
-
}
|
|
148
|
-
},
|
|
149
|
-
states: {
|
|
150
|
-
idle: {
|
|
151
|
-
on: {
|
|
152
|
-
"INPUT.COPY": {
|
|
153
|
-
target: "copied",
|
|
154
|
-
actions: ["invokeOnCopy"]
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
179
|
},
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
COPY_TIMEOUT: "idle"
|
|
161
|
-
},
|
|
162
|
-
on: {
|
|
163
|
-
COPY: {
|
|
164
|
-
target: "copied",
|
|
165
|
-
actions: ["copyToClipboard", "invokeOnCopy"]
|
|
166
|
-
},
|
|
167
|
-
"INPUT.COPY": {
|
|
168
|
-
actions: ["invokeOnCopy"]
|
|
169
|
-
}
|
|
170
|
-
}
|
|
180
|
+
"INPUT.COPY": {
|
|
181
|
+
actions: ["invokeOnCopy"]
|
|
171
182
|
}
|
|
172
183
|
}
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
implementations: {
|
|
187
|
+
effects: {
|
|
188
|
+
waitForTimeout({ prop, send }) {
|
|
189
|
+
return setRafTimeout(() => {
|
|
190
|
+
send({ type: "COPY.DONE" });
|
|
191
|
+
}, prop("timeout"));
|
|
192
|
+
}
|
|
173
193
|
},
|
|
174
|
-
{
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
ctx2.onStatusChange?.({ copied: true });
|
|
184
|
-
},
|
|
185
|
-
syncInputElement(ctx2) {
|
|
186
|
-
dom.setValue(dom.getInputEl(ctx2), ctx2.value);
|
|
187
|
-
}
|
|
194
|
+
actions: {
|
|
195
|
+
setValue({ context, event }) {
|
|
196
|
+
context.set("value", event.value);
|
|
197
|
+
},
|
|
198
|
+
copyToClipboard({ context, scope }) {
|
|
199
|
+
writeToClipboard(scope, context.get("value"));
|
|
200
|
+
},
|
|
201
|
+
invokeOnCopy({ prop }) {
|
|
202
|
+
prop("onStatusChange")?.({ copied: true });
|
|
188
203
|
},
|
|
189
|
-
|
|
190
|
-
|
|
204
|
+
syncInputElement({ context, scope }) {
|
|
205
|
+
const inputEl = getInputEl(scope);
|
|
206
|
+
if (!inputEl) return;
|
|
207
|
+
setElementValue(inputEl, context.get("value"));
|
|
191
208
|
}
|
|
192
209
|
}
|
|
193
|
-
|
|
194
|
-
}
|
|
210
|
+
}
|
|
211
|
+
});
|
|
195
212
|
var props = createProps()([
|
|
196
213
|
"getRootNode",
|
|
197
214
|
"id",
|
|
198
215
|
"ids",
|
|
199
216
|
"value",
|
|
217
|
+
"defaultValue",
|
|
200
218
|
"timeout",
|
|
201
|
-
"onStatusChange"
|
|
219
|
+
"onStatusChange",
|
|
220
|
+
"onValueChange"
|
|
202
221
|
]);
|
|
203
222
|
var contextProps = createSplitProps(props);
|
|
204
223
|
var indicatorProps = createProps()(["copied"]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/clipboard",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Core logic for the clipboard widget implemented as a state machine",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"js",
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
"url": "https://github.com/chakra-ui/zag/issues"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@zag-js/anatomy": "0.
|
|
31
|
-
"@zag-js/core": "0.
|
|
32
|
-
"@zag-js/dom-query": "0.
|
|
33
|
-
"@zag-js/utils": "0.
|
|
34
|
-
"@zag-js/types": "0.
|
|
30
|
+
"@zag-js/anatomy": "1.0.1",
|
|
31
|
+
"@zag-js/core": "1.0.1",
|
|
32
|
+
"@zag-js/dom-query": "1.0.1",
|
|
33
|
+
"@zag-js/utils": "1.0.1",
|
|
34
|
+
"@zag-js/types": "1.0.1"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"clean-package": "2.2.0"
|
|
@@ -41,9 +41,14 @@
|
|
|
41
41
|
"types": "dist/index.d.ts",
|
|
42
42
|
"exports": {
|
|
43
43
|
".": {
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
"import": {
|
|
45
|
+
"types": "./dist/index.d.mts",
|
|
46
|
+
"default": "./dist/index.mjs"
|
|
47
|
+
},
|
|
48
|
+
"require": {
|
|
49
|
+
"types": "./dist/index.d.ts",
|
|
50
|
+
"default": "./dist/index.js"
|
|
51
|
+
}
|
|
47
52
|
},
|
|
48
53
|
"./package.json": "./package.json"
|
|
49
54
|
},
|