@c9up/nebula 0.1.9 → 0.2.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/atoms/Avatar.d.ts +36 -9
- package/dist/atoms/Avatar.js +66 -27
- package/dist/atoms/index.d.ts +1 -1
- package/dist/atoms/index.js +1 -1
- package/dist/configure.d.ts +6 -0
- package/dist/configure.js +4 -22
- package/dist/lib/children.d.ts +30 -0
- package/dist/lib/id.d.ts +13 -29
- package/dist/lib/id.js +13 -39
- package/dist/lib/motion.d.ts +10 -0
- package/dist/lib/motion.js +10 -0
- package/dist/molecules/Accordion.d.ts +36 -15
- package/dist/molecules/Accordion.js +68 -45
- package/dist/molecules/Breadcrumb.d.ts +29 -11
- package/dist/molecules/Breadcrumb.js +52 -52
- package/dist/molecules/Collapsible.d.ts +18 -17
- package/dist/molecules/Collapsible.js +42 -34
- package/dist/molecules/Field.d.ts +66 -30
- package/dist/molecules/Field.js +106 -55
- package/dist/molecules/InputGroup.d.ts +39 -21
- package/dist/molecules/InputGroup.js +43 -27
- package/dist/molecules/InputOTP.d.ts +26 -3
- package/dist/molecules/InputOTP.js +109 -76
- package/dist/molecules/Message.js +5 -4
- package/dist/molecules/Pagination.d.ts +36 -11
- package/dist/molecules/Pagination.js +58 -74
- package/dist/molecules/RadioGroup.d.ts +25 -17
- package/dist/molecules/RadioGroup.js +47 -43
- package/dist/molecules/Resizable.d.ts +32 -7
- package/dist/molecules/Resizable.js +76 -49
- package/dist/molecules/Tabs.d.ts +49 -11
- package/dist/molecules/Tabs.js +107 -51
- package/dist/molecules/ToggleGroup.d.ts +16 -10
- package/dist/molecules/ToggleGroup.js +37 -21
- package/dist/molecules/index.d.ts +11 -11
- package/dist/molecules/index.js +11 -11
- package/dist/organisms/AlertDialog.d.ts +80 -28
- package/dist/organisms/AlertDialog.js +192 -67
- package/dist/organisms/Carousel.d.ts +25 -3
- package/dist/organisms/Carousel.js +75 -43
- package/dist/organisms/Combobox.d.ts +2 -1
- package/dist/organisms/Combobox.js +46 -16
- package/dist/organisms/Command.d.ts +73 -32
- package/dist/organisms/Command.js +209 -131
- package/dist/organisms/CommandDialog.js +5 -5
- package/dist/organisms/ContextMenu.d.ts +41 -19
- package/dist/organisms/ContextMenu.js +112 -71
- package/dist/organisms/DataTable.js +42 -8
- package/dist/organisms/Dialog.d.ts +97 -31
- package/dist/organisms/Dialog.js +193 -68
- package/dist/organisms/Drawer.d.ts +33 -10
- package/dist/organisms/Drawer.js +130 -52
- package/dist/organisms/DropdownMenu.d.ts +58 -11
- package/dist/organisms/DropdownMenu.js +137 -50
- package/dist/organisms/Form.js +9 -11
- package/dist/organisms/HoverCard.d.ts +39 -19
- package/dist/organisms/HoverCard.js +116 -40
- package/dist/organisms/Menubar.d.ts +55 -19
- package/dist/organisms/Menubar.js +166 -82
- package/dist/organisms/NavigationMenu.d.ts +77 -31
- package/dist/organisms/NavigationMenu.js +169 -98
- package/dist/organisms/Popover.d.ts +72 -12
- package/dist/organisms/Popover.js +132 -25
- package/dist/organisms/Questionnaire.js +5 -5
- package/dist/organisms/Select.d.ts +99 -18
- package/dist/organisms/Select.js +253 -111
- package/dist/organisms/Sheet.d.ts +57 -13
- package/dist/organisms/Sheet.js +141 -54
- package/dist/organisms/Sidebar.d.ts +146 -56
- package/dist/organisms/Sidebar.js +236 -128
- package/dist/organisms/Tooltip.d.ts +88 -14
- package/dist/organisms/Tooltip.js +210 -47
- package/dist/organisms/index.d.ts +15 -15
- package/dist/organisms/index.js +15 -15
- package/dist/primitives/floating.js +25 -0
- package/dist/primitives/floatingSurface.d.ts +11 -0
- package/dist/primitives/floatingSurface.js +8 -0
- package/dist/primitives/menu.d.ts +45 -1
- package/dist/primitives/menu.js +45 -14
- package/dist/primitives/menuParts.d.ts +113 -0
- package/dist/primitives/menuParts.js +207 -0
- package/dist/stubs.d.ts +13 -0
- package/dist/stubs.js +14 -0
- package/nebula.css +1 -1
- package/package.json +4 -3
- package/registry.json +18 -6
- package/src/atoms/Avatar.ts +141 -37
- package/src/atoms/index.ts +13 -1
- package/src/configure.ts +10 -23
- package/src/lib/children.ts +31 -0
- package/src/lib/id.ts +11 -39
- package/src/lib/motion.ts +12 -0
- package/src/molecules/Accordion.ts +120 -60
- package/src/molecules/Breadcrumb.ts +77 -76
- package/src/molecules/Collapsible.ts +77 -41
- package/src/molecules/Field.ts +187 -72
- package/src/molecules/InputGroup.ts +88 -35
- package/src/molecules/InputOTP.ts +150 -86
- package/src/molecules/Message.ts +6 -4
- package/src/molecules/Pagination.ts +96 -101
- package/src/molecules/RadioGroup.ts +70 -59
- package/src/molecules/Resizable.ts +138 -75
- package/src/molecules/Tabs.ts +187 -74
- package/src/molecules/ToggleGroup.ts +77 -38
- package/src/molecules/index.ts +85 -8
- package/src/organisms/AlertDialog.ts +319 -82
- package/src/organisms/Carousel.ts +129 -52
- package/src/organisms/Combobox.ts +64 -19
- package/src/organisms/Command.ts +342 -159
- package/src/organisms/CommandDialog.ts +6 -6
- package/src/organisms/ContextMenu.ts +185 -83
- package/src/organisms/DataTable.ts +56 -10
- package/src/organisms/Dialog.ts +312 -102
- package/src/organisms/Drawer.ts +219 -71
- package/src/organisms/DropdownMenu.ts +219 -61
- package/src/organisms/Form.ts +48 -34
- package/src/organisms/HoverCard.ts +187 -53
- package/src/organisms/Menubar.ts +247 -87
- package/src/organisms/NavigationMenu.ts +306 -128
- package/src/organisms/Popover.ts +215 -39
- package/src/organisms/Questionnaire.ts +9 -6
- package/src/organisms/Select.ts +397 -130
- package/src/organisms/Sheet.ts +252 -83
- package/src/organisms/Sidebar.ts +438 -185
- package/src/organisms/Tooltip.ts +315 -65
- package/src/organisms/index.ts +284 -14
- package/src/primitives/floating.ts +33 -0
- package/src/primitives/floatingSurface.ts +19 -0
- package/src/primitives/menu.ts +79 -18
- package/src/primitives/menuParts.ts +371 -0
- package/src/stubs.ts +16 -0
- package/stubs/config/nebula.stub +21 -0
package/src/organisms/Tooltip.ts
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Tooltip — a label that appears on hover or focus.
|
|
3
3
|
*
|
|
4
|
+
* Four parts, as upstream: a `TooltipProvider` that owns the timing for a
|
|
5
|
+
* group, a `Tooltip` that owns one tooltip's state, a `TooltipTrigger` and a
|
|
6
|
+
* `TooltipContent`. The state lives in the middle and both ends read it
|
|
7
|
+
* through context, which is why the trigger can describe content it never
|
|
8
|
+
* sees.
|
|
9
|
+
*
|
|
10
|
+
* TooltipProvider({ children: () => html`
|
|
11
|
+
* ${Tooltip({ children: () => html`
|
|
12
|
+
* ${TooltipTrigger({ children: () => Button({ children: "Save" }) })}
|
|
13
|
+
* ${TooltipContent({ children: "Saves and closes" })}
|
|
14
|
+
* ` })}
|
|
15
|
+
* ` })
|
|
16
|
+
*
|
|
17
|
+
* The `() =>` is not decoration. Aurora builds eagerly, so children handed
|
|
18
|
+
* over already built would have run before the provider existed and found no
|
|
19
|
+
* context — see `Parts`.
|
|
20
|
+
*
|
|
4
21
|
* Three rules separate a usable tooltip from an irritating one, and all three
|
|
5
22
|
* are about time rather than appearance:
|
|
6
23
|
*
|
|
@@ -11,8 +28,9 @@
|
|
|
11
28
|
* halfway.
|
|
12
29
|
* - **No delay between neighbours.** Once one tooltip is showing, the next one
|
|
13
30
|
* appears instantly; the user has clearly decided to browse the toolbar, and
|
|
14
|
-
* re-serving the delay each time makes the interface feel stuck. That is
|
|
15
|
-
* `
|
|
31
|
+
* re-serving the delay each time makes the interface feel stuck. That is
|
|
32
|
+
* what `skipDelayDuration` bounds, and why the window belongs to the
|
|
33
|
+
* provider rather than to either tooltip.
|
|
16
34
|
*
|
|
17
35
|
* Focus opens it with no delay at all. A keyboard user landing on a control
|
|
18
36
|
* has already committed to it, and a delay just looks like lag.
|
|
@@ -22,103 +40,335 @@
|
|
|
22
40
|
* only by its tooltip is unlabelled to anything that does not hover.
|
|
23
41
|
*/
|
|
24
42
|
|
|
25
|
-
import {
|
|
26
|
-
|
|
27
|
-
|
|
43
|
+
import {
|
|
44
|
+
component,
|
|
45
|
+
createContext,
|
|
46
|
+
html,
|
|
47
|
+
inject,
|
|
48
|
+
onUnmount,
|
|
49
|
+
provide,
|
|
50
|
+
signal,
|
|
51
|
+
type TemplateResult,
|
|
52
|
+
} from "@c9up/aurora";
|
|
53
|
+
import type { Child, Parts, Slot } from "../lib/children.js";
|
|
54
|
+
import { slot } from "../lib/children.js";
|
|
28
55
|
import { cn } from "../lib/cn.js";
|
|
29
56
|
import { uid } from "../lib/id.js";
|
|
30
|
-
import {
|
|
57
|
+
import { slideInFromSide, zoomInOut } from "../lib/motion.js";
|
|
31
58
|
import { type Reactive, read } from "../lib/props.js";
|
|
32
|
-
import type { Placement } from "../primitives/floating.js";
|
|
59
|
+
import type { Align, Placement, Side } from "../primitives/floating.js";
|
|
33
60
|
import { floatingSurface } from "../primitives/floatingSurface.js";
|
|
34
61
|
|
|
35
|
-
|
|
62
|
+
/** Upstream's default once shadcn has overridden Radix's 700ms. */
|
|
63
|
+
const DEFAULT_DELAY_MS = 0;
|
|
64
|
+
/** Radix's `skipDelayDuration`. */
|
|
65
|
+
const DEFAULT_SKIP_DELAY_MS = 300;
|
|
36
66
|
const CLOSE_DELAY_MS = 150;
|
|
37
|
-
/** How long after one closes that the next opens instantly. */
|
|
38
|
-
const GRACE_MS = 300;
|
|
39
67
|
|
|
40
|
-
|
|
41
|
-
|
|
68
|
+
interface TooltipGroup {
|
|
69
|
+
readonly delayDuration: number;
|
|
70
|
+
readonly skipDelayDuration: number;
|
|
71
|
+
readonly disableHoverableContent: boolean;
|
|
72
|
+
/** When the last tooltip in this group closed — the skip window. */
|
|
73
|
+
lastClosedAt: number;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* NAMED DEVIATION — a default, where Radix throws without a `TooltipProvider`.
|
|
78
|
+
*
|
|
79
|
+
* Radix raises so the skip-delay grouping is always an explicit choice. Here
|
|
80
|
+
* the default IS a group: one page-wide window, which is what nebula did
|
|
81
|
+
* before the parts were split out and what a single tooltip on a page wants.
|
|
82
|
+
* Wrapping a toolbar in a provider still gives it a window of its own, so
|
|
83
|
+
* nothing is lost — only the raise.
|
|
84
|
+
*/
|
|
85
|
+
const TooltipProviderContext = createContext<TooltipGroup>("TooltipProvider", {
|
|
86
|
+
delayDuration: DEFAULT_DELAY_MS,
|
|
87
|
+
skipDelayDuration: DEFAULT_SKIP_DELAY_MS,
|
|
88
|
+
disableHoverableContent: false,
|
|
89
|
+
lastClosedAt: 0,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
interface RegisteredContent {
|
|
93
|
+
readonly render: () => Child;
|
|
94
|
+
readonly side: Side;
|
|
95
|
+
readonly align: Align;
|
|
96
|
+
readonly sideOffset: number;
|
|
97
|
+
readonly class?: Reactive<string>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
interface TooltipApi {
|
|
101
|
+
readonly triggerId: string;
|
|
102
|
+
readonly contentId: string;
|
|
103
|
+
/** An accessor, not a signal: controlled tooltips read the caller's prop. */
|
|
104
|
+
readonly open: () => boolean;
|
|
105
|
+
/** Hover opens after the group's delay; focus opens at once. */
|
|
106
|
+
requestOpen(immediate: boolean): void;
|
|
107
|
+
requestClose(): void;
|
|
108
|
+
cancelPending(): void;
|
|
109
|
+
register(content: RegisteredContent): void;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const TooltipContext = createContext<TooltipApi>("Tooltip");
|
|
113
|
+
|
|
114
|
+
export interface TooltipProviderProps {
|
|
115
|
+
/** Hover delay before opening, in ms. */
|
|
116
|
+
delayDuration?: number;
|
|
117
|
+
/** How long after one closes that the next in the group opens instantly. */
|
|
118
|
+
skipDelayDuration?: number;
|
|
119
|
+
/** Close as soon as the pointer leaves the trigger, even towards the tooltip. */
|
|
120
|
+
disableHoverableContent?: boolean;
|
|
121
|
+
children?: Parts;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Timing shared by every tooltip inside it.
|
|
126
|
+
*
|
|
127
|
+
* Renders no element of its own, exactly as upstream: it is a context holder,
|
|
128
|
+
* and giving it a wrapper would put a box into a layout that did not ask for
|
|
129
|
+
* one. `data-slot="tooltip-provider"` is inert upstream for the same reason.
|
|
130
|
+
*/
|
|
131
|
+
export const TooltipProvider = component<TooltipProviderProps>((props) => {
|
|
132
|
+
provide<TooltipGroup>(TooltipProviderContext, {
|
|
133
|
+
delayDuration: props.delayDuration ?? DEFAULT_DELAY_MS,
|
|
134
|
+
skipDelayDuration: props.skipDelayDuration ?? DEFAULT_SKIP_DELAY_MS,
|
|
135
|
+
disableHoverableContent: props.disableHoverableContent ?? false,
|
|
136
|
+
lastClosedAt: 0,
|
|
137
|
+
});
|
|
138
|
+
return html`${props.children?.()}`;
|
|
139
|
+
});
|
|
42
140
|
|
|
43
141
|
export interface TooltipProps {
|
|
44
|
-
/**
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
contentClass?: Reactive<string>;
|
|
142
|
+
/** Controlled state. Omit to let the tooltip own it. */
|
|
143
|
+
open?: Reactive<boolean>;
|
|
144
|
+
defaultOpen?: boolean;
|
|
145
|
+
onOpenChange?: (open: boolean) => void;
|
|
146
|
+
/** Overrides the provider's delay for this tooltip only. */
|
|
147
|
+
delayDuration?: number;
|
|
148
|
+
disableHoverableContent?: boolean;
|
|
149
|
+
children?: Parts;
|
|
53
150
|
}
|
|
54
151
|
|
|
152
|
+
/**
|
|
153
|
+
* One tooltip: the state its trigger and its content both read.
|
|
154
|
+
*
|
|
155
|
+
* Renders no element of its own — see `TooltipProvider`. The parts are built
|
|
156
|
+
* here, inside setup, which is what puts the context within their reach; the
|
|
157
|
+
* content registers itself during that call, so the floating surface can be
|
|
158
|
+
* wired straight afterwards.
|
|
159
|
+
*/
|
|
55
160
|
export const Tooltip = component<TooltipProps>((props) => {
|
|
161
|
+
const group = inject(TooltipProviderContext);
|
|
56
162
|
const triggerId = uid("tooltip-trigger");
|
|
57
163
|
const contentId = uid("tooltip-content");
|
|
58
|
-
const
|
|
59
|
-
|
|
164
|
+
const internal = signal(props.defaultOpen ?? false);
|
|
165
|
+
let content: RegisteredContent | undefined;
|
|
60
166
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
61
167
|
|
|
62
|
-
|
|
168
|
+
/** Controlled when `open` reads as anything but `undefined`, on every read. */
|
|
169
|
+
function isOpen(): boolean {
|
|
170
|
+
const external = read(props.open);
|
|
171
|
+
return external === undefined ? internal() : external;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function setOpen(next: boolean): void {
|
|
175
|
+
// Written even when controlled: the caller may ignore `onOpenChange` and
|
|
176
|
+
// leave its own state put, and the internal value has to stay in step for
|
|
177
|
+
// the moment it stops controlling.
|
|
178
|
+
internal(next);
|
|
179
|
+
props.onOpenChange?.(next);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function cancelPending(): void {
|
|
63
183
|
if (timer !== undefined) clearTimeout(timer);
|
|
64
184
|
timer = undefined;
|
|
65
185
|
}
|
|
66
186
|
|
|
67
|
-
function
|
|
68
|
-
|
|
69
|
-
const
|
|
187
|
+
function requestOpen(immediate: boolean): void {
|
|
188
|
+
cancelPending();
|
|
189
|
+
const withinSkipWindow =
|
|
190
|
+
Date.now() - group.lastClosedAt < group.skipDelayDuration;
|
|
70
191
|
const delay =
|
|
71
|
-
immediate ||
|
|
192
|
+
immediate || withinSkipWindow
|
|
193
|
+
? 0
|
|
194
|
+
: (props.delayDuration ?? group.delayDuration);
|
|
72
195
|
if (delay === 0) {
|
|
73
|
-
|
|
196
|
+
setOpen(true);
|
|
74
197
|
return;
|
|
75
198
|
}
|
|
76
|
-
timer = setTimeout(() =>
|
|
199
|
+
timer = setTimeout(() => setOpen(true), delay);
|
|
77
200
|
}
|
|
78
201
|
|
|
79
|
-
function
|
|
80
|
-
|
|
202
|
+
function requestClose(): void {
|
|
203
|
+
cancelPending();
|
|
81
204
|
timer = setTimeout(() => {
|
|
82
|
-
|
|
83
|
-
lastClosedAt = Date.now();
|
|
84
|
-
},
|
|
205
|
+
setOpen(false);
|
|
206
|
+
group.lastClosedAt = Date.now();
|
|
207
|
+
}, CLOSE_DELAY_MS);
|
|
85
208
|
}
|
|
86
209
|
|
|
87
|
-
onUnmount(
|
|
210
|
+
onUnmount(cancelPending);
|
|
88
211
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
212
|
+
provide<TooltipApi>(TooltipContext, {
|
|
213
|
+
triggerId,
|
|
214
|
+
contentId,
|
|
215
|
+
open: isOpen,
|
|
216
|
+
requestOpen,
|
|
217
|
+
requestClose,
|
|
218
|
+
cancelPending,
|
|
219
|
+
register(registered) {
|
|
220
|
+
content = registered;
|
|
95
221
|
},
|
|
96
|
-
placement: props.placement ?? "top",
|
|
97
|
-
offset: 6,
|
|
98
|
-
outsidePointer: false,
|
|
99
|
-
content: () =>
|
|
100
|
-
html`<div
|
|
101
|
-
data-slot="tooltip-content"
|
|
102
|
-
id="${contentId}"
|
|
103
|
-
role="tooltip"
|
|
104
|
-
class="${cn(
|
|
105
|
-
"bg-primary text-primary-foreground z-50 w-fit rounded-md px-3 py-1.5 text-xs text-balance",
|
|
106
|
-
fadeInOut,
|
|
107
|
-
read(props.contentClass),
|
|
108
|
-
)}"
|
|
109
|
-
@pointerenter="${clear}"
|
|
110
|
-
@pointerleave="${scheduleClose}"
|
|
111
|
-
>${props.content}</div>`,
|
|
112
222
|
});
|
|
113
223
|
|
|
224
|
+
// Built HERE so the parts see the context, and so the content has
|
|
225
|
+
// registered by the time the surface below needs it.
|
|
226
|
+
const parts = props.children?.();
|
|
227
|
+
|
|
228
|
+
if (content !== undefined) {
|
|
229
|
+
const registered = content;
|
|
230
|
+
const placement: Placement =
|
|
231
|
+
registered.align === "center"
|
|
232
|
+
? registered.side
|
|
233
|
+
: `${registered.side}-${registered.align}`;
|
|
234
|
+
floatingSurface({
|
|
235
|
+
anchor: () => document.getElementById(triggerId),
|
|
236
|
+
open: isOpen,
|
|
237
|
+
onClose: () => {
|
|
238
|
+
cancelPending();
|
|
239
|
+
setOpen(false);
|
|
240
|
+
},
|
|
241
|
+
placement,
|
|
242
|
+
offset: registered.sideOffset,
|
|
243
|
+
arrowSize: ARROW_SIZE_PX,
|
|
244
|
+
arrow: (element) => element.querySelector('[data-slot="tooltip-arrow"]'),
|
|
245
|
+
// A tooltip is not a layer the pointer dismisses: it closes because
|
|
246
|
+
// the pointer left, which the trigger's own handlers already say.
|
|
247
|
+
outsidePointer: false,
|
|
248
|
+
content: () =>
|
|
249
|
+
renderContent(registered, contentId, {
|
|
250
|
+
hoverable: !(
|
|
251
|
+
props.disableHoverableContent ?? group.disableHoverableContent
|
|
252
|
+
),
|
|
253
|
+
cancelPending,
|
|
254
|
+
requestClose,
|
|
255
|
+
}),
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return html`${parts}`;
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
export interface TooltipTriggerProps {
|
|
263
|
+
children?: Slot;
|
|
264
|
+
class?: Reactive<string>;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* The control being described.
|
|
269
|
+
*
|
|
270
|
+
* NAMED DEVIATION — a wrapping `<span>`, where upstream is used as
|
|
271
|
+
* `<TooltipTrigger asChild>` and merges onto the control itself. `asChild`
|
|
272
|
+
* needs an element to merge INTO, and Aurora has no element until the template
|
|
273
|
+
* is rendered; a `<button>` wrapper would nest a button inside a button, which
|
|
274
|
+
* is invalid and unfocusable. `inline-flex` keeps the wrapper out of the
|
|
275
|
+
* layout. Focus still opens it because `focusin` bubbles out of the control.
|
|
276
|
+
*/
|
|
277
|
+
export const TooltipTrigger = component<TooltipTriggerProps>((props) => {
|
|
278
|
+
const tooltip = inject(TooltipContext);
|
|
114
279
|
return html`<span
|
|
115
280
|
data-slot="tooltip-trigger"
|
|
116
|
-
id="${triggerId}"
|
|
117
|
-
|
|
281
|
+
id="${tooltip.triggerId}"
|
|
282
|
+
data-state="${() => (tooltip.open() ? "open" : "closed")}"
|
|
283
|
+
aria-describedby="${() => (tooltip.open() ? tooltip.contentId : undefined)}"
|
|
118
284
|
class="${() => cn("inline-flex", read(props.class))}"
|
|
119
|
-
@pointerenter="${() =>
|
|
120
|
-
@pointerleave="${
|
|
121
|
-
@focusin="${() =>
|
|
122
|
-
@focusout="${
|
|
123
|
-
>${slot(props.
|
|
285
|
+
@pointerenter="${() => tooltip.requestOpen(false)}"
|
|
286
|
+
@pointerleave="${() => tooltip.requestClose()}"
|
|
287
|
+
@focusin="${() => tooltip.requestOpen(true)}"
|
|
288
|
+
@focusout="${() => tooltip.requestClose()}"
|
|
289
|
+
>${slot(props.children)}</span>`;
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
export interface TooltipContentProps {
|
|
293
|
+
children?: Child;
|
|
294
|
+
/** Which edge of the trigger to sit on. Flips when there is no room. */
|
|
295
|
+
side?: Side;
|
|
296
|
+
align?: Align;
|
|
297
|
+
sideOffset?: number;
|
|
298
|
+
class?: Reactive<string>;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* The label itself.
|
|
303
|
+
*
|
|
304
|
+
* Renders nothing where it is written: the tooltip portals its content out of
|
|
305
|
+
* the tree when it opens, so this registers what to build and returns an empty
|
|
306
|
+
* template. Writing it inside `Tooltip` — rather than passing content as a
|
|
307
|
+
* prop — is what keeps the call site shaped like the thing it describes.
|
|
308
|
+
*/
|
|
309
|
+
export const TooltipContent = component<TooltipContentProps>((props) => {
|
|
310
|
+
const tooltip = inject(TooltipContext);
|
|
311
|
+
tooltip.register({
|
|
312
|
+
render: () => props.children,
|
|
313
|
+
side: props.side ?? "top",
|
|
314
|
+
align: props.align ?? "center",
|
|
315
|
+
sideOffset: props.sideOffset ?? 0,
|
|
316
|
+
class: props.class,
|
|
317
|
+
});
|
|
318
|
+
return html``;
|
|
124
319
|
});
|
|
320
|
+
|
|
321
|
+
/** `size-2.5` in pixels — what the positioner centres on the anchor. */
|
|
322
|
+
const ARROW_SIZE_PX = 10;
|
|
323
|
+
|
|
324
|
+
const CONTENT_CLASSES =
|
|
325
|
+
"bg-foreground text-background z-50 w-fit origin-(--nebula-transform-origin) rounded-md px-3 py-1.5 text-xs text-balance";
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* NAMED DEVIATION — the arrow is placed per side here, where shadcn writes one
|
|
329
|
+
* `translate-y`. Radix wraps its arrow in a span the popper rotates to face the
|
|
330
|
+
* anchor, so upstream's single nudge is applied in an already-oriented frame.
|
|
331
|
+
* `autoPosition` positions the arrow along the CROSS axis only, by design, and
|
|
332
|
+
* leaves the facing edge to CSS — so the edge has to be named for each side.
|
|
333
|
+
* The look is upstream's: a 10px square, rotated, softly rounded.
|
|
334
|
+
*/
|
|
335
|
+
const ARROW_CLASSES = [
|
|
336
|
+
"bg-foreground fill-foreground absolute z-50 size-2.5 rotate-45 rounded-[2px]",
|
|
337
|
+
"data-[side=top]:bottom-0 data-[side=top]:translate-y-[calc(50%_-_2px)]",
|
|
338
|
+
"data-[side=bottom]:top-0 data-[side=bottom]:translate-y-[calc(-50%_+_2px)]",
|
|
339
|
+
"data-[side=left]:right-0 data-[side=left]:translate-x-[calc(50%_-_2px)]",
|
|
340
|
+
"data-[side=right]:left-0 data-[side=right]:translate-x-[calc(-50%_+_2px)]",
|
|
341
|
+
].join(" ");
|
|
342
|
+
|
|
343
|
+
interface ContentHandlers {
|
|
344
|
+
readonly hoverable: boolean;
|
|
345
|
+
cancelPending(): void;
|
|
346
|
+
requestClose(): void;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function renderContent(
|
|
350
|
+
registered: RegisteredContent,
|
|
351
|
+
contentId: string,
|
|
352
|
+
handlers: ContentHandlers,
|
|
353
|
+
): TemplateResult {
|
|
354
|
+
// Hoverable content keeps the tooltip open while the pointer is inside it,
|
|
355
|
+
// which is the only way to read a long one or click a link in it.
|
|
356
|
+
const enter = handlers.hoverable ? () => handlers.cancelPending() : undefined;
|
|
357
|
+
const leave = handlers.hoverable ? () => handlers.requestClose() : undefined;
|
|
358
|
+
return html`<div
|
|
359
|
+
data-slot="tooltip-content"
|
|
360
|
+
id="${contentId}"
|
|
361
|
+
role="tooltip"
|
|
362
|
+
class="${cn(
|
|
363
|
+
CONTENT_CLASSES,
|
|
364
|
+
zoomInOut,
|
|
365
|
+
slideInFromSide,
|
|
366
|
+
read(registered.class),
|
|
367
|
+
)}"
|
|
368
|
+
@pointerenter="${enter}"
|
|
369
|
+
@pointerleave="${leave}"
|
|
370
|
+
>${registered.render()}<span
|
|
371
|
+
data-slot="tooltip-arrow"
|
|
372
|
+
class="${ARROW_CLASSES}"
|
|
373
|
+
></span></div>`;
|
|
374
|
+
}
|