-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathtooltip-controller.js
More file actions
191 lines (165 loc) · 4.45 KB
/
tooltip-controller.js
File metadata and controls
191 lines (165 loc) · 4.45 KB
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
/**
* @license
* Copyright (c) 2022 - 2026 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import { SlotController } from './slot-controller.js';
/**
* A controller that manages the slotted tooltip element.
*/
export class TooltipController extends SlotController {
constructor(host) {
// Do not provide slot factory to create tooltip lazily.
super(host, 'tooltip');
this.setTarget(host);
this.__onContentChange = this.__onContentChange.bind(this);
}
/**
* Override to initialize the newly added custom tooltip.
*
* @param {Node} tooltipNode
* @protected
* @override
*/
initCustomNode(tooltipNode) {
tooltipNode.target = this.target;
if (this.ariaTarget !== undefined) {
tooltipNode.ariaTarget = this.ariaTarget;
}
if (this.context !== undefined) {
tooltipNode.context = this.context;
}
if (this.manual !== undefined) {
tooltipNode.manual = this.manual;
}
if (this.position !== undefined) {
tooltipNode._position = this.position;
}
if (this.shouldShow !== undefined) {
tooltipNode.shouldShow = this.shouldShow;
}
if (!this.manual) {
this.host.setAttribute('has-tooltip', '');
}
this.__notifyChange(tooltipNode);
tooltipNode.addEventListener('content-changed', this.__onContentChange);
}
/**
* Override to notify the host when the tooltip is removed.
*
* @param {Node} tooltipNode
* @protected
* @override
*/
teardownNode(tooltipNode) {
if (!this.manual) {
this.host.removeAttribute('has-tooltip');
}
tooltipNode.removeEventListener('content-changed', this.__onContentChange);
this.__notifyChange(null);
}
/**
* Set an HTML element for linking with the tooltip overlay
* via `aria-describedby` attribute used by screen readers.
* @param {HTMLElement} ariaTarget
*/
setAriaTarget(ariaTarget) {
this.ariaTarget = ariaTarget;
const tooltipNode = this.node;
if (tooltipNode) {
tooltipNode.ariaTarget = ariaTarget;
}
}
/**
* Set a context object to be used by generator.
* @param {object} context
*/
setContext(context) {
this.context = context;
const tooltipNode = this.node;
if (tooltipNode) {
tooltipNode.context = context;
}
}
/**
* Toggle manual state on the slotted tooltip.
* @param {boolean} manual
*/
setManual(manual) {
this.manual = manual;
const tooltipNode = this.node;
if (tooltipNode) {
tooltipNode.manual = manual;
}
}
/**
* Set default position for the slotted tooltip.
* This can be overridden by setting the position
* using corresponding property or attribute.
* @param {string} position
*/
setPosition(position) {
this.position = position;
const tooltipNode = this.node;
if (tooltipNode) {
tooltipNode._position = position;
}
}
/**
* Set function used to detect whether to show
* the tooltip based on a condition.
* @param {Function} shouldShow
*/
setShouldShow(shouldShow) {
this.shouldShow = shouldShow;
const tooltipNode = this.node;
if (tooltipNode) {
tooltipNode.shouldShow = shouldShow;
}
}
/**
* Set an HTML element to attach the tooltip to.
* @param {HTMLElement} target
*/
setTarget(target) {
this.target = target;
const tooltipNode = this.node;
if (tooltipNode) {
tooltipNode.target = target;
}
}
/**
* Schedule opening the slotted tooltip. Respects the tooltip's
* configured `hoverDelay` / `focusDelay` and the shared warm-up state.
* No-op when no tooltip is slotted.
*
* @param {{ hover?: boolean, focus?: boolean, immediate?: boolean }} [options]
*/
open(options) {
const tooltipNode = this.node;
if (tooltipNode && tooltipNode.isConnected) {
tooltipNode._stateController.open(options);
}
}
/**
* Schedule closing the slotted tooltip. Respects the tooltip's
* configured `hideDelay` unless `immediate` is true.
* No-op when no tooltip is slotted.
*
* @param {boolean} [immediate]
*/
close(immediate) {
const tooltipNode = this.node;
if (tooltipNode) {
tooltipNode._stateController.close(immediate);
}
}
/** @private */
__onContentChange(event) {
this.__notifyChange(event.target);
}
/** @private */
__notifyChange(node) {
this.dispatchEvent(new CustomEvent('tooltip-changed', { detail: { node } }));
}
}