Skip to content

Commit 82be823

Browse files
authored
Merge pull request #424 from rackerlabs/debug-tooltip-compat-audit
Debug tooltip compat audit
2 parents 70e8821 + 778cd39 commit 82be823

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

docs/components/tooltips/test.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ <h2>Unconventional HTML5-compatible IDs</h2>
4747
<div class="hxCol">
4848
<h3>ID on Target</h3>
4949
<p>
50-
<span id="000c075715ed46eb61242f85e80aaaa1">Hover Here</span>
51-
<hx-tooltip for="000c075715ed46eb61242f85e80aaaa1">
50+
<span id="a000c075715ed46eb61242f85e80aaaa1">Hover Here</span>
51+
<hx-tooltip for="a000c075715ed46eb61242f85e80aaaa1">
5252
Do you like my hat?
5353
</hx-tooltip>
5454
</p>
@@ -58,7 +58,7 @@ <h3>ID on Tooltip</h3>
5858
<p>
5959
<span id="html-tip-2">Hover Here</span>
6060
<hx-tooltip
61-
id="100d90208e58f24216be64de517bbbb2"
61+
id="b100d90208e58f24216be64de517bbbb2"
6262
for="html-tip-2"
6363
>
6464
Do you like my hat?
@@ -68,10 +68,10 @@ <h3>ID on Tooltip</h3>
6868
<div class="hxCol">
6969
<h3>ID on Both Tooltip and Target</h3>
7070
<p>
71-
<span id="200c075715ed46eb61242f85e80cccc3">Hover Here</span>
71+
<span id="c200c075715ed46eb61242f85e80cccc3">Hover Here</span>
7272
<hx-tooltip
73-
for="200c075715ed46eb61242f85e80cccc3"
74-
id="200d90208e58f24216be64de517dddd3"
73+
for="c200c075715ed46eb61242f85e80cccc3"
74+
id="c200d90208e58f24216be64de517dddd3"
7575
>
7676
Do you like my hat?
7777
</hx-tooltip>

src/helix-ui/mixins/Positionable.js

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const Positionable = (superclass) => {
3939
this.addEventListener('close', this.__onClose);
4040

4141
if (this.open) {
42-
this.__addOpenListeners();
42+
this.$emit('open');
4343
}
4444
}
4545

@@ -100,8 +100,12 @@ export const Positionable = (superclass) => {
100100
* @type {HTMLElement}
101101
*/
102102
get relativeElement () {
103+
if (!this.isConnected) {
104+
return;
105+
}
106+
103107
if (this.relativeTo) {
104-
return this.getRootNode().getElementById(this.relativeTo);
108+
return this.getRootNode().querySelector(`#${this.relativeTo}`);
105109
} else {
106110
return this.controlElement;
107111
}
@@ -119,28 +123,14 @@ export const Positionable = (superclass) => {
119123
this.setAttribute('relative-to', value);
120124
}
121125

122-
/**
123-
* Calculate fixed {x,y} coordinates relative to another HTML element.
124-
*
125-
* @returns {Object} Coordinate object with `x` and `y` properties.
126-
*/
127-
getCoordinates () {
128-
let posRect = this.getBoundingClientRect();
129-
let relRect = this.relativeElement.getBoundingClientRect();
130-
let calculate = offsetFunctionMap[this.position];
131-
let opts = this.__getDeltas();
132-
133-
return calculate(posRect, relRect, opts);
134-
}
135-
136126
/**
137127
* Calculate and apply new (x,y) coordinates.
138128
*
139129
* Requires the element to be open with a `relativeElement`.
140130
*/
141131
reposition () {
142132
if (this.open && this.relativeElement) {
143-
let { x, y } = this.getCoordinates();
133+
let { x, y } = this.__getCoordinates();
144134

145135
this.style.top = `${y}px`;
146136
this.style.left = `${x}px`;
@@ -153,11 +143,33 @@ export const Positionable = (superclass) => {
153143
* Add event listeners that only apply when open.
154144
*/
155145
__addOpenListeners () {
146+
if (!this.controlElement) {
147+
return;
148+
}
149+
156150
document.addEventListener('click', this.__onDocumentClick);
157151
document.addEventListener('scroll', this.__onDocumentScroll, { passive: true });
158152
window.addEventListener('resize', this.__onWindowResize, { passive: true });
159153
}
160154

155+
/**
156+
* Calculate fixed {x,y} coordinates relative to another HTML element.
157+
*
158+
* @returns {Object} Coordinate object with `x` and `y` properties.
159+
*/
160+
__getCoordinates () {
161+
if (!this.relativeElement) {
162+
return { x: 0, y: 0 };
163+
}
164+
165+
let posRect = this.getBoundingClientRect();
166+
let relRect = this.relativeElement.getBoundingClientRect();
167+
let calculate = offsetFunctionMap[this.position];
168+
let opts = this.__getDeltas();
169+
170+
return calculate(posRect, relRect, opts);
171+
}
172+
161173
/**
162174
* Calculate X and Y adjustments based on configuration via the following attributes.
163175
* * `data-margin`
@@ -220,6 +232,10 @@ export const Positionable = (superclass) => {
220232
* @param {Event} evt
221233
*/
222234
__onDocumentClick (evt) {
235+
if (!this.controlElement) {
236+
return;
237+
}
238+
223239
let inComponent = this.contains(evt.target);
224240
let inControl = this.controlElement.contains(evt.target);
225241
let isBackground = (!inComponent && !inControl);
@@ -240,9 +256,7 @@ export const Positionable = (superclass) => {
240256
* Positionable 'open' event listener.
241257
*/
242258
__onOpen () {
243-
if (this.relativeElement) {
244-
this.__addOpenListeners();
245-
}
259+
this.__addOpenListeners();
246260
this.reposition();
247261
}
248262

0 commit comments

Comments
 (0)