@glob-ui/tooltip 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/LICENSE +21 -0
- package/README.md +16 -2
- package/dist/index.d.mts +26 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +141 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +115 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +67 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kevin Martínez E
|
|
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,17 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @glob-ui/tooltip
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A short description that appears on hover or keyboard focus. It stays open while the pointer is over it.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @glob-ui/tooltip
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Documentation
|
|
12
|
+
|
|
13
|
+
Full docs, examples and the accessibility table: [Tooltip](https://github.com/kevin-martineze/GlobUI/tree/main/apps/docs/content/docs/components/tooltip.mdx).
|
|
14
|
+
|
|
15
|
+
## License
|
|
16
|
+
|
|
17
|
+
MIT. See [LICENSE](./LICENSE).
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { Placement } from '@glob-ui/hooks';
|
|
3
|
+
|
|
4
|
+
interface TooltipProps {
|
|
5
|
+
content: React.ReactNode;
|
|
6
|
+
/** One element that can hold a ref and receive focus (usually a button). */
|
|
7
|
+
children: React.ReactElement;
|
|
8
|
+
placement?: Placement;
|
|
9
|
+
/** Hover delay before opening, in ms. Keyboard focus opens immediately. */
|
|
10
|
+
delayDuration?: number;
|
|
11
|
+
/**
|
|
12
|
+
* Delay before closing after the pointer leaves, in ms. Moving onto the tooltip within this
|
|
13
|
+
* time keeps it open (WCAG 1.4.13, hoverable).
|
|
14
|
+
*/
|
|
15
|
+
closeDelay?: number;
|
|
16
|
+
/** Gap between trigger and tooltip, in px. */
|
|
17
|
+
offset?: number;
|
|
18
|
+
arrow?: boolean;
|
|
19
|
+
className?: string;
|
|
20
|
+
}
|
|
21
|
+
declare function Tooltip({ content, children, placement, delayDuration, closeDelay, offset, arrow, className, }: TooltipProps): react_jsx_runtime.JSX.Element;
|
|
22
|
+
declare namespace Tooltip {
|
|
23
|
+
var displayName: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { Tooltip, type TooltipProps };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { Placement } from '@glob-ui/hooks';
|
|
3
|
+
|
|
4
|
+
interface TooltipProps {
|
|
5
|
+
content: React.ReactNode;
|
|
6
|
+
/** One element that can hold a ref and receive focus (usually a button). */
|
|
7
|
+
children: React.ReactElement;
|
|
8
|
+
placement?: Placement;
|
|
9
|
+
/** Hover delay before opening, in ms. Keyboard focus opens immediately. */
|
|
10
|
+
delayDuration?: number;
|
|
11
|
+
/**
|
|
12
|
+
* Delay before closing after the pointer leaves, in ms. Moving onto the tooltip within this
|
|
13
|
+
* time keeps it open (WCAG 1.4.13, hoverable).
|
|
14
|
+
*/
|
|
15
|
+
closeDelay?: number;
|
|
16
|
+
/** Gap between trigger and tooltip, in px. */
|
|
17
|
+
offset?: number;
|
|
18
|
+
arrow?: boolean;
|
|
19
|
+
className?: string;
|
|
20
|
+
}
|
|
21
|
+
declare function Tooltip({ content, children, placement, delayDuration, closeDelay, offset, arrow, className, }: TooltipProps): react_jsx_runtime.JSX.Element;
|
|
22
|
+
declare namespace Tooltip {
|
|
23
|
+
var displayName: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { Tooltip, type TooltipProps };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
Tooltip: () => Tooltip
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/tooltip.tsx
|
|
29
|
+
var import_react = require("react");
|
|
30
|
+
var import_utils = require("@glob-ui/utils");
|
|
31
|
+
var import_slot = require("@glob-ui/slot");
|
|
32
|
+
var import_hooks = require("@glob-ui/hooks");
|
|
33
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
34
|
+
var oppositeSide = { top: "bottom", right: "left", bottom: "top", left: "right" };
|
|
35
|
+
function Tooltip({
|
|
36
|
+
content,
|
|
37
|
+
children,
|
|
38
|
+
placement = "top",
|
|
39
|
+
delayDuration = 500,
|
|
40
|
+
closeDelay = 150,
|
|
41
|
+
offset = 6,
|
|
42
|
+
arrow = false,
|
|
43
|
+
className
|
|
44
|
+
}) {
|
|
45
|
+
const [open, setOpen] = (0, import_react.useState)(false);
|
|
46
|
+
const [arrowEl, setArrowEl] = (0, import_react.useState)(null);
|
|
47
|
+
const timer = (0, import_react.useRef)(void 0);
|
|
48
|
+
const id = (0, import_react.useId)();
|
|
49
|
+
const { setTrigger, setPanel, floatingStyles, placement: finalPlacement, middlewareData } = (0, import_hooks.useFloatingPanel)({
|
|
50
|
+
open,
|
|
51
|
+
onOpenChange: setOpen,
|
|
52
|
+
placement,
|
|
53
|
+
offset: offset + (arrow ? 4 : 0),
|
|
54
|
+
arrow: arrow ? arrowEl : null,
|
|
55
|
+
fitViewport: false,
|
|
56
|
+
restoreFocus: false
|
|
57
|
+
});
|
|
58
|
+
const show = (0, import_react.useCallback)((delay) => {
|
|
59
|
+
clearTimeout(timer.current);
|
|
60
|
+
if (delay <= 0) setOpen(true);
|
|
61
|
+
else timer.current = setTimeout(() => setOpen(true), delay);
|
|
62
|
+
}, []);
|
|
63
|
+
const hide = (0, import_react.useCallback)((delay) => {
|
|
64
|
+
clearTimeout(timer.current);
|
|
65
|
+
if (delay <= 0) setOpen(false);
|
|
66
|
+
else timer.current = setTimeout(() => setOpen(false), delay);
|
|
67
|
+
}, []);
|
|
68
|
+
(0, import_react.useEffect)(() => () => clearTimeout(timer.current), []);
|
|
69
|
+
(0, import_react.useEffect)(() => {
|
|
70
|
+
if (!open) return;
|
|
71
|
+
const onKeyDown = (e) => {
|
|
72
|
+
if (e.key === "Escape") hide(0);
|
|
73
|
+
};
|
|
74
|
+
document.addEventListener("keydown", onKeyDown);
|
|
75
|
+
return () => document.removeEventListener("keydown", onKeyDown);
|
|
76
|
+
}, [open, hide]);
|
|
77
|
+
const child = import_react.Children.only(children);
|
|
78
|
+
const childDescribedBy = (0, import_react.isValidElement)(child) ? child.props["aria-describedby"] : void 0;
|
|
79
|
+
const side = finalPlacement.split("-")[0];
|
|
80
|
+
const arrowData = middlewareData.arrow;
|
|
81
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
|
|
82
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
83
|
+
import_slot.Slot,
|
|
84
|
+
{
|
|
85
|
+
ref: setTrigger,
|
|
86
|
+
"aria-describedby": [childDescribedBy, id].filter(Boolean).join(" "),
|
|
87
|
+
onPointerEnter: (e) => {
|
|
88
|
+
if (e.pointerType !== "touch") show(delayDuration);
|
|
89
|
+
},
|
|
90
|
+
onPointerLeave: (e) => {
|
|
91
|
+
if (e.pointerType !== "touch") hide(closeDelay);
|
|
92
|
+
},
|
|
93
|
+
onPointerDown: () => hide(0),
|
|
94
|
+
onFocus: (e) => {
|
|
95
|
+
if (e.currentTarget.matches(":focus-visible")) show(0);
|
|
96
|
+
},
|
|
97
|
+
onBlur: () => hide(0),
|
|
98
|
+
children: child
|
|
99
|
+
}
|
|
100
|
+
),
|
|
101
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
102
|
+
"div",
|
|
103
|
+
{
|
|
104
|
+
ref: setPanel,
|
|
105
|
+
id,
|
|
106
|
+
role: "tooltip",
|
|
107
|
+
popover: "manual",
|
|
108
|
+
style: floatingStyles,
|
|
109
|
+
onPointerEnter: () => clearTimeout(timer.current),
|
|
110
|
+
onPointerLeave: () => hide(closeDelay),
|
|
111
|
+
className: (0, import_utils.cn)(
|
|
112
|
+
"m-0 overflow-visible rounded-glob-sm border-0 bg-glob-text px-2 py-1 text-xs text-glob-surface shadow-glob-md",
|
|
113
|
+
"opacity-0 transition-[opacity,overlay,display] transition-discrete duration-100 open:opacity-100 starting:open:opacity-0 motion-reduce:transition-none",
|
|
114
|
+
className
|
|
115
|
+
),
|
|
116
|
+
children: [
|
|
117
|
+
content,
|
|
118
|
+
arrow && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
119
|
+
"div",
|
|
120
|
+
{
|
|
121
|
+
ref: setArrowEl,
|
|
122
|
+
"aria-hidden": "true",
|
|
123
|
+
className: "absolute h-2 w-2 rotate-45 bg-glob-text",
|
|
124
|
+
style: {
|
|
125
|
+
left: (arrowData == null ? void 0 : arrowData.x) != null ? `${arrowData.x}px` : void 0,
|
|
126
|
+
top: (arrowData == null ? void 0 : arrowData.y) != null ? `${arrowData.y}px` : void 0,
|
|
127
|
+
[oppositeSide[side]]: "-4px"
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
)
|
|
131
|
+
]
|
|
132
|
+
}
|
|
133
|
+
)
|
|
134
|
+
] });
|
|
135
|
+
}
|
|
136
|
+
Tooltip.displayName = "Tooltip";
|
|
137
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
138
|
+
0 && (module.exports = {
|
|
139
|
+
Tooltip
|
|
140
|
+
});
|
|
141
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/tooltip.tsx"],"sourcesContent":["export { Tooltip } from \"./tooltip\"\nexport type { TooltipProps } from \"./tooltip\"\n","\"use client\"\n\nimport { Children, isValidElement, useCallback, useEffect, useId, useRef, useState } from \"react\"\nimport { cn } from \"@glob-ui/utils\"\nimport { Slot } from \"@glob-ui/slot\"\nimport { useFloatingPanel, type Placement } from \"@glob-ui/hooks\"\n\nconst oppositeSide = { top: \"bottom\", right: \"left\", bottom: \"top\", left: \"right\" } as const\n\nexport interface TooltipProps {\n content: React.ReactNode\n /** One element that can hold a ref and receive focus (usually a button). */\n children: React.ReactElement\n placement?: Placement\n /** Hover delay before opening, in ms. Keyboard focus opens immediately. */\n delayDuration?: number\n /**\n * Delay before closing after the pointer leaves, in ms. Moving onto the tooltip within this\n * time keeps it open (WCAG 1.4.13, hoverable).\n */\n closeDelay?: number\n /** Gap between trigger and tooltip, in px. */\n offset?: number\n arrow?: boolean\n className?: string\n}\n\nfunction Tooltip({\n content,\n children,\n placement = \"top\",\n delayDuration = 500,\n closeDelay = 150,\n offset = 6,\n arrow = false,\n className,\n}: TooltipProps) {\n const [open, setOpen] = useState(false)\n const [arrowEl, setArrowEl] = useState<HTMLDivElement | null>(null)\n const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined)\n const id = useId()\n\n // Destructured: the compiler would treat a whole object passed to `ref=` as a ref.\n const { setTrigger, setPanel, floatingStyles, placement: finalPlacement, middlewareData } = useFloatingPanel({\n open,\n onOpenChange: setOpen,\n placement,\n offset: offset + (arrow ? 4 : 0),\n arrow: arrow ? arrowEl : null,\n fitViewport: false,\n restoreFocus: false,\n })\n\n const show = useCallback((delay: number) => {\n clearTimeout(timer.current)\n if (delay <= 0) setOpen(true)\n else timer.current = setTimeout(() => setOpen(true), delay)\n }, [])\n const hide = useCallback((delay: number) => {\n clearTimeout(timer.current)\n if (delay <= 0) setOpen(false)\n else timer.current = setTimeout(() => setOpen(false), delay)\n }, [])\n\n useEffect(() => () => clearTimeout(timer.current), [])\n\n // popover=\"manual\" has no Esc handling of its own.\n useEffect(() => {\n if (!open) return\n const onKeyDown = (e: KeyboardEvent) => {\n if (e.key === \"Escape\") hide(0)\n }\n document.addEventListener(\"keydown\", onKeyDown)\n return () => document.removeEventListener(\"keydown\", onKeyDown)\n }, [open, hide])\n\n const child = Children.only(children)\n const childDescribedBy = isValidElement<{ \"aria-describedby\"?: string }>(child)\n ? child.props[\"aria-describedby\"]\n : undefined\n const side = finalPlacement.split(\"-\")[0] as keyof typeof oppositeSide\n const arrowData = middlewareData.arrow\n\n return (\n <>\n <Slot\n ref={setTrigger}\n aria-describedby={[childDescribedBy, id].filter(Boolean).join(\" \")}\n onPointerEnter={(e: React.PointerEvent) => {\n if (e.pointerType !== \"touch\") show(delayDuration)\n }}\n onPointerLeave={(e: React.PointerEvent) => {\n if (e.pointerType !== \"touch\") hide(closeDelay)\n }}\n onPointerDown={() => hide(0)}\n onFocus={(e: React.FocusEvent<HTMLElement>) => {\n // Keyboard focus only: a tap or click also focuses, but is not :focus-visible.\n if (e.currentTarget.matches(\":focus-visible\")) show(0)\n }}\n onBlur={() => hide(0)}\n >\n {child}\n </Slot>\n <div\n ref={setPanel}\n id={id}\n role=\"tooltip\"\n popover=\"manual\"\n style={floatingStyles}\n // Hoverable: entering the tooltip cancels the pending close, leaving starts it again.\n onPointerEnter={() => clearTimeout(timer.current)}\n onPointerLeave={() => hide(closeDelay)}\n className={cn(\n \"m-0 overflow-visible rounded-glob-sm border-0 bg-glob-text px-2 py-1 text-xs text-glob-surface shadow-glob-md\",\n \"opacity-0 transition-[opacity,overlay,display] transition-discrete duration-100 open:opacity-100 starting:open:opacity-0 motion-reduce:transition-none\",\n className\n )}\n >\n {content}\n {arrow && (\n <div\n ref={setArrowEl}\n aria-hidden=\"true\"\n className=\"absolute h-2 w-2 rotate-45 bg-glob-text\"\n style={{\n left: arrowData?.x != null ? `${arrowData.x}px` : undefined,\n top: arrowData?.y != null ? `${arrowData.y}px` : undefined,\n [oppositeSide[side]]: \"-4px\",\n }}\n />\n )}\n </div>\n </>\n )\n}\nTooltip.displayName = \"Tooltip\"\n\nexport { Tooltip }\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEA,mBAA0F;AAC1F,mBAAmB;AACnB,kBAAqB;AACrB,mBAAiD;AA+E7C;AA7EJ,IAAM,eAAe,EAAE,KAAK,UAAU,OAAO,QAAQ,QAAQ,OAAO,MAAM,QAAQ;AAoBlF,SAAS,QAAQ;AAAA,EACf;AAAA,EACA;AAAA,EACA,YAAY;AAAA,EACZ,gBAAgB;AAAA,EAChB,aAAa;AAAA,EACb,SAAS;AAAA,EACT,QAAQ;AAAA,EACR;AACF,GAAiB;AACf,QAAM,CAAC,MAAM,OAAO,QAAI,uBAAS,KAAK;AACtC,QAAM,CAAC,SAAS,UAAU,QAAI,uBAAgC,IAAI;AAClE,QAAM,YAAQ,qBAAkD,MAAS;AACzE,QAAM,SAAK,oBAAM;AAGjB,QAAM,EAAE,YAAY,UAAU,gBAAgB,WAAW,gBAAgB,eAAe,QAAI,+BAAiB;AAAA,IAC3G;AAAA,IACA,cAAc;AAAA,IACd;AAAA,IACA,QAAQ,UAAU,QAAQ,IAAI;AAAA,IAC9B,OAAO,QAAQ,UAAU;AAAA,IACzB,aAAa;AAAA,IACb,cAAc;AAAA,EAChB,CAAC;AAED,QAAM,WAAO,0BAAY,CAAC,UAAkB;AAC1C,iBAAa,MAAM,OAAO;AAC1B,QAAI,SAAS,EAAG,SAAQ,IAAI;AAAA,QACvB,OAAM,UAAU,WAAW,MAAM,QAAQ,IAAI,GAAG,KAAK;AAAA,EAC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IACA;AAAA,MAAC;AAAA;AAAA,QACC,KAAK;AAAA,QACL;AAAA,QACA,MAAK;AAAA,QACL,SAAQ;AAAA,QACR,OAAO;AAAA,QAEP,gBAAgB,MAAM,aAAa,MAAM,OAAO;AAAA,QAChD,gBAAgB,MAAM,KAAK,UAAU;AAAA,QACrC,eAAW;AAAA,UACT;AAAA,UACA;AAAA,UACA;AAAA,QACF;AAAA,QAEC;AAAA;AAAA,UACA,SACC;AAAA,YAAC;AAAA;AAAA,cACC,KAAK;AAAA,cACL,eAAY;AAAA,cACZ,WAAU;AAAA,cACV,OAAO;AAAA,gBACL,OAAM,uCAAW,MAAK,OAAO,GAAG,UAAU,CAAC,OAAO;AAAA,gBAClD,MAAK,uCAAW,MAAK,OAAO,GAAG,UAAU,CAAC,OAAO;AAAA,gBACjD,CAAC,aAAa,IAAI,CAAC,GAAG;AAAA,cACxB;AAAA;AAAA,UACF;AAAA;AAAA;AAAA,IAEJ;AAAA,KACF;AAEJ;AACA,QAAQ,cAAc;","names":[]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
// src/tooltip.tsx
|
|
4
|
+
import { Children, isValidElement, useCallback, useEffect, useId, useRef, useState } from "react";
|
|
5
|
+
import { cn } from "@glob-ui/utils";
|
|
6
|
+
import { Slot } from "@glob-ui/slot";
|
|
7
|
+
import { useFloatingPanel } from "@glob-ui/hooks";
|
|
8
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
9
|
+
var oppositeSide = { top: "bottom", right: "left", bottom: "top", left: "right" };
|
|
10
|
+
function Tooltip({
|
|
11
|
+
content,
|
|
12
|
+
children,
|
|
13
|
+
placement = "top",
|
|
14
|
+
delayDuration = 500,
|
|
15
|
+
closeDelay = 150,
|
|
16
|
+
offset = 6,
|
|
17
|
+
arrow = false,
|
|
18
|
+
className
|
|
19
|
+
}) {
|
|
20
|
+
const [open, setOpen] = useState(false);
|
|
21
|
+
const [arrowEl, setArrowEl] = useState(null);
|
|
22
|
+
const timer = useRef(void 0);
|
|
23
|
+
const id = useId();
|
|
24
|
+
const { setTrigger, setPanel, floatingStyles, placement: finalPlacement, middlewareData } = useFloatingPanel({
|
|
25
|
+
open,
|
|
26
|
+
onOpenChange: setOpen,
|
|
27
|
+
placement,
|
|
28
|
+
offset: offset + (arrow ? 4 : 0),
|
|
29
|
+
arrow: arrow ? arrowEl : null,
|
|
30
|
+
fitViewport: false,
|
|
31
|
+
restoreFocus: false
|
|
32
|
+
});
|
|
33
|
+
const show = useCallback((delay) => {
|
|
34
|
+
clearTimeout(timer.current);
|
|
35
|
+
if (delay <= 0) setOpen(true);
|
|
36
|
+
else timer.current = setTimeout(() => setOpen(true), delay);
|
|
37
|
+
}, []);
|
|
38
|
+
const hide = useCallback((delay) => {
|
|
39
|
+
clearTimeout(timer.current);
|
|
40
|
+
if (delay <= 0) setOpen(false);
|
|
41
|
+
else timer.current = setTimeout(() => setOpen(false), delay);
|
|
42
|
+
}, []);
|
|
43
|
+
useEffect(() => () => clearTimeout(timer.current), []);
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
if (!open) return;
|
|
46
|
+
const onKeyDown = (e) => {
|
|
47
|
+
if (e.key === "Escape") hide(0);
|
|
48
|
+
};
|
|
49
|
+
document.addEventListener("keydown", onKeyDown);
|
|
50
|
+
return () => document.removeEventListener("keydown", onKeyDown);
|
|
51
|
+
}, [open, hide]);
|
|
52
|
+
const child = Children.only(children);
|
|
53
|
+
const childDescribedBy = isValidElement(child) ? child.props["aria-describedby"] : void 0;
|
|
54
|
+
const side = finalPlacement.split("-")[0];
|
|
55
|
+
const arrowData = middlewareData.arrow;
|
|
56
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
57
|
+
/* @__PURE__ */ jsx(
|
|
58
|
+
Slot,
|
|
59
|
+
{
|
|
60
|
+
ref: setTrigger,
|
|
61
|
+
"aria-describedby": [childDescribedBy, id].filter(Boolean).join(" "),
|
|
62
|
+
onPointerEnter: (e) => {
|
|
63
|
+
if (e.pointerType !== "touch") show(delayDuration);
|
|
64
|
+
},
|
|
65
|
+
onPointerLeave: (e) => {
|
|
66
|
+
if (e.pointerType !== "touch") hide(closeDelay);
|
|
67
|
+
},
|
|
68
|
+
onPointerDown: () => hide(0),
|
|
69
|
+
onFocus: (e) => {
|
|
70
|
+
if (e.currentTarget.matches(":focus-visible")) show(0);
|
|
71
|
+
},
|
|
72
|
+
onBlur: () => hide(0),
|
|
73
|
+
children: child
|
|
74
|
+
}
|
|
75
|
+
),
|
|
76
|
+
/* @__PURE__ */ jsxs(
|
|
77
|
+
"div",
|
|
78
|
+
{
|
|
79
|
+
ref: setPanel,
|
|
80
|
+
id,
|
|
81
|
+
role: "tooltip",
|
|
82
|
+
popover: "manual",
|
|
83
|
+
style: floatingStyles,
|
|
84
|
+
onPointerEnter: () => clearTimeout(timer.current),
|
|
85
|
+
onPointerLeave: () => hide(closeDelay),
|
|
86
|
+
className: cn(
|
|
87
|
+
"m-0 overflow-visible rounded-glob-sm border-0 bg-glob-text px-2 py-1 text-xs text-glob-surface shadow-glob-md",
|
|
88
|
+
"opacity-0 transition-[opacity,overlay,display] transition-discrete duration-100 open:opacity-100 starting:open:opacity-0 motion-reduce:transition-none",
|
|
89
|
+
className
|
|
90
|
+
),
|
|
91
|
+
children: [
|
|
92
|
+
content,
|
|
93
|
+
arrow && /* @__PURE__ */ jsx(
|
|
94
|
+
"div",
|
|
95
|
+
{
|
|
96
|
+
ref: setArrowEl,
|
|
97
|
+
"aria-hidden": "true",
|
|
98
|
+
className: "absolute h-2 w-2 rotate-45 bg-glob-text",
|
|
99
|
+
style: {
|
|
100
|
+
left: (arrowData == null ? void 0 : arrowData.x) != null ? `${arrowData.x}px` : void 0,
|
|
101
|
+
top: (arrowData == null ? void 0 : arrowData.y) != null ? `${arrowData.y}px` : void 0,
|
|
102
|
+
[oppositeSide[side]]: "-4px"
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
)
|
|
106
|
+
]
|
|
107
|
+
}
|
|
108
|
+
)
|
|
109
|
+
] });
|
|
110
|
+
}
|
|
111
|
+
Tooltip.displayName = "Tooltip";
|
|
112
|
+
export {
|
|
113
|
+
Tooltip
|
|
114
|
+
};
|
|
115
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/tooltip.tsx"],"sourcesContent":["\"use client\"\n\nimport { Children, isValidElement, useCallback, useEffect, useId, useRef, useState } from \"react\"\nimport { cn } from \"@glob-ui/utils\"\nimport { Slot } from \"@glob-ui/slot\"\nimport { useFloatingPanel, type Placement } from \"@glob-ui/hooks\"\n\nconst oppositeSide = { top: \"bottom\", right: \"left\", bottom: \"top\", left: \"right\" } as const\n\nexport interface TooltipProps {\n content: React.ReactNode\n /** One element that can hold a ref and receive focus (usually a button). */\n children: React.ReactElement\n placement?: Placement\n /** Hover delay before opening, in ms. Keyboard focus opens immediately. */\n delayDuration?: number\n /**\n * Delay before closing after the pointer leaves, in ms. Moving onto the tooltip within this\n * time keeps it open (WCAG 1.4.13, hoverable).\n */\n closeDelay?: number\n /** Gap between trigger and tooltip, in px. */\n offset?: number\n arrow?: boolean\n className?: string\n}\n\nfunction Tooltip({\n content,\n children,\n placement = \"top\",\n delayDuration = 500,\n closeDelay = 150,\n offset = 6,\n arrow = false,\n className,\n}: TooltipProps) {\n const [open, setOpen] = useState(false)\n const [arrowEl, setArrowEl] = useState<HTMLDivElement | null>(null)\n const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined)\n const id = useId()\n\n // Destructured: the compiler would treat a whole object passed to `ref=` as a ref.\n const { setTrigger, setPanel, floatingStyles, placement: finalPlacement, middlewareData } = useFloatingPanel({\n open,\n onOpenChange: setOpen,\n placement,\n offset: offset + (arrow ? 4 : 0),\n arrow: arrow ? arrowEl : null,\n fitViewport: false,\n restoreFocus: false,\n })\n\n const show = useCallback((delay: number) => {\n clearTimeout(timer.current)\n if (delay <= 0) setOpen(true)\n else timer.current = setTimeout(() => setOpen(true), delay)\n }, [])\n const hide = useCallback((delay: number) => {\n clearTimeout(timer.current)\n if (delay <= 0) setOpen(false)\n else timer.current = setTimeout(() => setOpen(false), delay)\n }, [])\n\n useEffect(() => () => clearTimeout(timer.current), [])\n\n // popover=\"manual\" has no Esc handling of its own.\n useEffect(() => {\n if (!open) return\n const onKeyDown = (e: KeyboardEvent) => {\n if (e.key === \"Escape\") hide(0)\n }\n document.addEventListener(\"keydown\", onKeyDown)\n return () => document.removeEventListener(\"keydown\", onKeyDown)\n }, [open, hide])\n\n const child = Children.only(children)\n const childDescribedBy = isValidElement<{ \"aria-describedby\"?: string }>(child)\n ? child.props[\"aria-describedby\"]\n : undefined\n const side = finalPlacement.split(\"-\")[0] as keyof typeof oppositeSide\n const arrowData = middlewareData.arrow\n\n return (\n <>\n <Slot\n ref={setTrigger}\n aria-describedby={[childDescribedBy, id].filter(Boolean).join(\" \")}\n onPointerEnter={(e: React.PointerEvent) => {\n if (e.pointerType !== \"touch\") show(delayDuration)\n }}\n onPointerLeave={(e: React.PointerEvent) => {\n if (e.pointerType !== \"touch\") hide(closeDelay)\n }}\n onPointerDown={() => hide(0)}\n onFocus={(e: React.FocusEvent<HTMLElement>) => {\n // Keyboard focus only: a tap or click also focuses, but is not :focus-visible.\n if (e.currentTarget.matches(\":focus-visible\")) show(0)\n }}\n onBlur={() => hide(0)}\n >\n {child}\n </Slot>\n <div\n ref={setPanel}\n id={id}\n role=\"tooltip\"\n popover=\"manual\"\n style={floatingStyles}\n // Hoverable: entering the tooltip cancels the pending close, leaving starts it again.\n onPointerEnter={() => clearTimeout(timer.current)}\n onPointerLeave={() => hide(closeDelay)}\n className={cn(\n \"m-0 overflow-visible rounded-glob-sm border-0 bg-glob-text px-2 py-1 text-xs text-glob-surface shadow-glob-md\",\n \"opacity-0 transition-[opacity,overlay,display] transition-discrete duration-100 open:opacity-100 starting:open:opacity-0 motion-reduce:transition-none\",\n className\n )}\n >\n {content}\n {arrow && (\n <div\n ref={setArrowEl}\n aria-hidden=\"true\"\n className=\"absolute h-2 w-2 rotate-45 bg-glob-text\"\n style={{\n left: arrowData?.x != null ? `${arrowData.x}px` : undefined,\n top: arrowData?.y != null ? `${arrowData.y}px` : undefined,\n [oppositeSide[side]]: \"-4px\",\n }}\n />\n )}\n </div>\n </>\n )\n}\nTooltip.displayName = \"Tooltip\"\n\nexport { Tooltip }\n"],"mappings":";;;AAEA,SAAS,UAAU,gBAAgB,aAAa,WAAW,OAAO,QAAQ,gBAAgB;AAC1F,SAAS,UAAU;AACnB,SAAS,YAAY;AACrB,SAAS,wBAAwC;AA+E7C,mBACE,KAkBA,YAnBF;AA7EJ,IAAM,eAAe,EAAE,KAAK,UAAU,OAAO,QAAQ,QAAQ,OAAO,MAAM,QAAQ;AAoBlF,SAAS,QAAQ;AAAA,EACf;AAAA,EACA;AAAA,EACA,YAAY;AAAA,EACZ,gBAAgB;AAAA,EAChB,aAAa;AAAA,EACb,SAAS;AAAA,EACT,QAAQ;AAAA,EACR;AACF,GAAiB;AACf,QAAM,CAAC,MAAM,OAAO,IAAI,SAAS,KAAK;AACtC,QAAM,CAAC,SAAS,UAAU,IAAI,SAAgC,IAAI;AAClE,QAAM,QAAQ,OAAkD,MAAS;AACzE,QAAM,KAAK,MAAM;AAGjB,QAAM,EAAE,YAAY,UAAU,gBAAgB,WAAW,gBAAgB,eAAe,IAAI,iBAAiB;AAAA,IAC3G;AAAA,IACA,cAAc;AAAA,IACd;AAAA,IACA,QAAQ,UAAU,QAAQ,IAAI;AAAA,IAC9B,OAAO,QAAQ,UAAU;AAAA,IACzB,aAAa;AAAA,IACb,cAAc;AAAA,EAChB,CAAC;AAED,QAAM,OAAO,YAAY,CAAC,UAAkB;AAC1C,iBAAa,MAAM,OAAO;AAC1B,QAAI,SAAS,EAAG,SAAQ,IAAI;AAAA,QACvB,OAAM,UAAU,WAAW,MAAM,QAAQ,IAAI,GAAG,KAAK;AA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,IACA;AAAA,MAAC;AAAA;AAAA,QACC,KAAK;AAAA,QACL;AAAA,QACA,MAAK;AAAA,QACL,SAAQ;AAAA,QACR,OAAO;AAAA,QAEP,gBAAgB,MAAM,aAAa,MAAM,OAAO;AAAA,QAChD,gBAAgB,MAAM,KAAK,UAAU;AAAA,QACrC,WAAW;AAAA,UACT;AAAA,UACA;AAAA,UACA;AAAA,QACF;AAAA,QAEC;AAAA;AAAA,UACA,SACC;AAAA,YAAC;AAAA;AAAA,cACC,KAAK;AAAA,cACL,eAAY;AAAA,cACZ,WAAU;AAAA,cACV,OAAO;AAAA,gBACL,OAAM,uCAAW,MAAK,OAAO,GAAG,UAAU,CAAC,OAAO;AAAA,gBAClD,MAAK,uCAAW,MAAK,OAAO,GAAG,UAAU,CAAC,OAAO;AAAA,gBACjD,CAAC,aAAa,IAAI,CAAC,GAAG;AAAA,cACxB;AAAA;AAAA,UACF;AAAA;AAAA;AAAA,IAEJ;AAAA,KACF;AAEJ;AACA,QAAQ,cAAc;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,70 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@glob-ui/tooltip",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"module": "./dist/index.mjs",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": {
|
|
10
|
+
"types": "./dist/index.d.mts",
|
|
11
|
+
"default": "./dist/index.mjs"
|
|
12
|
+
},
|
|
13
|
+
"require": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"./package.json": "./package.json"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist/",
|
|
22
|
+
"LICENSE",
|
|
23
|
+
"README.md"
|
|
24
|
+
],
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"react": "^19",
|
|
27
|
+
"react-dom": "^19"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@glob-ui/hooks": "0.1.0",
|
|
31
|
+
"@glob-ui/slot": "0.1.0",
|
|
32
|
+
"@glob-ui/utils": "0.1.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"tsup": "^8.5.1"
|
|
36
|
+
},
|
|
37
|
+
"description": "A short description that appears on hover or keyboard focus. It stays open while the pointer is over it.",
|
|
38
|
+
"keywords": [
|
|
39
|
+
"react",
|
|
40
|
+
"glob-ui",
|
|
41
|
+
"components",
|
|
42
|
+
"accessible",
|
|
43
|
+
"tailwind",
|
|
44
|
+
"tooltip"
|
|
45
|
+
],
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"author": "Kevin Martínez E",
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "git+https://github.com/kevin-martineze/GlobUI.git",
|
|
51
|
+
"directory": "packages/tooltip"
|
|
52
|
+
},
|
|
53
|
+
"homepage": "https://github.com/kevin-martineze/GlobUI#readme",
|
|
54
|
+
"bugs": {
|
|
55
|
+
"url": "https://github.com/kevin-martineze/GlobUI/issues"
|
|
56
|
+
},
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"access": "public",
|
|
59
|
+
"provenance": true
|
|
60
|
+
},
|
|
61
|
+
"sideEffects": false,
|
|
62
|
+
"type": "commonjs",
|
|
63
|
+
"engines": {
|
|
64
|
+
"node": ">=18.18"
|
|
65
|
+
},
|
|
66
|
+
"scripts": {
|
|
67
|
+
"build": "tsup",
|
|
68
|
+
"dev": "tsup --watch --no-clean"
|
|
69
|
+
}
|
|
6
70
|
}
|