@zag-js/clipboard 1.34.0 → 1.35.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.
package/dist/index.js CHANGED
@@ -1,234 +1,39 @@
1
- 'use strict';
2
-
3
- var anatomy$1 = require('@zag-js/anatomy');
4
- var domQuery = require('@zag-js/dom-query');
5
- var core = require('@zag-js/core');
6
- var utils = require('@zag-js/utils');
7
- var types = require('@zag-js/types');
8
-
9
- // src/clipboard.anatomy.ts
10
- var anatomy = anatomy$1.createAnatomy("clipboard").parts("root", "control", "trigger", "indicator", "input", "label");
11
- var parts = anatomy.build();
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);
17
- function createNode(doc, text) {
18
- const node = doc.createElement("pre");
19
- Object.assign(node.style, {
20
- width: "1px",
21
- height: "1px",
22
- position: "fixed",
23
- top: "5px"
24
- });
25
- node.textContent = text;
26
- return node;
27
- }
28
- function copyNode(node) {
29
- const win = domQuery.getWindow(node);
30
- const selection = win.getSelection();
31
- if (selection == null) {
32
- return Promise.reject(new Error());
33
- }
34
- selection.removeAllRanges();
35
- const doc = node.ownerDocument;
36
- const range = doc.createRange();
37
- range.selectNodeContents(node);
38
- selection.addRange(range);
39
- doc.execCommand("copy");
40
- selection.removeAllRanges();
41
- return Promise.resolve();
42
- }
43
- function copyText(doc, text) {
44
- const win = doc.defaultView || window;
45
- if (win.navigator.clipboard?.writeText !== void 0) {
46
- return win.navigator.clipboard.writeText(text);
47
- }
48
- if (!doc.body) {
49
- return Promise.reject(new Error());
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 });
50
15
  }
51
- const node = createNode(doc, text);
52
- doc.body.appendChild(node);
53
- copyNode(node);
54
- doc.body.removeChild(node);
55
- return Promise.resolve();
56
- }
16
+ return to;
17
+ };
18
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
57
20
 
58
- // src/clipboard.connect.ts
59
- function connect(service, normalize) {
60
- const { state, send, context, scope } = service;
61
- const copied = state.matches("copied");
62
- return {
63
- copied,
64
- value: context.get("value"),
65
- setValue(value) {
66
- send({ type: "VALUE.SET", value });
67
- },
68
- copy() {
69
- send({ type: "COPY" });
70
- },
71
- getRootProps() {
72
- return normalize.element({
73
- ...parts.root.attrs,
74
- "data-copied": domQuery.dataAttr(copied),
75
- id: getRootId(scope)
76
- });
77
- },
78
- getLabelProps() {
79
- return normalize.label({
80
- ...parts.label.attrs,
81
- htmlFor: getInputId(scope),
82
- "data-copied": domQuery.dataAttr(copied),
83
- id: getLabelId(scope)
84
- });
85
- },
86
- getControlProps() {
87
- return normalize.element({
88
- ...parts.control.attrs,
89
- "data-copied": domQuery.dataAttr(copied)
90
- });
91
- },
92
- getInputProps() {
93
- return normalize.input({
94
- ...parts.input.attrs,
95
- defaultValue: context.get("value"),
96
- "data-copied": domQuery.dataAttr(copied),
97
- readOnly: true,
98
- "data-readonly": "true",
99
- id: getInputId(scope),
100
- onFocus(event) {
101
- event.currentTarget.select();
102
- },
103
- onCopy() {
104
- send({ type: "INPUT.COPY" });
105
- }
106
- });
107
- },
108
- getTriggerProps() {
109
- return normalize.button({
110
- ...parts.trigger.attrs,
111
- type: "button",
112
- "aria-label": copied ? "Copied to clipboard" : "Copy to clipboard",
113
- "data-copied": domQuery.dataAttr(copied),
114
- onClick() {
115
- send({ type: "COPY" });
116
- }
117
- });
118
- },
119
- getIndicatorProps(props2) {
120
- return normalize.element({
121
- ...parts.indicator.attrs,
122
- hidden: props2.copied !== copied
123
- });
124
- }
125
- };
126
- }
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: {
165
- on: {
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"
177
- },
178
- COPY: {
179
- target: "copied",
180
- actions: ["copyToClipboard", "invokeOnCopy"]
181
- },
182
- "INPUT.COPY": {
183
- actions: ["invokeOnCopy"]
184
- }
185
- }
186
- }
187
- },
188
- implementations: {
189
- effects: {
190
- waitForTimeout({ prop, send }) {
191
- return utils.setRafTimeout(() => {
192
- send({ type: "COPY.DONE" });
193
- }, prop("timeout"));
194
- }
195
- },
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 });
205
- },
206
- syncInputElement({ context, scope }) {
207
- const inputEl = getInputEl(scope);
208
- if (!inputEl) return;
209
- domQuery.setElementValue(inputEl, context.get("value"));
210
- }
211
- }
212
- }
21
+ // src/index.ts
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
+ anatomy: () => import_clipboard.anatomy,
25
+ connect: () => import_clipboard2.connect,
26
+ machine: () => import_clipboard3.machine
27
+ });
28
+ module.exports = __toCommonJS(index_exports);
29
+ var import_clipboard = require("./clipboard.anatomy.cjs");
30
+ var import_clipboard2 = require("./clipboard.connect.cjs");
31
+ var import_clipboard3 = require("./clipboard.machine.cjs");
32
+ __reExport(index_exports, require("./clipboard.props.cjs"), module.exports);
33
+ // Annotate the CommonJS export names for ESM import in node:
34
+ 0 && (module.exports = {
35
+ anatomy,
36
+ connect,
37
+ machine,
38
+ ...require("./clipboard.props.cjs")
213
39
  });
