datocms-react-ui 2.0.17 → 2.0.19
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/cjs/HotKey/index.js +105 -0
- package/dist/cjs/HotKey/index.js.map +1 -0
- package/dist/cjs/HotKey/styles.module.css.json +1 -0
- package/dist/cjs/Tooltip/Tooltip/index.js +116 -0
- package/dist/cjs/Tooltip/Tooltip/index.js.map +1 -0
- package/dist/cjs/Tooltip/TooltipContent/index.js +147 -0
- package/dist/cjs/Tooltip/TooltipContent/index.js.map +1 -0
- package/dist/cjs/Tooltip/TooltipContent/styles.module.css.json +1 -0
- package/dist/cjs/Tooltip/TooltipDelayGroup/index.js +140 -0
- package/dist/cjs/Tooltip/TooltipDelayGroup/index.js.map +1 -0
- package/dist/cjs/Tooltip/TooltipTrigger/index.js +102 -0
- package/dist/cjs/Tooltip/TooltipTrigger/index.js.map +1 -0
- package/dist/cjs/Tooltip/index.js +12 -0
- package/dist/cjs/Tooltip/index.js.map +1 -0
- package/dist/cjs/Tooltip/utils.js +165 -0
- package/dist/cjs/Tooltip/utils.js.map +1 -0
- package/dist/cjs/index.js +2 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/HotKey/index.d.ts +70 -0
- package/dist/esm/HotKey/index.js +75 -0
- package/dist/esm/HotKey/index.js.map +1 -0
- package/dist/esm/HotKey/styles.module.css.json +1 -0
- package/dist/esm/Tooltip/Tooltip/index.d.ts +74 -0
- package/dist/esm/Tooltip/Tooltip/index.js +89 -0
- package/dist/esm/Tooltip/Tooltip/index.js.map +1 -0
- package/dist/esm/Tooltip/TooltipContent/index.d.ts +68 -0
- package/dist/esm/Tooltip/TooltipContent/index.js +118 -0
- package/dist/esm/Tooltip/TooltipContent/index.js.map +1 -0
- package/dist/esm/Tooltip/TooltipContent/styles.module.css.json +1 -0
- package/dist/esm/Tooltip/TooltipDelayGroup/index.d.ts +118 -0
- package/dist/esm/Tooltip/TooltipDelayGroup/index.js +113 -0
- package/dist/esm/Tooltip/TooltipDelayGroup/index.js.map +1 -0
- package/dist/esm/Tooltip/TooltipTrigger/index.d.ts +45 -0
- package/dist/esm/Tooltip/TooltipTrigger/index.js +76 -0
- package/dist/esm/Tooltip/TooltipTrigger/index.js.map +1 -0
- package/dist/esm/Tooltip/index.d.ts +8 -0
- package/dist/esm/Tooltip/index.js +5 -0
- package/dist/esm/Tooltip/index.js.map +1 -0
- package/dist/esm/Tooltip/utils.d.ts +166 -0
- package/dist/esm/Tooltip/utils.js +135 -0
- package/dist/esm/Tooltip/utils.js.map +1 -0
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/types/HotKey/index.d.ts +70 -0
- package/dist/types/Tooltip/Tooltip/index.d.ts +74 -0
- package/dist/types/Tooltip/TooltipContent/index.d.ts +68 -0
- package/dist/types/Tooltip/TooltipDelayGroup/index.d.ts +118 -0
- package/dist/types/Tooltip/TooltipTrigger/index.d.ts +45 -0
- package/dist/types/Tooltip/index.d.ts +8 -0
- package/dist/types/Tooltip/utils.d.ts +166 -0
- package/dist/types/index.d.ts +2 -0
- package/package.json +4 -3
- package/src/HotKey/index.tsx +95 -0
- package/src/HotKey/styles.module.css +22 -0
- package/src/HotKey/styles.module.css.json +1 -0
- package/src/Tooltip/Tooltip/index.tsx +85 -0
- package/src/Tooltip/TooltipContent/index.tsx +145 -0
- package/src/Tooltip/TooltipContent/styles.module.css +10 -0
- package/src/Tooltip/TooltipContent/styles.module.css.json +1 -0
- package/src/Tooltip/TooltipDelayGroup/index.tsx +128 -0
- package/src/Tooltip/TooltipTrigger/index.tsx +71 -0
- package/src/Tooltip/index.ts +8 -0
- package/src/Tooltip/utils.ts +176 -0
- package/src/global.css +2 -0
- package/src/index.ts +2 -0
- package/styles.css +1 -1
- package/types.json +4864 -3053
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import type { Placement } from '@floating-ui/react';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
export declare function getSharedPortalRoot(): HTMLDivElement;
|
|
4
|
+
export declare function releaseSharedPortalRoot(): void;
|
|
5
|
+
export interface TooltipOptions {
|
|
6
|
+
/** Whether the tooltip is initially open (uncontrolled mode) */
|
|
7
|
+
initialOpen?: boolean;
|
|
8
|
+
/** Placement of the tooltip relative to its trigger */
|
|
9
|
+
placement?: Placement;
|
|
10
|
+
/** Controlled open state */
|
|
11
|
+
open?: boolean;
|
|
12
|
+
/** Callback when open state changes (controlled mode) */
|
|
13
|
+
onOpenChange?: (open: boolean) => void;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Hook that manages tooltip state and positioning logic.
|
|
17
|
+
*
|
|
18
|
+
* This hook provides the core tooltip behavior including hover/focus detection,
|
|
19
|
+
* positioning, and accessibility features. Use this when you need full control
|
|
20
|
+
* over the tooltip structure.
|
|
21
|
+
*
|
|
22
|
+
* @example Basic tooltip hook usage
|
|
23
|
+
*
|
|
24
|
+
* Build a custom tooltip implementation using the hook directly for maximum flexibility:
|
|
25
|
+
*
|
|
26
|
+
* ```js
|
|
27
|
+
* function MyComponent() {
|
|
28
|
+
* const tooltip = useTooltip({ placement: 'top' });
|
|
29
|
+
*
|
|
30
|
+
* return (
|
|
31
|
+
* <>
|
|
32
|
+
* <button
|
|
33
|
+
* ref={tooltip.refs.setReference}
|
|
34
|
+
* {...tooltip.getReferenceProps()}
|
|
35
|
+
* >
|
|
36
|
+
* Hover me
|
|
37
|
+
* </button>
|
|
38
|
+
*
|
|
39
|
+
* {tooltip.open && (
|
|
40
|
+
* <div
|
|
41
|
+
* ref={tooltip.refs.setFloating}
|
|
42
|
+
* style={tooltip.floatingStyles}
|
|
43
|
+
* {...tooltip.getFloatingProps()}
|
|
44
|
+
* >
|
|
45
|
+
* Tooltip content
|
|
46
|
+
* </div>
|
|
47
|
+
* )}
|
|
48
|
+
* </>
|
|
49
|
+
* );
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @example Controlled tooltip
|
|
54
|
+
*
|
|
55
|
+
* Control the tooltip's open state programmatically for click-to-toggle behavior:
|
|
56
|
+
*
|
|
57
|
+
* ```js
|
|
58
|
+
* function ControlledTooltip() {
|
|
59
|
+
* const [open, setOpen] = React.useState(false);
|
|
60
|
+
* const tooltip = useTooltip({ open, onOpenChange: setOpen });
|
|
61
|
+
*
|
|
62
|
+
* return (
|
|
63
|
+
* <Canvas ctx={ctx}>
|
|
64
|
+
* <button onClick={() => setOpen(!open)}>
|
|
65
|
+
* Toggle tooltip
|
|
66
|
+
* </button>
|
|
67
|
+
* </Canvas>
|
|
68
|
+
* );
|
|
69
|
+
* }
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export declare function useTooltip({ initialOpen, placement, open: controlledOpen, onOpenChange: setControlledOpen, }?: TooltipOptions): {
|
|
73
|
+
placement: Placement;
|
|
74
|
+
strategy: import("@floating-ui/utils").Strategy;
|
|
75
|
+
middlewareData: import("@floating-ui/core").MiddlewareData;
|
|
76
|
+
x: number;
|
|
77
|
+
y: number;
|
|
78
|
+
isPositioned: boolean;
|
|
79
|
+
update: () => void;
|
|
80
|
+
floatingStyles: React.CSSProperties;
|
|
81
|
+
refs: {
|
|
82
|
+
reference: React.MutableRefObject<import("@floating-ui/react-dom").ReferenceType | null>;
|
|
83
|
+
floating: React.MutableRefObject<HTMLElement | null>;
|
|
84
|
+
setReference: (node: import("@floating-ui/react-dom").ReferenceType | null) => void;
|
|
85
|
+
setFloating: (node: HTMLElement | null) => void;
|
|
86
|
+
} & import("@floating-ui/react").ExtendedRefs<import("@floating-ui/react").ReferenceType>;
|
|
87
|
+
elements: {
|
|
88
|
+
reference: import("@floating-ui/react-dom").ReferenceType | null;
|
|
89
|
+
floating: HTMLElement | null;
|
|
90
|
+
} & import("@floating-ui/react").ExtendedElements<import("@floating-ui/react").ReferenceType>;
|
|
91
|
+
context: {
|
|
92
|
+
x: number;
|
|
93
|
+
y: number;
|
|
94
|
+
placement: Placement;
|
|
95
|
+
strategy: import("@floating-ui/utils").Strategy;
|
|
96
|
+
middlewareData: import("@floating-ui/core").MiddlewareData;
|
|
97
|
+
isPositioned: boolean;
|
|
98
|
+
update: () => void;
|
|
99
|
+
floatingStyles: React.CSSProperties;
|
|
100
|
+
open: boolean;
|
|
101
|
+
onOpenChange: (open: boolean, event?: Event | undefined, reason?: import("@floating-ui/react").OpenChangeReason | undefined) => void;
|
|
102
|
+
events: import("@floating-ui/react").FloatingEvents;
|
|
103
|
+
dataRef: React.MutableRefObject<import("@floating-ui/react").ContextData>;
|
|
104
|
+
nodeId: string | undefined;
|
|
105
|
+
floatingId: string | undefined;
|
|
106
|
+
refs: import("@floating-ui/react").ExtendedRefs<import("@floating-ui/react").ReferenceType>;
|
|
107
|
+
elements: import("@floating-ui/react").ExtendedElements<import("@floating-ui/react").ReferenceType>;
|
|
108
|
+
};
|
|
109
|
+
getReferenceProps: (userProps?: React.HTMLProps<Element> | undefined) => Record<string, unknown>;
|
|
110
|
+
getFloatingProps: (userProps?: React.HTMLProps<HTMLElement> | undefined) => Record<string, unknown>;
|
|
111
|
+
getItemProps: (userProps?: (Omit<React.HTMLProps<HTMLElement>, "selected" | "active"> & {
|
|
112
|
+
active?: boolean | undefined;
|
|
113
|
+
selected?: boolean | undefined;
|
|
114
|
+
}) | undefined) => Record<string, unknown>;
|
|
115
|
+
open: boolean;
|
|
116
|
+
setOpen: (open: boolean) => void;
|
|
117
|
+
};
|
|
118
|
+
declare type ContextType = ReturnType<typeof useTooltip> | null;
|
|
119
|
+
export declare const TooltipContext: React.Context<ContextType>;
|
|
120
|
+
export declare const useTooltipState: () => {
|
|
121
|
+
placement: Placement;
|
|
122
|
+
strategy: import("@floating-ui/utils").Strategy;
|
|
123
|
+
middlewareData: import("@floating-ui/core").MiddlewareData;
|
|
124
|
+
x: number;
|
|
125
|
+
y: number;
|
|
126
|
+
isPositioned: boolean;
|
|
127
|
+
update: () => void;
|
|
128
|
+
floatingStyles: React.CSSProperties;
|
|
129
|
+
refs: {
|
|
130
|
+
reference: React.MutableRefObject<import("@floating-ui/react-dom").ReferenceType | null>;
|
|
131
|
+
floating: React.MutableRefObject<HTMLElement | null>;
|
|
132
|
+
setReference: (node: import("@floating-ui/react-dom").ReferenceType | null) => void;
|
|
133
|
+
setFloating: (node: HTMLElement | null) => void;
|
|
134
|
+
} & import("@floating-ui/react").ExtendedRefs<import("@floating-ui/react").ReferenceType>;
|
|
135
|
+
elements: {
|
|
136
|
+
reference: import("@floating-ui/react-dom").ReferenceType | null;
|
|
137
|
+
floating: HTMLElement | null;
|
|
138
|
+
} & import("@floating-ui/react").ExtendedElements<import("@floating-ui/react").ReferenceType>;
|
|
139
|
+
context: {
|
|
140
|
+
x: number;
|
|
141
|
+
y: number;
|
|
142
|
+
placement: Placement;
|
|
143
|
+
strategy: import("@floating-ui/utils").Strategy;
|
|
144
|
+
middlewareData: import("@floating-ui/core").MiddlewareData;
|
|
145
|
+
isPositioned: boolean;
|
|
146
|
+
update: () => void;
|
|
147
|
+
floatingStyles: React.CSSProperties;
|
|
148
|
+
open: boolean;
|
|
149
|
+
onOpenChange: (open: boolean, event?: Event | undefined, reason?: import("@floating-ui/react").OpenChangeReason | undefined) => void;
|
|
150
|
+
events: import("@floating-ui/react").FloatingEvents;
|
|
151
|
+
dataRef: React.MutableRefObject<import("@floating-ui/react").ContextData>;
|
|
152
|
+
nodeId: string | undefined;
|
|
153
|
+
floatingId: string | undefined;
|
|
154
|
+
refs: import("@floating-ui/react").ExtendedRefs<import("@floating-ui/react").ReferenceType>;
|
|
155
|
+
elements: import("@floating-ui/react").ExtendedElements<import("@floating-ui/react").ReferenceType>;
|
|
156
|
+
};
|
|
157
|
+
getReferenceProps: (userProps?: React.HTMLProps<Element> | undefined) => Record<string, unknown>;
|
|
158
|
+
getFloatingProps: (userProps?: React.HTMLProps<HTMLElement> | undefined) => Record<string, unknown>;
|
|
159
|
+
getItemProps: (userProps?: (Omit<React.HTMLProps<HTMLElement>, "selected" | "active"> & {
|
|
160
|
+
active?: boolean | undefined;
|
|
161
|
+
selected?: boolean | undefined;
|
|
162
|
+
}) | undefined) => Record<string, unknown>;
|
|
163
|
+
open: boolean;
|
|
164
|
+
setOpen: (open: boolean) => void;
|
|
165
|
+
};
|
|
166
|
+
export {};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export * from './FieldHint';
|
|
|
9
9
|
export * from './FieldWrapper';
|
|
10
10
|
export * from './Form';
|
|
11
11
|
export * from './FormLabel';
|
|
12
|
+
export * from './HotKey';
|
|
12
13
|
export * from './icons';
|
|
13
14
|
export * from './Section';
|
|
14
15
|
export * from './SelectField';
|
|
@@ -23,6 +24,7 @@ export * from './TextareaInput';
|
|
|
23
24
|
export * from './TextField';
|
|
24
25
|
export * from './TextInput';
|
|
25
26
|
export * from './Toolbar';
|
|
27
|
+
export * from './Tooltip';
|
|
26
28
|
export * from './useClickOutside';
|
|
27
29
|
export * from './useMediaQuery';
|
|
28
30
|
export * from './VerticalSplit';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "datocms-react-ui",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.19",
|
|
4
4
|
"description": "React components to use inside DatoCMS plugins",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"datocms",
|
|
@@ -40,8 +40,9 @@
|
|
|
40
40
|
"url": "https://github.com/datocms/plugins-sdk/issues"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
+
"@floating-ui/react": "^0.27.16",
|
|
43
44
|
"classnames": "^2.3.1",
|
|
44
|
-
"datocms-plugin-sdk": "^2.0.
|
|
45
|
+
"datocms-plugin-sdk": "^2.0.18",
|
|
45
46
|
"react-intersection-observer": "^8.31.0",
|
|
46
47
|
"react-select": "^5.2.1",
|
|
47
48
|
"scroll-into-view-if-needed": "^2.2.20"
|
|
@@ -57,5 +58,5 @@
|
|
|
57
58
|
"postcss-nested": "^5.0.6",
|
|
58
59
|
"typedoc": "^0.26.7"
|
|
59
60
|
},
|
|
60
|
-
"gitHead": "
|
|
61
|
+
"gitHead": "136cd08943c66f818b29e51313acc009f26163e8"
|
|
61
62
|
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import styles from './styles.module.css.json';
|
|
3
|
+
|
|
4
|
+
const isMac = navigator.platform.indexOf('Mac') > -1;
|
|
5
|
+
const modifierKey = isMac ? '⌘' : 'Ctrl';
|
|
6
|
+
|
|
7
|
+
export type HotKeyProps = {
|
|
8
|
+
/**
|
|
9
|
+
* Keyboard shortcut string. Use "mod" for platform-specific modifier (Cmd on Mac, Ctrl elsewhere).
|
|
10
|
+
* Separate keys with "+". Examples: "mod+s", "mod+shift+p", "alt+enter"
|
|
11
|
+
*/
|
|
12
|
+
hotkey: string;
|
|
13
|
+
/** Optional label to display before the key combination */
|
|
14
|
+
label?: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* HotKey component displays keyboard shortcuts in a platform-aware format.
|
|
19
|
+
*
|
|
20
|
+
* The component automatically detects the user's platform and renders appropriate
|
|
21
|
+
* modifier key symbols (⌘ for Mac, Ctrl for Windows/Linux).
|
|
22
|
+
*
|
|
23
|
+
* @example Basic usage
|
|
24
|
+
*
|
|
25
|
+
* Display a simple keyboard shortcut without a label:
|
|
26
|
+
*
|
|
27
|
+
* ```js
|
|
28
|
+
* <Canvas ctx={ctx}>
|
|
29
|
+
* <HotKey hotkey="mod+s" />
|
|
30
|
+
* </Canvas>;
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @example With label
|
|
34
|
+
*
|
|
35
|
+
* Include a descriptive label to explain what the keyboard shortcut does:
|
|
36
|
+
*
|
|
37
|
+
* ```js
|
|
38
|
+
* <Canvas ctx={ctx}>
|
|
39
|
+
* <HotKey hotkey="mod+s" label="Save" />
|
|
40
|
+
* </Canvas>;
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @example Multiple hotkeys
|
|
44
|
+
*
|
|
45
|
+
* Display a list of keyboard shortcuts with labels for documenting available commands:
|
|
46
|
+
*
|
|
47
|
+
* ```js
|
|
48
|
+
* <Canvas ctx={ctx}>
|
|
49
|
+
* <div style={{ display: 'flex', flexDirection: 'column', gap: 'var(--spacing-m)' }}>
|
|
50
|
+
* <HotKey hotkey="mod+c" label="Copy" />
|
|
51
|
+
* <HotKey hotkey="mod+v" label="Paste" />
|
|
52
|
+
* <HotKey hotkey="mod+shift+z" label="Redo" />
|
|
53
|
+
* <HotKey hotkey="alt+enter" label="Submit" />
|
|
54
|
+
* </div>
|
|
55
|
+
* </Canvas>;
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* @example Platform-specific rendering
|
|
59
|
+
*
|
|
60
|
+
* The component automatically adapts modifier keys based on the user's platform:
|
|
61
|
+
* - `mod` renders as `⌘` on Mac and `Ctrl` on Windows/Linux
|
|
62
|
+
* - `alt` renders as `⌥` on Mac and `Alt` on Windows/Linux
|
|
63
|
+
*
|
|
64
|
+
* This ensures the correct symbols are displayed for the user's operating system:
|
|
65
|
+
*
|
|
66
|
+
* ```js
|
|
67
|
+
* <Canvas ctx={ctx}>
|
|
68
|
+
* <div style={{ display: 'flex', flexDirection: 'column', gap: 'var(--spacing-m)' }}>
|
|
69
|
+
* <HotKey hotkey="mod+k" label="Open command palette" />
|
|
70
|
+
* <HotKey hotkey="alt+enter" label="Submit form" />
|
|
71
|
+
* <HotKey hotkey="mod+alt+f" label="Find and replace" />
|
|
72
|
+
* </div>
|
|
73
|
+
* </Canvas>;
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export function HotKey({ hotkey, label }: HotKeyProps) {
|
|
77
|
+
const keys = hotkey
|
|
78
|
+
.replace('mod', modifierKey)
|
|
79
|
+
.replace('alt', isMac ? '⌥' : 'Alt')
|
|
80
|
+
.split(/\+/)
|
|
81
|
+
.map((e) => e.charAt(0).toUpperCase() + e.slice(1));
|
|
82
|
+
|
|
83
|
+
return (
|
|
84
|
+
<div className={styles.hotKey}>
|
|
85
|
+
{label && <span className={styles.label}>{label}</span>}
|
|
86
|
+
<div className={styles.keys}>
|
|
87
|
+
{keys.map((key) => (
|
|
88
|
+
<span key={key} className={styles.hotKeyKey}>
|
|
89
|
+
{key}
|
|
90
|
+
</span>
|
|
91
|
+
))}
|
|
92
|
+
</div>
|
|
93
|
+
</div>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
.hotKey {
|
|
2
|
+
display: inline-flex;
|
|
3
|
+
align-items: center;
|
|
4
|
+
gap: 15px;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
.label {
|
|
8
|
+
display: flex;
|
|
9
|
+
align-items: center;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
.keys {
|
|
13
|
+
display: inline-flex;
|
|
14
|
+
align-items: center;
|
|
15
|
+
gap: 4px;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
.hotKeyKey {
|
|
19
|
+
padding: 5px 8px;
|
|
20
|
+
background: var(--light-color);
|
|
21
|
+
border-radius: 3px;
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"hotKey":"_hotKey_1eko8_1","label":"_label_1eko8_7","keys":"_keys_1eko8_12","hotKeyKey":"_hotKeyKey_1eko8_18"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { TooltipContext, TooltipOptions, useTooltip } from '../utils';
|
|
3
|
+
|
|
4
|
+
export type TooltipProps = {
|
|
5
|
+
children?: React.ReactNode;
|
|
6
|
+
} & TooltipOptions;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Tooltip wrapper component that provides context for TooltipTrigger and TooltipContent.
|
|
10
|
+
*
|
|
11
|
+
* This is a compound component pattern. The Tooltip component itself doesn't render anything,
|
|
12
|
+
* but provides the necessary context for its children (TooltipTrigger and TooltipContent).
|
|
13
|
+
*
|
|
14
|
+
* @example Basic tooltip
|
|
15
|
+
*
|
|
16
|
+
* Create a simple tooltip that appears when hovering over a button:
|
|
17
|
+
*
|
|
18
|
+
* ```js
|
|
19
|
+
* <Canvas ctx={ctx}>
|
|
20
|
+
* <Tooltip>
|
|
21
|
+
* <TooltipTrigger>
|
|
22
|
+
* <Button>Hover me</Button>
|
|
23
|
+
* </TooltipTrigger>
|
|
24
|
+
* <TooltipContent>
|
|
25
|
+
* This is helpful information!
|
|
26
|
+
* </TooltipContent>
|
|
27
|
+
* </Tooltip>
|
|
28
|
+
* </Canvas>;
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @example Tooltip with custom placement
|
|
32
|
+
*
|
|
33
|
+
* Control where the tooltip appears relative to its trigger using the `placement` prop:
|
|
34
|
+
*
|
|
35
|
+
* ```js
|
|
36
|
+
* <Canvas ctx={ctx}>
|
|
37
|
+
* <Tooltip placement="right">
|
|
38
|
+
* <TooltipTrigger>
|
|
39
|
+
* <Button>Right tooltip</Button>
|
|
40
|
+
* </TooltipTrigger>
|
|
41
|
+
* <TooltipContent>
|
|
42
|
+
* Appears on the right side
|
|
43
|
+
* </TooltipContent>
|
|
44
|
+
* </Tooltip>
|
|
45
|
+
* </Canvas>;
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @example Multiple tooltips
|
|
49
|
+
*
|
|
50
|
+
* Use multiple tooltips on the same page to provide contextual help for different actions:
|
|
51
|
+
*
|
|
52
|
+
* ```js
|
|
53
|
+
* <Canvas ctx={ctx}>
|
|
54
|
+
* <div style={{ display: 'flex', gap: 'var(--spacing-m)' }}>
|
|
55
|
+
* <Tooltip>
|
|
56
|
+
* <TooltipTrigger>
|
|
57
|
+
* <Button leftIcon={<SaveIcon />} />
|
|
58
|
+
* </TooltipTrigger>
|
|
59
|
+
* <TooltipContent>
|
|
60
|
+
* Save changes (⌘S)
|
|
61
|
+
* </TooltipContent>
|
|
62
|
+
* </Tooltip>
|
|
63
|
+
*
|
|
64
|
+
* <Tooltip>
|
|
65
|
+
* <TooltipTrigger>
|
|
66
|
+
* <Button leftIcon={<DeleteIcon />} />
|
|
67
|
+
* </TooltipTrigger>
|
|
68
|
+
* <TooltipContent>
|
|
69
|
+
* Delete item
|
|
70
|
+
* </TooltipContent>
|
|
71
|
+
* </Tooltip>
|
|
72
|
+
* </div>
|
|
73
|
+
* </Canvas>;
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export function Tooltip({ children, ...options }: TooltipProps) {
|
|
77
|
+
// This can accept any props as options, e.g. `placement`,
|
|
78
|
+
// or other positioning options.
|
|
79
|
+
const tooltip = useTooltip(options);
|
|
80
|
+
return (
|
|
81
|
+
<TooltipContext.Provider value={tooltip}>
|
|
82
|
+
{children}
|
|
83
|
+
</TooltipContext.Provider>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FloatingPortal,
|
|
3
|
+
useDelayGroup,
|
|
4
|
+
useDelayGroupContext,
|
|
5
|
+
useMergeRefs,
|
|
6
|
+
useTransitionStyles,
|
|
7
|
+
} from '@floating-ui/react';
|
|
8
|
+
import * as React from 'react';
|
|
9
|
+
import { Canvas, useCtx } from '../../Canvas';
|
|
10
|
+
import {
|
|
11
|
+
getSharedPortalRoot,
|
|
12
|
+
releaseSharedPortalRoot,
|
|
13
|
+
useTooltipState,
|
|
14
|
+
} from '../utils';
|
|
15
|
+
import s from './styles.module.css.json';
|
|
16
|
+
|
|
17
|
+
export type TooltipContentProps = {
|
|
18
|
+
children?: React.ReactNode;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* TooltipContent contains the content displayed in the floating tooltip.
|
|
23
|
+
*
|
|
24
|
+
* The content is automatically wrapped in a Canvas component to inherit the DatoCMS
|
|
25
|
+
* theme and styling. The tooltip uses a portal to render outside the normal DOM
|
|
26
|
+
* hierarchy, ensuring proper positioning and z-index stacking.
|
|
27
|
+
*
|
|
28
|
+
* @example Simple text tooltip
|
|
29
|
+
*
|
|
30
|
+
* Display plain text in a tooltip to provide helpful information:
|
|
31
|
+
*
|
|
32
|
+
* ```js
|
|
33
|
+
* <Canvas ctx={ctx}>
|
|
34
|
+
* <Tooltip>
|
|
35
|
+
* <TooltipTrigger>
|
|
36
|
+
* <Button>Delete</Button>
|
|
37
|
+
* </TooltipTrigger>
|
|
38
|
+
* <TooltipContent>
|
|
39
|
+
* This action cannot be undone
|
|
40
|
+
* </TooltipContent>
|
|
41
|
+
* </Tooltip>
|
|
42
|
+
* </Canvas>;
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @example Rich content tooltip
|
|
46
|
+
*
|
|
47
|
+
* Include formatted content with custom styles for more detailed information:
|
|
48
|
+
*
|
|
49
|
+
* ```js
|
|
50
|
+
* <Canvas ctx={ctx}>
|
|
51
|
+
* <Tooltip placement="right">
|
|
52
|
+
* <TooltipTrigger>
|
|
53
|
+
* <Button leftIcon={<HelpIcon />}>Help</Button>
|
|
54
|
+
* </TooltipTrigger>
|
|
55
|
+
* <TooltipContent>
|
|
56
|
+
* <div>
|
|
57
|
+
* <strong>Need assistance?</strong>
|
|
58
|
+
* <p style={{ margin: '5px 0 0 0', fontSize: 'var(--font-size-xs)' }}>
|
|
59
|
+
* Contact support@example.com
|
|
60
|
+
* </p>
|
|
61
|
+
* </div>
|
|
62
|
+
* </TooltipContent>
|
|
63
|
+
* </Tooltip>
|
|
64
|
+
* </Canvas>;
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* @example Tooltip with keyboard shortcut
|
|
68
|
+
*
|
|
69
|
+
* Combine tooltips with the HotKey component to show keyboard shortcuts:
|
|
70
|
+
*
|
|
71
|
+
* ```js
|
|
72
|
+
* <Canvas ctx={ctx}>
|
|
73
|
+
* <Tooltip>
|
|
74
|
+
* <TooltipTrigger>
|
|
75
|
+
* <Button leftIcon={<SaveIcon />}>Save</Button>
|
|
76
|
+
* </TooltipTrigger>
|
|
77
|
+
* <TooltipContent>
|
|
78
|
+
* <HotKey hotkey="mod+s" label="Save changes" />
|
|
79
|
+
* </TooltipContent>
|
|
80
|
+
* </Tooltip>
|
|
81
|
+
* </Canvas>;
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export const TooltipContent = React.forwardRef<
|
|
85
|
+
HTMLDivElement,
|
|
86
|
+
TooltipContentProps
|
|
87
|
+
>(function TooltipContent({ children }, propRef) {
|
|
88
|
+
const ctx = useCtx();
|
|
89
|
+
const state = useTooltipState();
|
|
90
|
+
const { isInstantPhase, currentId } = useDelayGroupContext();
|
|
91
|
+
const ref = useMergeRefs([state.refs.setFloating, propRef]);
|
|
92
|
+
|
|
93
|
+
// Use the shared portal root
|
|
94
|
+
const portalRootRef = React.useRef<HTMLDivElement | null>(null);
|
|
95
|
+
|
|
96
|
+
React.useEffect(() => {
|
|
97
|
+
// Get the shared portal root and increment ref count
|
|
98
|
+
portalRootRef.current = getSharedPortalRoot();
|
|
99
|
+
|
|
100
|
+
// Cleanup function to release the shared portal root
|
|
101
|
+
return () => {
|
|
102
|
+
releaseSharedPortalRoot();
|
|
103
|
+
};
|
|
104
|
+
}, []);
|
|
105
|
+
|
|
106
|
+
useDelayGroup(state.context, { id: state.context.floatingId });
|
|
107
|
+
|
|
108
|
+
const instantDuration = 0;
|
|
109
|
+
const duration = 250;
|
|
110
|
+
|
|
111
|
+
const { isMounted, styles } = useTransitionStyles(state.context, {
|
|
112
|
+
duration: isInstantPhase
|
|
113
|
+
? {
|
|
114
|
+
open: instantDuration,
|
|
115
|
+
// `id` is this component's `id`
|
|
116
|
+
// `currentId` is the current group's `id`
|
|
117
|
+
close:
|
|
118
|
+
currentId === state.context.floatingId ? duration : instantDuration,
|
|
119
|
+
}
|
|
120
|
+
: duration,
|
|
121
|
+
initial: {
|
|
122
|
+
opacity: 0,
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
if (!isMounted) return null;
|
|
127
|
+
|
|
128
|
+
return (
|
|
129
|
+
<FloatingPortal root={portalRootRef}>
|
|
130
|
+
<Canvas ctx={ctx} noAutoResizer>
|
|
131
|
+
<div
|
|
132
|
+
ref={ref}
|
|
133
|
+
style={{
|
|
134
|
+
...state.floatingStyles,
|
|
135
|
+
...styles,
|
|
136
|
+
}}
|
|
137
|
+
{...state.getFloatingProps()}
|
|
138
|
+
className={s.tooltip}
|
|
139
|
+
>
|
|
140
|
+
{children}
|
|
141
|
+
</div>
|
|
142
|
+
</Canvas>
|
|
143
|
+
</FloatingPortal>
|
|
144
|
+
);
|
|
145
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"tooltip":"_tooltip_3z5rn_1"}
|