@destyler/edit 0.0.7-beta.7 → 0.0.7-beta.9

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.cjs DELETED
@@ -1,526 +0,0 @@
1
- 'use strict';
2
-
3
- const anatomy$1 = require('@zag-js/anatomy');
4
- const domQuery = require('@zag-js/dom-query');
5
- const core = require('@zag-js/core');
6
- const interactOutside = require('@zag-js/interact-outside');
7
- const utils = require('@zag-js/utils');
8
- const types = require('@zag-js/types');
9
-
10
- const anatomy = anatomy$1.createAnatomy("edit").parts(
11
- "root",
12
- "area",
13
- "label",
14
- "preview",
15
- "input",
16
- "editTrigger",
17
- "submitTrigger",
18
- "cancelTrigger",
19
- "control"
20
- );
21
- const parts = anatomy.build();
22
-
23
- const dom = domQuery.createScope({
24
- getRootId: (ctx) => ctx.ids?.root ?? `editable:${ctx.id}`,
25
- getAreaId: (ctx) => ctx.ids?.area ?? `editable:${ctx.id}:area`,
26
- getLabelId: (ctx) => ctx.ids?.label ?? `editable:${ctx.id}:label`,
27
- getPreviewId: (ctx) => ctx.ids?.preview ?? `editable:${ctx.id}:preview`,
28
- getInputId: (ctx) => ctx.ids?.input ?? `editable:${ctx.id}:input`,
29
- getControlId: (ctx) => ctx.ids?.control ?? `editable:${ctx.id}:control`,
30
- getSubmitTriggerId: (ctx) => ctx.ids?.submitTrigger ?? `editable:${ctx.id}:submit`,
31
- getCancelTriggerId: (ctx) => ctx.ids?.cancelTrigger ?? `editable:${ctx.id}:cancel`,
32
- getEditTriggerId: (ctx) => ctx.ids?.editTrigger ?? `editable:${ctx.id}:edit`,
33
- getInputEl: (ctx) => dom.getById(ctx, dom.getInputId(ctx)),
34
- getPreviewEl: (ctx) => dom.getById(ctx, dom.getPreviewId(ctx)),
35
- getSubmitTriggerEl: (ctx) => dom.getById(ctx, dom.getSubmitTriggerId(ctx)),
36
- getCancelTriggerEl: (ctx) => dom.getById(ctx, dom.getCancelTriggerId(ctx)),
37
- getEditTriggerEl: (ctx) => dom.getById(ctx, dom.getEditTriggerId(ctx))
38
- });
39
-
40
- function connect(state, send, normalize) {
41
- const disabled = state.context.disabled;
42
- const interactive = state.context.isInteractive;
43
- const readOnly = state.context.readOnly;
44
- const invalid = state.context.invalid;
45
- const autoResize = state.context.autoResize;
46
- const translations = state.context.translations;
47
- const editing = state.matches("edit");
48
- const placeholderProp = state.context.placeholder;
49
- const placeholder = typeof placeholderProp === "string" ? { edit: placeholderProp, preview: placeholderProp } : placeholderProp;
50
- const value = state.context.value;
51
- const empty = value.trim() === "";
52
- const valueText = empty ? placeholder?.preview ?? "" : value;
53
- return {
54
- editing,
55
- empty,
56
- value,
57
- valueText,
58
- setValue(value2) {
59
- send({ type: "VALUE.SET", value: value2, src: "setValue" });
60
- },
61
- clearValue() {
62
- send({ type: "VALUE.SET", value: "", src: "clearValue" });
63
- },
64
- edit() {
65
- if (!interactive)
66
- return;
67
- send("EDIT");
68
- },
69
- cancel() {
70
- if (!interactive)
71
- return;
72
- send("CANCEL");
73
- },
74
- submit() {
75
- if (!interactive)
76
- return;
77
- send("SUBMIT");
78
- },
79
- getRootProps() {
80
- return normalize.element({
81
- ...parts.root.attrs,
82
- id: dom.getRootId(state.context),
83
- dir: state.context.dir
84
- });
85
- },
86
- getAreaProps() {
87
- return normalize.element({
88
- ...parts.area.attrs,
89
- "id": dom.getAreaId(state.context),
90
- "dir": state.context.dir,
91
- "style": autoResize ? { display: "inline-grid" } : void 0,
92
- "data-focus": domQuery.dataAttr(editing),
93
- "data-disabled": domQuery.dataAttr(disabled),
94
- "data-placeholder-shown": domQuery.dataAttr(empty)
95
- });
96
- },
97
- getLabelProps() {
98
- return normalize.label({
99
- ...parts.label.attrs,
100
- "id": dom.getLabelId(state.context),
101
- "dir": state.context.dir,
102
- "htmlFor": dom.getInputId(state.context),
103
- "data-focus": domQuery.dataAttr(editing),
104
- "data-invalid": domQuery.dataAttr(invalid),
105
- onClick() {
106
- if (editing)
107
- return;
108
- const previewEl = dom.getPreviewEl(state.context);
109
- previewEl?.focus({ preventScroll: true });
110
- }
111
- });
112
- },
113
- getInputProps() {
114
- return normalize.input({
115
- ...parts.input.attrs,
116
- "dir": state.context.dir,
117
- "aria-label": translations.input,
118
- "name": state.context.name,
119
- "form": state.context.form,
120
- "id": dom.getInputId(state.context),
121
- "hidden": autoResize ? void 0 : !editing,
122
- "placeholder": placeholder?.edit,
123
- "maxLength": state.context.maxLength,
124
- "required": state.context.required,
125
- "disabled": disabled,
126
- "data-disabled": domQuery.dataAttr(disabled),
127
- "readOnly": readOnly,
128
- "data-readonly": domQuery.dataAttr(readOnly),
129
- "aria-invalid": domQuery.ariaAttr(invalid),
130
- "data-invalid": domQuery.dataAttr(invalid),
131
- "data-autoresize": domQuery.dataAttr(autoResize),
132
- "defaultValue": value,
133
- "size": autoResize ? 1 : void 0,
134
- onChange(event) {
135
- send({ type: "VALUE.SET", src: "input.change", value: event.currentTarget.value });
136
- },
137
- onKeyDown(event) {
138
- if (event.defaultPrevented)
139
- return;
140
- if (domQuery.isComposingEvent(event))
141
- return;
142
- const keyMap = {
143
- Escape() {
144
- send("CANCEL");
145
- event.preventDefault();
146
- },
147
- Enter(event2) {
148
- if (!state.context.submitOnEnter)
149
- return;
150
- const { localName } = event2.currentTarget;
151
- if (localName === "textarea") {
152
- const submitMod = domQuery.isApple() ? event2.metaKey : event2.ctrlKey;
153
- if (!submitMod)
154
- return;
155
- send({ type: "SUBMIT", src: "keydown.enter" });
156
- return;
157
- }
158
- if (localName === "input" && !event2.shiftKey && !event2.metaKey) {
159
- send({ type: "SUBMIT", src: "keydown.enter" });
160
- event2.preventDefault();
161
- }
162
- }
163
- };
164
- const exec = keyMap[event.key];
165
- if (exec) {
166
- exec(event);
167
- }
168
- },
169
- "style": autoResize ? {
170
- gridArea: "1 / 1 / auto / auto",
171
- visibility: !editing ? "hidden" : void 0
172
- } : void 0
173
- });
174
- },
175
- getPreviewProps() {
176
- return normalize.element({
177
- "id": dom.getPreviewId(state.context),
178
- ...parts.preview.attrs,
179
- "dir": state.context.dir,
180
- "data-placeholder-shown": domQuery.dataAttr(empty),
181
- "aria-readonly": domQuery.ariaAttr(readOnly),
182
- "data-readonly": domQuery.dataAttr(disabled),
183
- "data-disabled": domQuery.dataAttr(disabled),
184
- "aria-disabled": domQuery.ariaAttr(disabled),
185
- "aria-invalid": domQuery.ariaAttr(invalid),
186
- "data-invalid": domQuery.dataAttr(invalid),
187
- "aria-label": translations.edit,
188
- "data-autoresize": domQuery.dataAttr(autoResize),
189
- "children": valueText,
190
- "hidden": autoResize ? void 0 : editing,
191
- "tabIndex": interactive ? 0 : void 0,
192
- onClick() {
193
- if (!interactive)
194
- return;
195
- if (state.context.activationMode !== "click")
196
- return;
197
- send({ type: "EDIT", src: "click" });
198
- },
199
- onFocus() {
200
- if (!interactive)
201
- return;
202
- if (state.context.activationMode !== "focus")
203
- return;
204
- send({ type: "EDIT", src: "focus" });
205
- },
206
- onDoubleClick(event) {
207
- if (event.defaultPrevented)
208
- return;
209
- if (!interactive)
210
- return;
211
- if (state.context.activationMode !== "dblclick")
212
- return;
213
- send({ type: "EDIT", src: "dblclick" });
214
- },
215
- "style": autoResize ? {
216
- whiteSpace: "pre",
217
- userSelect: "none",
218
- gridArea: "1 / 1 / auto / auto",
219
- visibility: editing ? "hidden" : void 0,
220
- // in event the preview overflow's the parent element
221
- overflow: "hidden",
222
- textOverflow: "ellipsis"
223
- } : void 0
224
- });
225
- },
226
- getEditTriggerProps() {
227
- return normalize.button({
228
- ...parts.editTrigger.attrs,
229
- "id": dom.getEditTriggerId(state.context),
230
- "dir": state.context.dir,
231
- "aria-label": translations.edit,
232
- "hidden": editing,
233
- "type": "button",
234
- "disabled": disabled,
235
- onClick(event) {
236
- if (event.defaultPrevented)
237
- return;
238
- if (!interactive)
239
- return;
240
- send({ type: "EDIT", src: "edit.click" });
241
- }
242
- });
243
- },
244
- getControlProps() {
245
- return normalize.element({
246
- id: dom.getControlId(state.context),
247
- ...parts.control.attrs,
248
- dir: state.context.dir
249
- });
250
- },
251
- getSubmitTriggerProps() {
252
- return normalize.button({
253
- ...parts.submitTrigger.attrs,
254
- "dir": state.context.dir,
255
- "id": dom.getSubmitTriggerId(state.context),
256
- "aria-label": translations.submit,
257
- "hidden": !editing,
258
- "disabled": disabled,
259
- "type": "button",
260
- onClick(event) {
261
- if (event.defaultPrevented)
262
- return;
263
- if (!interactive)
264
- return;
265
- send({ type: "SUBMIT", src: "submit.click" });
266
- }
267
- });
268
- },
269
- getCancelTriggerProps() {
270
- return normalize.button({
271
- ...parts.cancelTrigger.attrs,
272
- "dir": state.context.dir,
273
- "aria-label": translations.cancel,
274
- "id": dom.getCancelTriggerId(state.context),
275
- "hidden": !editing,
276
- "type": "button",
277
- "disabled": disabled,
278
- onClick(event) {
279
- if (event.defaultPrevented)
280
- return;
281
- if (!interactive)
282
- return;
283
- send({ type: "CANCEL", src: "cancel.click" });
284
- }
285
- });
286
- }
287
- };
288
- }
289
-
290
- const sync = {
291
- value: (ctx) => {
292
- const inputEl = dom.getInputEl(ctx);
293
- dom.setValue(inputEl, ctx.value);
294
- }
295
- };
296
- const invoke = {
297
- change(ctx) {
298
- ctx.onValueChange?.({ value: ctx.value });
299
- sync.value(ctx);
300
- }
301
- };
302
- const set = {
303
- value(ctx, value) {
304
- if (utils.isEqual(ctx.value, value))
305
- return;
306
- ctx.value = value;
307
- invoke.change(ctx);
308
- }
309
- };
310
- const submitOnEnter = (ctx) => ["both", "enter"].includes(ctx.submitMode);
311
- const submitOnBlur = (ctx) => ["both", "blur"].includes(ctx.submitMode);
312
- function machine(userContext) {
313
- const ctx = utils.compact(userContext);
314
- return core.createMachine(
315
- {
316
- id: "editable",
317
- initial: ctx.edit ? "edit" : "preview",
318
- entry: ctx.edit ? ["focusInput"] : void 0,
319
- context: {
320
- activationMode: "focus",
321
- submitMode: "both",
322
- value: "",
323
- previousValue: "",
324
- selectOnFocus: true,
325
- disabled: false,
326
- readOnly: false,
327
- ...ctx,
328
- translations: {
329
- input: "editable input",
330
- edit: "edit",
331
- submit: "submit",
332
- cancel: "cancel",
333
- ...ctx.translations
334
- }
335
- },
336
- watch: {
337
- value: ["syncInputValue"],
338
- edit: ["toggleEditing"]
339
- },
340
- computed: {
341
- submitOnEnter,
342
- submitOnBlur,
343
- isInteractive: (ctx2) => !(ctx2.disabled || ctx2.readOnly)
344
- },
345
- on: {
346
- "VALUE.SET": {
347
- actions: "setValue"
348
- }
349
- },
350
- states: {
351
- preview: {
352
- // https://bugzilla.mozilla.org/show_bug.cgi?id=559561
353
- entry: ["blurInputIfNeeded"],
354
- on: {
355
- "CONTROLLED.EDIT": {
356
- target: "edit",
357
- actions: ["setPreviousValue", "focusInput"]
358
- },
359
- "EDIT": [
360
- {
361
- guard: "isEditControlled",
362
- actions: ["invokeOnEdit"]
363
- },
364
- {
365
- target: "edit",
366
- actions: ["setPreviousValue", "focusInput", "invokeOnEdit"]
367
- }
368
- ]
369
- }
370
- },
371
- edit: {
372
- activities: ["trackInteractOutside"],
373
- on: {
374
- "CONTROLLED.PREVIEW": [
375
- {
376
- guard: "isSubmitEvent",
377
- target: "preview",
378
- actions: ["setPreviousValue", "restoreFocus", "invokeOnSubmit"]
379
- },
380
- {
381
- target: "preview",
382
- actions: ["revertValue", "restoreFocus", "invokeOnCancel"]
383
- }
384
- ],
385
- "CANCEL": [
386
- {
387
- guard: "isEditControlled",
388
- actions: ["invokeOnPreview"]
389
- },
390
- {
391
- target: "preview",
392
- actions: ["revertValue", "restoreFocus", "invokeOnCancel", "invokeOnPreview"]
393
- }
394
- ],
395
- "SUBMIT": [
396
- {
397
- guard: "isEditControlled",
398
- actions: ["invokeOnPreview"]
399
- },
400
- {
401
- target: "preview",
402
- actions: ["setPreviousValue", "restoreFocus", "invokeOnSubmit", "invokeOnPreview"]
403
- }
404
- ]
405
- }
406
- }
407
- }
408
- },
409
- {
410
- guards: {
411
- isEditControlled: (ctx2) => !!ctx2["edit.controlled"],
412
- isSubmitEvent: (_ctx, evt) => evt.previousEvent?.type === "SUBMIT"
413
- },
414
- activities: {
415
- trackInteractOutside(ctx2, _evt, { send }) {
416
- return interactOutside.trackInteractOutside(dom.getInputEl(ctx2), {
417
- exclude(target) {
418
- const ignore = [dom.getCancelTriggerEl(ctx2), dom.getSubmitTriggerEl(ctx2)];
419
- return ignore.some((el) => domQuery.contains(el, target));
420
- },
421
- onFocusOutside: ctx2.onFocusOutside,
422
- onPointerDownOutside: ctx2.onPointerDownOutside,
423
- onInteractOutside(event) {
424
- ctx2.onInteractOutside?.(event);
425
- if (event.defaultPrevented)
426
- return;
427
- const { focusable } = event.detail;
428
- send({ type: submitOnBlur(ctx2) ? "SUBMIT" : "CANCEL", src: "interact-outside", focusable });
429
- }
430
- });
431
- }
432
- },
433
- actions: {
434
- restoreFocus(ctx2, evt) {
435
- if (evt.focusable)
436
- return;
437
- domQuery.raf(() => {
438
- const finalEl = ctx2.finalFocusEl?.() ?? dom.getEditTriggerEl(ctx2);
439
- finalEl?.focus({ preventScroll: true });
440
- });
441
- },
442
- focusInput(ctx2) {
443
- domQuery.raf(() => {
444
- const inputEl = dom.getInputEl(ctx2);
445
- if (!inputEl)
446
- return;
447
- if (ctx2.selectOnFocus) {
448
- inputEl.select();
449
- } else {
450
- inputEl.focus({ preventScroll: true });
451
- }
452
- });
453
- },
454
- invokeOnCancel(ctx2) {
455
- ctx2.onValueRevert?.({ value: ctx2.previousValue });
456
- },
457
- invokeOnSubmit(ctx2) {
458
- ctx2.onValueCommit?.({ value: ctx2.value });
459
- },
460
- invokeOnEdit(ctx2) {
461
- ctx2.onEditChange?.({ edit: true });
462
- },
463
- invokeOnPreview(ctx2) {
464
- ctx2.onEditChange?.({ edit: false });
465
- },
466
- toggleEditing(ctx2, evt, { send }) {
467
- send({ type: ctx2.edit ? "CONTROLLED.EDIT" : "CONTROLLED.PREVIEW", previousEvent: evt });
468
- },
469
- syncInputValue(ctx2) {
470
- sync.value(ctx2);
471
- },
472
- setValue(ctx2, evt) {
473
- const value = ctx2.maxLength != null ? evt.value.slice(0, ctx2.maxLength) : evt.value;
474
- set.value(ctx2, value);
475
- },
476
- setPreviousValue(ctx2) {
477
- ctx2.previousValue = ctx2.value;
478
- },
479
- revertValue(ctx2) {
480
- set.value(ctx2, ctx2.previousValue);
481
- },
482
- blurInputIfNeeded(ctx2) {
483
- dom.getInputEl(ctx2)?.blur();
484
- }
485
- }
486
- }
487
- );
488
- }
489
-
490
- const props = types.createProps()([
491
- "activationMode",
492
- "autoResize",
493
- "dir",
494
- "disabled",
495
- "finalFocusEl",
496
- "form",
497
- "getRootNode",
498
- "id",
499
- "ids",
500
- "invalid",
501
- "maxLength",
502
- "name",
503
- "onEditChange",
504
- "onFocusOutside",
505
- "onInteractOutside",
506
- "onPointerDownOutside",
507
- "onValueChange",
508
- "onValueCommit",
509
- "onValueRevert",
510
- "placeholder",
511
- "readOnly",
512
- "required",
513
- "selectOnFocus",
514
- "edit",
515
- "edit.controlled",
516
- "submitMode",
517
- "translations",
518
- "value"
519
- ]);
520
- const splitProps = utils.createSplitProps(props);
521
-
522
- exports.anatomy = anatomy;
523
- exports.connect = connect;
524
- exports.machine = machine;
525
- exports.props = props;
526
- exports.splitProps = splitProps;