@zag-js/clipboard 0.0.0-dev-20240202223441
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/LICENSE +21 -0
- package/README.md +19 -0
- package/dist/index.d.mts +65 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.js +177 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +148 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +56 -0
- package/src/clipboard.anatomy.ts +5 -0
- package/src/clipboard.connect.ts +29 -0
- package/src/clipboard.dom.ts +65 -0
- package/src/clipboard.machine.ts +61 -0
- package/src/clipboard.types.ts +68 -0
- package/src/index.ts +4 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Chakra UI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# @zag-js/clipboard
|
|
2
|
+
|
|
3
|
+
Core logic for the clipboard widget implemented as a state machine
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
yarn add @zag-js/clipboard
|
|
9
|
+
# or
|
|
10
|
+
npm i @zag-js/clipboard
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Contribution
|
|
14
|
+
|
|
15
|
+
Yes please! See the [contributing guidelines](https://github.com/chakra-ui/zag/blob/main/CONTRIBUTING.md) for details.
|
|
16
|
+
|
|
17
|
+
## Licence
|
|
18
|
+
|
|
19
|
+
This project is licensed under the terms of the [MIT license](https://github.com/chakra-ui/zag/blob/main/LICENSE).
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import * as _zag_js_anatomy from '@zag-js/anatomy';
|
|
2
|
+
import { RequiredBy, CommonProperties, PropTypes, Context, NormalizeProps } from '@zag-js/types';
|
|
3
|
+
import * as _zag_js_core from '@zag-js/core';
|
|
4
|
+
import { StateMachine } from '@zag-js/core';
|
|
5
|
+
|
|
6
|
+
declare const anatomy: _zag_js_anatomy.AnatomyInstance<"trigger" | "indicator">;
|
|
7
|
+
|
|
8
|
+
interface CopyStatusDetails {
|
|
9
|
+
copied: boolean;
|
|
10
|
+
}
|
|
11
|
+
interface PublicContext extends CommonProperties {
|
|
12
|
+
/**
|
|
13
|
+
* The value to be copied to the clipboard
|
|
14
|
+
*/
|
|
15
|
+
value: string;
|
|
16
|
+
/**
|
|
17
|
+
* The function to be called when the value is copied to the clipboard
|
|
18
|
+
*/
|
|
19
|
+
onCopyStatusChange?: (details: CopyStatusDetails) => void;
|
|
20
|
+
/**
|
|
21
|
+
* The timeout for the copy operation
|
|
22
|
+
*/
|
|
23
|
+
timeout: number;
|
|
24
|
+
}
|
|
25
|
+
type PrivateContext = Context<{}>;
|
|
26
|
+
type ComputedContext = Readonly<{}>;
|
|
27
|
+
type UserDefinedContext = RequiredBy<PublicContext, "id">;
|
|
28
|
+
interface MachineContext extends PublicContext, PrivateContext, ComputedContext {
|
|
29
|
+
}
|
|
30
|
+
interface MachineState {
|
|
31
|
+
value: "idle" | "copied";
|
|
32
|
+
}
|
|
33
|
+
type State = StateMachine.State<MachineContext, MachineState>;
|
|
34
|
+
type Send = StateMachine.Send<StateMachine.AnyEventObject>;
|
|
35
|
+
interface IndicatorProps {
|
|
36
|
+
copied: boolean;
|
|
37
|
+
}
|
|
38
|
+
interface MachineApi<T extends PropTypes = PropTypes> {
|
|
39
|
+
/**
|
|
40
|
+
* The value to be copied to the clipboard
|
|
41
|
+
*/
|
|
42
|
+
value: string;
|
|
43
|
+
/**
|
|
44
|
+
* Set the value to be copied to the clipboard
|
|
45
|
+
*/
|
|
46
|
+
setValue(value: string): void;
|
|
47
|
+
/**
|
|
48
|
+
* The props for the trigger button
|
|
49
|
+
*/
|
|
50
|
+
triggerProps: T["button"];
|
|
51
|
+
/**
|
|
52
|
+
* The props for the indicator
|
|
53
|
+
*/
|
|
54
|
+
getIndicatorProps(props: IndicatorProps): T["element"];
|
|
55
|
+
/**
|
|
56
|
+
* Whether the value has been copied to the clipboard
|
|
57
|
+
*/
|
|
58
|
+
isCopied: boolean;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
|
|
62
|
+
|
|
63
|
+
declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
|
|
64
|
+
|
|
65
|
+
export { type UserDefinedContext as Context, anatomy, connect, machine };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import * as _zag_js_anatomy from '@zag-js/anatomy';
|
|
2
|
+
import { RequiredBy, CommonProperties, PropTypes, Context, NormalizeProps } from '@zag-js/types';
|
|
3
|
+
import * as _zag_js_core from '@zag-js/core';
|
|
4
|
+
import { StateMachine } from '@zag-js/core';
|
|
5
|
+
|
|
6
|
+
declare const anatomy: _zag_js_anatomy.AnatomyInstance<"trigger" | "indicator">;
|
|
7
|
+
|
|
8
|
+
interface CopyStatusDetails {
|
|
9
|
+
copied: boolean;
|
|
10
|
+
}
|
|
11
|
+
interface PublicContext extends CommonProperties {
|
|
12
|
+
/**
|
|
13
|
+
* The value to be copied to the clipboard
|
|
14
|
+
*/
|
|
15
|
+
value: string;
|
|
16
|
+
/**
|
|
17
|
+
* The function to be called when the value is copied to the clipboard
|
|
18
|
+
*/
|
|
19
|
+
onCopyStatusChange?: (details: CopyStatusDetails) => void;
|
|
20
|
+
/**
|
|
21
|
+
* The timeout for the copy operation
|
|
22
|
+
*/
|
|
23
|
+
timeout: number;
|
|
24
|
+
}
|
|
25
|
+
type PrivateContext = Context<{}>;
|
|
26
|
+
type ComputedContext = Readonly<{}>;
|
|
27
|
+
type UserDefinedContext = RequiredBy<PublicContext, "id">;
|
|
28
|
+
interface MachineContext extends PublicContext, PrivateContext, ComputedContext {
|
|
29
|
+
}
|
|
30
|
+
interface MachineState {
|
|
31
|
+
value: "idle" | "copied";
|
|
32
|
+
}
|
|
33
|
+
type State = StateMachine.State<MachineContext, MachineState>;
|
|
34
|
+
type Send = StateMachine.Send<StateMachine.AnyEventObject>;
|
|
35
|
+
interface IndicatorProps {
|
|
36
|
+
copied: boolean;
|
|
37
|
+
}
|
|
38
|
+
interface MachineApi<T extends PropTypes = PropTypes> {
|
|
39
|
+
/**
|
|
40
|
+
* The value to be copied to the clipboard
|
|
41
|
+
*/
|
|
42
|
+
value: string;
|
|
43
|
+
/**
|
|
44
|
+
* Set the value to be copied to the clipboard
|
|
45
|
+
*/
|
|
46
|
+
setValue(value: string): void;
|
|
47
|
+
/**
|
|
48
|
+
* The props for the trigger button
|
|
49
|
+
*/
|
|
50
|
+
triggerProps: T["button"];
|
|
51
|
+
/**
|
|
52
|
+
* The props for the indicator
|
|
53
|
+
*/
|
|
54
|
+
getIndicatorProps(props: IndicatorProps): T["element"];
|
|
55
|
+
/**
|
|
56
|
+
* Whether the value has been copied to the clipboard
|
|
57
|
+
*/
|
|
58
|
+
isCopied: boolean;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
|
|
62
|
+
|
|
63
|
+
declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
|
|
64
|
+
|
|
65
|
+
export { type UserDefinedContext as Context, anatomy, connect, machine };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
anatomy: () => anatomy,
|
|
24
|
+
connect: () => connect,
|
|
25
|
+
machine: () => machine
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(src_exports);
|
|
28
|
+
|
|
29
|
+
// src/clipboard.anatomy.ts
|
|
30
|
+
var import_anatomy = require("@zag-js/anatomy");
|
|
31
|
+
var anatomy = (0, import_anatomy.createAnatomy)("clipboard").parts("trigger", "indicator");
|
|
32
|
+
var parts = anatomy.build();
|
|
33
|
+
|
|
34
|
+
// src/clipboard.connect.ts
|
|
35
|
+
var import_dom_query = require("@zag-js/dom-query");
|
|
36
|
+
function connect(state, send, normalize) {
|
|
37
|
+
const isCopied = state.matches("copied");
|
|
38
|
+
return {
|
|
39
|
+
isCopied,
|
|
40
|
+
value: state.context.value,
|
|
41
|
+
setValue(value) {
|
|
42
|
+
send({ type: "VALUE.SET", value });
|
|
43
|
+
},
|
|
44
|
+
triggerProps: normalize.button({
|
|
45
|
+
...parts.trigger,
|
|
46
|
+
"data-copied": (0, import_dom_query.dataAttr)(isCopied),
|
|
47
|
+
onClick() {
|
|
48
|
+
send({ type: "COPY" });
|
|
49
|
+
}
|
|
50
|
+
}),
|
|
51
|
+
getIndicatorProps(props) {
|
|
52
|
+
return normalize.element({
|
|
53
|
+
...parts.indicator,
|
|
54
|
+
hidden: props.copied !== isCopied
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// src/clipboard.machine.ts
|
|
61
|
+
var import_core = require("@zag-js/core");
|
|
62
|
+
var import_utils2 = require("@zag-js/utils");
|
|
63
|
+
|
|
64
|
+
// src/clipboard.dom.ts
|
|
65
|
+
var import_dom_query2 = require("@zag-js/dom-query");
|
|
66
|
+
var import_utils = require("@zag-js/utils");
|
|
67
|
+
function createNode(doc, text) {
|
|
68
|
+
const node = doc.createElement("pre");
|
|
69
|
+
Object.assign(node.style, {
|
|
70
|
+
width: "1px",
|
|
71
|
+
height: "1px",
|
|
72
|
+
position: "fixed",
|
|
73
|
+
top: "5px"
|
|
74
|
+
});
|
|
75
|
+
node.textContent = text;
|
|
76
|
+
return node;
|
|
77
|
+
}
|
|
78
|
+
var dom = (0, import_dom_query2.createScope)({
|
|
79
|
+
getRootId: (ctx) => `clip-${ctx.id}`,
|
|
80
|
+
getTriggerId: (ctx) => `clip-trigger-${ctx.id}`,
|
|
81
|
+
getIndicatorId: (ctx) => `clip-indicator-${ctx.id}`,
|
|
82
|
+
writeToClipboard: (ctx) => copyText(dom.getDoc(ctx), ctx.value)
|
|
83
|
+
});
|
|
84
|
+
function copyNode(node) {
|
|
85
|
+
const win = (0, import_dom_query2.getWindow)(node);
|
|
86
|
+
const selection = win.getSelection();
|
|
87
|
+
if (selection == null) {
|
|
88
|
+
return Promise.reject(new Error());
|
|
89
|
+
}
|
|
90
|
+
selection.removeAllRanges();
|
|
91
|
+
const doc = node.ownerDocument;
|
|
92
|
+
const range = doc.createRange();
|
|
93
|
+
range.selectNodeContents(node);
|
|
94
|
+
selection.addRange(range);
|
|
95
|
+
doc.execCommand("copy");
|
|
96
|
+
selection.removeAllRanges();
|
|
97
|
+
return Promise.resolve();
|
|
98
|
+
}
|
|
99
|
+
function copyText(doc, text) {
|
|
100
|
+
const win = doc.defaultView || window;
|
|
101
|
+
if ((0, import_utils.hasProp)(win.navigator, "clipboard")) {
|
|
102
|
+
return win.navigator.clipboard.writeText(text);
|
|
103
|
+
}
|
|
104
|
+
if (!doc.body) {
|
|
105
|
+
return Promise.reject(new Error());
|
|
106
|
+
}
|
|
107
|
+
const node = createNode(doc, text);
|
|
108
|
+
doc.body.appendChild(node);
|
|
109
|
+
copyNode(node);
|
|
110
|
+
doc.body.removeChild(node);
|
|
111
|
+
return Promise.resolve();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// src/clipboard.machine.ts
|
|
115
|
+
function machine(userContext) {
|
|
116
|
+
const ctx = (0, import_utils2.compact)(userContext);
|
|
117
|
+
return (0, import_core.createMachine)(
|
|
118
|
+
{
|
|
119
|
+
id: "clipboard",
|
|
120
|
+
initial: "idle",
|
|
121
|
+
context: {
|
|
122
|
+
value: "",
|
|
123
|
+
timeout: 3e3,
|
|
124
|
+
...ctx
|
|
125
|
+
},
|
|
126
|
+
on: {
|
|
127
|
+
"VALUE.SET": {
|
|
128
|
+
actions: ["setValue"]
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
states: {
|
|
132
|
+
idle: {
|
|
133
|
+
on: {
|
|
134
|
+
COPY: {
|
|
135
|
+
target: "copied",
|
|
136
|
+
actions: ["copyToClipboard", "invokeOnCopied"]
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
copied: {
|
|
141
|
+
after: {
|
|
142
|
+
COPY_TIMEOUT: "idle"
|
|
143
|
+
},
|
|
144
|
+
on: {
|
|
145
|
+
COPY: {
|
|
146
|
+
target: "copied",
|
|
147
|
+
actions: ["copyToClipboard", "invokeOnCopied"]
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
actions: {
|
|
155
|
+
setValue(ctx2, evt) {
|
|
156
|
+
ctx2.value = evt.value;
|
|
157
|
+
},
|
|
158
|
+
copyToClipboard(ctx2) {
|
|
159
|
+
dom.writeToClipboard(ctx2);
|
|
160
|
+
},
|
|
161
|
+
invokeOnCopied(ctx2) {
|
|
162
|
+
ctx2.onCopyStatusChange?.({ copied: true });
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
delays: {
|
|
166
|
+
COPY_TIMEOUT: (ctx2) => ctx2.timeout
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
172
|
+
0 && (module.exports = {
|
|
173
|
+
anatomy,
|
|
174
|
+
connect,
|
|
175
|
+
machine
|
|
176
|
+
});
|
|
177
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/clipboard.anatomy.ts","../src/clipboard.connect.ts","../src/clipboard.machine.ts","../src/clipboard.dom.ts"],"sourcesContent":["export { anatomy } from \"./clipboard.anatomy\"\nexport { connect } from \"./clipboard.connect\"\nexport { machine } from \"./clipboard.machine\"\nexport type { UserDefinedContext as Context } from \"./clipboard.types\"\n","import { createAnatomy } from \"@zag-js/anatomy\"\n\nexport const anatomy = createAnatomy(\"clipboard\").parts(\"trigger\", \"indicator\")\n\nexport const parts = anatomy.build()\n","import { dataAttr } from \"@zag-js/dom-query\"\nimport type { NormalizeProps, PropTypes } from \"@zag-js/types\"\nimport { parts } from \"./clipboard.anatomy\"\nimport type { MachineApi, Send, State } from \"./clipboard.types\"\n\nexport function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T> {\n const isCopied = state.matches(\"copied\")\n\n return {\n isCopied,\n value: state.context.value,\n setValue(value) {\n send({ type: \"VALUE.SET\", value })\n },\n triggerProps: normalize.button({\n ...parts.trigger,\n \"data-copied\": dataAttr(isCopied),\n onClick() {\n send({ type: \"COPY\" })\n },\n }),\n getIndicatorProps(props) {\n return normalize.element({\n ...parts.indicator,\n hidden: props.copied !== isCopied,\n })\n },\n }\n}\n","import { createMachine } from \"@zag-js/core\"\nimport { compact } from \"@zag-js/utils\"\nimport { dom } from \"./clipboard.dom\"\nimport type { MachineContext, MachineState, UserDefinedContext } from \"./clipboard.types\"\n\nexport function machine(userContext: UserDefinedContext) {\n const ctx = compact(userContext)\n return createMachine<MachineContext, MachineState>(\n {\n id: \"clipboard\",\n initial: \"idle\",\n context: {\n value: \"\",\n timeout: 3000,\n ...ctx,\n },\n on: {\n \"VALUE.SET\": {\n actions: [\"setValue\"],\n },\n },\n states: {\n idle: {\n on: {\n COPY: {\n target: \"copied\",\n actions: [\"copyToClipboard\", \"invokeOnCopied\"],\n },\n },\n },\n copied: {\n after: {\n COPY_TIMEOUT: \"idle\",\n },\n on: {\n COPY: {\n target: \"copied\",\n actions: [\"copyToClipboard\", \"invokeOnCopied\"],\n },\n },\n },\n },\n },\n {\n actions: {\n setValue(ctx, evt) {\n ctx.value = evt.value\n },\n copyToClipboard(ctx) {\n dom.writeToClipboard(ctx)\n },\n invokeOnCopied(ctx) {\n ctx.onCopyStatusChange?.({ copied: true })\n },\n },\n delays: {\n COPY_TIMEOUT: (ctx) => ctx.timeout,\n },\n },\n )\n}\n","import { createScope, getWindow } from \"@zag-js/dom-query\"\nimport { hasProp } from \"@zag-js/utils\"\nimport type { MachineContext as Ctx } from \"./clipboard.types\"\n\nfunction createNode(doc: Document, text: string): HTMLElement {\n const node = doc.createElement(\"pre\")\n Object.assign(node.style, {\n width: \"1px\",\n height: \"1px\",\n position: \"fixed\",\n top: \"5px\",\n })\n node.textContent = text\n return node\n}\n\nexport const dom = createScope({\n getRootId: (ctx: Ctx) => `clip-${ctx.id}`,\n getTriggerId: (ctx: Ctx) => `clip-trigger-${ctx.id}`,\n getIndicatorId: (ctx: Ctx) => `clip-indicator-${ctx.id}`,\n writeToClipboard: (ctx: Ctx) => copyText(dom.getDoc(ctx), ctx.value),\n})\n\nfunction copyNode(node: HTMLElement): Promise<void> {\n const win = getWindow(node)\n\n const selection = win.getSelection()\n\n if (selection == null) {\n return Promise.reject(new Error())\n }\n\n selection.removeAllRanges()\n\n const doc = node.ownerDocument\n\n const range = doc.createRange()\n range.selectNodeContents(node)\n selection.addRange(range)\n\n doc.execCommand(\"copy\")\n selection.removeAllRanges()\n\n return Promise.resolve()\n}\n\nfunction copyText(doc: Document, text: string): Promise<void> {\n const win = doc.defaultView || window\n\n if (hasProp(win.navigator, \"clipboard\")) {\n return win.navigator.clipboard.writeText(text)\n }\n\n if (!doc.body) {\n return Promise.reject(new Error())\n }\n\n const node = createNode(doc, text)\n\n doc.body.appendChild(node)\n copyNode(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AAA,cACJ,QAAQ;AAAA,cACR,SAAS,CAAC,mBAAmB,gBAAgB;AAAA,YAC/C;AAAA,UACF;AAAA,QACF;AAAA,QACA,QAAQ;AAAA,UACN,OAAO;AAAA,YACL,cAAc;AAAA,UAChB;AAAA,UACA,IAAI;AAAA,YACF,MAAM;AAAA,cACJ,QAAQ;AAAA,cACR,SAAS,CAAC,mBAAmB,gBAAgB;AAAA,YAC/C;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IACA;AAAA,MACE,SAAS;AAAA,QACP,SAASC,MAAK,KAAK;AACjB,UAAAA,KAAI,QAAQ,IAAI;AAAA,QAClB;AAAA,QACA,gBAAgBA,MAAK;AACnB,cAAI,iBAAiBA,IAAG;AAAA,QAC1B;AAAA,QACA,eAAeA,MAAK;AAClB,UAAAA,KAAI,qBAAqB,EAAE,QAAQ,KAAK,CAAC;AAAA,QAC3C;AAAA,MACF;AAAA,MACA,QAAQ;AAAA,QACN,cAAc,CAACA,SAAQA,KAAI;AAAA,MAC7B;AAAA,IACF;AAAA,EACF;AACF;","names":["import_utils","import_dom_query","ctx"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
// src/clipboard.anatomy.ts
|
|
2
|
+
import { createAnatomy } from "@zag-js/anatomy";
|
|
3
|
+
var anatomy = createAnatomy("clipboard").parts("trigger", "indicator");
|
|
4
|
+
var parts = anatomy.build();
|
|
5
|
+
|
|
6
|
+
// src/clipboard.connect.ts
|
|
7
|
+
import { dataAttr } from "@zag-js/dom-query";
|
|
8
|
+
function connect(state, send, normalize) {
|
|
9
|
+
const isCopied = state.matches("copied");
|
|
10
|
+
return {
|
|
11
|
+
isCopied,
|
|
12
|
+
value: state.context.value,
|
|
13
|
+
setValue(value) {
|
|
14
|
+
send({ type: "VALUE.SET", value });
|
|
15
|
+
},
|
|
16
|
+
triggerProps: normalize.button({
|
|
17
|
+
...parts.trigger,
|
|
18
|
+
"data-copied": dataAttr(isCopied),
|
|
19
|
+
onClick() {
|
|
20
|
+
send({ type: "COPY" });
|
|
21
|
+
}
|
|
22
|
+
}),
|
|
23
|
+
getIndicatorProps(props) {
|
|
24
|
+
return normalize.element({
|
|
25
|
+
...parts.indicator,
|
|
26
|
+
hidden: props.copied !== isCopied
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/clipboard.machine.ts
|
|
33
|
+
import { createMachine } from "@zag-js/core";
|
|
34
|
+
import { compact } from "@zag-js/utils";
|
|
35
|
+
|
|
36
|
+
// src/clipboard.dom.ts
|
|
37
|
+
import { createScope, getWindow } from "@zag-js/dom-query";
|
|
38
|
+
import { hasProp } from "@zag-js/utils";
|
|
39
|
+
function createNode(doc, text) {
|
|
40
|
+
const node = doc.createElement("pre");
|
|
41
|
+
Object.assign(node.style, {
|
|
42
|
+
width: "1px",
|
|
43
|
+
height: "1px",
|
|
44
|
+
position: "fixed",
|
|
45
|
+
top: "5px"
|
|
46
|
+
});
|
|
47
|
+
node.textContent = text;
|
|
48
|
+
return node;
|
|
49
|
+
}
|
|
50
|
+
var dom = createScope({
|
|
51
|
+
getRootId: (ctx) => `clip-${ctx.id}`,
|
|
52
|
+
getTriggerId: (ctx) => `clip-trigger-${ctx.id}`,
|
|
53
|
+
getIndicatorId: (ctx) => `clip-indicator-${ctx.id}`,
|
|
54
|
+
writeToClipboard: (ctx) => copyText(dom.getDoc(ctx), ctx.value)
|
|
55
|
+
});
|
|
56
|
+
function copyNode(node) {
|
|
57
|
+
const win = getWindow(node);
|
|
58
|
+
const selection = win.getSelection();
|
|
59
|
+
if (selection == null) {
|
|
60
|
+
return Promise.reject(new Error());
|
|
61
|
+
}
|
|
62
|
+
selection.removeAllRanges();
|
|
63
|
+
const doc = node.ownerDocument;
|
|
64
|
+
const range = doc.createRange();
|
|
65
|
+
range.selectNodeContents(node);
|
|
66
|
+
selection.addRange(range);
|
|
67
|
+
doc.execCommand("copy");
|
|
68
|
+
selection.removeAllRanges();
|
|
69
|
+
return Promise.resolve();
|
|
70
|
+
}
|
|
71
|
+
function copyText(doc, text) {
|
|
72
|
+
const win = doc.defaultView || window;
|
|
73
|
+
if (hasProp(win.navigator, "clipboard")) {
|
|
74
|
+
return win.navigator.clipboard.writeText(text);
|
|
75
|
+
}
|
|
76
|
+
if (!doc.body) {
|
|
77
|
+
return Promise.reject(new Error());
|
|
78
|
+
}
|
|
79
|
+
const node = createNode(doc, text);
|
|
80
|
+
doc.body.appendChild(node);
|
|
81
|
+
copyNode(node);
|
|
82
|
+
doc.body.removeChild(node);
|
|
83
|
+
return Promise.resolve();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// src/clipboard.machine.ts
|
|
87
|
+
function machine(userContext) {
|
|
88
|
+
const ctx = compact(userContext);
|
|
89
|
+
return createMachine(
|
|
90
|
+
{
|
|
91
|
+
id: "clipboard",
|
|
92
|
+
initial: "idle",
|
|
93
|
+
context: {
|
|
94
|
+
value: "",
|
|
95
|
+
timeout: 3e3,
|
|
96
|
+
...ctx
|
|
97
|
+
},
|
|
98
|
+
on: {
|
|
99
|
+
"VALUE.SET": {
|
|
100
|
+
actions: ["setValue"]
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
states: {
|
|
104
|
+
idle: {
|
|
105
|
+
on: {
|
|
106
|
+
COPY: {
|
|
107
|
+
target: "copied",
|
|
108
|
+
actions: ["copyToClipboard", "invokeOnCopied"]
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
copied: {
|
|
113
|
+
after: {
|
|
114
|
+
COPY_TIMEOUT: "idle"
|
|
115
|
+
},
|
|
116
|
+
on: {
|
|
117
|
+
COPY: {
|
|
118
|
+
target: "copied",
|
|
119
|
+
actions: ["copyToClipboard", "invokeOnCopied"]
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
actions: {
|
|
127
|
+
setValue(ctx2, evt) {
|
|
128
|
+
ctx2.value = evt.value;
|
|
129
|
+
},
|
|
130
|
+
copyToClipboard(ctx2) {
|
|
131
|
+
dom.writeToClipboard(ctx2);
|
|
132
|
+
},
|
|
133
|
+
invokeOnCopied(ctx2) {
|
|
134
|
+
ctx2.onCopyStatusChange?.({ copied: true });
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
delays: {
|
|
138
|
+
COPY_TIMEOUT: (ctx2) => ctx2.timeout
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
export {
|
|
144
|
+
anatomy,
|
|
145
|
+
connect,
|
|
146
|
+
machine
|
|
147
|
+
};
|
|
148
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/clipboard.anatomy.ts","../src/clipboard.connect.ts","../src/clipboard.machine.ts","../src/clipboard.dom.ts"],"sourcesContent":["import { createAnatomy } from \"@zag-js/anatomy\"\n\nexport const anatomy = createAnatomy(\"clipboard\").parts(\"trigger\", \"indicator\")\n\nexport const parts = anatomy.build()\n","import { dataAttr } from \"@zag-js/dom-query\"\nimport type { NormalizeProps, PropTypes } from \"@zag-js/types\"\nimport { parts } from \"./clipboard.anatomy\"\nimport type { MachineApi, Send, State } from \"./clipboard.types\"\n\nexport function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T> {\n const isCopied = state.matches(\"copied\")\n\n return {\n isCopied,\n value: state.context.value,\n setValue(value) {\n send({ type: \"VALUE.SET\", value })\n },\n triggerProps: normalize.button({\n ...parts.trigger,\n \"data-copied\": dataAttr(isCopied),\n onClick() {\n send({ type: \"COPY\" })\n },\n }),\n getIndicatorProps(props) {\n return normalize.element({\n ...parts.indicator,\n hidden: props.copied !== isCopied,\n })\n },\n }\n}\n","import { createMachine } from \"@zag-js/core\"\nimport { compact } from \"@zag-js/utils\"\nimport { dom } from \"./clipboard.dom\"\nimport type { MachineContext, MachineState, UserDefinedContext } from \"./clipboard.types\"\n\nexport function machine(userContext: UserDefinedContext) {\n const ctx = compact(userContext)\n return createMachine<MachineContext, MachineState>(\n {\n id: \"clipboard\",\n initial: \"idle\",\n context: {\n value: \"\",\n timeout: 3000,\n ...ctx,\n },\n on: {\n \"VALUE.SET\": {\n actions: [\"setValue\"],\n },\n },\n states: {\n idle: {\n on: {\n COPY: {\n target: \"copied\",\n actions: [\"copyToClipboard\", \"invokeOnCopied\"],\n },\n },\n },\n copied: {\n after: {\n COPY_TIMEOUT: \"idle\",\n },\n on: {\n COPY: {\n target: \"copied\",\n actions: [\"copyToClipboard\", \"invokeOnCopied\"],\n },\n },\n },\n },\n },\n {\n actions: {\n setValue(ctx, evt) {\n ctx.value = evt.value\n },\n copyToClipboard(ctx) {\n dom.writeToClipboard(ctx)\n },\n invokeOnCopied(ctx) {\n ctx.onCopyStatusChange?.({ copied: true })\n },\n },\n delays: {\n COPY_TIMEOUT: (ctx) => ctx.timeout,\n },\n },\n )\n}\n","import { createScope, getWindow } from \"@zag-js/dom-query\"\nimport { hasProp } from \"@zag-js/utils\"\nimport type { MachineContext as Ctx } from \"./clipboard.types\"\n\nfunction createNode(doc: Document, text: string): HTMLElement {\n const node = doc.createElement(\"pre\")\n Object.assign(node.style, {\n width: \"1px\",\n height: \"1px\",\n position: \"fixed\",\n top: \"5px\",\n })\n node.textContent = text\n return node\n}\n\nexport const dom = createScope({\n getRootId: (ctx: Ctx) => `clip-${ctx.id}`,\n getTriggerId: (ctx: Ctx) => `clip-trigger-${ctx.id}`,\n getIndicatorId: (ctx: Ctx) => `clip-indicator-${ctx.id}`,\n writeToClipboard: (ctx: Ctx) => copyText(dom.getDoc(ctx), ctx.value),\n})\n\nfunction copyNode(node: HTMLElement): Promise<void> {\n const win = getWindow(node)\n\n const selection = win.getSelection()\n\n if (selection == null) {\n return Promise.reject(new Error())\n }\n\n selection.removeAllRanges()\n\n const doc = node.ownerDocument\n\n const range = doc.createRange()\n range.selectNodeContents(node)\n selection.addRange(range)\n\n doc.execCommand(\"copy\")\n selection.removeAllRanges()\n\n return Promise.resolve()\n}\n\nfunction copyText(doc: Document, text: string): Promise<void> {\n const win = doc.defaultView || window\n\n if (hasProp(win.navigator, \"clipboard\")) {\n return win.navigator.clipboard.writeText(text)\n }\n\n if (!doc.body) {\n return Promise.reject(new Error())\n }\n\n const node = createNode(doc, text)\n\n doc.body.appendChild(node)\n copyNode(node)\n doc.body.removeChild(node)\n\n return Promise.resolve()\n}\n"],"mappings":";AAAA,SAAS,qBAAqB;AAEvB,IAAM,UAAU,cAAc,WAAW,EAAE,MAAM,WAAW,WAAW;AAEvE,IAAM,QAAQ,QAAQ,MAAM;;;ACJnC,SAAS,gBAAgB;AAKlB,SAAS,QAA6B,OAAc,MAAY,WAA6C;AAClH,QAAM,WAAW,MAAM,QAAQ,QAAQ;AAEvC,SAAO;AAAA,IACL;AAAA,IACA,OAAO,MAAM,QAAQ;AAAA,IACrB,SAAS,OAAO;AACd,WAAK,EAAE,MAAM,aAAa,MAAM,CAAC;AAAA,IACnC;AAAA,IACA,cAAc,UAAU,OAAO;AAAA,MAC7B,GAAG,MAAM;AAAA,MACT,eAAe,SAAS,QAAQ;AAAA,MAChC,UAAU;AACR,aAAK,EAAE,MAAM,OAAO,CAAC;AAAA,MACvB;AAAA,IACF,CAAC;AAAA,IACD,kBAAkB,OAAO;AACvB,aAAO,UAAU,QAAQ;AAAA,QACvB,GAAG,MAAM;AAAA,QACT,QAAQ,MAAM,WAAW;AAAA,MAC3B,CAAC;AAAA,IACH;AAAA,EACF;AACF;;;AC5BA,SAAS,qBAAqB;AAC9B,SAAS,eAAe;;;ACDxB,SAAS,aAAa,iBAAiB;AACvC,SAAS,eAAe;AAGxB,SAAS,WAAW,KAAe,MAA2B;AAC5D,QAAM,OAAO,IAAI,cAAc,KAAK;AACpC,SAAO,OAAO,KAAK,OAAO;AAAA,IACxB,OAAO;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,IAAI;AAAA,MACJ,SAAS;AAAA,MACT,SAAS;AAAA,QACP,OAAO;AAAA,QACP,SAAS;AAAA,QACT,GAAG;AAAA,MACL;AAAA,MACA,IAAI;AAAA,QACF,aAAa;AAAA,UACX,SAAS,CAAC,UAAU;AAAA,QACtB;AAAA,MACF;AAAA,MACA,QAAQ;AAAA,QACN,MAAM;AAAA,UACJ,IAAI;AAAA,YACF,MAAM;AAAA,cACJ,QAAQ;AAAA,cACR,SAAS,CAAC,mBAAmB,gBAAgB;AAAA,YAC/C;AAAA,UACF;AAAA,QACF;AAAA,QACA,QAAQ;AAAA,UACN,OAAO;AAAA,YACL,cAAc;AAAA,UAChB;AAAA,UACA,IAAI;AAAA,YACF,MAAM;AAAA,cACJ,QAAQ;AAAA,cACR,SAAS,CAAC,mBAAmB,gBAAgB;AAAA,YAC/C;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IACA;AAAA,MACE,SAAS;AAAA,QACP,SAASA,MAAK,KAAK;AACjB,UAAAA,KAAI,QAAQ,IAAI;AAAA,QAClB;AAAA,QACA,gBAAgBA,MAAK;AACnB,cAAI,iBAAiBA,IAAG;AAAA,QAC1B;AAAA,QACA,eAAeA,MAAK;AAClB,UAAAA,KAAI,qBAAqB,EAAE,QAAQ,KAAK,CAAC;AAAA,QAC3C;AAAA,MACF;AAAA,MACA,QAAQ;AAAA,QACN,cAAc,CAACA,SAAQA,KAAI;AAAA,MAC7B;AAAA,IACF;AAAA,EACF;AACF;","names":["ctx"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zag-js/clipboard",
|
|
3
|
+
"version": "0.0.0-dev-20240202223441",
|
|
4
|
+
"description": "Core logic for the clipboard widget implemented as a state machine",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"js",
|
|
7
|
+
"machine",
|
|
8
|
+
"xstate",
|
|
9
|
+
"statechart",
|
|
10
|
+
"component",
|
|
11
|
+
"chakra-ui",
|
|
12
|
+
"clipboard"
|
|
13
|
+
],
|
|
14
|
+
"author": "Segun Adebayo <sage@adebayosegun.com>",
|
|
15
|
+
"homepage": "https://github.com/chakra-ui/zag#readme",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"main": "dist/index.js",
|
|
18
|
+
"repository": "https://github.com/chakra-ui/zag/tree/main/packages/clipboard",
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"src"
|
|
23
|
+
],
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/chakra-ui/zag/issues"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@zag-js/anatomy": "0.0.0-dev-20240202223441",
|
|
32
|
+
"@zag-js/core": "0.0.0-dev-20240202223441",
|
|
33
|
+
"@zag-js/dom-query": "0.0.0-dev-20240202223441",
|
|
34
|
+
"@zag-js/utils": "0.0.0-dev-20240202223441",
|
|
35
|
+
"@zag-js/types": "0.0.0-dev-20240202223441"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"clean-package": "2.2.0"
|
|
39
|
+
},
|
|
40
|
+
"clean-package": "../../../clean-package.config.json",
|
|
41
|
+
"module": "dist/index.mjs",
|
|
42
|
+
"types": "dist/index.d.ts",
|
|
43
|
+
"exports": {
|
|
44
|
+
".": {
|
|
45
|
+
"types": "./dist/index.d.ts",
|
|
46
|
+
"import": "./dist/index.mjs",
|
|
47
|
+
"require": "./dist/index.js"
|
|
48
|
+
},
|
|
49
|
+
"./package.json": "./package.json"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "tsup",
|
|
53
|
+
"lint": "eslint src --ext .ts,.tsx",
|
|
54
|
+
"typecheck": "tsc --noEmit"
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { dataAttr } from "@zag-js/dom-query"
|
|
2
|
+
import type { NormalizeProps, PropTypes } from "@zag-js/types"
|
|
3
|
+
import { parts } from "./clipboard.anatomy"
|
|
4
|
+
import type { MachineApi, Send, State } from "./clipboard.types"
|
|
5
|
+
|
|
6
|
+
export function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T> {
|
|
7
|
+
const isCopied = state.matches("copied")
|
|
8
|
+
|
|
9
|
+
return {
|
|
10
|
+
isCopied,
|
|
11
|
+
value: state.context.value,
|
|
12
|
+
setValue(value) {
|
|
13
|
+
send({ type: "VALUE.SET", value })
|
|
14
|
+
},
|
|
15
|
+
triggerProps: normalize.button({
|
|
16
|
+
...parts.trigger,
|
|
17
|
+
"data-copied": dataAttr(isCopied),
|
|
18
|
+
onClick() {
|
|
19
|
+
send({ type: "COPY" })
|
|
20
|
+
},
|
|
21
|
+
}),
|
|
22
|
+
getIndicatorProps(props) {
|
|
23
|
+
return normalize.element({
|
|
24
|
+
...parts.indicator,
|
|
25
|
+
hidden: props.copied !== isCopied,
|
|
26
|
+
})
|
|
27
|
+
},
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { createScope, getWindow } from "@zag-js/dom-query"
|
|
2
|
+
import { hasProp } from "@zag-js/utils"
|
|
3
|
+
import type { MachineContext as Ctx } from "./clipboard.types"
|
|
4
|
+
|
|
5
|
+
function createNode(doc: Document, text: string): HTMLElement {
|
|
6
|
+
const node = doc.createElement("pre")
|
|
7
|
+
Object.assign(node.style, {
|
|
8
|
+
width: "1px",
|
|
9
|
+
height: "1px",
|
|
10
|
+
position: "fixed",
|
|
11
|
+
top: "5px",
|
|
12
|
+
})
|
|
13
|
+
node.textContent = text
|
|
14
|
+
return node
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const dom = createScope({
|
|
18
|
+
getRootId: (ctx: Ctx) => `clip-${ctx.id}`,
|
|
19
|
+
getTriggerId: (ctx: Ctx) => `clip-trigger-${ctx.id}`,
|
|
20
|
+
getIndicatorId: (ctx: Ctx) => `clip-indicator-${ctx.id}`,
|
|
21
|
+
writeToClipboard: (ctx: Ctx) => copyText(dom.getDoc(ctx), ctx.value),
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
function copyNode(node: HTMLElement): Promise<void> {
|
|
25
|
+
const win = getWindow(node)
|
|
26
|
+
|
|
27
|
+
const selection = win.getSelection()
|
|
28
|
+
|
|
29
|
+
if (selection == null) {
|
|
30
|
+
return Promise.reject(new Error())
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
selection.removeAllRanges()
|
|
34
|
+
|
|
35
|
+
const doc = node.ownerDocument
|
|
36
|
+
|
|
37
|
+
const range = doc.createRange()
|
|
38
|
+
range.selectNodeContents(node)
|
|
39
|
+
selection.addRange(range)
|
|
40
|
+
|
|
41
|
+
doc.execCommand("copy")
|
|
42
|
+
selection.removeAllRanges()
|
|
43
|
+
|
|
44
|
+
return Promise.resolve()
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function copyText(doc: Document, text: string): Promise<void> {
|
|
48
|
+
const win = doc.defaultView || window
|
|
49
|
+
|
|
50
|
+
if (hasProp(win.navigator, "clipboard")) {
|
|
51
|
+
return win.navigator.clipboard.writeText(text)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!doc.body) {
|
|
55
|
+
return Promise.reject(new Error())
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const node = createNode(doc, text)
|
|
59
|
+
|
|
60
|
+
doc.body.appendChild(node)
|
|
61
|
+
copyNode(node)
|
|
62
|
+
doc.body.removeChild(node)
|
|
63
|
+
|
|
64
|
+
return Promise.resolve()
|
|
65
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { createMachine } from "@zag-js/core"
|
|
2
|
+
import { compact } from "@zag-js/utils"
|
|
3
|
+
import { dom } from "./clipboard.dom"
|
|
4
|
+
import type { MachineContext, MachineState, UserDefinedContext } from "./clipboard.types"
|
|
5
|
+
|
|
6
|
+
export function machine(userContext: UserDefinedContext) {
|
|
7
|
+
const ctx = compact(userContext)
|
|
8
|
+
return createMachine<MachineContext, MachineState>(
|
|
9
|
+
{
|
|
10
|
+
id: "clipboard",
|
|
11
|
+
initial: "idle",
|
|
12
|
+
context: {
|
|
13
|
+
value: "",
|
|
14
|
+
timeout: 3000,
|
|
15
|
+
...ctx,
|
|
16
|
+
},
|
|
17
|
+
on: {
|
|
18
|
+
"VALUE.SET": {
|
|
19
|
+
actions: ["setValue"],
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
states: {
|
|
23
|
+
idle: {
|
|
24
|
+
on: {
|
|
25
|
+
COPY: {
|
|
26
|
+
target: "copied",
|
|
27
|
+
actions: ["copyToClipboard", "invokeOnCopied"],
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
copied: {
|
|
32
|
+
after: {
|
|
33
|
+
COPY_TIMEOUT: "idle",
|
|
34
|
+
},
|
|
35
|
+
on: {
|
|
36
|
+
COPY: {
|
|
37
|
+
target: "copied",
|
|
38
|
+
actions: ["copyToClipboard", "invokeOnCopied"],
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
actions: {
|
|
46
|
+
setValue(ctx, evt) {
|
|
47
|
+
ctx.value = evt.value
|
|
48
|
+
},
|
|
49
|
+
copyToClipboard(ctx) {
|
|
50
|
+
dom.writeToClipboard(ctx)
|
|
51
|
+
},
|
|
52
|
+
invokeOnCopied(ctx) {
|
|
53
|
+
ctx.onCopyStatusChange?.({ copied: true })
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
delays: {
|
|
57
|
+
COPY_TIMEOUT: (ctx) => ctx.timeout,
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
)
|
|
61
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { StateMachine as S } from "@zag-js/core"
|
|
2
|
+
import type { CommonProperties, Context, PropTypes, RequiredBy } from "@zag-js/types"
|
|
3
|
+
|
|
4
|
+
export interface CopyStatusDetails {
|
|
5
|
+
copied: boolean
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface PublicContext extends CommonProperties {
|
|
9
|
+
/**
|
|
10
|
+
* The value to be copied to the clipboard
|
|
11
|
+
*/
|
|
12
|
+
value: string
|
|
13
|
+
/**
|
|
14
|
+
* The function to be called when the value is copied to the clipboard
|
|
15
|
+
*/
|
|
16
|
+
onCopyStatusChange?: (details: CopyStatusDetails) => void
|
|
17
|
+
/**
|
|
18
|
+
* The timeout for the copy operation
|
|
19
|
+
*/
|
|
20
|
+
timeout: number
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type PrivateContext = Context<{}>
|
|
24
|
+
|
|
25
|
+
type ComputedContext = Readonly<{}>
|
|
26
|
+
|
|
27
|
+
export type UserDefinedContext = RequiredBy<PublicContext, "id">
|
|
28
|
+
|
|
29
|
+
export interface MachineContext extends PublicContext, PrivateContext, ComputedContext {}
|
|
30
|
+
|
|
31
|
+
export interface MachineState {
|
|
32
|
+
value: "idle" | "copied"
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type State = S.State<MachineContext, MachineState>
|
|
36
|
+
|
|
37
|
+
export type Send = S.Send<S.AnyEventObject>
|
|
38
|
+
|
|
39
|
+
/* -----------------------------------------------------------------------------
|
|
40
|
+
* Component API
|
|
41
|
+
* -----------------------------------------------------------------------------*/
|
|
42
|
+
|
|
43
|
+
export interface IndicatorProps {
|
|
44
|
+
copied: boolean
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface MachineApi<T extends PropTypes = PropTypes> {
|
|
48
|
+
/**
|
|
49
|
+
* The value to be copied to the clipboard
|
|
50
|
+
*/
|
|
51
|
+
value: string
|
|
52
|
+
/**
|
|
53
|
+
* Set the value to be copied to the clipboard
|
|
54
|
+
*/
|
|
55
|
+
setValue(value: string): void
|
|
56
|
+
/**
|
|
57
|
+
* The props for the trigger button
|
|
58
|
+
*/
|
|
59
|
+
triggerProps: T["button"]
|
|
60
|
+
/**
|
|
61
|
+
* The props for the indicator
|
|
62
|
+
*/
|
|
63
|
+
getIndicatorProps(props: IndicatorProps): T["element"]
|
|
64
|
+
/**
|
|
65
|
+
* Whether the value has been copied to the clipboard
|
|
66
|
+
*/
|
|
67
|
+
isCopied: boolean
|
|
68
|
+
}
|
package/src/index.ts
ADDED