@destyler/tooltip 0.2.11 → 0.2.12
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.d.mts +6 -9
- package/dist/index.mjs +79 -64
- package/package.json +8 -8
package/dist/index.d.mts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
import * as _destyler_anatomy0 from "@destyler/anatomy";
|
|
2
1
|
import { Placement, PositioningOptions } from "@destyler/popper";
|
|
3
|
-
import * as _destyler_xstate0 from "@destyler/xstate";
|
|
4
2
|
import { AnyEventObject, Machine, XSend, XState } from "@destyler/xstate";
|
|
5
3
|
import { CommonProperties, DirectionProperty, NormalizeProps, PropTypes, RequiredBy } from "@destyler/types";
|
|
6
|
-
|
|
7
4
|
//#region src/anatomy.d.ts
|
|
8
|
-
declare const anatomy:
|
|
5
|
+
export declare const anatomy: import("@destyler/anatomy").AnatomyInstance<"trigger" | "arrow" | "arrowTip" | "positioner" | "content">;
|
|
9
6
|
//#endregion
|
|
10
7
|
//#region src/types.d.ts
|
|
11
8
|
interface OpenChangeDetails {
|
|
@@ -138,13 +135,13 @@ interface MachineApi<T extends PropTypes = PropTypes> {
|
|
|
138
135
|
}
|
|
139
136
|
//#endregion
|
|
140
137
|
//#region src/connect.d.ts
|
|
141
|
-
declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
|
|
138
|
+
export declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
|
|
142
139
|
//#endregion
|
|
143
140
|
//#region src/machine.d.ts
|
|
144
|
-
declare function machine(userContext: UserDefinedContext):
|
|
141
|
+
export declare function machine(userContext: UserDefinedContext): import("@destyler/xstate").Machine<MachineContext, MachineState, import("@destyler/xstate").AnyEventObject>;
|
|
145
142
|
//#endregion
|
|
146
143
|
//#region src/props.d.ts
|
|
147
|
-
declare const props: ("open" | "ids" | "id" | "openDelay" | "closeDelay" | "closeOnPointerDown" | "closeOnEscape" | "closeOnScroll" | "closeOnClick" | "interactive" | "onOpenChange" | "aria-label" | "positioning" | "disabled" | "defaultOpen" | "dir" | "getRootNode")[];
|
|
148
|
-
declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "open" | "ids" | "id" | "openDelay" | "closeDelay" | "closeOnPointerDown" | "closeOnEscape" | "closeOnScroll" | "closeOnClick" | "interactive" | "onOpenChange" | "aria-label" | "positioning" | "disabled" | "defaultOpen" | "dir" | "getRootNode">];
|
|
144
|
+
export declare const props: ("open" | "ids" | "id" | "openDelay" | "closeDelay" | "closeOnPointerDown" | "closeOnEscape" | "closeOnScroll" | "closeOnClick" | "interactive" | "onOpenChange" | "aria-label" | "positioning" | "disabled" | "defaultOpen" | "dir" | "getRootNode")[];
|
|
145
|
+
export declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "open" | "ids" | "id" | "openDelay" | "closeDelay" | "closeOnPointerDown" | "closeOnEscape" | "closeOnScroll" | "closeOnClick" | "interactive" | "onOpenChange" | "aria-label" | "positioning" | "disabled" | "defaultOpen" | "dir" | "getRootNode">];
|
|
149
146
|
//#endregion
|
|
150
|
-
export {
|
|
147
|
+
export type { MachineApi as Api, UserDefinedContext as Context, ElementIds, OpenChangeDetails, Placement, PositioningOptions, Service };
|
package/dist/index.mjs
CHANGED
|
@@ -2,14 +2,12 @@ import { createAnatomy } from "@destyler/anatomy";
|
|
|
2
2
|
import { addDomEvent, createScope, dataAttr, getOverflowAncestors, isComposingEvent } from "@destyler/dom";
|
|
3
3
|
import { isFocusVisible, trackFocusVisible } from "@destyler/focus-visible";
|
|
4
4
|
import { getPlacement, getPlacementStyles } from "@destyler/popper";
|
|
5
|
-
import { createMachine, guards, proxy, subscribe } from "@destyler/xstate";
|
|
5
|
+
import { ActionTypes, createMachine, guards, proxy, subscribe } from "@destyler/xstate";
|
|
6
6
|
import { compact, createSplitProps, isControlled, resolveControllableOpen, withControllableProvided } from "@destyler/utils";
|
|
7
7
|
import { createProps } from "@destyler/types";
|
|
8
|
-
|
|
9
8
|
//#region src/anatomy.ts
|
|
10
9
|
const anatomy = createAnatomy("tooltip").parts("trigger", "arrow", "arrowTip", "positioner", "content");
|
|
11
10
|
const parts = anatomy.build();
|
|
12
|
-
|
|
13
11
|
//#endregion
|
|
14
12
|
//#region src/dom.ts
|
|
15
13
|
const dom = createScope({
|
|
@@ -22,7 +20,6 @@ const dom = createScope({
|
|
|
22
20
|
getPositionerEl: (ctx) => dom.getById(ctx, dom.getPositionerId(ctx)),
|
|
23
21
|
getArrowEl: (ctx) => dom.getById(ctx, dom.getArrowId(ctx))
|
|
24
22
|
});
|
|
25
|
-
|
|
26
23
|
//#endregion
|
|
27
24
|
//#region src/store.ts
|
|
28
25
|
const store = proxy({
|
|
@@ -33,7 +30,6 @@ const store = proxy({
|
|
|
33
30
|
this.id = val;
|
|
34
31
|
}
|
|
35
32
|
});
|
|
36
|
-
|
|
37
33
|
//#endregion
|
|
38
34
|
//#region src/connect.ts
|
|
39
35
|
function connect(state, send, normalize) {
|
|
@@ -50,7 +46,8 @@ function connect(state, send, normalize) {
|
|
|
50
46
|
return {
|
|
51
47
|
open,
|
|
52
48
|
setOpen(nextOpen) {
|
|
53
|
-
|
|
49
|
+
const pending = state.matches("opening") || state.matches("closing");
|
|
50
|
+
if (nextOpen === open && !pending) return;
|
|
54
51
|
send(nextOpen ? "OPEN" : "CLOSE");
|
|
55
52
|
},
|
|
56
53
|
reposition(options = {}) {
|
|
@@ -162,10 +159,11 @@ function connect(state, send, normalize) {
|
|
|
162
159
|
}
|
|
163
160
|
};
|
|
164
161
|
}
|
|
165
|
-
|
|
166
162
|
//#endregion
|
|
167
163
|
//#region src/machine.ts
|
|
168
164
|
const { and, not } = guards;
|
|
165
|
+
const ownerKeys = /* @__PURE__ */ new WeakMap();
|
|
166
|
+
let visibleOwnerKey;
|
|
169
167
|
function machine(userContext) {
|
|
170
168
|
const ctx = compact(withControllableProvided(userContext, ["open"]));
|
|
171
169
|
const { initialOpen } = resolveControllableOpen(ctx);
|
|
@@ -173,6 +171,7 @@ function machine(userContext) {
|
|
|
173
171
|
id: "tooltip",
|
|
174
172
|
initial: initialOpen ? "open" : "closed",
|
|
175
173
|
activities: ["trackFocusVisible"],
|
|
174
|
+
exit: ["clearGlobalId"],
|
|
176
175
|
context: {
|
|
177
176
|
openDelay: 1e3,
|
|
178
177
|
closeDelay: 500,
|
|
@@ -190,7 +189,7 @@ function machine(userContext) {
|
|
|
190
189
|
...ctx.positioning
|
|
191
190
|
}
|
|
192
191
|
},
|
|
193
|
-
computed: { hasAriaLabel: (ctx
|
|
192
|
+
computed: { hasAriaLabel: (ctx) => !!ctx["aria-label"] },
|
|
194
193
|
watch: {
|
|
195
194
|
disabled: ["closeIfDisabled"],
|
|
196
195
|
open: ["toggleVisibility"]
|
|
@@ -209,14 +208,21 @@ function machine(userContext) {
|
|
|
209
208
|
actions: ["invokeOnOpen"]
|
|
210
209
|
}],
|
|
211
210
|
"POINTER_LEAVE": { actions: ["clearPointerMoveOpened"] },
|
|
212
|
-
"POINTER_MOVE": [
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
211
|
+
"POINTER_MOVE": [
|
|
212
|
+
{
|
|
213
|
+
guard: and("noVisibleTooltip", not("hasPointerMoveOpened")),
|
|
214
|
+
target: "opening"
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
guard: and("isOpenControlled", not("hasPointerMoveOpened")),
|
|
218
|
+
actions: ["setPointerMoveOpened", "invokeOnOpen"]
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
guard: not("hasPointerMoveOpened"),
|
|
222
|
+
target: "open",
|
|
223
|
+
actions: ["setPointerMoveOpened", "invokeOnOpen"]
|
|
224
|
+
}
|
|
225
|
+
]
|
|
220
226
|
}
|
|
221
227
|
},
|
|
222
228
|
opening: {
|
|
@@ -312,6 +318,7 @@ function machine(userContext) {
|
|
|
312
318
|
on: {
|
|
313
319
|
"CONTROLLED.CLOSE": "closed",
|
|
314
320
|
"CONTROLLED.OPEN": "open",
|
|
321
|
+
"OPEN": "open",
|
|
315
322
|
"CLOSE": [{
|
|
316
323
|
guard: "isOpenControlled",
|
|
317
324
|
actions: ["invokeOnClose"]
|
|
@@ -340,30 +347,30 @@ function machine(userContext) {
|
|
|
340
347
|
}
|
|
341
348
|
}, {
|
|
342
349
|
activities: {
|
|
343
|
-
trackFocusVisible(ctx
|
|
344
|
-
return trackFocusVisible({ root: dom.getRootNode(ctx
|
|
350
|
+
trackFocusVisible(ctx) {
|
|
351
|
+
return trackFocusVisible({ root: dom.getRootNode(ctx) });
|
|
345
352
|
},
|
|
346
|
-
trackPositioning(ctx
|
|
347
|
-
ctx
|
|
348
|
-
const getPositionerEl = () => dom.getPositionerEl(ctx
|
|
349
|
-
return getPlacement(dom.getTriggerEl(ctx
|
|
350
|
-
...ctx
|
|
353
|
+
trackPositioning(ctx) {
|
|
354
|
+
ctx.currentPlacement ||= ctx.positioning.placement;
|
|
355
|
+
const getPositionerEl = () => dom.getPositionerEl(ctx);
|
|
356
|
+
return getPlacement(dom.getTriggerEl(ctx), getPositionerEl, {
|
|
357
|
+
...ctx.positioning,
|
|
351
358
|
defer: true,
|
|
352
359
|
onComplete(data) {
|
|
353
|
-
ctx
|
|
360
|
+
ctx.currentPlacement = data.placement;
|
|
354
361
|
}
|
|
355
362
|
});
|
|
356
363
|
},
|
|
357
|
-
trackPointerlockChange(ctx
|
|
364
|
+
trackPointerlockChange(ctx, _evt, { send }) {
|
|
358
365
|
const onChange = () => send({
|
|
359
366
|
type: "CLOSE",
|
|
360
367
|
src: "pointerlock:change"
|
|
361
368
|
});
|
|
362
|
-
return addDomEvent(dom.getDoc(ctx
|
|
369
|
+
return addDomEvent(dom.getDoc(ctx), "pointerlockchange", onChange, false);
|
|
363
370
|
},
|
|
364
|
-
trackScroll(ctx
|
|
365
|
-
if (!ctx
|
|
366
|
-
const triggerEl = dom.getTriggerEl(ctx
|
|
371
|
+
trackScroll(ctx, _evt, { send }) {
|
|
372
|
+
if (!ctx.closeOnScroll) return;
|
|
373
|
+
const triggerEl = dom.getTriggerEl(ctx);
|
|
367
374
|
if (!triggerEl) return;
|
|
368
375
|
const cleanups = getOverflowAncestors(triggerEl).map((overflowParent) => {
|
|
369
376
|
const onScroll = () => {
|
|
@@ -381,16 +388,16 @@ function machine(userContext) {
|
|
|
381
388
|
cleanups.forEach((fn) => fn?.());
|
|
382
389
|
};
|
|
383
390
|
},
|
|
384
|
-
trackStore(ctx
|
|
391
|
+
trackStore(ctx, _evt, { send }) {
|
|
385
392
|
return subscribe(store, () => {
|
|
386
|
-
if (store.id !== ctx
|
|
393
|
+
if (store.id !== ctx.id) send({
|
|
387
394
|
type: "CLOSE",
|
|
388
395
|
src: "id.change"
|
|
389
396
|
});
|
|
390
397
|
});
|
|
391
398
|
},
|
|
392
|
-
trackEscapeKey(ctx
|
|
393
|
-
if (!ctx
|
|
399
|
+
trackEscapeKey(ctx, _evt, { send }) {
|
|
400
|
+
if (!ctx.closeOnEscape) return;
|
|
394
401
|
const onKeyDown = (event) => {
|
|
395
402
|
if (isComposingEvent(event)) return;
|
|
396
403
|
if (event.key !== "Escape") return;
|
|
@@ -400,70 +407,79 @@ function machine(userContext) {
|
|
|
400
407
|
src: "keydown.escape"
|
|
401
408
|
});
|
|
402
409
|
};
|
|
403
|
-
return addDomEvent(dom.getDoc(ctx
|
|
410
|
+
return addDomEvent(dom.getDoc(ctx), "keydown", onKeyDown, true);
|
|
404
411
|
}
|
|
405
412
|
},
|
|
406
413
|
actions: {
|
|
407
|
-
setGlobalId(ctx
|
|
408
|
-
|
|
414
|
+
setGlobalId(ctx) {
|
|
415
|
+
let ownerKey = ownerKeys.get(ctx);
|
|
416
|
+
if (!ownerKey) {
|
|
417
|
+
ownerKey = Symbol("tooltip-owner");
|
|
418
|
+
ownerKeys.set(ctx, ownerKey);
|
|
419
|
+
}
|
|
420
|
+
visibleOwnerKey = ownerKey;
|
|
421
|
+
store.setId(ctx.id);
|
|
409
422
|
},
|
|
410
|
-
clearGlobalId(ctx
|
|
411
|
-
if (
|
|
423
|
+
clearGlobalId(ctx, event) {
|
|
424
|
+
if (event.type === ActionTypes.Stop && ownerKeys.get(ctx) !== visibleOwnerKey) return;
|
|
425
|
+
if (ctx.id === store.id) {
|
|
426
|
+
visibleOwnerKey = void 0;
|
|
427
|
+
store.setId(null);
|
|
428
|
+
}
|
|
412
429
|
},
|
|
413
|
-
invokeOnOpen(ctx
|
|
414
|
-
ctx
|
|
430
|
+
invokeOnOpen(ctx) {
|
|
431
|
+
ctx.onOpenChange?.({ open: true });
|
|
415
432
|
},
|
|
416
|
-
invokeOnClose(ctx
|
|
417
|
-
ctx
|
|
433
|
+
invokeOnClose(ctx) {
|
|
434
|
+
ctx.onOpenChange?.({ open: false });
|
|
418
435
|
},
|
|
419
|
-
closeIfDisabled(ctx
|
|
420
|
-
if (!ctx
|
|
436
|
+
closeIfDisabled(ctx, _evt, { send }) {
|
|
437
|
+
if (!ctx.disabled) return;
|
|
421
438
|
send({
|
|
422
439
|
type: "CLOSE",
|
|
423
440
|
src: "disabled.change"
|
|
424
441
|
});
|
|
425
442
|
},
|
|
426
|
-
reposition(ctx
|
|
427
|
-
const getPositionerEl = () => dom.getPositionerEl(ctx
|
|
428
|
-
getPlacement(dom.getTriggerEl(ctx
|
|
429
|
-
...ctx
|
|
443
|
+
reposition(ctx, evt) {
|
|
444
|
+
const getPositionerEl = () => dom.getPositionerEl(ctx);
|
|
445
|
+
getPlacement(dom.getTriggerEl(ctx), getPositionerEl, {
|
|
446
|
+
...ctx.positioning,
|
|
430
447
|
...evt.options,
|
|
431
448
|
defer: true,
|
|
432
449
|
listeners: false,
|
|
433
450
|
onComplete(data) {
|
|
434
|
-
ctx
|
|
451
|
+
ctx.currentPlacement = data.placement;
|
|
435
452
|
}
|
|
436
453
|
});
|
|
437
454
|
},
|
|
438
|
-
toggleVisibility(ctx
|
|
455
|
+
toggleVisibility(ctx, evt, { send }) {
|
|
439
456
|
queueMicrotask(() => {
|
|
440
457
|
send({
|
|
441
|
-
type: ctx
|
|
458
|
+
type: ctx.open ? "CONTROLLED.OPEN" : "CONTROLLED.CLOSE",
|
|
442
459
|
previousEvent: evt
|
|
443
460
|
});
|
|
444
461
|
});
|
|
445
462
|
},
|
|
446
|
-
setPointerMoveOpened(ctx
|
|
447
|
-
ctx
|
|
463
|
+
setPointerMoveOpened(ctx) {
|
|
464
|
+
ctx.hasPointerMoveOpened = true;
|
|
448
465
|
},
|
|
449
|
-
clearPointerMoveOpened(ctx
|
|
450
|
-
ctx
|
|
466
|
+
clearPointerMoveOpened(ctx) {
|
|
467
|
+
ctx.hasPointerMoveOpened = false;
|
|
451
468
|
}
|
|
452
469
|
},
|
|
453
470
|
guards: {
|
|
454
471
|
noVisibleTooltip: () => store.id === null,
|
|
455
|
-
isVisible: (ctx
|
|
456
|
-
isInteractive: (ctx
|
|
457
|
-
hasPointerMoveOpened: (ctx
|
|
458
|
-
isOpenControlled: (ctx
|
|
472
|
+
isVisible: (ctx) => ctx.id === store.id,
|
|
473
|
+
isInteractive: (ctx) => ctx.interactive,
|
|
474
|
+
hasPointerMoveOpened: (ctx) => !!ctx.hasPointerMoveOpened,
|
|
475
|
+
isOpenControlled: (ctx) => isControlled(ctx, "open")
|
|
459
476
|
},
|
|
460
477
|
delays: {
|
|
461
|
-
OPEN_DELAY: (ctx
|
|
462
|
-
CLOSE_DELAY: (ctx
|
|
478
|
+
OPEN_DELAY: (ctx) => ctx.openDelay,
|
|
479
|
+
CLOSE_DELAY: (ctx) => ctx.closeDelay
|
|
463
480
|
}
|
|
464
481
|
});
|
|
465
482
|
}
|
|
466
|
-
|
|
467
483
|
//#endregion
|
|
468
484
|
//#region src/props.ts
|
|
469
485
|
const props = createProps()([
|
|
@@ -486,6 +502,5 @@ const props = createProps()([
|
|
|
486
502
|
"positioning"
|
|
487
503
|
]);
|
|
488
504
|
const splitProps = createSplitProps(props);
|
|
489
|
-
|
|
490
505
|
//#endregion
|
|
491
|
-
export { anatomy, connect, machine, props, splitProps };
|
|
506
|
+
export { anatomy, connect, machine, props, splitProps };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@destyler/tooltip",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.12",
|
|
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",
|
|
@@ -22,13 +22,13 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@destyler/anatomy": "0.2.
|
|
26
|
-
"@destyler/
|
|
27
|
-
"@destyler/
|
|
28
|
-
"@destyler/
|
|
29
|
-
"@destyler/
|
|
30
|
-
"@destyler/
|
|
31
|
-
"@destyler/
|
|
25
|
+
"@destyler/anatomy": "0.2.12",
|
|
26
|
+
"@destyler/dom": "0.2.12",
|
|
27
|
+
"@destyler/focus-visible": "0.2.12",
|
|
28
|
+
"@destyler/popper": "0.2.12",
|
|
29
|
+
"@destyler/types": "0.2.12",
|
|
30
|
+
"@destyler/utils": "0.2.12",
|
|
31
|
+
"@destyler/xstate": "0.2.12"
|
|
32
32
|
},
|
|
33
33
|
"publishConfig": {
|
|
34
34
|
"access": "public"
|