@destyler/tooltip 0.0.7-beta.5 → 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.js ADDED
@@ -0,0 +1,491 @@
1
+ import { createAnatomy } from "@destyler/anatomy";
2
+ import { addDomEvent, createScope, dataAttr, getOverflowAncestors, isComposingEvent } from "@destyler/dom";
3
+ import { isFocusVisible, trackFocusVisible } from "@destyler/focus-visible";
4
+ import { getPlacement, getPlacementStyles } from "@destyler/popper";
5
+ import { createMachine, guards, proxy, subscribe } from "@destyler/xstate";
6
+ import { compact, createSplitProps } from "@destyler/utils";
7
+ import { createProps } from "@destyler/types";
8
+
9
+ //#region src/anatomy.ts
10
+ const anatomy = createAnatomy("tooltip").parts("trigger", "arrow", "arrowTip", "positioner", "content");
11
+ const parts = anatomy.build();
12
+
13
+ //#endregion
14
+ //#region src/dom.ts
15
+ const dom = createScope({
16
+ getTriggerId: (ctx) => ctx.ids?.trigger ?? `tooltip:${ctx.id}:trigger`,
17
+ getContentId: (ctx) => ctx.ids?.content ?? `tooltip:${ctx.id}:content`,
18
+ getArrowId: (ctx) => ctx.ids?.arrow ?? `tooltip:${ctx.id}:arrow`,
19
+ getPositionerId: (ctx) => ctx.ids?.positioner ?? `tooltip:${ctx.id}:popper`,
20
+ getTriggerEl: (ctx) => dom.getById(ctx, dom.getTriggerId(ctx)),
21
+ getContentEl: (ctx) => dom.getById(ctx, dom.getContentId(ctx)),
22
+ getPositionerEl: (ctx) => dom.getById(ctx, dom.getPositionerId(ctx)),
23
+ getArrowEl: (ctx) => dom.getById(ctx, dom.getArrowId(ctx))
24
+ });
25
+
26
+ //#endregion
27
+ //#region src/store.ts
28
+ const store = proxy({
29
+ id: null,
30
+ prevId: null,
31
+ setId(val) {
32
+ this.prevId = this.id;
33
+ this.id = val;
34
+ }
35
+ });
36
+
37
+ //#endregion
38
+ //#region src/connect.ts
39
+ function connect(state, send, normalize) {
40
+ const id = state.context.id;
41
+ const hasAriaLabel = state.context.hasAriaLabel;
42
+ const open = state.hasTag("open");
43
+ const triggerId = dom.getTriggerId(state.context);
44
+ const contentId = dom.getContentId(state.context);
45
+ const disabled = state.context.disabled;
46
+ const popperStyles = getPlacementStyles({
47
+ ...state.context.positioning,
48
+ placement: state.context.currentPlacement
49
+ });
50
+ return {
51
+ open,
52
+ setOpen(nextOpen) {
53
+ if (nextOpen === open) return;
54
+ send(nextOpen ? "OPEN" : "CLOSE");
55
+ },
56
+ reposition(options = {}) {
57
+ send({
58
+ type: "POSITIONING.SET",
59
+ options
60
+ });
61
+ },
62
+ getTriggerProps() {
63
+ return normalize.button({
64
+ ...parts.trigger.attrs,
65
+ "id": triggerId,
66
+ "dir": state.context.dir,
67
+ "data-expanded": dataAttr(open),
68
+ "data-state": open ? "open" : "closed",
69
+ "aria-describedby": open ? contentId : void 0,
70
+ onClick(event) {
71
+ if (event.defaultPrevented) return;
72
+ if (disabled) return;
73
+ if (!state.context.closeOnClick) return;
74
+ send({
75
+ type: "CLOSE",
76
+ src: "trigger.click"
77
+ });
78
+ },
79
+ onFocus(event) {
80
+ if (event.defaultPrevented) return;
81
+ if (disabled) return;
82
+ if (state.event.src === "trigger.pointerdown") return;
83
+ if (!isFocusVisible()) return;
84
+ send({
85
+ type: "OPEN",
86
+ src: "trigger.focus"
87
+ });
88
+ },
89
+ onBlur(event) {
90
+ if (event.defaultPrevented) return;
91
+ if (disabled) return;
92
+ if (id === store.id) send({
93
+ type: "CLOSE",
94
+ src: "trigger.blur"
95
+ });
96
+ },
97
+ onPointerDown(event) {
98
+ if (event.defaultPrevented) return;
99
+ if (disabled) return;
100
+ if (!state.context.closeOnPointerDown) return;
101
+ if (id === store.id) send({
102
+ type: "CLOSE",
103
+ src: "trigger.pointerdown"
104
+ });
105
+ },
106
+ onPointerMove(event) {
107
+ if (event.defaultPrevented) return;
108
+ if (disabled) return;
109
+ if (event.pointerType === "touch") return;
110
+ send("POINTER_MOVE");
111
+ },
112
+ onPointerLeave() {
113
+ if (disabled) return;
114
+ send("POINTER_LEAVE");
115
+ },
116
+ onPointerCancel() {
117
+ if (disabled) return;
118
+ send("POINTER_LEAVE");
119
+ }
120
+ });
121
+ },
122
+ getArrowProps() {
123
+ return normalize.element({
124
+ id: dom.getArrowId(state.context),
125
+ ...parts.arrow.attrs,
126
+ dir: state.context.dir,
127
+ style: popperStyles.arrow
128
+ });
129
+ },
130
+ getArrowTipProps() {
131
+ return normalize.element({
132
+ ...parts.arrowTip.attrs,
133
+ dir: state.context.dir,
134
+ style: popperStyles.arrowTip
135
+ });
136
+ },
137
+ getPositionerProps() {
138
+ return normalize.element({
139
+ id: dom.getPositionerId(state.context),
140
+ ...parts.positioner.attrs,
141
+ dir: state.context.dir,
142
+ style: popperStyles.floating
143
+ });
144
+ },
145
+ getContentProps() {
146
+ return normalize.element({
147
+ ...parts.content.attrs,
148
+ "dir": state.context.dir,
149
+ "hidden": !open,
150
+ "data-state": open ? "open" : "closed",
151
+ "role": hasAriaLabel ? void 0 : "tooltip",
152
+ "id": hasAriaLabel ? void 0 : contentId,
153
+ "data-placement": state.context.currentPlacement,
154
+ onPointerEnter() {
155
+ send("CONTENT.POINTER_MOVE");
156
+ },
157
+ onPointerLeave() {
158
+ send("CONTENT.POINTER_LEAVE");
159
+ },
160
+ "style": { pointerEvents: state.context.interactive ? "auto" : "none" }
161
+ });
162
+ }
163
+ };
164
+ }
165
+
166
+ //#endregion
167
+ //#region src/machine.ts
168
+ const { and, not } = guards;
169
+ function machine(userContext) {
170
+ const ctx = compact(userContext);
171
+ return createMachine({
172
+ id: "tooltip",
173
+ initial: ctx.open ? "open" : "closed",
174
+ activities: ["trackFocusVisible"],
175
+ context: {
176
+ openDelay: 1e3,
177
+ closeDelay: 500,
178
+ closeOnPointerDown: true,
179
+ closeOnEscape: true,
180
+ interactive: false,
181
+ closeOnScroll: true,
182
+ closeOnClick: true,
183
+ disabled: false,
184
+ ...ctx,
185
+ currentPlacement: void 0,
186
+ hasPointerMoveOpened: false,
187
+ positioning: {
188
+ placement: "bottom",
189
+ ...ctx.positioning
190
+ }
191
+ },
192
+ computed: { hasAriaLabel: (ctx$1) => !!ctx$1["aria-label"] },
193
+ watch: {
194
+ disabled: ["closeIfDisabled"],
195
+ open: ["toggleVisibility"]
196
+ },
197
+ states: {
198
+ closed: {
199
+ tags: ["closed"],
200
+ entry: ["clearGlobalId"],
201
+ on: {
202
+ "CONTROLLED.OPEN": "open",
203
+ "OPEN": [{
204
+ guard: "isOpenControlled",
205
+ actions: ["invokeOnOpen"]
206
+ }, {
207
+ target: "open",
208
+ actions: ["invokeOnOpen"]
209
+ }],
210
+ "POINTER_LEAVE": { actions: ["clearPointerMoveOpened"] },
211
+ "POINTER_MOVE": [{
212
+ guard: and("noVisibleTooltip", not("hasPointerMoveOpened")),
213
+ target: "opening"
214
+ }, {
215
+ guard: not("hasPointerMoveOpened"),
216
+ target: "open",
217
+ actions: ["setPointerMoveOpened", "invokeOnOpen"]
218
+ }]
219
+ }
220
+ },
221
+ opening: {
222
+ tags: ["closed"],
223
+ activities: ["trackScroll", "trackPointerlockChange"],
224
+ after: { OPEN_DELAY: [{
225
+ guard: "isOpenControlled",
226
+ actions: ["setPointerMoveOpened", "invokeOnOpen"]
227
+ }, {
228
+ target: "open",
229
+ actions: ["setPointerMoveOpened", "invokeOnOpen"]
230
+ }] },
231
+ on: {
232
+ "CONTROLLED.OPEN": "open",
233
+ "CONTROLLED.CLOSE": "closed",
234
+ "OPEN": [{
235
+ guard: "isOpenControlled",
236
+ actions: ["invokeOnOpen"]
237
+ }, {
238
+ target: "open",
239
+ actions: ["invokeOnOpen"]
240
+ }],
241
+ "POINTER_LEAVE": [{
242
+ guard: "isOpenControlled",
243
+ actions: [
244
+ "clearPointerMoveOpened",
245
+ "invokeOnClose",
246
+ "toggleVisibility"
247
+ ]
248
+ }, {
249
+ target: "closed",
250
+ actions: ["clearPointerMoveOpened", "invokeOnClose"]
251
+ }],
252
+ "CLOSE": [{
253
+ guard: "isOpenControlled",
254
+ actions: ["invokeOnClose", "toggleVisibility"]
255
+ }, {
256
+ target: "closed",
257
+ actions: ["invokeOnClose"]
258
+ }]
259
+ }
260
+ },
261
+ open: {
262
+ tags: ["open"],
263
+ activities: [
264
+ "trackEscapeKey",
265
+ "trackScroll",
266
+ "trackPointerlockChange",
267
+ "trackPositioning"
268
+ ],
269
+ entry: ["setGlobalId"],
270
+ on: {
271
+ "CONTROLLED.CLOSE": "closed",
272
+ "CLOSE": [{
273
+ guard: "isOpenControlled",
274
+ actions: ["invokeOnClose"]
275
+ }, {
276
+ target: "closed",
277
+ actions: ["invokeOnClose"]
278
+ }],
279
+ "POINTER_LEAVE": [
280
+ {
281
+ guard: "isVisible",
282
+ target: "closing",
283
+ actions: ["clearPointerMoveOpened"]
284
+ },
285
+ {
286
+ guard: "isOpenControlled",
287
+ actions: ["clearPointerMoveOpened", "invokeOnClose"]
288
+ },
289
+ {
290
+ target: "closed",
291
+ actions: ["clearPointerMoveOpened", "invokeOnClose"]
292
+ }
293
+ ],
294
+ "CONTENT.POINTER_LEAVE": {
295
+ guard: "isInteractive",
296
+ target: "closing"
297
+ },
298
+ "POSITIONING.SET": { actions: "reposition" }
299
+ }
300
+ },
301
+ closing: {
302
+ tags: ["open"],
303
+ activities: ["trackStore", "trackPositioning"],
304
+ after: { CLOSE_DELAY: [{
305
+ guard: "isOpenControlled",
306
+ actions: ["invokeOnClose"]
307
+ }, {
308
+ target: "closed",
309
+ actions: ["invokeOnClose"]
310
+ }] },
311
+ on: {
312
+ "CONTROLLED.CLOSE": "closed",
313
+ "CONTROLLED.OPEN": "open",
314
+ "CLOSE": [{
315
+ guard: "isOpenControlled",
316
+ actions: ["invokeOnClose"]
317
+ }, {
318
+ target: "closed",
319
+ actions: ["invokeOnClose"]
320
+ }],
321
+ "POINTER_MOVE": [{
322
+ guard: "isOpenControlled",
323
+ actions: [
324
+ "setPointerMoveOpened",
325
+ "invokeOnOpen",
326
+ "toggleVisibility"
327
+ ]
328
+ }, {
329
+ target: "open",
330
+ actions: ["setPointerMoveOpened", "invokeOnOpen"]
331
+ }],
332
+ "CONTENT.POINTER_MOVE": {
333
+ guard: "isInteractive",
334
+ target: "open"
335
+ },
336
+ "POSITIONING.SET": { actions: "reposition" }
337
+ }
338
+ }
339
+ }
340
+ }, {
341
+ activities: {
342
+ trackFocusVisible(ctx$1) {
343
+ return trackFocusVisible({ root: dom.getRootNode(ctx$1) });
344
+ },
345
+ trackPositioning(ctx$1) {
346
+ ctx$1.currentPlacement ||= ctx$1.positioning.placement;
347
+ const getPositionerEl = () => dom.getPositionerEl(ctx$1);
348
+ return getPlacement(dom.getTriggerEl(ctx$1), getPositionerEl, {
349
+ ...ctx$1.positioning,
350
+ defer: true,
351
+ onComplete(data) {
352
+ ctx$1.currentPlacement = data.placement;
353
+ }
354
+ });
355
+ },
356
+ trackPointerlockChange(ctx$1, _evt, { send }) {
357
+ const onChange = () => send({
358
+ type: "CLOSE",
359
+ src: "pointerlock:change"
360
+ });
361
+ return addDomEvent(dom.getDoc(ctx$1), "pointerlockchange", onChange, false);
362
+ },
363
+ trackScroll(ctx$1, _evt, { send }) {
364
+ if (!ctx$1.closeOnScroll) return;
365
+ const triggerEl = dom.getTriggerEl(ctx$1);
366
+ if (!triggerEl) return;
367
+ const overflowParents = getOverflowAncestors(triggerEl);
368
+ const cleanups = overflowParents.map((overflowParent) => {
369
+ const onScroll = () => {
370
+ send({
371
+ type: "CLOSE",
372
+ src: "scroll"
373
+ });
374
+ };
375
+ return addDomEvent(overflowParent, "scroll", onScroll, {
376
+ passive: true,
377
+ capture: true
378
+ });
379
+ });
380
+ return () => {
381
+ cleanups.forEach((fn) => fn?.());
382
+ };
383
+ },
384
+ trackStore(ctx$1, _evt, { send }) {
385
+ return subscribe(store, () => {
386
+ if (store.id !== ctx$1.id) send({
387
+ type: "CLOSE",
388
+ src: "id.change"
389
+ });
390
+ });
391
+ },
392
+ trackEscapeKey(ctx$1, _evt, { send }) {
393
+ if (!ctx$1.closeOnEscape) return;
394
+ const onKeyDown = (event) => {
395
+ if (isComposingEvent(event)) return;
396
+ if (event.key !== "Escape") return;
397
+ event.stopPropagation();
398
+ send({
399
+ type: "CLOSE",
400
+ src: "keydown.escape"
401
+ });
402
+ };
403
+ return addDomEvent(dom.getDoc(ctx$1), "keydown", onKeyDown, true);
404
+ }
405
+ },
406
+ actions: {
407
+ setGlobalId(ctx$1) {
408
+ store.setId(ctx$1.id);
409
+ },
410
+ clearGlobalId(ctx$1) {
411
+ if (ctx$1.id === store.id) store.setId(null);
412
+ },
413
+ invokeOnOpen(ctx$1) {
414
+ ctx$1.onOpenChange?.({ open: true });
415
+ },
416
+ invokeOnClose(ctx$1) {
417
+ ctx$1.onOpenChange?.({ open: false });
418
+ },
419
+ closeIfDisabled(ctx$1, _evt, { send }) {
420
+ if (!ctx$1.disabled) return;
421
+ send({
422
+ type: "CLOSE",
423
+ src: "disabled.change"
424
+ });
425
+ },
426
+ reposition(ctx$1, evt) {
427
+ const getPositionerEl = () => dom.getPositionerEl(ctx$1);
428
+ getPlacement(dom.getTriggerEl(ctx$1), getPositionerEl, {
429
+ ...ctx$1.positioning,
430
+ ...evt.options,
431
+ defer: true,
432
+ listeners: false,
433
+ onComplete(data) {
434
+ ctx$1.currentPlacement = data.placement;
435
+ }
436
+ });
437
+ },
438
+ toggleVisibility(ctx$1, evt, { send }) {
439
+ queueMicrotask(() => {
440
+ send({
441
+ type: ctx$1.open ? "CONTROLLED.OPEN" : "CONTROLLED.CLOSE",
442
+ previousEvent: evt
443
+ });
444
+ });
445
+ },
446
+ setPointerMoveOpened(ctx$1) {
447
+ ctx$1.hasPointerMoveOpened = true;
448
+ },
449
+ clearPointerMoveOpened(ctx$1) {
450
+ ctx$1.hasPointerMoveOpened = false;
451
+ }
452
+ },
453
+ guards: {
454
+ noVisibleTooltip: () => store.id === null,
455
+ isVisible: (ctx$1) => ctx$1.id === store.id,
456
+ isInteractive: (ctx$1) => ctx$1.interactive,
457
+ hasPointerMoveOpened: (ctx$1) => !!ctx$1.hasPointerMoveOpened,
458
+ isOpenControlled: (ctx$1) => !!ctx$1["open.controlled"]
459
+ },
460
+ delays: {
461
+ OPEN_DELAY: (ctx$1) => ctx$1.openDelay,
462
+ CLOSE_DELAY: (ctx$1) => ctx$1.closeDelay
463
+ }
464
+ });
465
+ }
466
+
467
+ //#endregion
468
+ //#region src/props.ts
469
+ const props = createProps()([
470
+ "aria-label",
471
+ "closeDelay",
472
+ "closeOnEscape",
473
+ "closeOnPointerDown",
474
+ "closeOnScroll",
475
+ "closeOnClick",
476
+ "dir",
477
+ "disabled",
478
+ "getRootNode",
479
+ "id",
480
+ "ids",
481
+ "interactive",
482
+ "onOpenChange",
483
+ "open.controlled",
484
+ "open",
485
+ "openDelay",
486
+ "positioning"
487
+ ]);
488
+ const splitProps = createSplitProps(props);
489
+
490
+ //#endregion
491
+ export { anatomy, connect, machine, props, splitProps };
package/package.json CHANGED
@@ -1,38 +1,33 @@
1
1
  {
2
2
  "name": "@destyler/tooltip",
3
3
  "type": "module",
4
- "version": "0.0.7-beta.5",
4
+ "version": "0.0.7-beta.9",
5
5
  "description": "A popup that displays information related to an element when the element receives keyboard focus or the mouse hovers over it.",
6
6
  "author": "Elone Hoo <elonehoo@gmail.com>",
7
7
  "license": "MIT",
8
8
  "exports": {
9
- ".": {
10
- "types": "./dist/index.d.mts",
11
- "default": "./dist/index.mjs"
12
- },
13
- "./*": "./*"
9
+ ".": "./dist/index.js"
14
10
  },
15
- "main": "dist/index.cjs",
16
- "module": "dist/index.mjs",
11
+ "main": "dist/index.js",
12
+ "module": "dist/index.js",
17
13
  "types": "dist/index.d.ts",
18
14
  "files": [
19
15
  "*.d.ts",
20
16
  "dist"
21
17
  ],
22
18
  "dependencies": {
23
- "@zag-js/anatomy": "^0.82.1",
24
- "@zag-js/core": "^0.82.1",
25
- "@zag-js/dom-event": "^0.80.0",
26
- "@zag-js/dom-query": "^0.82.1",
27
- "@zag-js/focus-visible": "^0.82.1",
28
- "@zag-js/popper": "^0.82.1",
29
- "@zag-js/types": "^0.82.1",
30
- "@zag-js/utils": "^0.82.1"
19
+ "@destyler/anatomy": "0.0.7-beta.9",
20
+ "@destyler/focus-visible": "0.0.7-beta.9",
21
+ "@destyler/dom": "0.0.7-beta.9",
22
+ "@destyler/popper": "0.0.7-beta.9",
23
+ "@destyler/types": "0.0.7-beta.9",
24
+ "@destyler/utils": "0.0.7-beta.9",
25
+ "@destyler/xstate": "0.0.7-beta.9"
31
26
  },
32
27
  "scripts": {
33
28
  "clear": "rimraf ./dist",
34
29
  "clear:node_modules": "rimraf ./node_modules",
35
- "build": "pnpm run clear && unbuild",
36
- "stub": "unbuild --stub"
30
+ "build": "pnpm run clear && tsdown",
31
+ "watch": "tsdown --watch"
37
32
  }
38
33
  }