Skip to content

Commit 85c3fe6

Browse files
committed
fix(#450): hx-popover open on render
1 parent fc7ff81 commit 85c3fe6

File tree

4 files changed

+58
-83
lines changed

4 files changed

+58
-83
lines changed

docs/components/popover/test.html

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
{% extends 'test.njk' %}
88

99
{% block content %}
10+
{# TODO: Write tests to set shadow position on open and render. #}
11+
1012
<section>
11-
<h2>Appearance</h2>
13+
<h2 id="appearance">Appearance</h2>
1214
<div class="popover-appearance">
1315
<hx-popover>
1416
<hx-div>
@@ -27,7 +29,7 @@ <h2>Appearance</h2>
2729
</section>
2830

2931
<section>
30-
<h2>Full Markup Appearance</h2>
32+
<h2 id="full-markup-appearance">Full Markup Appearance</h2>
3133
<div class="popover-appearance">
3234
<hx-popover position="right-bottom">
3335
<header>
@@ -158,7 +160,7 @@ <h2>Scrollable Content (configured hx-div)</h2>
158160
</section>
159161

160162
<section>
161-
<h2>Scrolling Content (Body and Foot)</h2>
163+
<h2 id="scrolling-content-body-foot">Scrolling Content (Body and Foot)</h2>
162164
<div class="popover-appearance">
163165
<hx-popover position="left">
164166
<hx-div scroll="vertical">
@@ -196,7 +198,7 @@ <h2>Scrolling Content (Body and Foot)</h2>
196198
</section>
197199

198200
<section>
199-
<h2>Scrolling Content (Body only)</h2>
201+
<h2 id="scrolling-content-body">Scrolling Content (Body only)</h2>
200202
<div class="popover-appearance">
201203
<hx-popover position="left">
202204
<hx-div scroll="vertical">
@@ -368,6 +370,35 @@ <h2>Within Scrollable Container</h2>
368370
</hx-div>
369371
</section>
370372

373+
<section>
374+
<header>
375+
<h2 id="popover-in-table-header">Popover Inside a Table Header</h2>
376+
</header>
377+
<table class="hxTable">
378+
<thead>
379+
<tr>
380+
<th>
381+
Table Header
382+
<hx-disclosure aria-controls="testPopover" class="hxBtn" open>
383+
<hx-icon type="cog"></hx-icon>
384+
</hx-disclosure>
385+
<hx-popover id="testPopover" open position="right">
386+
<header>
387+
Popover Header
388+
</header>
389+
390+
<hx-div>
391+
<p>
392+
This is my popover body.
393+
</p>
394+
</hx-div>
395+
</hx-popover>
396+
</th>
397+
</tr>
398+
</thead>
399+
</table>
400+
</section>
401+
371402
<h2>Deprecated Markup</h2>
372403

373404
<section>
@@ -523,33 +554,4 @@ <h3>Deprecated Scrolling Content</h3>
523554
</hx-popover>
524555
</div>
525556
</section>
526-
527-
<section>
528-
<header>
529-
<h2 id="popover-in-table-header">Popover Inside a Table Header</h2>
530-
</header>
531-
<table class="hxTable">
532-
<thead>
533-
<tr>
534-
<th>
535-
Table Header
536-
<hx-disclosure aria-controls="testPopover" class="hxBtn" open>
537-
<hx-icon type="cog"></hx-icon>
538-
</hx-disclosure>
539-
<hx-popover id="testPopover" open position="right">
540-
<header>
541-
Popover Header
542-
</header>
543-
544-
<hx-div>
545-
<p>
546-
This is my popover body.
547-
</p>
548-
</hx-div>
549-
</hx-popover>
550-
</th>
551-
</tr>
552-
</thead>
553-
</table>
554-
</section>
555557
{% endblock %}

src/helix-ui/elements/HXPopoverElement.js

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,44 +31,16 @@ export class HXPopoverElement extends _ProtoClass {
3131
this.POSITION_OFFSET = 20;
3232
}
3333

34-
/** @override */
35-
$onConnect () {
36-
super.$onConnect();
37-
this.addEventListener('reposition', this._onReposition);
38-
}
39-
40-
$onDisconnect () {
41-
super.$onDisconnect();
42-
this.removeEventListener('reposition', this._onReposition);
43-
}
44-
45-
/** @override */
46-
$onAttributeChange (attr, oldVal, newVal) {
47-
super.$onAttributeChange(attr, oldVal, newVal);
48-
49-
if (attr === 'position') {
50-
this._setShadowPosition(newVal);
51-
}
52-
}
53-
5434
/** @private */
5535
get _elRoot () {
5636
return this.shadowRoot.getElementById('hxPopover');
5737
}
5838

5939
/**
60-
* Update visual display of arrow in Shadow DOM based on optimal position.
61-
* @private
62-
*/
63-
_onReposition () {
64-
this._setShadowPosition(this.optimumPosition);
65-
}
66-
67-
/**
68-
* @private
40+
* @override
6941
* @param {NormalizedPositionString}
7042
*/
71-
_setShadowPosition (position) {
43+
setShadowPosition (position) {
7244
this._elRoot.setAttribute('position', position);
7345
}
7446
}

src/helix-ui/elements/HXTooltipElement.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,13 @@ export class HXTooltipElement extends _ProtoClass {
5353
this.$defaultAttribute('id', `tip-${generateId()}`);
5454
this.$defaultAttribute('role', 'tooltip');
5555

56-
this.addEventListener('reposition', this._onReposition);
5756
this._connectToControl();
5857
}
5958

6059
/** @override */
6160
$onDisconnect () {
6261
super.$onDisconnect();
6362

64-
this.removeEventListener('reposition', this._onReposition);
6563
this._detachListeners();
6664
}
6765

@@ -74,14 +72,8 @@ export class HXTooltipElement extends _ProtoClass {
7472
$onAttributeChange (attr, oldVal, newVal) {
7573
super.$onAttributeChange(attr, oldVal, newVal);
7674

77-
switch (attr) {
78-
case 'for':
79-
this._connectToControl();
80-
break;
81-
82-
case 'position':
83-
this._setShadowPosition(newVal);
84-
break;
75+
if (attr === 'for') {
76+
this._connectToControl();
8577
}
8678
}
8779

@@ -111,6 +103,14 @@ export class HXTooltipElement extends _ProtoClass {
111103
this.setAttribute('for', value);
112104
}
113105

106+
/**
107+
* @override
108+
* @param {NormalizedPositionString}
109+
*/
110+
setShadowPosition (position) {
111+
this._elRoot.setAttribute('position', position);
112+
}
113+
114114
/** @private */
115115
get _elRoot () {
116116
return this.shadowRoot.getElementById('hxTooltip');
@@ -288,16 +288,6 @@ export class HXTooltipElement extends _ProtoClass {
288288
}
289289
}
290290

291-
/** @private */
292-
_onReposition () {
293-
this._setShadowPosition(this.optimumPosition);
294-
}
295-
296-
/** @private */
297-
_setShadowPosition (position) {
298-
this._elRoot.setAttribute('position', position);
299-
}
300-
301291
/**
302292
* Show Tooltip after delay
303293
*

src/helix-ui/mixins/Positionable.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export const Positionable = (superclass) => {
114114
super.$onAttributeChange(attr, oldVal, newVal);
115115

116116
if (attr === 'position') {
117+
this.setShadowPosition(newVal);
117118
this.reposition();
118119
}
119120
}
@@ -212,11 +213,21 @@ export const Positionable = (superclass) => {
212213
this.style.left = `${x}px`;
213214

214215
this._optimumPosition = position;
216+
this.setShadowPosition(position);
215217

216218
this.$emit('reposition');
217219
}
218220
}
219221

222+
/**
223+
* Used to communicate position changes to Shadow DOM for subclasses that care.
224+
*
225+
* @abstract
226+
* @ignore
227+
* @param {NormalizedPositionString}
228+
*/
229+
setShadowPosition (position) {} // eslint-disable-line no-unused-vars
230+
220231
/**
221232
* Add active event listeners (e.g, document `click`)
222233
* These listeners rely on `this.controlElement` to manipulate

0 commit comments

Comments
 (0)