Skip to content

Commit aac07e0

Browse files
Microtask within connectedCallback ensures all property assignments and DOM changes are complete before setup logic runs
1 parent 470f15f commit aac07e0

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

COMPONENT-NAME.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,21 @@ export class ComponentNameElement extends HTMLElement {
2525
}
2626

2727
connectedCallback() {
28-
// Upgrade properties that may have been set before the element was defined
29-
this._upgradeProperty('exampleAttribute');
28+
Promise.resolve().then(() => {
29+
// Upgrade properties that may have been set before the element was defined
30+
this._upgradeProperty('exampleAttribute');
3031

31-
// Check for global attributes before setting defaults
32-
// Don't override author-set attributes
33-
if (!this.hasAttribute('role')) {
34-
// this.setAttribute('role', 'group'); // Example - set appropriate role
35-
}
36-
if (!this.hasAttribute('tabindex')) {
37-
// this.setAttribute('tabindex', 0); // Example - set if focusable
38-
}
32+
// Check for global attributes before setting defaults
33+
// Don't override author-set attributes
34+
if (!this.hasAttribute('role')) {
35+
// this.setAttribute('role', 'group'); // Example - set appropriate role
36+
}
37+
if (!this.hasAttribute('tabindex')) {
38+
// this.setAttribute('tabindex', 0); // Example - set if focusable
39+
}
3940

40-
this.render();
41+
this.render();
42+
});
4143
}
4244

4345
attributeChangedCallback(name, oldValue, newValue) {
@@ -104,7 +106,7 @@ export class ComponentNameElement extends HTMLElement {
104106
:host {
105107
display: block;
106108
}
107-
109+
108110
/* Support the hidden attribute properly */
109111
:host([hidden]) {
110112
display: none;

0 commit comments

Comments
 (0)