tooltip-turn 0.0.0-stage → 0.1.0
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/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +159 -2
- package/dist/index.d.ts +134 -0
- package/dist/index.js +214 -0
- package/dist/react.d.ts +59 -0
- package/dist/react.js +80 -0
- package/package.json +71 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
First release.
|
|
6
|
+
|
|
7
|
+
- `createTooltipTurn` and the shared `tooltipTurn`: one tooltip on the page at a time. The first one a mouse rests on waits 400ms. While tooltips are warm, the next opens at once and replaces the open one. Taps, clicks and the keyboard never wait.
|
|
8
|
+
- `useTooltipTurn` for React, from `tooltip-turn/react`.
|
|
9
|
+
- Both delays can be changed.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Cameron Spears
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,160 @@
|
|
|
1
|
-
#
|
|
1
|
+
# tooltip-turn
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
One tooltip on the page at a time, the way a Mac does it, in about 1 KB minified and gzipped. No
|
|
4
|
+
dependencies.
|
|
5
|
+
|
|
6
|
+
[Demo](https://cameronspears.github.io/tooltip-turn/demo/) (rest on a button, slide along the row, then try the row without it)
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install tooltip-turn
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```js
|
|
13
|
+
import { tooltipTurn as turn } from 'tooltip-turn';
|
|
14
|
+
|
|
15
|
+
const button = document.querySelector('#save');
|
|
16
|
+
const tip = document.querySelector('#save-tip');
|
|
17
|
+
|
|
18
|
+
turn.subscribe(() => {
|
|
19
|
+
tip.hidden = !turn.stage(button).startsWith('open');
|
|
20
|
+
});
|
|
21
|
+
button.addEventListener('pointerenter', () => turn.ask(button, button));
|
|
22
|
+
button.addEventListener('pointerleave', () => turn.leave(button));
|
|
23
|
+
button.addEventListener('focus', () => turn.ask(button, button));
|
|
24
|
+
button.addEventListener('blur', () => turn.leave(button));
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Do the same for every tooltip on the page. They all share `tooltipTurn`, so only one is ever seen.
|
|
28
|
+
|
|
29
|
+
Or in React:
|
|
30
|
+
|
|
31
|
+
```jsx
|
|
32
|
+
import { useTooltipTurn } from 'tooltip-turn/react';
|
|
33
|
+
|
|
34
|
+
function SaveButton() {
|
|
35
|
+
const tip = useTooltipTurn('save-tip');
|
|
36
|
+
return (
|
|
37
|
+
<>
|
|
38
|
+
<button {...tip.anchorProps}>Save</button>
|
|
39
|
+
<div {...tip.tooltipProps} hidden={!tip.visible}>
|
|
40
|
+
Save your changes
|
|
41
|
+
</div>
|
|
42
|
+
</>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## How it behaves
|
|
48
|
+
|
|
49
|
+
<picture>
|
|
50
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/cameronspears/tooltip-turn/main/.github/assets/timing-dark.svg" />
|
|
51
|
+
<img alt="Timeline: the mouse rests on A, which waits 400ms and opens. Moving to B opens it at once and closes A. After leaving, tooltips stay warm for 500ms, then a rest on C waits 400ms again." src="https://raw.githubusercontent.com/cameronspears/tooltip-turn/main/.github/assets/timing-light.svg" />
|
|
52
|
+
</picture>
|
|
53
|
+
|
|
54
|
+
The whole page shares one turn, and only the tooltip that has it is shown. The first one a mouse
|
|
55
|
+
rests on waits 400 ms, so a mouse passing over a toolbar doesn't set tooltips off. Moving to another
|
|
56
|
+
anchor while it waits starts the wait again, so a quick sweep opens nothing.
|
|
57
|
+
|
|
58
|
+
Once one is open, the next opens as soon as you reach its anchor, and the open one goes. After the
|
|
59
|
+
last one closes, the next still opens at once for 500 ms. Then it goes back to waiting.
|
|
60
|
+
|
|
61
|
+
A click, a tap or a key never waits, because the person asked. While anyone is subscribed, the store
|
|
62
|
+
listens for `pointerdown`, `keydown`, `pointerover` and `pointerout` on `window` to tell a passing
|
|
63
|
+
mouse from a press. Pressing the anchor you're resting on opens its tooltip straight away.
|
|
64
|
+
|
|
65
|
+
Touch sends `pointerleave` as soon as the finger lifts, which would close the tooltip again. So for
|
|
66
|
+
taps, handle enter and leave for the mouse only, and ask on `pointerdown` with `{ hover: false }`,
|
|
67
|
+
which never waits:
|
|
68
|
+
|
|
69
|
+
```js
|
|
70
|
+
button.addEventListener('pointerenter', (e) => e.pointerType === 'mouse' && turn.ask(button, button));
|
|
71
|
+
button.addEventListener('pointerleave', (e) => e.pointerType === 'mouse' && turn.leave(button));
|
|
72
|
+
button.addEventListener('pointerdown', (e) => {
|
|
73
|
+
if (e.pointerType !== 'mouse') turn.ask(button, button, { hover: false });
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Each tooltip reads its own stage: `off`, `waiting`, `open`, `open-quick` or `replaced`. Show it only
|
|
78
|
+
for `open` and `open-quick`. If you fade tooltips, `open-quick` means it took over from another and
|
|
79
|
+
can come in faster, and `replaced` means it just lost the turn and should go at once, with no fade,
|
|
80
|
+
so two are never seen together.
|
|
81
|
+
|
|
82
|
+
[demo/index.html](demo/index.html) is a full hand-rolled example, with taps, Escape and
|
|
83
|
+
click-outside.
|
|
84
|
+
|
|
85
|
+
## React
|
|
86
|
+
|
|
87
|
+
`useTooltipTurn(id)` takes the tooltip element's id. Its `anchorProps` handle the mouse, focus and
|
|
88
|
+
blur, and point the anchor at the tooltip with `aria-describedby`. A tap opens the tooltip, and a
|
|
89
|
+
second tap closes it. While it's open, Escape or a press anywhere else closes it. It leaves the turn
|
|
90
|
+
when the component unmounts.
|
|
91
|
+
|
|
92
|
+
React is an optional peer dependency. The core never imports it.
|
|
93
|
+
|
|
94
|
+
## With react-tooltip or Floating UI
|
|
95
|
+
|
|
96
|
+
Let the library place and draw the tooltip, and let tooltip-turn decide when it shows.
|
|
97
|
+
|
|
98
|
+
Set the library's own show delay to 0. The wait lives in tooltip-turn. A second delay in the library
|
|
99
|
+
would add to it, and the hand-off from one tooltip to the next would no longer be instant.
|
|
100
|
+
|
|
101
|
+
Keep the library's tooltip mounted and hide it with CSS while it isn't its turn, for example
|
|
102
|
+
`opacity: 0; pointer-events: none`. The library still thinks it's open. You only keep it out of
|
|
103
|
+
sight until its turn.
|
|
104
|
+
|
|
105
|
+
With react-tooltip, use its `setIsOpen` callback:
|
|
106
|
+
|
|
107
|
+
```jsx
|
|
108
|
+
import { Tooltip } from 'react-tooltip';
|
|
109
|
+
import { tooltipTurn } from 'tooltip-turn';
|
|
110
|
+
import { useTooltipTurn } from 'tooltip-turn/react';
|
|
111
|
+
|
|
112
|
+
const tip = useTooltipTurn('save');
|
|
113
|
+
|
|
114
|
+
<Tooltip
|
|
115
|
+
id="save"
|
|
116
|
+
delayShow={0}
|
|
117
|
+
setIsOpen={(open) =>
|
|
118
|
+
open ? tip.ask(tooltipTurn.findAnchor('[data-tooltip-id="save"]')) : tip.leave()
|
|
119
|
+
}
|
|
120
|
+
className={tip.visible ? '' : 'off-stage'}
|
|
121
|
+
/>;
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
With Floating UI, call `tip.ask(anchor)` and `tip.leave()` from `onOpenChange`, and drop
|
|
125
|
+
`FloatingDelayGroup`, since tooltip-turn does that job.
|
|
126
|
+
|
|
127
|
+
When the library doesn't tell you the anchor, `findAnchor(selector)` returns the matching element
|
|
128
|
+
under the pointer, or else the focused one. If an anchor has nothing to show, don't ask for the
|
|
129
|
+
turn, or an empty tooltip will take it from a real one.
|
|
130
|
+
|
|
131
|
+
## Options
|
|
132
|
+
|
|
133
|
+
`createTooltipTurn(options)` makes a turn with other delays:
|
|
134
|
+
|
|
135
|
+
- `coldDelay` (default `400`): how long in ms a mouse rests on an anchor before the first tooltip
|
|
136
|
+
opens.
|
|
137
|
+
- `coolDown` (default `500`): how long in ms after the last tooltip closes the next still opens at
|
|
138
|
+
once.
|
|
139
|
+
|
|
140
|
+
Tooltips only take turns with others on the same turn, so share one. `tooltipTurn` is the page's
|
|
141
|
+
shared turn with the defaults. In React, pass your own with `useTooltipTurn(id, { store })`.
|
|
142
|
+
|
|
143
|
+
A turn has `subscribe`, `getState`, `stage(owner)`, `ask(owner, anchor, { hover })`,
|
|
144
|
+
`leave(owner)` and `findAnchor(selector)`. An owner can be anything that names one tooltip, compared
|
|
145
|
+
with `===`. Its shape fits React's `useSyncExternalStore`.
|
|
146
|
+
|
|
147
|
+
On the server every stage is `off`, so it's safe in SSR frameworks.
|
|
148
|
+
|
|
149
|
+
## Alternatives
|
|
150
|
+
|
|
151
|
+
Floating UI's `FloatingDelayGroup` and Radix's `Tooltip.Provider` do the same job inside their own
|
|
152
|
+
libraries. If every tooltip on your page already uses one of them, use theirs. tooltip-turn is for
|
|
153
|
+
pages that mix libraries, use react-tooltip, or draw their own tooltips.
|
|
154
|
+
|
|
155
|
+
It only decides when a tooltip shows. It doesn't place, draw or animate it. It's built for hover
|
|
156
|
+
tooltips: menus and popovers that people open on purpose shouldn't share the turn.
|
|
157
|
+
|
|
158
|
+
## License
|
|
159
|
+
|
|
160
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One tooltip on the page at a time, as on a Mac.
|
|
3
|
+
*
|
|
4
|
+
* Every tooltip asks a shared turn before it shows, and only the one whose turn it is may be
|
|
5
|
+
* seen. The first one a mouse rests on waits a moment, so sweeping across a page doesn't flash
|
|
6
|
+
* tooltips over everything. Once one is open, the next opens at once and the open one goes, until
|
|
7
|
+
* no tooltip has been open for a short while. A tap, a click or the keyboard never waits: the
|
|
8
|
+
* person asked for the tooltip.
|
|
9
|
+
*
|
|
10
|
+
* The wait lives here, not in the tooltip library, so give your library no delay of its own.
|
|
11
|
+
*/
|
|
12
|
+
/** How long a mouse rests on a cold anchor before its tooltip opens, by default. */
|
|
13
|
+
export declare const DEFAULT_COLD_DELAY_MS = 400;
|
|
14
|
+
/** How long tooltips stay warm after the last one closes, by default. */
|
|
15
|
+
export declare const DEFAULT_COOL_DOWN_MS = 500;
|
|
16
|
+
/** Anything that names one tooltip: a string id, a symbol, an object. Compared with `===`. */
|
|
17
|
+
export type TooltipOwner = unknown;
|
|
18
|
+
/** Whose turn it is. */
|
|
19
|
+
export type TurnState =
|
|
20
|
+
/** Nothing is open, and a mouse must rest on an anchor before its tooltip opens. */
|
|
21
|
+
{
|
|
22
|
+
status: 'cold';
|
|
23
|
+
}
|
|
24
|
+
/** A mouse is resting on `anchor`. Nothing shows yet; `owner` opens if the mouse stays. */
|
|
25
|
+
| {
|
|
26
|
+
status: 'waiting';
|
|
27
|
+
owner: TooltipOwner;
|
|
28
|
+
anchor: Element;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* `owner` is the one tooltip on screen. `quick` when it followed another, so it can come in
|
|
32
|
+
* faster; `tookOverFrom` is the tooltip it replaced, which should go at once instead of fading.
|
|
33
|
+
*/
|
|
34
|
+
| {
|
|
35
|
+
status: 'open';
|
|
36
|
+
owner: TooltipOwner;
|
|
37
|
+
quick: boolean;
|
|
38
|
+
tookOverFrom: TooltipOwner | null;
|
|
39
|
+
}
|
|
40
|
+
/** Nothing is open, but `last` just was: the next opens without waiting. */
|
|
41
|
+
| {
|
|
42
|
+
status: 'warm';
|
|
43
|
+
last: TooltipOwner;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Where one tooltip stands. Only `open` and `open-quick` are seen. `replaced` is off screen like
|
|
47
|
+
* the rest, but just lost the turn, so it should vanish at once instead of fading out.
|
|
48
|
+
*/
|
|
49
|
+
export type TooltipStage = 'off' | 'replaced' | 'waiting' | 'open' | 'open-quick';
|
|
50
|
+
/** Settings for {@link createTooltipTurn}. */
|
|
51
|
+
export interface TooltipTurnOptions {
|
|
52
|
+
/** How long a mouse must rest on an anchor before the first tooltip opens. Default 400ms. */
|
|
53
|
+
coldDelay?: number;
|
|
54
|
+
/** How long after the last tooltip closes the next still opens at once. Default 500ms. */
|
|
55
|
+
coolDown?: number;
|
|
56
|
+
}
|
|
57
|
+
/** Settings for one {@link TooltipTurn.ask} call. */
|
|
58
|
+
export interface AskOptions {
|
|
59
|
+
/**
|
|
60
|
+
* Whether a mouse is only passing over the anchor, which is what has to wait. Leave it out and
|
|
61
|
+
* the store works it out from the last input: a press or a key means the person asked.
|
|
62
|
+
*/
|
|
63
|
+
hover?: boolean;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* A shared turn that decides which one tooltip may be seen. Made by {@link createTooltipTurn}.
|
|
67
|
+
* Its shape fits React's `useSyncExternalStore` and any other subscribe/get store.
|
|
68
|
+
*/
|
|
69
|
+
export interface TooltipTurn {
|
|
70
|
+
/**
|
|
71
|
+
* Listen for changes. While anyone listens, the store watches pointer and key input on
|
|
72
|
+
* `window`, so it can tell a passing mouse from a press. When the last listener goes, the
|
|
73
|
+
* turn resets to cold.
|
|
74
|
+
* @param listener Called after every change. Read the new state with `getState` or `stage`.
|
|
75
|
+
* @returns A function that stops listening.
|
|
76
|
+
*/
|
|
77
|
+
subscribe(listener: () => void): () => void;
|
|
78
|
+
/**
|
|
79
|
+
* The whole turn: who has it and in what state. The same object until something changes.
|
|
80
|
+
* @returns The current {@link TurnState}.
|
|
81
|
+
*/
|
|
82
|
+
getState(): TurnState;
|
|
83
|
+
/**
|
|
84
|
+
* Where one tooltip stands. Show it only for `open` and `open-quick`.
|
|
85
|
+
* @param owner The tooltip to look up.
|
|
86
|
+
* @param at A turn to read instead of the current one.
|
|
87
|
+
* @returns That tooltip's {@link TooltipStage}.
|
|
88
|
+
*/
|
|
89
|
+
stage(owner: TooltipOwner, at?: TurnState): TooltipStage;
|
|
90
|
+
/**
|
|
91
|
+
* A tooltip wants to open. It waits (a cold mouse hover), opens at once (a press, a key, or
|
|
92
|
+
* while warm), or takes over from the open one.
|
|
93
|
+
* @param owner The tooltip asking: any value, compared with `===`.
|
|
94
|
+
* @param anchor The element the tooltip explains. A hover with no anchor is ignored.
|
|
95
|
+
* @param options Set `hover` to say yourself whether this is a passing mouse.
|
|
96
|
+
*/
|
|
97
|
+
ask(owner: TooltipOwner, anchor: Element | null, options?: AskOptions): void;
|
|
98
|
+
/**
|
|
99
|
+
* A tooltip closed, gave up waiting, or is gone. Ignored if it isn't that tooltip's turn, so a
|
|
100
|
+
* late close from a replaced tooltip is harmless.
|
|
101
|
+
* @param owner The tooltip leaving.
|
|
102
|
+
*/
|
|
103
|
+
leave(owner: TooltipOwner): void;
|
|
104
|
+
/**
|
|
105
|
+
* The nearest element matching `selector` around what the pointer is on, or else around what
|
|
106
|
+
* has focus. Handy when a library tells you a tooltip opened but not on which anchor.
|
|
107
|
+
* @param selector A CSS selector, for example `[data-tooltip-id="save"]`.
|
|
108
|
+
* @returns The anchor, or `null` if neither the pointer nor focus is inside one.
|
|
109
|
+
*/
|
|
110
|
+
findAnchor(selector: string): Element | null;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Make a turn of your own, for example with other delays. Every tooltip that should take turns
|
|
114
|
+
* must share the same one. Most pages want the shared {@link tooltipTurn} instead.
|
|
115
|
+
* @param options `coldDelay` and `coolDown` in milliseconds.
|
|
116
|
+
* @returns A new {@link TooltipTurn}, cold.
|
|
117
|
+
* @example
|
|
118
|
+
* const turn = createTooltipTurn({ coldDelay: 600 });
|
|
119
|
+
* turn.subscribe(() => {
|
|
120
|
+
* tip.hidden = !turn.stage(button).startsWith('open');
|
|
121
|
+
* });
|
|
122
|
+
* button.addEventListener('pointerenter', () => turn.ask(button, button));
|
|
123
|
+
* button.addEventListener('pointerleave', () => turn.leave(button));
|
|
124
|
+
* button.addEventListener('focus', () => turn.ask(button, button));
|
|
125
|
+
* button.addEventListener('blur', () => turn.leave(button));
|
|
126
|
+
*/
|
|
127
|
+
export declare function createTooltipTurn(options?: TooltipTurnOptions): TooltipTurn;
|
|
128
|
+
/**
|
|
129
|
+
* The page's shared turn, with the default delays (400ms to open cold, 500ms to cool down).
|
|
130
|
+
* @example
|
|
131
|
+
* import { tooltipTurn } from 'tooltip-turn';
|
|
132
|
+
* tooltipTurn.ask(button, button);
|
|
133
|
+
*/
|
|
134
|
+
export declare const tooltipTurn: TooltipTurn;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One tooltip on the page at a time, as on a Mac.
|
|
3
|
+
*
|
|
4
|
+
* Every tooltip asks a shared turn before it shows, and only the one whose turn it is may be
|
|
5
|
+
* seen. The first one a mouse rests on waits a moment, so sweeping across a page doesn't flash
|
|
6
|
+
* tooltips over everything. Once one is open, the next opens at once and the open one goes, until
|
|
7
|
+
* no tooltip has been open for a short while. A tap, a click or the keyboard never waits: the
|
|
8
|
+
* person asked for the tooltip.
|
|
9
|
+
*
|
|
10
|
+
* The wait lives here, not in the tooltip library, so give your library no delay of its own.
|
|
11
|
+
*/
|
|
12
|
+
/** How long a mouse rests on a cold anchor before its tooltip opens, by default. */
|
|
13
|
+
export const DEFAULT_COLD_DELAY_MS = 400;
|
|
14
|
+
/** How long tooltips stay warm after the last one closes, by default. */
|
|
15
|
+
export const DEFAULT_COOL_DOWN_MS = 500;
|
|
16
|
+
const COLD = { status: 'cold' };
|
|
17
|
+
/**
|
|
18
|
+
* The only way a turn changes. A move that isn't allowed returns the same turn: a tooltip can't
|
|
19
|
+
* close or finish waiting on another's turn, and a hover with no anchor has already moved on.
|
|
20
|
+
*/
|
|
21
|
+
function move(turn, m) {
|
|
22
|
+
switch (m.type) {
|
|
23
|
+
case 'ask': {
|
|
24
|
+
if (m.hover && !m.anchor)
|
|
25
|
+
return turn;
|
|
26
|
+
if (turn.status === 'open') {
|
|
27
|
+
if (turn.owner === m.owner)
|
|
28
|
+
return turn;
|
|
29
|
+
return { status: 'open', owner: m.owner, quick: true, tookOverFrom: turn.owner };
|
|
30
|
+
}
|
|
31
|
+
if (turn.status === 'warm') {
|
|
32
|
+
// `last` may still be fading out: it goes at once, so two are never seen together.
|
|
33
|
+
const last = turn.last === m.owner ? null : turn.last;
|
|
34
|
+
return { status: 'open', owner: m.owner, quick: true, tookOverFrom: last };
|
|
35
|
+
}
|
|
36
|
+
if (m.hover && m.anchor) {
|
|
37
|
+
const sameWait = turn.status === 'waiting' && turn.owner === m.owner && turn.anchor === m.anchor;
|
|
38
|
+
// A new anchor starts the wait again, so sweeping down a column never opens one.
|
|
39
|
+
return sameWait ? turn : { status: 'waiting', owner: m.owner, anchor: m.anchor };
|
|
40
|
+
}
|
|
41
|
+
return { status: 'open', owner: m.owner, quick: false, tookOverFrom: null };
|
|
42
|
+
}
|
|
43
|
+
case 'rested':
|
|
44
|
+
return turn.status === 'waiting' && turn.owner === m.owner
|
|
45
|
+
? { status: 'open', owner: m.owner, quick: false, tookOverFrom: null }
|
|
46
|
+
: turn;
|
|
47
|
+
case 'leave':
|
|
48
|
+
if (turn.status === 'waiting' && turn.owner === m.owner)
|
|
49
|
+
return COLD;
|
|
50
|
+
if (turn.status === 'open' && turn.owner === m.owner)
|
|
51
|
+
return { status: 'warm', last: m.owner };
|
|
52
|
+
return turn;
|
|
53
|
+
case 'cooled':
|
|
54
|
+
return turn.status === 'warm' ? COLD : turn;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function stageOf(owner, at) {
|
|
58
|
+
if (at.status === 'waiting')
|
|
59
|
+
return at.owner === owner ? 'waiting' : 'off';
|
|
60
|
+
if (at.status === 'open') {
|
|
61
|
+
if (at.owner === owner)
|
|
62
|
+
return at.quick ? 'open-quick' : 'open';
|
|
63
|
+
return at.tookOverFrom === owner ? 'replaced' : 'off';
|
|
64
|
+
}
|
|
65
|
+
return 'off';
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Make a turn of your own, for example with other delays. Every tooltip that should take turns
|
|
69
|
+
* must share the same one. Most pages want the shared {@link tooltipTurn} instead.
|
|
70
|
+
* @param options `coldDelay` and `coolDown` in milliseconds.
|
|
71
|
+
* @returns A new {@link TooltipTurn}, cold.
|
|
72
|
+
* @example
|
|
73
|
+
* const turn = createTooltipTurn({ coldDelay: 600 });
|
|
74
|
+
* turn.subscribe(() => {
|
|
75
|
+
* tip.hidden = !turn.stage(button).startsWith('open');
|
|
76
|
+
* });
|
|
77
|
+
* button.addEventListener('pointerenter', () => turn.ask(button, button));
|
|
78
|
+
* button.addEventListener('pointerleave', () => turn.leave(button));
|
|
79
|
+
* button.addEventListener('focus', () => turn.ask(button, button));
|
|
80
|
+
* button.addEventListener('blur', () => turn.leave(button));
|
|
81
|
+
*/
|
|
82
|
+
export function createTooltipTurn(options = {}) {
|
|
83
|
+
const coldDelay = options.coldDelay ?? DEFAULT_COLD_DELAY_MS;
|
|
84
|
+
const coolDown = options.coolDown ?? DEFAULT_COOL_DOWN_MS;
|
|
85
|
+
let turn = COLD;
|
|
86
|
+
const listeners = new Set();
|
|
87
|
+
let timer = null;
|
|
88
|
+
// A press or a key means the person asked; a mouse moving means they may only be passing by.
|
|
89
|
+
let asked = false;
|
|
90
|
+
// The element under the pointer. `undefined` until the first pointer move is seen.
|
|
91
|
+
let hovered;
|
|
92
|
+
let watching = false;
|
|
93
|
+
const clearTimer = () => {
|
|
94
|
+
if (timer)
|
|
95
|
+
clearTimeout(timer);
|
|
96
|
+
timer = null;
|
|
97
|
+
};
|
|
98
|
+
// Whether a pointer or focus is still on the anchor. If we haven't seen the pointer yet, trust
|
|
99
|
+
// the caller, who will say when it leaves.
|
|
100
|
+
const stillOn = (anchor) => {
|
|
101
|
+
if (!anchor.isConnected)
|
|
102
|
+
return false;
|
|
103
|
+
if (hovered === undefined)
|
|
104
|
+
return true;
|
|
105
|
+
return anchor.contains(hovered) || anchor.contains(document.activeElement);
|
|
106
|
+
};
|
|
107
|
+
function dispatch(m) {
|
|
108
|
+
const next = move(turn, m);
|
|
109
|
+
if (next === turn)
|
|
110
|
+
return;
|
|
111
|
+
turn = next;
|
|
112
|
+
clearTimer();
|
|
113
|
+
if (next.status === 'waiting') {
|
|
114
|
+
timer = setTimeout(() => {
|
|
115
|
+
timer = null;
|
|
116
|
+
// Libraries often report a mouse leaving a little late, so look for ourselves.
|
|
117
|
+
dispatch({ type: stillOn(next.anchor) ? 'rested' : 'leave', owner: next.owner });
|
|
118
|
+
}, coldDelay);
|
|
119
|
+
}
|
|
120
|
+
else if (next.status === 'warm') {
|
|
121
|
+
timer = setTimeout(() => {
|
|
122
|
+
timer = null;
|
|
123
|
+
dispatch({ type: 'cooled' });
|
|
124
|
+
}, coolDown);
|
|
125
|
+
}
|
|
126
|
+
listeners.forEach((listener) => listener());
|
|
127
|
+
}
|
|
128
|
+
const onPointerDown = (event) => {
|
|
129
|
+
asked = true;
|
|
130
|
+
// Pressing the anchor you are resting on is asking for its tooltip.
|
|
131
|
+
if (turn.status === 'waiting' && event.target instanceof Node) {
|
|
132
|
+
if (turn.anchor.contains(event.target))
|
|
133
|
+
dispatch({ type: 'rested', owner: turn.owner });
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
const onKeyDown = () => {
|
|
137
|
+
asked = true;
|
|
138
|
+
};
|
|
139
|
+
const onPointerOver = (event) => {
|
|
140
|
+
hovered = event.target instanceof Element ? event.target : null;
|
|
141
|
+
// Moving onto a new element with a mouse is hover again. Only the mouse: touch fires this too.
|
|
142
|
+
if (event.pointerType === 'mouse' && event.buttons === 0)
|
|
143
|
+
asked = false;
|
|
144
|
+
};
|
|
145
|
+
const onPointerOut = (event) => {
|
|
146
|
+
// The pointer left the page.
|
|
147
|
+
if (!event.relatedTarget)
|
|
148
|
+
hovered = null;
|
|
149
|
+
};
|
|
150
|
+
const inputEvents = [
|
|
151
|
+
['pointerdown', onPointerDown],
|
|
152
|
+
['keydown', onKeyDown],
|
|
153
|
+
['pointerover', onPointerOver],
|
|
154
|
+
['pointerout', onPointerOut],
|
|
155
|
+
];
|
|
156
|
+
function watchInput() {
|
|
157
|
+
if (watching || typeof window === 'undefined')
|
|
158
|
+
return;
|
|
159
|
+
watching = true;
|
|
160
|
+
for (const [type, handler] of inputEvents) {
|
|
161
|
+
window.addEventListener(type, handler, { capture: true, passive: true });
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
function unwatchInput() {
|
|
165
|
+
if (!watching)
|
|
166
|
+
return;
|
|
167
|
+
watching = false;
|
|
168
|
+
for (const [type, handler] of inputEvents) {
|
|
169
|
+
window.removeEventListener(type, handler, { capture: true });
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
subscribe(listener) {
|
|
174
|
+
listeners.add(listener);
|
|
175
|
+
watchInput();
|
|
176
|
+
return () => {
|
|
177
|
+
listeners.delete(listener);
|
|
178
|
+
if (listeners.size === 0) {
|
|
179
|
+
unwatchInput();
|
|
180
|
+
clearTimer();
|
|
181
|
+
turn = COLD;
|
|
182
|
+
asked = false;
|
|
183
|
+
hovered = undefined;
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
},
|
|
187
|
+
getState: () => turn,
|
|
188
|
+
stage: (owner, at = turn) => stageOf(owner, at),
|
|
189
|
+
ask(owner, anchor, askOptions = {}) {
|
|
190
|
+
watchInput();
|
|
191
|
+
dispatch({ type: 'ask', owner, anchor, hover: askOptions.hover ?? !asked });
|
|
192
|
+
},
|
|
193
|
+
leave(owner) {
|
|
194
|
+
dispatch({ type: 'leave', owner });
|
|
195
|
+
},
|
|
196
|
+
findAnchor(selector) {
|
|
197
|
+
if (typeof document === 'undefined')
|
|
198
|
+
return null;
|
|
199
|
+
for (const from of [hovered, document.activeElement]) {
|
|
200
|
+
const found = from?.isConnected ? from.closest(selector) : null;
|
|
201
|
+
if (found)
|
|
202
|
+
return found;
|
|
203
|
+
}
|
|
204
|
+
return null;
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* The page's shared turn, with the default delays (400ms to open cold, 500ms to cool down).
|
|
210
|
+
* @example
|
|
211
|
+
* import { tooltipTurn } from 'tooltip-turn';
|
|
212
|
+
* tooltipTurn.ask(button, button);
|
|
213
|
+
*/
|
|
214
|
+
export const tooltipTurn = createTooltipTurn();
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type FocusEvent, type PointerEvent as ReactPointerEvent } from 'react';
|
|
2
|
+
import { type AskOptions, type TooltipStage, type TooltipTurn } from './index.js';
|
|
3
|
+
export type { AskOptions, TooltipStage, TooltipTurn } from './index.js';
|
|
4
|
+
/** Settings for {@link useTooltipTurn}. */
|
|
5
|
+
export interface UseTooltipTurnOptions {
|
|
6
|
+
/** The turn to share. Defaults to the page's shared `tooltipTurn`. */
|
|
7
|
+
store?: TooltipTurn;
|
|
8
|
+
}
|
|
9
|
+
/** Props to spread on the element a tooltip explains. */
|
|
10
|
+
export interface TooltipAnchorProps {
|
|
11
|
+
/** A mouse arriving asks for the turn (and may wait). */
|
|
12
|
+
onPointerEnter(event: ReactPointerEvent<Element>): void;
|
|
13
|
+
/** A mouse leaving gives the turn back. */
|
|
14
|
+
onPointerLeave(event: ReactPointerEvent<Element>): void;
|
|
15
|
+
/** A tap or pen press opens the tooltip at once, or closes it if open. */
|
|
16
|
+
onPointerDown(event: ReactPointerEvent<Element>): void;
|
|
17
|
+
/** Focus asks for the turn; from the keyboard it never waits. */
|
|
18
|
+
onFocus(event: FocusEvent<Element>): void;
|
|
19
|
+
/** Losing focus gives the turn back. */
|
|
20
|
+
onBlur(event: FocusEvent<Element>): void;
|
|
21
|
+
/** Points at the tooltip, so screen readers read it with the anchor. */
|
|
22
|
+
'aria-describedby': string;
|
|
23
|
+
}
|
|
24
|
+
/** What {@link useTooltipTurn} returns. */
|
|
25
|
+
export interface UseTooltipTurn {
|
|
26
|
+
/** Where this tooltip stands. Show it only when `visible`. */
|
|
27
|
+
stage: TooltipStage;
|
|
28
|
+
/** True for `open` and `open-quick`. */
|
|
29
|
+
visible: boolean;
|
|
30
|
+
/** Spread on the element the tooltip explains. */
|
|
31
|
+
anchorProps: TooltipAnchorProps;
|
|
32
|
+
/** Spread on the tooltip itself. */
|
|
33
|
+
tooltipProps: {
|
|
34
|
+
id: string;
|
|
35
|
+
role: 'tooltip';
|
|
36
|
+
};
|
|
37
|
+
/** Ask for the turn yourself, for example from a tooltip library's open callback. */
|
|
38
|
+
ask(anchor: Element | null, options?: AskOptions): void;
|
|
39
|
+
/** Give the turn back, for example from a tooltip library's close callback. */
|
|
40
|
+
leave(): void;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* One tooltip's place in the page's turn. Re-renders only when this tooltip's stage changes.
|
|
44
|
+
* Escape or a press outside the anchor closes it while open, and it leaves the turn on unmount.
|
|
45
|
+
* @param id The tooltip element's id. The anchor points at it with aria-describedby.
|
|
46
|
+
* @param options `store` to share a turn other than the default one.
|
|
47
|
+
* @returns The stage, whether to show the tooltip, and props for the anchor and the tooltip.
|
|
48
|
+
* @example
|
|
49
|
+
* function SaveButton() {
|
|
50
|
+
* const tip = useTooltipTurn('save-tip');
|
|
51
|
+
* return (
|
|
52
|
+
* <>
|
|
53
|
+
* <button {...tip.anchorProps}>Save</button>
|
|
54
|
+
* <div {...tip.tooltipProps} hidden={!tip.visible}>Save your changes</div>
|
|
55
|
+
* </>
|
|
56
|
+
* );
|
|
57
|
+
* }
|
|
58
|
+
*/
|
|
59
|
+
export declare function useTooltipTurn(id: string, options?: UseTooltipTurnOptions): UseTooltipTurn;
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useCallback, useEffect, useRef, useState, useSyncExternalStore, } from 'react';
|
|
3
|
+
import { tooltipTurn } from './index.js';
|
|
4
|
+
const offStage = () => 'off';
|
|
5
|
+
/**
|
|
6
|
+
* One tooltip's place in the page's turn. Re-renders only when this tooltip's stage changes.
|
|
7
|
+
* Escape or a press outside the anchor closes it while open, and it leaves the turn on unmount.
|
|
8
|
+
* @param id The tooltip element's id. The anchor points at it with aria-describedby.
|
|
9
|
+
* @param options `store` to share a turn other than the default one.
|
|
10
|
+
* @returns The stage, whether to show the tooltip, and props for the anchor and the tooltip.
|
|
11
|
+
* @example
|
|
12
|
+
* function SaveButton() {
|
|
13
|
+
* const tip = useTooltipTurn('save-tip');
|
|
14
|
+
* return (
|
|
15
|
+
* <>
|
|
16
|
+
* <button {...tip.anchorProps}>Save</button>
|
|
17
|
+
* <div {...tip.tooltipProps} hidden={!tip.visible}>Save your changes</div>
|
|
18
|
+
* </>
|
|
19
|
+
* );
|
|
20
|
+
* }
|
|
21
|
+
*/
|
|
22
|
+
export function useTooltipTurn(id, options = {}) {
|
|
23
|
+
const store = options.store ?? tooltipTurn;
|
|
24
|
+
const [owner] = useState(() => Symbol(id));
|
|
25
|
+
const anchorRef = useRef(null);
|
|
26
|
+
const readStage = useCallback(() => store.stage(owner), [store, owner]);
|
|
27
|
+
const stage = useSyncExternalStore(store.subscribe, readStage, offStage);
|
|
28
|
+
const visible = stage === 'open' || stage === 'open-quick';
|
|
29
|
+
const ask = useCallback((anchor, askOptions) => {
|
|
30
|
+
anchorRef.current = anchor;
|
|
31
|
+
store.ask(owner, anchor, askOptions);
|
|
32
|
+
}, [store, owner]);
|
|
33
|
+
const leave = useCallback(() => store.leave(owner), [store, owner]);
|
|
34
|
+
// While open: Escape, or a press outside the anchor, closes it. Taps don't always move focus
|
|
35
|
+
// (Safari doesn't focus a tapped button), so blur alone isn't enough.
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
if (!visible)
|
|
38
|
+
return undefined;
|
|
39
|
+
const onKeyDown = (event) => {
|
|
40
|
+
if (event.key === 'Escape')
|
|
41
|
+
leave();
|
|
42
|
+
};
|
|
43
|
+
const onPointerDown = (event) => {
|
|
44
|
+
const anchor = anchorRef.current;
|
|
45
|
+
if (!anchor || !(event.target instanceof Node) || !anchor.contains(event.target))
|
|
46
|
+
leave();
|
|
47
|
+
};
|
|
48
|
+
document.addEventListener('keydown', onKeyDown);
|
|
49
|
+
document.addEventListener('pointerdown', onPointerDown, true);
|
|
50
|
+
return () => {
|
|
51
|
+
document.removeEventListener('keydown', onKeyDown);
|
|
52
|
+
document.removeEventListener('pointerdown', onPointerDown, true);
|
|
53
|
+
};
|
|
54
|
+
}, [visible, leave]);
|
|
55
|
+
// A tooltip that unmounts never reports closing.
|
|
56
|
+
useEffect(() => leave, [leave]);
|
|
57
|
+
const anchorProps = {
|
|
58
|
+
onPointerEnter: (event) => {
|
|
59
|
+
if (event.pointerType === 'mouse')
|
|
60
|
+
ask(event.currentTarget);
|
|
61
|
+
},
|
|
62
|
+
onPointerLeave: (event) => {
|
|
63
|
+
if (event.pointerType === 'mouse')
|
|
64
|
+
leave();
|
|
65
|
+
},
|
|
66
|
+
// A tap or a pen press toggles the tooltip, and never waits.
|
|
67
|
+
onPointerDown: (event) => {
|
|
68
|
+
if (event.pointerType === 'mouse')
|
|
69
|
+
return;
|
|
70
|
+
if (visible)
|
|
71
|
+
leave();
|
|
72
|
+
else
|
|
73
|
+
ask(event.currentTarget, { hover: false });
|
|
74
|
+
},
|
|
75
|
+
onFocus: (event) => ask(event.currentTarget),
|
|
76
|
+
onBlur: () => leave(),
|
|
77
|
+
'aria-describedby': id,
|
|
78
|
+
};
|
|
79
|
+
return { stage, visible, anchorProps, tooltipProps: { id, role: 'tooltip' }, ask, leave };
|
|
80
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,73 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tooltip-turn",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "One tooltip on the page at a time, as on a Mac: the first waits, the next opens at once.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Cameron Spears",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/cameronspears/tooltip-turn.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/cameronspears/tooltip-turn#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/cameronspears/tooltip-turn/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./react": {
|
|
22
|
+
"types": "./dist/react.d.ts",
|
|
23
|
+
"default": "./dist/react.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"CHANGELOG.md"
|
|
29
|
+
],
|
|
30
|
+
"sideEffects": false,
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
33
|
+
"test": "vitest run",
|
|
34
|
+
"typecheck": "tsc --noEmit",
|
|
35
|
+
"demo": "pnpm build && python3 -m http.server 4173"
|
|
36
|
+
},
|
|
37
|
+
"keywords": [
|
|
38
|
+
"tooltip",
|
|
39
|
+
"hover",
|
|
40
|
+
"delay",
|
|
41
|
+
"hover-intent",
|
|
42
|
+
"macos",
|
|
43
|
+
"react",
|
|
44
|
+
"hook",
|
|
45
|
+
"react-tooltip",
|
|
46
|
+
"floating-ui"
|
|
47
|
+
],
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"react": ">=18"
|
|
50
|
+
},
|
|
51
|
+
"peerDependenciesMeta": {
|
|
52
|
+
"react": {
|
|
53
|
+
"optional": true
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@types/react": "^19.2.0",
|
|
58
|
+
"@types/react-dom": "^19.2.0",
|
|
59
|
+
"jsdom": "^27.4.0",
|
|
60
|
+
"react": "^19.2.0",
|
|
61
|
+
"react-dom": "^19.2.0",
|
|
62
|
+
"typescript": "^5.9.3",
|
|
63
|
+
"vitest": "^4.1.12"
|
|
64
|
+
},
|
|
65
|
+
"publishConfig": {
|
|
66
|
+
"provenance": true,
|
|
67
|
+
"access": "public"
|
|
68
|
+
},
|
|
69
|
+
"engines": {
|
|
70
|
+
"node": ">=20"
|
|
71
|
+
},
|
|
72
|
+
"packageManager": "pnpm@10.26.0"
|
|
73
|
+
}
|