214
- var props = types.createProps()([
215
- "getRootNode",
216
- "id",
217
- "ids",
218
- "value",
219
- "defaultValue",
220
- "timeout",
221
- "onStatusChange",
222
- "onValueChange"
223
- ]);
224
- var contextProps = utils.createSplitProps(props);
225
- var indicatorProps = types.createProps()(["copied"]);
226
- var splitIndicatorProps = utils.createSplitProps(indicatorProps);
227
-
228
- exports.anatomy = anatomy;
229
- exports.connect = connect;
230
- exports.contextProps = contextProps;
231
- exports.indicatorProps = indicatorProps;
232
- exports.machine = machine;
233
- exports.props = props;
234
- exports.splitIndicatorProps = splitIndicatorProps;
package/dist/index.mjs CHANGED
@@ -1,226 +1,10 @@
1
- import { createAnatomy } from '@zag-js/anatomy';
2
- import { setElementValue, getWindow, dataAttr } from '@zag-js/dom-query';
3
- import { createMachine } from '@zag-js/core';
4
- import { setRafTimeout, createSplitProps } from '@zag-js/utils';
5
- import { createProps } from '@zag-js/types';
6
-
7
- // src/clipboard.anatomy.ts
8
- var anatomy = createAnatomy("clipboard").parts("root", "control", "trigger", "indicator", "input", "label");
9
- var parts = anatomy.build();
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);
15
- function createNode(doc, text) {
16
- const node = doc.createElement("pre");
17
- Object.assign(node.style, {
18
- width: "1px",
19
- height: "1px",
20
- position: "fixed",
21
- top: "5px"
22
- });
23
- node.textContent = text;
24
- return node;
25
- }
26
- function copyNode(node) {
27
- const win = getWindow(node);
28
- const selection = win.getSelection();
29
- if (selection == null) {
30
- return Promise.reject(new Error());
31
- }
32
- selection.removeAllRanges();
33
- const doc = node.ownerDocument;
34
- const range = doc.createRange();
35
- range.selectNodeContents(node);
36
- selection.addRange(range);
37
- doc.execCommand("copy");
38
- selection.removeAllRanges();
39
- return Promise.resolve();
40
- }
41
- function copyText(doc, text) {
42
- const win = doc.defaultView || window;
43
- if (win.navigator.clipboard?.writeText !== void 0) {
44
- return win.navigator.clipboard.writeText(text);
45
- }
46
- if (!doc.body) {
47
- return Promise.reject(new Error());
48
- }
49
- const node = createNode(doc, text);
50
- doc.body.appendChild(node);
51
- copyNode(node);
52
- doc.body.removeChild(node);
53
- return Promise.resolve();
54
- }
55
-
56
- // src/clipboard.connect.ts
57
- function connect(service, normalize) {
58
- const { state, send, context, scope } = service;
59
- const copied = state.matches("copied");
60
- return {
61
- copied,
62
- value: context.get("value"),
63
- setValue(value) {
64
- send({ type: "VALUE.SET", value });
65
- },
66
- copy() {
67
- send({ type: "COPY" });
68
- },
69
- getRootProps() {
70
- return normalize.element({
71
- ...parts.root.attrs,
72
- "data-copied": dataAttr(copied),
73
- id: getRootId(scope)
74
- });
75
- },
76
- getLabelProps() {
77
- return normalize.label({
78
- ...parts.label.attrs,
79
- htmlFor: getInputId(scope),
80
- "data-copied": dataAttr(copied),
81
- id: getLabelId(scope)
82
- });
83
- },
84
- getControlProps() {
85
- return normalize.element({
86
- ...parts.control.attrs,
87
- "data-copied": dataAttr(copied)
88
- });
89
- },
90
- getInputProps() {
91
- return normalize.input({
92
- ...parts.input.attrs,
93
- defaultValue: context.get("value"),
94
- "data-copied": dataAttr(copied),
95
- readOnly: true,
96
- "data-readonly": "true",
97
- id: getInputId(scope),
98
- onFocus(event) {
99
- event.currentTarget.select();
100
- },
101
- onCopy() {
102
- send({ type: "INPUT.COPY" });
103
- }
104
- });
105
- },
106
- getTriggerProps() {
107
- return normalize.button({
108
- ...parts.trigger.attrs,
109
- type: "button",
110
- "aria-label": copied ? "Copied to clipboard" : "Copy to clipboard",
111
- "data-copied": dataAttr(copied),
112
- onClick() {
113
- send({ type: "COPY" });
114
- }
115
- });
116
- },
117
- getIndicatorProps(props2) {
118
- return normalize.element({
119
- ...parts.indicator.attrs,
120
- hidden: props2.copied !== copied
121
- });
122
- }
123
- };
124
- }
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: {
163
- on: {
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"
175
- },
176
- COPY: {
177
- target: "copied",
178
- actions: ["copyToClipboard", "invokeOnCopy"]
179
- },
180
- "INPUT.COPY": {
181
- actions: ["invokeOnCopy"]
182
- }
183
- }
184
- }
185
- },
186
- implementations: {
187
- effects: {
188
- waitForTimeout({ prop, send }) {
189
- return setRafTimeout(() => {
190
- send({ type: "COPY.DONE" });
191
- }, prop("timeout"));
192
- }
193
- },
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 });
203
- },
204
- syncInputElement({ context, scope }) {
205
- const inputEl = getInputEl(scope);
206
- if (!inputEl) return;
207
- setElementValue(inputEl, context.get("value"));
208
- }
209
- }
210
- }
211
- });
212
- var props = createProps()([
213
- "getRootNode",
214
- "id",
215
- "ids",
216
- "value",
217
- "defaultValue",
218
- "timeout",
219
- "onStatusChange",
220
- "onValueChange"
221
- ]);
222
- var contextProps = createSplitProps(props);
223
- var indicatorProps = createProps()(["copied"]);
224
- var splitIndicatorProps = createSplitProps(indicatorProps);
225
-
226
- export { anatomy, connect, contextProps, indicatorProps, machine, props, splitIndicatorProps };
1
+ // src/index.ts
2
+ import { anatomy } from "./clipboard.anatomy.mjs";
3
+ import { connect } from "./clipboard.connect.mjs";
4
+ import { machine } from "./clipboard.machine.mjs";
5
+ export * from "./clipboard.props.mjs";
6
+ export {
7
+ anatomy,
8
+ connect,
9
+ machine
10
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/clipboard",
3
- "version": "1.34.0",
3
+ "version": "1.35.0",
4
4
  "description": "Core logic for the clipboard widget implemented as a state machine",
5
5
  "keywords": [
6
6
  "js",
@@ -27,16 +27,16 @@
27
27
  "url": "https://github.com/chakra-ui/zag/issues"
28
28
  },
29
29
  "dependencies": {
30
- "@zag-js/anatomy": "1.34.0",
31
- "@zag-js/core": "1.34.0",
32
- "@zag-js/dom-query": "1.34.0",
33
- "@zag-js/utils": "1.34.0",
34
- "@zag-js/types": "1.34.0"
30
+ "@zag-js/anatomy": "1.35.0",
31
+ "@zag-js/core": "1.35.0",
32
+ "@zag-js/dom-query": "1.35.0",
33
+ "@zag-js/utils": "1.35.0",
34
+ "@zag-js/types": "1.35.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "clean-package": "2.2.0"
38
38
  },
39
- "clean-package": "../../../clean-package.config.json",
39
+ "clean-package": "./clean-package.config.json",
40
40
  "module": "dist/index.mjs",
41
41
  "types": "dist/index.d.ts",
42
42
  "exports": {
@@ -50,6 +50,16 @@
50
50
  "default": "./dist/index.js"
51
51
  }
52
52
  },
53
+ "./anatomy": {
54
+ "import": {
55
+ "types": "./dist/clipboard.anatomy.d.mts",
56
+ "default": "./dist/clipboard.anatomy.mjs"
57
+ },
58
+ "require": {
59
+ "types": "./dist/clipboard.anatomy.d.ts",
60
+ "default": "./dist/clipboard.anatomy.js"
61
+ }
62
+ },
53
63
  "./package.json": "./package.json"
54
64
  },
55
65
  "scripts": {