-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
Tooltip.tsx
275 lines (240 loc) · 9 KB
/
Tooltip.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import Component from '../Component';
import type Mithril from 'mithril';
import classList from '../utils/classList';
import extractText from '../utils/extractText';
export interface TooltipAttrs extends Mithril.CommonAttributes<TooltipAttrs, Tooltip> {
/**
* Tooltip textual content.
*
* String arrays, like those provided by the translator, will be flattened
* into strings.
*/
text: string | string[];
/**
* Use to manually show or hide the tooltip. `undefined` will show based on cursor events.
*
* Default: `undefined`.
*/
tooltipVisible?: boolean;
/**
* Whether to show on focus.
*
* Default: `true`.
*/
showOnFocus?: boolean;
/**
* Tooltip position around element.
*
* Default: `'top'`.
*/
position?: 'top' | 'bottom' | 'left' | 'right';
/**
* Whether HTML content is allowed in the tooltip.
*
* **Warning:** this is a possible XSS attack vector. This option shouldn't
* be used wherever possible, and may not work when we migrate to another
* tooltip library. Be prepared for this to break in Flarum stable.
*
* Default: `false`.
*
* @deprecated
*/
html?: boolean;
/**
* Sets the delay between a trigger state occurring and the tooltip appearing
* on-screen.
*
* **Warning:** this option may be removed when switching to another tooltip
* library. Be prepared for this to break in Flarum stable.
*
* Default: `0`.
*
* @deprecated
*/
delay?: number;
/**
* Used to disable the warning for passing text to the `title` attribute.
*
* Tooltip text should be passed to the `text` attribute.
*/
ignoreTitleWarning?: boolean;
}
/**
* The `Tooltip` component is used to create a tooltip for an element. It
* requires a single child element to be passed to it. Passing multiple
* children or fragments will throw an error.
*
* You should use this for any tooltips you create to allow for backwards
* compatibility when we switch to another tooltip library instead of
* Bootstrap tooltips.
*
* If you need to pass multiple children, surround them with another element,
* such as a `<span>` or `<div>`.
*
* **Note:** this component will overwrite the `title` attribute of the first
* child you pass to it, as this is how the current tooltip system works in
* Flarum. This shouldn't be an issue if you're using this component correctly.
*
* @example <caption>Basic usage</caption>
* <Tooltip text="You wish!">
* <Button>
* Click for free money!
* </Button>
* </Tooltip>
*
* @example <caption>Use of `position` and `showOnFocus` attrs</caption>
* <Tooltip text="Woah! That's cool!" position="bottom" showOnFocus>
* <span>3 replies</span>
* </Tooltip>
*
* @example <caption>Incorrect usage</caption>
* // This is wrong! Surround the children with a <span> or similar.
* <Tooltip text="This won't work">
* Click
* <a href="/">here</a>
* </Tooltip>
*/
export default class Tooltip extends Component<TooltipAttrs> {
private firstChild: Mithril.Vnode<any, any> | null = null;
private childDomNode: HTMLElement | null = null;
private oldText: string = '';
private oldVisibility: boolean | undefined;
private shouldRecreateTooltip: boolean = false;
private shouldChangeTooltipVisibility: boolean = false;
view(vnode: Mithril.Vnode<TooltipAttrs, this>) {
/**
* We know this will be a ChildArray and not a primitive as this
* vnode is a component, not a text or trusted HTML vnode.
*/
const children = vnode.children as Mithril.ChildArray | undefined;
// We remove these to get the remaining attrs to pass to the DOM element
const { text, tooltipVisible, showOnFocus = true, position = 'top', ignoreTitleWarning = false, html = false, delay = 0, ...attrs } = this.attrs;
if ((this.attrs as any).title && !ignoreTitleWarning) {
console.warn(
'`title` attribute was passed to Tooltip component. Was this intentional? Tooltip content should be passed to the `text` attr instead.'
);
}
const realText = this.getRealText();
// We need to recreate the tooltip if the text has changed
if (realText !== this.oldText) {
this.oldText = realText;
this.shouldRecreateTooltip = true;
}
if (tooltipVisible !== this.oldVisibility) {
this.oldVisibility = this.attrs.tooltipVisible;
this.shouldChangeTooltipVisibility = true;
}
// We'll try our best to detect any issues created by devs before they cause any weird effects.
// Throwing an error will prevent the forum rendering, but will be better at alerting devs to
// an issue.
if (typeof children === 'undefined') {
throw new Error(
`Tooltip component was provided with no direct child DOM element. Tooltips must contain a single direct DOM node to attach to.`
);
}
if (children.length !== 1) {
throw new Error(
`Tooltip component was either passed more than one or no child node.\n\nPlease wrap multiple children in another element, such as a <div> or <span>.`
);
}
const firstChild = children[0];
if (typeof firstChild !== 'object' || Array.isArray(firstChild) || firstChild === null) {
throw new Error(
`Tooltip component was provided with no direct child DOM element. Tooltips must contain a single direct DOM node to attach to.`
);
}
if (typeof firstChild.tag === 'string' && ['#', '[', '<'].includes(firstChild.tag)) {
throw new Error(
`Tooltip component with provided with a vnode with tag "${firstChild.tag}". This is not a DOM element, so is not a valid child element. Please wrap this vnode in another element, such as a <div> or <span>.`
);
}
this.firstChild = firstChild;
return children;
}
oncreate(vnode: Mithril.VnodeDOM<TooltipAttrs, this>) {
super.oncreate(vnode);
this.checkDomNodeChanged();
this.recreateTooltip();
}
onupdate(vnode: Mithril.VnodeDOM<TooltipAttrs, this>) {
super.onupdate(vnode);
this.checkDomNodeChanged();
this.recreateTooltip();
}
private recreateTooltip() {
if (this.shouldRecreateTooltip && this.childDomNode !== null) {
$(this.childDomNode).tooltip(
'destroy',
// @ts-expect-error We don't want this arg to be part of the public API. It only exists to prevent deprecation warnings when using `$.tooltip` in this component.
'DANGEROUS_tooltip_jquery_fn_deprecation_exempt'
);
this.createTooltip();
this.shouldRecreateTooltip = false;
}
if (this.shouldChangeTooltipVisibility) {
this.shouldChangeTooltipVisibility = false;
this.updateVisibility();
}
}
private updateVisibility() {
if (this.childDomNode === null) return;
if (this.attrs.tooltipVisible === true) {
$(this.childDomNode).tooltip(
'show',
// @ts-expect-error We don't want this arg to be part of the public API. It only exists to prevent deprecation warnings when using `$.tooltip` in this component.
'DANGEROUS_tooltip_jquery_fn_deprecation_exempt'
);
} else if (this.attrs.tooltipVisible === false) {
$(this.childDomNode).tooltip(
'hide',
// @ts-expect-error We don't want this arg to be part of the public API. It only exists to prevent deprecation warnings when using `$.tooltip` in this component.
'DANGEROUS_tooltip_jquery_fn_deprecation_exempt'
);
}
}
private createTooltip() {
if (this.childDomNode === null) return;
const {
showOnFocus = true,
position = 'top',
delay,
// This will have no effect when switching to CSS tooltips
html = false,
tooltipVisible,
text,
} = this.attrs;
// Fancy "hack" to assemble the trigger string
const trigger = typeof tooltipVisible === 'boolean' ? 'manual' : classList('hover', [showOnFocus && 'focus']);
const realText = this.getRealText();
this.childDomNode.setAttribute('title', realText);
this.childDomNode.setAttribute('aria-label', realText);
// https://getbootstrap.com/docs/3.3/javascript/#tooltips-options
$(this.childDomNode).tooltip(
{
html,
delay,
placement: position,
trigger,
},
// @ts-expect-error We don't want this arg to be part of the public API. It only exists to prevent deprecation warnings when using `$.tooltip` in this component.
'DANGEROUS_tooltip_jquery_fn_deprecation_exempt'
);
}
private getRealText(): string {
const { text } = this.attrs;
return Array.isArray(text) ? extractText(text) : text;
}
/**
* Checks if the tooltip DOM node has changed.
*
* If it has, it updates `this.childDomNode` to the new node, and sets
* `shouldRecreateTooltip` to `true`.
*/
private checkDomNodeChanged() {
const domNode = (this.firstChild as Mithril.VnodeDOM<any, any>).dom as HTMLElement;
if (domNode && !domNode.isSameNode(this.childDomNode)) {
this.childDomNode = domNode;
this.shouldRecreateTooltip = true;
}
}
}