@zag-js/tooltip 0.0.0-dev-20230706120929 → 0.0.0-dev-20230710093134

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.
@@ -1,133 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const domQuery = require('@zag-js/dom-query');
6
- const popper = require('@zag-js/popper');
7
- const visuallyHidden = require('@zag-js/visually-hidden');
8
- const tooltip_anatomy = require('./tooltip.anatomy.js');
9
- const tooltip_dom = require('./tooltip.dom.js');
10
- const tooltip_store = require('./tooltip.store.js');
11
-
12
- function connect(state, send, normalize) {
13
- const id = state.context.id;
14
- const hasAriaLabel = state.context.hasAriaLabel;
15
- const isOpen = state.hasTag("open");
16
- const triggerId = tooltip_dom.dom.getTriggerId(state.context);
17
- const contentId = tooltip_dom.dom.getContentId(state.context);
18
- const isDisabled = state.context.disabled;
19
- const popperStyles = popper.getPlacementStyles({
20
- placement: state.context.currentPlacement
21
- });
22
- return {
23
- /**
24
- * Whether the tooltip is open.
25
- */
26
- isOpen,
27
- /**
28
- * Function to open the tooltip.
29
- */
30
- open() {
31
- send("OPEN");
32
- },
33
- /**
34
- * Function to close the tooltip.
35
- */
36
- close() {
37
- send("CLOSE");
38
- },
39
- /**
40
- * Returns the animation state of the tooltip.
41
- */
42
- getAnimationState() {
43
- return {
44
- enter: tooltip_store.store.prevId === null && id === tooltip_store.store.id,
45
- exit: tooltip_store.store.id === null
46
- };
47
- },
48
- /**
49
- * Function to reposition the popover
50
- */
51
- setPositioning(options = {}) {
52
- send({ type: "SET_POSITIONING", options });
53
- },
54
- triggerProps: normalize.button({
55
- ...tooltip_anatomy.parts.trigger.attrs,
56
- id: triggerId,
57
- "data-expanded": domQuery.dataAttr(isOpen),
58
- "aria-describedby": isOpen ? contentId : void 0,
59
- onClick() {
60
- send("CLICK");
61
- },
62
- onFocus() {
63
- send("FOCUS");
64
- },
65
- onBlur() {
66
- if (id === tooltip_store.store.id) {
67
- send("BLUR");
68
- }
69
- },
70
- onPointerDown() {
71
- if (isDisabled)
72
- return;
73
- if (id === tooltip_store.store.id) {
74
- send("POINTER_DOWN");
75
- }
76
- },
77
- onPointerMove() {
78
- if (isDisabled)
79
- return;
80
- send("POINTER_ENTER");
81
- },
82
- onPointerLeave() {
83
- if (isDisabled)
84
- return;
85
- send("POINTER_LEAVE");
86
- },
87
- onPointerCancel() {
88
- if (isDisabled)
89
- return;
90
- send("POINTER_LEAVE");
91
- }
92
- }),
93
- arrowProps: normalize.element({
94
- id: tooltip_dom.dom.getArrowId(state.context),
95
- ...tooltip_anatomy.parts.arrow.attrs,
96
- style: popperStyles.arrow
97
- }),
98
- arrowTipProps: normalize.element({
99
- ...tooltip_anatomy.parts.arrowTip.attrs,
100
- style: popperStyles.arrowTip
101
- }),
102
- positionerProps: normalize.element({
103
- id: tooltip_dom.dom.getPositionerId(state.context),
104
- ...tooltip_anatomy.parts.positioner.attrs,
105
- style: popperStyles.floating
106
- }),
107
- contentProps: normalize.element({
108
- ...tooltip_anatomy.parts.content.attrs,
109
- hidden: !isOpen,
110
- role: hasAriaLabel ? void 0 : "tooltip",
111
- id: hasAriaLabel ? void 0 : contentId,
112
- "data-placement": state.context.currentPlacement,
113
- onPointerEnter() {
114
- send("TOOLTIP_POINTER_ENTER");
115
- },
116
- onPointerLeave() {
117
- send("TOOLTIP_POINTER_LEAVE");
118
- },
119
- style: {
120
- pointerEvents: state.context.interactive ? "auto" : "none"
121
- }
122
- }),
123
- labelProps: normalize.element({
124
- ...tooltip_anatomy.parts.label.attrs,
125
- id: contentId,
126
- role: "tooltip",
127
- style: visuallyHidden.visuallyHiddenStyle,
128
- children: state.context["aria-label"]
129
- })
130
- };
131
- }
132
-
133
- exports.connect = connect;
@@ -1,129 +0,0 @@
1
- import { dataAttr } from '@zag-js/dom-query';
2
- import { getPlacementStyles } from '@zag-js/popper';
3
- import { visuallyHiddenStyle } from '@zag-js/visually-hidden';
4
- import { parts } from './tooltip.anatomy.mjs';
5
- import { dom } from './tooltip.dom.mjs';
6
- import { store } from './tooltip.store.mjs';
7
-
8
- function connect(state, send, normalize) {
9
- const id = state.context.id;
10
- const hasAriaLabel = state.context.hasAriaLabel;
11
- const isOpen = state.hasTag("open");
12
- const triggerId = dom.getTriggerId(state.context);
13
- const contentId = dom.getContentId(state.context);
14
- const isDisabled = state.context.disabled;
15
- const popperStyles = getPlacementStyles({
16
- placement: state.context.currentPlacement
17
- });
18
- return {
19
- /**
20
- * Whether the tooltip is open.
21
- */
22
- isOpen,
23
- /**
24
- * Function to open the tooltip.
25
- */
26
- open() {
27
- send("OPEN");
28
- },
29
- /**
30
- * Function to close the tooltip.
31
- */
32
- close() {
33
- send("CLOSE");
34
- },
35
- /**
36
- * Returns the animation state of the tooltip.
37
- */
38
- getAnimationState() {
39
- return {
40
- enter: store.prevId === null && id === store.id,
41
- exit: store.id === null
42
- };
43
- },
44
- /**
45
- * Function to reposition the popover
46
- */
47
- setPositioning(options = {}) {
48
- send({ type: "SET_POSITIONING", options });
49
- },
50
- triggerProps: normalize.button({
51
- ...parts.trigger.attrs,
52
- id: triggerId,
53
- "data-expanded": dataAttr(isOpen),
54
- "aria-describedby": isOpen ? contentId : void 0,
55
- onClick() {
56
- send("CLICK");
57
- },
58
- onFocus() {
59
- send("FOCUS");
60
- },
61
- onBlur() {
62
- if (id === store.id) {
63
- send("BLUR");
64
- }
65
- },
66
- onPointerDown() {
67
- if (isDisabled)
68
- return;
69
- if (id === store.id) {
70
- send("POINTER_DOWN");
71
- }
72
- },
73
- onPointerMove() {
74
- if (isDisabled)
75
- return;
76
- send("POINTER_ENTER");
77
- },
78
- onPointerLeave() {
79
- if (isDisabled)
80
- return;
81
- send("POINTER_LEAVE");
82
- },
83
- onPointerCancel() {
84
- if (isDisabled)
85
- return;
86
- send("POINTER_LEAVE");
87
- }
88
- }),
89
- arrowProps: normalize.element({
90
- id: dom.getArrowId(state.context),
91
- ...parts.arrow.attrs,
92
- style: popperStyles.arrow
93
- }),
94
- arrowTipProps: normalize.element({
95
- ...parts.arrowTip.attrs,
96
- style: popperStyles.arrowTip
97
- }),
98
- positionerProps: normalize.element({
99
- id: dom.getPositionerId(state.context),
100
- ...parts.positioner.attrs,
101
- style: popperStyles.floating
102
- }),
103
- contentProps: normalize.element({
104
- ...parts.content.attrs,
105
- hidden: !isOpen,
106
- role: hasAriaLabel ? void 0 : "tooltip",
107
- id: hasAriaLabel ? void 0 : contentId,
108
- "data-placement": state.context.currentPlacement,
109
- onPointerEnter() {
110
- send("TOOLTIP_POINTER_ENTER");
111
- },
112
- onPointerLeave() {
113
- send("TOOLTIP_POINTER_LEAVE");
114
- },
115
- style: {
116
- pointerEvents: state.context.interactive ? "auto" : "none"
117
- }
118
- }),
119
- labelProps: normalize.element({
120
- ...parts.label.attrs,
121
- id: contentId,
122
- role: "tooltip",
123
- style: visuallyHiddenStyle,
124
- children: state.context["aria-label"]
125
- })
126
- };
127
- }
128
-
129
- export { connect };
@@ -1,31 +0,0 @@
1
- import type { MachineContext as Ctx } from "./tooltip.types";
2
- export declare const dom: {
3
- getRootNode: (ctx: {
4
- getRootNode?: (() => Node | ShadowRoot | Document) | undefined;
5
- }) => ShadowRoot | Document;
6
- getDoc: (ctx: {
7
- getRootNode?: (() => Node | ShadowRoot | Document) | undefined;
8
- }) => Document;
9
- getWin: (ctx: {
10
- getRootNode?: (() => Node | ShadowRoot | Document) | undefined;
11
- }) => Window & typeof globalThis;
12
- getActiveElement: (ctx: {
13
- getRootNode?: (() => Node | ShadowRoot | Document) | undefined;
14
- }) => HTMLElement | null;
15
- getById: <T extends HTMLElement = HTMLElement>(ctx: {
16
- getRootNode?: (() => Node | ShadowRoot | Document) | undefined;
17
- }, id: string) => T | null;
18
- queryById: <T_1 extends HTMLElement = HTMLElement>(ctx: {
19
- getRootNode?: (() => Node | ShadowRoot | Document) | undefined;
20
- }, id: string) => T_1;
21
- } & {
22
- getTriggerId: (ctx: Ctx) => string;
23
- getContentId: (ctx: Ctx) => string;
24
- getArrowId: (ctx: Ctx) => string;
25
- getPositionerId: (ctx: Ctx) => string;
26
- getTriggerEl: (ctx: Ctx) => HTMLElement | null;
27
- getContentEl: (ctx: Ctx) => HTMLElement | null;
28
- getPositionerEl: (ctx: Ctx) => HTMLElement | null;
29
- getArrowEl: (ctx: Ctx) => HTMLElement | null;
30
- getScrollParent: (ctx: Ctx) => HTMLElement;
31
- };
@@ -1,19 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const domQuery = require('@zag-js/dom-query');
6
-
7
- const dom = domQuery.createScope({
8
- getTriggerId: (ctx) => ctx.ids?.trigger ?? `tooltip:${ctx.id}:trigger`,
9
- getContentId: (ctx) => ctx.ids?.content ?? `tooltip:${ctx.id}:content`,
10
- getArrowId: (ctx) => ctx.ids?.arrow ?? `tooltip:${ctx.id}:arrow`,
11
- getPositionerId: (ctx) => ctx.ids?.positioner ?? `tooltip:${ctx.id}:popper`,
12
- getTriggerEl: (ctx) => dom.getById(ctx, dom.getTriggerId(ctx)),
13
- getContentEl: (ctx) => dom.getById(ctx, dom.getContentId(ctx)),
14
- getPositionerEl: (ctx) => dom.getById(ctx, dom.getPositionerId(ctx)),
15
- getArrowEl: (ctx) => dom.getById(ctx, dom.getArrowId(ctx)),
16
- getScrollParent: (ctx) => domQuery.getScrollParent(dom.getTriggerEl(ctx))
17
- });
18
-
19
- exports.dom = dom;
@@ -1,15 +0,0 @@
1
- import { createScope, getScrollParent } from '@zag-js/dom-query';
2
-
3
- const dom = createScope({
4
- getTriggerId: (ctx) => ctx.ids?.trigger ?? `tooltip:${ctx.id}:trigger`,
5
- getContentId: (ctx) => ctx.ids?.content ?? `tooltip:${ctx.id}:content`,
6
- getArrowId: (ctx) => ctx.ids?.arrow ?? `tooltip:${ctx.id}:arrow`,
7
- getPositionerId: (ctx) => ctx.ids?.positioner ?? `tooltip:${ctx.id}:popper`,
8
- getTriggerEl: (ctx) => dom.getById(ctx, dom.getTriggerId(ctx)),
9
- getContentEl: (ctx) => dom.getById(ctx, dom.getContentId(ctx)),
10
- getPositionerEl: (ctx) => dom.getById(ctx, dom.getPositionerId(ctx)),
11
- getArrowEl: (ctx) => dom.getById(ctx, dom.getArrowId(ctx)),
12
- getScrollParent: (ctx) => getScrollParent(dom.getTriggerEl(ctx))
13
- });
14
-
15
- export { dom };
@@ -1,3 +0,0 @@
1
- import type { Machine, StateMachine } from '@zag-js/core';
2
- import type { MachineContext, MachineState, UserDefinedContext } from "./tooltip.types";
3
- export declare function machine(userContext: UserDefinedContext): Machine<MachineContext, MachineState, StateMachine.AnyEventObject>;
@@ -1,239 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const core = require('@zag-js/core');
6
- const domEvent = require('@zag-js/dom-event');
7
- const domQuery = require('@zag-js/dom-query');
8
- const popper = require('@zag-js/popper');
9
- const utils = require('@zag-js/utils');
10
- const tooltip_dom = require('./tooltip.dom.js');
11
- const tooltip_store = require('./tooltip.store.js');
12
-
13
- function machine(userContext) {
14
- const ctx = utils.compact(userContext);
15
- return core.createMachine(
16
- {
17
- id: "tooltip",
18
- initial: "closed",
19
- context: {
20
- openDelay: 1e3,
21
- closeDelay: 500,
22
- closeOnPointerDown: true,
23
- closeOnEsc: true,
24
- interactive: true,
25
- currentPlacement: void 0,
26
- ...ctx,
27
- positioning: {
28
- placement: "bottom",
29
- ...ctx.positioning
30
- }
31
- },
32
- computed: {
33
- hasAriaLabel: (ctx2) => !!ctx2["aria-label"]
34
- },
35
- watch: {
36
- disabled: ["closeIfDisabled"],
37
- open: ["toggleVisibility"]
38
- },
39
- on: {
40
- OPEN: "open",
41
- CLOSE: "closed"
42
- },
43
- states: {
44
- closed: {
45
- tags: ["closed"],
46
- entry: ["clearGlobalId", "invokeOnClose"],
47
- on: {
48
- FOCUS: "open",
49
- POINTER_ENTER: [
50
- {
51
- guard: "noVisibleTooltip",
52
- target: "opening"
53
- },
54
- { target: "open" }
55
- ]
56
- }
57
- },
58
- opening: {
59
- tags: ["closed"],
60
- activities: ["trackScroll", "trackPointerlockChange"],
61
- after: {
62
- OPEN_DELAY: "open"
63
- },
64
- on: {
65
- POINTER_LEAVE: "closed",
66
- BLUR: "closed",
67
- SCROLL: "closed",
68
- POINTER_LOCK_CHANGE: "closed",
69
- POINTER_DOWN: {
70
- guard: "closeOnPointerDown",
71
- target: "closed"
72
- }
73
- }
74
- },
75
- open: {
76
- tags: ["open"],
77
- activities: [
78
- "trackEscapeKey",
79
- "trackDisabledTriggerOnSafari",
80
- "trackScroll",
81
- "trackPointerlockChange",
82
- "trackPositioning"
83
- ],
84
- entry: ["setGlobalId", "invokeOnOpen"],
85
- on: {
86
- POINTER_LEAVE: [
87
- {
88
- guard: "isVisible",
89
- target: "closing"
90
- },
91
- { target: "closed" }
92
- ],
93
- BLUR: "closed",
94
- ESCAPE: "closed",
95
- SCROLL: "closed",
96
- POINTER_LOCK_CHANGE: "closed",
97
- TOOLTIP_POINTER_LEAVE: {
98
- guard: "isInteractive",
99
- target: "closing"
100
- },
101
- POINTER_DOWN: {
102
- guard: "closeOnPointerDown",
103
- target: "closed"
104
- },
105
- CLICK: "closed",
106
- SET_POSITIONING: {
107
- actions: "setPositioning"
108
- }
109
- }
110
- },
111
- closing: {
112
- tags: ["open"],
113
- activities: ["trackStore", "trackPositioning"],
114
- after: {
115
- CLOSE_DELAY: "closed"
116
- },
117
- on: {
118
- FORCE_CLOSE: "closed",
119
- POINTER_ENTER: "open",
120
- TOOLTIP_POINTER_ENTER: {
121
- guard: "isInteractive",
122
- target: "open"
123
- }
124
- }
125
- }
126
- }
127
- },
128
- {
129
- activities: {
130
- trackPositioning(ctx2) {
131
- ctx2.currentPlacement = ctx2.positioning.placement;
132
- const getPositionerEl = () => tooltip_dom.dom.getPositionerEl(ctx2);
133
- return popper.getPlacement(tooltip_dom.dom.getTriggerEl(ctx2), getPositionerEl, {
134
- ...ctx2.positioning,
135
- defer: true,
136
- onComplete(data) {
137
- ctx2.currentPlacement = data.placement;
138
- },
139
- onCleanup() {
140
- ctx2.currentPlacement = void 0;
141
- }
142
- });
143
- },
144
- trackPointerlockChange(ctx2, _evt, { send }) {
145
- const onChange = () => send("POINTER_LOCK_CHANGE");
146
- return domEvent.addDomEvent(tooltip_dom.dom.getDoc(ctx2), "pointerlockchange", onChange, false);
147
- },
148
- trackScroll(ctx2, _evt, { send }) {
149
- const trigger = tooltip_dom.dom.getTriggerEl(ctx2);
150
- if (!trigger)
151
- return;
152
- const cleanups = domQuery.getScrollParents(trigger).map((el) => {
153
- const opts = { passive: true, capture: true };
154
- return domEvent.addDomEvent(el, "scroll", () => send("SCROLL"), opts);
155
- });
156
- return () => {
157
- cleanups.forEach((fn) => fn?.());
158
- };
159
- },
160
- trackStore(ctx2, _evt, { send }) {
161
- return core.subscribe(tooltip_store.store, () => {
162
- if (tooltip_store.store.id !== ctx2.id) {
163
- send("FORCE_CLOSE");
164
- }
165
- });
166
- },
167
- trackDisabledTriggerOnSafari(ctx2, _evt, { send }) {
168
- if (!domQuery.isSafari())
169
- return;
170
- const doc = tooltip_dom.dom.getDoc(ctx2);
171
- return domEvent.addDomEvent(doc, "pointermove", (event) => {
172
- const selector = "[data-part=trigger][data-expanded]";
173
- if (domQuery.isHTMLElement(event.target) && event.target.closest(selector))
174
- return;
175
- send("POINTER_LEAVE");
176
- });
177
- },
178
- trackEscapeKey(ctx2, _evt, { send }) {
179
- if (!ctx2.closeOnEsc)
180
- return;
181
- const doc = tooltip_dom.dom.getDoc(ctx2);
182
- return domEvent.addDomEvent(doc, "keydown", (event) => {
183
- if (event.key === "Escape") {
184
- send("ESCAPE");
185
- }
186
- });
187
- }
188
- },
189
- actions: {
190
- setGlobalId(ctx2) {
191
- tooltip_store.store.setId(ctx2.id);
192
- },
193
- clearGlobalId(ctx2) {
194
- if (ctx2.id === tooltip_store.store.id) {
195
- tooltip_store.store.setId(null);
196
- }
197
- },
198
- invokeOnOpen(ctx2, evt) {
199
- const omit = ["TOOLTIP_POINTER_ENTER", "POINTER_ENTER"];
200
- if (!omit.includes(evt.type)) {
201
- ctx2.onOpen?.();
202
- }
203
- },
204
- invokeOnClose(ctx2) {
205
- ctx2.onClose?.();
206
- },
207
- closeIfDisabled(ctx2, _evt, { send }) {
208
- if (ctx2.disabled) {
209
- send("CLOSE");
210
- }
211
- },
212
- setPositioning(ctx2, evt) {
213
- const getPositionerEl = () => tooltip_dom.dom.getPositionerEl(ctx2);
214
- popper.getPlacement(tooltip_dom.dom.getTriggerEl(ctx2), getPositionerEl, {
215
- ...ctx2.positioning,
216
- ...evt.options,
217
- defer: true,
218
- listeners: false
219
- });
220
- },
221
- toggleVisibility(ctx2, _evt, { send }) {
222
- send({ type: ctx2.open ? "OPEN" : "CLOSE", src: "controlled" });
223
- }
224
- },
225
- guards: {
226
- closeOnPointerDown: (ctx2) => ctx2.closeOnPointerDown,
227
- noVisibleTooltip: () => tooltip_store.store.id === null,
228
- isVisible: (ctx2) => ctx2.id === tooltip_store.store.id,
229
- isInteractive: (ctx2) => ctx2.interactive
230
- },
231
- delays: {
232
- OPEN_DELAY: (ctx2) => ctx2.openDelay,
233
- CLOSE_DELAY: (ctx2) => ctx2.closeDelay
234
- }
235
- }
236
- );
237
- }
238
-
239
- exports.machine = machine;