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