Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(shadow-wrapper): wait for shadowStyles load before calling shadowReadyCallback #611

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion blocks/ShadowWrapper/ShadowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,33 @@ export function shadowed(Base) {

shadowReadyCallback() {}

/**
* @private
*
* This is super tricky workaround to wait for shadow styles to load. When `shadowStyles` is defined, symbiote will
* wait for it to load before rendering the component. So the `render` method becomes async. We need to call
* `shadowReadyCallback` right after the rendering is done. But we can't just call it after `render` because it's
* async. So we need to wait for the shadow styles to load and then call `shadowReadyCallback`.
*/
async _waitForShadowStylesLoad() {
if (!this.shadowRoot) {
return;
}
// @ts-expect-error
const shadowStylesUrl = this.constructor['__shadowStylesUrl'];
/** @type {HTMLLinkElement | null} */
const link = this.shadowRoot.querySelector(`link[href="${shadowStylesUrl}"]`);

if (!link) {
return;
}

await new Promise((resolve, reject) => {
link.addEventListener('load', resolve);
link.addEventListener('error', reject);
});
}

initCallback() {
super.initCallback();
this.setAttribute('hidden', '');
Expand All @@ -41,8 +68,9 @@ export function shadowed(Base) {
link.onload = () => {
// CSS modules can be not loaded at this moment
// TODO: investigate better solution
window.requestAnimationFrame(() => {
window.requestAnimationFrame(async () => {
this.render();
await this._waitForShadowStylesLoad();
window.setTimeout(() => {
this.removeAttribute('hidden');
this.shadowReadyCallback();
Expand Down
Loading