@stackoverflow/stacks 2.5.7 → 2.6.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.
@@ -1,343 +1,366 @@
1
- import * as Stacks from "../../stacks";
2
- import { BasePopoverController, PopoverController } from "./popover";
3
-
4
- export interface TooltipOptions {
5
- placement: string;
6
- }
7
-
8
- export class TooltipController extends BasePopoverController {
9
- static targets = [];
10
-
11
- protected popoverSelectorAttribute = "aria-describedby";
12
-
13
- private boundScheduleShow!: () => void;
14
- private boundHide!: () => void;
15
- private boundHideIfWithin!: () => void;
16
- private boundHideOnEscapeKeyEvent!: () => void;
17
- private boundClearActiveTimeout!: () => void;
18
- private activeTimeout!: number;
19
-
20
- /**
21
- * Binds mouseover and mouseout events in addition to BasePopoverController.connect
22
- */
23
- connect() {
24
- super.connect();
25
-
26
- // Only bind to mouse events if the pointer device supports hover behavior.
27
- // Otherwise we run into issues with mobile browser showing popovers for
28
- // click events and not being able to hide them.
29
- if (window.matchMedia("(hover: hover)").matches) {
30
- this.bindMouseEvents();
31
- }
32
- this.bindKeyboardEvents();
33
- }
34
-
35
- /**
36
- * Unbinds mouse events in addition to BasePopoverController.disconnect
37
- */
38
- disconnect() {
39
- this.unbindKeyboardEvents();
40
- this.unbindMouseEvents();
41
- super.disconnect();
42
- }
43
-
44
- /**
45
- * Attempts to show the tooltip popover so long as no other Stacks-managed popover is
46
- * present on the page.
47
- */
48
- show(dispatcher: Event | Element | null = null) {
49
- // check and see if this controller coexists with a popover
50
- const controller =
51
- Stacks.application.getControllerForElementAndIdentifier(
52
- this.element,
53
- "s-popover"
54
- );
55
-
56
- // if the controller exists and already has a visible popover, don't show the tooltip
57
- if (controller && (<PopoverController>controller).isVisible) {
58
- return;
59
- }
60
-
61
- super.show(dispatcher);
62
- }
63
-
64
- /**
65
- * Sets up a tooltip popover show after a delay.
66
- */
67
- scheduleShow(dispatcher: Event | Element | null = null) {
68
- window.clearTimeout(this.activeTimeout);
69
- this.activeTimeout = window.setTimeout(
70
- () => this.show(dispatcher),
71
- 300
72
- );
73
- }
74
-
75
- /**
76
- * Cancels the scheduled tooltip popover display and hides it if already displayed
77
- */
78
- scheduleHide(dispatcher: Event | Element | null = null) {
79
- window.clearTimeout(this.activeTimeout);
80
- this.activeTimeout = window.setTimeout(
81
- () => super.hide(dispatcher),
82
- 100
83
- );
84
- }
85
-
86
- /**
87
- * Cancels the activeTimeout
88
- */
89
- clearActiveTimeout() {
90
- clearTimeout(this.activeTimeout);
91
- }
92
-
93
- /**
94
- * Applies data-s-tooltip-html-title and title attributes.
95
- */
96
- applyTitleAttributes() {
97
- let content: Node;
98
-
99
- const htmlTitle = this.data.get("html-title");
100
- if (htmlTitle) {
101
- // eslint-disable-next-line no-unsanitized/method
102
- content = document
103
- .createRange()
104
- .createContextualFragment(htmlTitle);
105
- } else {
106
- const plainTitle = this.element.getAttribute("title");
107
- if (plainTitle) {
108
- content = document.createTextNode(plainTitle);
109
- } else {
110
- return null;
111
- }
112
- }
113
-
114
- this.data.delete("html-title");
115
- this.element.removeAttribute("title");
116
-
117
- let popoverId = this.element.getAttribute("aria-describedby");
118
- if (!popoverId) {
119
- popoverId = TooltipController.generateId();
120
- this.element.setAttribute("aria-describedby", popoverId);
121
- }
122
-
123
- let popover = document.getElementById(popoverId);
124
- if (!popover) {
125
- popover = document.createElement("div");
126
- popover.id = popoverId;
127
- popover.className = "s-popover s-popover__tooltip";
128
- popover.setAttribute("role", "tooltip");
129
-
130
- const parentNode = this.element.parentNode;
131
- if (parentNode) {
132
- // insertBefore inserts at end if element.nextSibling is null.
133
- parentNode.insertBefore(popover, this.element.nextSibling);
134
- } else {
135
- document.body.appendChild(popover);
136
- }
137
- }
138
-
139
- const arrow = popover.querySelector(".s-popover--arrow");
140
-
141
- // clear and set the content of the popover
142
- popover.innerHTML = "";
143
- popover.appendChild(content);
144
-
145
- // create the arrow if necessary
146
- if (arrow) {
147
- popover.appendChild(arrow);
148
- } else {
149
- popover.insertAdjacentHTML(
150
- "beforeend",
151
- `<div class="s-popover--arrow"></div>`
152
- );
153
- }
154
-
155
- this.scheduleUpdate();
156
-
157
- return popover;
158
- }
159
-
160
- /**
161
- * Automatically hides the tooltip popover when a Stacks popover is shown anywhere on
162
- * the page.
163
- */
164
- protected bindDocumentEvents() {
165
- this.boundHideIfWithin =
166
- this.boundHideIfWithin || this.hideIfWithin.bind(this);
167
-
168
- document.addEventListener("s-popover:shown", this.boundHideIfWithin);
169
- }
170
-
171
- /**
172
- * Unbinds all mouse events
173
- */
174
- protected unbindDocumentEvents() {
175
- document.removeEventListener("s-popover:shown", this.boundHideIfWithin);
176
- }
177
-
178
- /**
179
- * Attempts to generate a new tooltip popover from the title attribute if no popover
180
- * was present when requested, otherwise throws an error.
181
- */
182
- protected generatePopover(): HTMLElement | null {
183
- return this.applyTitleAttributes();
184
- }
185
-
186
- /**
187
- * Hides the tooltip if is or is within the event's target.
188
- * @param event An event object from s-popover:shown
189
- */
190
- private hideIfWithin(event: Event) {
191
- if ((<Element>event.target).contains(this.referenceElement)) {
192
- this.scheduleHide();
193
- }
194
- }
195
-
196
- private hideOnEscapeKeyEvent(event: KeyboardEvent) {
197
- if (event.key === "Escape") {
198
- this.scheduleHide();
199
- }
200
- }
201
- /**
202
- * Binds mouse events to show/hide on reference element hover
203
- */
204
- private bindKeyboardEvents() {
205
- this.boundScheduleShow =
206
- this.boundScheduleShow || this.scheduleShow.bind(this);
207
- this.boundHide = this.boundHide || this.scheduleHide.bind(this);
208
- this.boundHideOnEscapeKeyEvent =
209
- this.boundHideOnEscapeKeyEvent ||
210
- this.hideOnEscapeKeyEvent.bind(this);
211
-
212
- this.referenceElement.addEventListener("focus", this.boundScheduleShow);
213
- this.referenceElement.addEventListener("blur", this.boundHide);
214
- document.addEventListener("keyup", this.boundHideOnEscapeKeyEvent);
215
- }
216
- /**
217
- * Unbinds all mouse events
218
- */
219
- private unbindKeyboardEvents() {
220
- this.referenceElement.removeEventListener(
221
- "focus",
222
- this.boundScheduleShow
223
- );
224
- this.referenceElement.removeEventListener("blur", this.boundHide);
225
- document.removeEventListener("keyup", this.boundHideOnEscapeKeyEvent);
226
- }
227
-
228
- /**
229
- * Binds mouse events to show/hide on reference element hover
230
- */
231
- private bindMouseEvents() {
232
- this.boundScheduleShow =
233
- this.boundScheduleShow || this.scheduleShow.bind(this);
234
- this.boundHide = this.boundHide || this.scheduleHide.bind(this);
235
- this.boundClearActiveTimeout =
236
- this.boundClearActiveTimeout || this.clearActiveTimeout.bind(this);
237
-
238
- this.referenceElement.addEventListener(
239
- "mouseover",
240
- this.boundScheduleShow
241
- );
242
- this.referenceElement.addEventListener("mouseout", this.boundHide);
243
- this.popoverElement.addEventListener(
244
- "mouseover",
245
- this.boundClearActiveTimeout
246
- );
247
- this.popoverElement.addEventListener("mouseout", this.boundHide);
248
- }
249
-
250
- /**
251
- * Unbinds all mouse events
252
- */
253
- private unbindMouseEvents() {
254
- this.referenceElement.removeEventListener(
255
- "mouseover",
256
- this.boundScheduleShow
257
- );
258
- this.referenceElement.removeEventListener("mouseout", this.boundHide);
259
- this.referenceElement.removeEventListener(
260
- "focus",
261
- this.boundScheduleShow
262
- );
263
- this.referenceElement.removeEventListener("blur", this.boundHide);
264
- this.popoverElement.removeEventListener(
265
- "mouseover",
266
- this.boundClearActiveTimeout
267
- );
268
- this.popoverElement.removeEventListener("mouseout", this.boundHide);
269
- }
270
-
271
- /**
272
- * Generates an ID for tooltips created with setTooltip.
273
- */
274
- private static generateId() {
275
- // generate a random number, then convert to a well formatted string
276
- return (
277
- "--stacks-s-tooltip-" + Math.random().toString(36).substring(2, 10)
278
- );
279
- }
280
- }
281
-
282
- /**
283
- * Adds or updates a Stacks tooltip on a given element, initializing the controller if necessary
284
- * @param element The element to add a tooltip to.
285
- * @param html An HTML string to populate the tooltip with.
286
- * @param options Options for rendering the tooltip.
287
- */
288
- export function setTooltipHtml(
289
- element: Element,
290
- html: string,
291
- options?: TooltipOptions
292
- ) {
293
- element.setAttribute("data-s-tooltip-html-title", html);
294
- element.removeAttribute("title");
295
- applyOptionsAndTitleAttributes(element, options);
296
- }
297
-
298
- /**
299
- * Adds or updates a Stacks tooltip on a given element, initializing the controller if necessary
300
- * @param element The element to add a tooltip to.
301
- * @param text A plain text string to populate the tooltip with.
302
- * @param options Options for rendering the tooltip.
303
- */
304
- export function setTooltipText(
305
- element: Element,
306
- text: string,
307
- options?: TooltipOptions
308
- ) {
309
- element.setAttribute("title", text);
310
- element.removeAttribute("data-s-tooltip-html-title");
311
- applyOptionsAndTitleAttributes(element, options);
312
- }
313
-
314
- /**
315
- * Shared helper for setTooltip* to initialize and set tooltip content
316
- * @param element The element to add a tooltip to.
317
- * @param options Options for rendering the tooltip.
318
- */
319
- function applyOptionsAndTitleAttributes(
320
- element: Element,
321
- options?: TooltipOptions
322
- ) {
323
- if (options && options.placement) {
324
- element.setAttribute("data-s-tooltip-placement", options.placement);
325
- }
326
-
327
- const controller = <TooltipController>(
328
- Stacks.application.getControllerForElementAndIdentifier(
329
- element,
330
- "s-tooltip"
331
- )
332
- );
333
-
334
- if (controller) {
335
- controller.applyTitleAttributes();
336
- } else {
337
- const dataController = element.getAttribute("data-controller");
338
- element.setAttribute(
339
- "data-controller",
340
- `${dataController ? dataController : ""} s-tooltip`
341
- );
342
- }
343
- }
1
+ import * as Stacks from "../../stacks";
2
+ import { BasePopoverController, PopoverController } from "./popover";
3
+
4
+ export interface TooltipOptions {
5
+ placement: string;
6
+ }
7
+
8
+ export class TooltipController extends BasePopoverController {
9
+ static targets = [];
10
+
11
+ protected popoverSelectorAttribute = "aria-describedby";
12
+
13
+ private boundScheduleShow!: () => void;
14
+ private boundHide!: () => void;
15
+ private boundHideIfWithin!: () => void;
16
+ private boundHideOnEscapeKeyEvent!: () => void;
17
+ private boundHideIfNotFocused!: () => void;
18
+ private boundClearActiveTimeout!: () => void;
19
+ private activeTimeout!: number;
20
+
21
+ /**
22
+ * Binds mouseover and mouseout events in addition to BasePopoverController.connect
23
+ */
24
+ connect() {
25
+ super.connect();
26
+
27
+ // Only bind to mouse events if the pointer device supports hover behavior.
28
+ // Otherwise we run into issues with mobile browser showing popovers for
29
+ // click events and not being able to hide them.
30
+ if (window.matchMedia("(hover: hover)").matches) {
31
+ this.bindMouseEvents();
32
+ }
33
+ this.bindKeyboardEvents();
34
+ }
35
+
36
+ /**
37
+ * Unbinds mouse events in addition to BasePopoverController.disconnect
38
+ */
39
+ disconnect() {
40
+ this.unbindKeyboardEvents();
41
+ this.unbindMouseEvents();
42
+ super.disconnect();
43
+ }
44
+
45
+ /**
46
+ * Attempts to show the tooltip popover so long as no other Stacks-managed popover is
47
+ * present on the page.
48
+ */
49
+ show(dispatcher: Event | Element | null = null) {
50
+ // check and see if this controller coexists with a popover
51
+ const controller =
52
+ Stacks.application.getControllerForElementAndIdentifier(
53
+ this.element,
54
+ "s-popover"
55
+ );
56
+
57
+ // if the controller exists and already has a visible popover, don't show the tooltip
58
+ if (controller && (<PopoverController>controller).isVisible) {
59
+ return;
60
+ }
61
+
62
+ super.show(dispatcher);
63
+ }
64
+
65
+ /**
66
+ * Sets up a tooltip popover show after a delay.
67
+ */
68
+ scheduleShow(dispatcher: Event | Element | null = null) {
69
+ window.clearTimeout(this.activeTimeout);
70
+ this.activeTimeout = window.setTimeout(
71
+ () => this.show(dispatcher),
72
+ 300
73
+ );
74
+ }
75
+
76
+ /**
77
+ * Cancels the scheduled tooltip popover display and hides it if already displayed
78
+ */
79
+ scheduleHide(dispatcher: Event | Element | null = null) {
80
+ window.clearTimeout(this.activeTimeout);
81
+ this.activeTimeout = window.setTimeout(
82
+ () => super.hide(dispatcher),
83
+ 100
84
+ );
85
+ }
86
+
87
+ /**
88
+ * Cancels the activeTimeout
89
+ */
90
+ clearActiveTimeout() {
91
+ clearTimeout(this.activeTimeout);
92
+ }
93
+
94
+ /**
95
+ * Applies data-s-tooltip-html-title and title attributes.
96
+ */
97
+ applyTitleAttributes() {
98
+ let content: Node;
99
+
100
+ const htmlTitle = this.data.get("html-title");
101
+ if (htmlTitle) {
102
+ // eslint-disable-next-line no-unsanitized/method
103
+ content = document
104
+ .createRange()
105
+ .createContextualFragment(htmlTitle);
106
+ } else {
107
+ const plainTitle = this.element.getAttribute("title");
108
+ if (plainTitle) {
109
+ content = document.createTextNode(plainTitle);
110
+ } else {
111
+ return null;
112
+ }
113
+ }
114
+
115
+ this.data.delete("html-title");
116
+ this.element.removeAttribute("title");
117
+
118
+ let popoverId = this.element.getAttribute("aria-describedby");
119
+ if (!popoverId) {
120
+ popoverId = TooltipController.generateId();
121
+ this.element.setAttribute("aria-describedby", popoverId);
122
+ }
123
+
124
+ let popover = document.getElementById(popoverId);
125
+ if (!popover) {
126
+ popover = document.createElement("div");
127
+ popover.id = popoverId;
128
+ popover.className = "s-popover s-popover__tooltip";
129
+ popover.setAttribute("role", "tooltip");
130
+
131
+ const parentNode = this.element.parentNode;
132
+ if (parentNode) {
133
+ // insertBefore inserts at end if element.nextSibling is null.
134
+ parentNode.insertBefore(popover, this.element.nextSibling);
135
+ } else {
136
+ document.body.appendChild(popover);
137
+ }
138
+ }
139
+
140
+ const arrow = popover.querySelector(".s-popover--arrow");
141
+
142
+ // clear and set the content of the popover
143
+ popover.innerHTML = "";
144
+ popover.appendChild(content);
145
+
146
+ // create the arrow if necessary
147
+ if (arrow) {
148
+ popover.appendChild(arrow);
149
+ } else {
150
+ popover.insertAdjacentHTML(
151
+ "beforeend",
152
+ `<div class="s-popover--arrow"></div>`
153
+ );
154
+ }
155
+
156
+ this.scheduleUpdate();
157
+
158
+ return popover;
159
+ }
160
+
161
+ /**
162
+ * Automatically hides the tooltip popover when a Stacks popover is shown anywhere on
163
+ * the page.
164
+ */
165
+ protected bindDocumentEvents() {
166
+ this.boundHideIfWithin =
167
+ this.boundHideIfWithin || this.hideIfWithin.bind(this);
168
+
169
+ document.addEventListener("s-popover:shown", this.boundHideIfWithin);
170
+ }
171
+
172
+ /**
173
+ * Unbinds all mouse events
174
+ */
175
+ protected unbindDocumentEvents() {
176
+ document.removeEventListener("s-popover:shown", this.boundHideIfWithin);
177
+ }
178
+
179
+ /**
180
+ * Attempts to generate a new tooltip popover from the title attribute if no popover
181
+ * was present when requested, otherwise throws an error.
182
+ */
183
+ protected generatePopover(): HTMLElement | null {
184
+ return this.applyTitleAttributes();
185
+ }
186
+
187
+ /**
188
+ * Hides the tooltip if is or is within the event's target.
189
+ * @param event An event object from s-popover:shown
190
+ */
191
+ private hideIfWithin(event: Event) {
192
+ if ((<Element>event.target).contains(this.referenceElement)) {
193
+ this.scheduleHide();
194
+ }
195
+ }
196
+
197
+ /**
198
+ * Hides the tooltip if the focus is not on the popover element.
199
+ * @param event A focusout event object from the reference or popover element
200
+ */
201
+ private hideIfNotFocused(event: FocusEvent) {
202
+ if (
203
+ !this.referenceElement.contains(event.relatedTarget as Node) &&
204
+ !this.popoverElement.contains(event.relatedTarget as Node)
205
+ ) {
206
+ this.scheduleHide();
207
+ }
208
+ }
209
+
210
+ private hideOnEscapeKeyEvent(event: KeyboardEvent) {
211
+ if (event.key === "Escape") {
212
+ this.scheduleHide();
213
+ }
214
+ }
215
+ /**
216
+ * Binds mouse events to show/hide on reference element hover
217
+ */
218
+ private bindKeyboardEvents() {
219
+ this.boundScheduleShow =
220
+ this.boundScheduleShow || this.scheduleShow.bind(this);
221
+ this.boundHideOnEscapeKeyEvent =
222
+ this.boundHideOnEscapeKeyEvent ||
223
+ this.hideOnEscapeKeyEvent.bind(this);
224
+ this.boundHideIfNotFocused =
225
+ this.boundHideIfNotFocused || this.hideIfNotFocused.bind(this);
226
+ this.referenceElement.addEventListener("focus", this.boundScheduleShow);
227
+ this.referenceElement.addEventListener(
228
+ "focusout",
229
+ this.boundHideIfNotFocused.bind(this)
230
+ );
231
+ this.popoverElement.addEventListener(
232
+ "focusout",
233
+ this.boundHideIfNotFocused.bind(this)
234
+ );
235
+ document.addEventListener("keyup", this.boundHideOnEscapeKeyEvent);
236
+ }
237
+
238
+ /**
239
+ * Unbinds all mouse events
240
+ */
241
+ private unbindKeyboardEvents() {
242
+ this.referenceElement.removeEventListener(
243
+ "focus",
244
+ this.boundScheduleShow
245
+ );
246
+ this.referenceElement.removeEventListener(
247
+ "focusout",
248
+ this.boundHideIfNotFocused
249
+ );
250
+ this.referenceElement.removeEventListener(
251
+ "focusout",
252
+ this.boundHideIfNotFocused
253
+ );
254
+ document.removeEventListener("keyup", this.boundHideOnEscapeKeyEvent);
255
+ }
256
+
257
+ /**
258
+ * Binds mouse events to show/hide on reference element hover
259
+ */
260
+ private bindMouseEvents() {
261
+ this.boundScheduleShow =
262
+ this.boundScheduleShow || this.scheduleShow.bind(this);
263
+ this.boundHide = this.boundHide || this.scheduleHide.bind(this);
264
+ this.boundClearActiveTimeout =
265
+ this.boundClearActiveTimeout || this.clearActiveTimeout.bind(this);
266
+ this.referenceElement.addEventListener(
267
+ "mouseover",
268
+ this.boundScheduleShow
269
+ );
270
+ this.referenceElement.addEventListener("mouseout", this.boundHide);
271
+ this.popoverElement.addEventListener(
272
+ "mouseover",
273
+ this.boundClearActiveTimeout
274
+ );
275
+ this.popoverElement.addEventListener("mouseout", this.boundHide);
276
+ }
277
+
278
+ /**
279
+ * Unbinds all mouse events
280
+ */
281
+ private unbindMouseEvents() {
282
+ this.referenceElement.removeEventListener(
283
+ "mouseover",
284
+ this.boundScheduleShow
285
+ );
286
+ this.referenceElement.removeEventListener("mouseout", this.boundHide);
287
+ this.popoverElement.removeEventListener(
288
+ "mouseover",
289
+ this.boundClearActiveTimeout
290
+ );
291
+ this.popoverElement.removeEventListener("mouseout", this.boundHide);
292
+ }
293
+
294
+ /**
295
+ * Generates an ID for tooltips created with setTooltip.
296
+ */
297
+ private static generateId() {
298
+ // generate a random number, then convert to a well formatted string
299
+ return (
300
+ "--stacks-s-tooltip-" + Math.random().toString(36).substring(2, 10)
301
+ );
302
+ }
303
+ }
304
+
305
+ /**
306
+ * Adds or updates a Stacks tooltip on a given element, initializing the controller if necessary
307
+ * @param element The element to add a tooltip to.
308
+ * @param html An HTML string to populate the tooltip with.
309
+ * @param options Options for rendering the tooltip.
310
+ */
311
+ export function setTooltipHtml(
312
+ element: Element,
313
+ html: string,
314
+ options?: TooltipOptions
315
+ ) {
316
+ element.setAttribute("data-s-tooltip-html-title", html);
317
+ element.removeAttribute("title");
318
+ applyOptionsAndTitleAttributes(element, options);
319
+ }
320
+
321
+ /**
322
+ * Adds or updates a Stacks tooltip on a given element, initializing the controller if necessary
323
+ * @param element The element to add a tooltip to.
324
+ * @param text A plain text string to populate the tooltip with.
325
+ * @param options Options for rendering the tooltip.
326
+ */
327
+ export function setTooltipText(
328
+ element: Element,
329
+ text: string,
330
+ options?: TooltipOptions
331
+ ) {
332
+ element.setAttribute("title", text);
333
+ element.removeAttribute("data-s-tooltip-html-title");
334
+ applyOptionsAndTitleAttributes(element, options);
335
+ }
336
+
337
+ /**
338
+ * Shared helper for setTooltip* to initialize and set tooltip content
339
+ * @param element The element to add a tooltip to.
340
+ * @param options Options for rendering the tooltip.
341
+ */
342
+ function applyOptionsAndTitleAttributes(
343
+ element: Element,
344
+ options?: TooltipOptions
345
+ ) {
346
+ if (options && options.placement) {
347
+ element.setAttribute("data-s-tooltip-placement", options.placement);
348
+ }
349
+
350
+ const controller = <TooltipController>(
351
+ Stacks.application.getControllerForElementAndIdentifier(
352
+ element,
353
+ "s-tooltip"
354
+ )
355
+ );
356
+
357
+ if (controller) {
358
+ controller.applyTitleAttributes();
359
+ } else {
360
+ const dataController = element.getAttribute("data-controller");
361
+ element.setAttribute(
362
+ "data-controller",
363
+ `${dataController ? dataController : ""} s-tooltip`
364
+ );
365
+ }
366
+ }