componentsReady function which returns a promise that resolves as soon
as all currently used components are loaded and ready to use.componentsReady function is provided as part of the following components packages:@porsche-design-system/components-js@porsche-design-system/components-angular@porsche-design-system/components-react@porsche-design-system/components-vueAttention
whenDefined function from the
componentsReady is quite flexible. You can call it wherever and as often as you like. By default, it uses the current
document's body element to look for any web component of the Porsche Design System.import { componentsReady } from '@porsche-design-system/components-{js|angular|react|vue}';
const doSomeStuff = async () => {
// doing some changes to the DOM and add new Porsche Design System components to it
await componentsReady();
// some code that relies on the newly added components
};
componentsReady you can pass any HTMLElement as a parameter. This is
useful when you want to show a loading indicator for only a part of your application's interface, e.g. the sidebar and
only care about components inside.readyStatereadyState is set to complete, but you
can adjust it to other states like interactive or loading based on your requirements.import { componentsReady } from '@porsche-design-system/components-{js|angular|react|vue}';
const initSomeSidebar = async () => {
const sidebarEl = document.querySelector('.sidebar');
let showSpinner = true;
// wait until all Porsche Design System components used within sidebar are ready
await componentsReady(sidebarEl, 'interactive');
showSpinner = false;
};
jest, jsdom with @testing-libraryjasmine, karma with TestBedjest, jsdom with @testing-library/reactImportant note
@porsche-design-system/components-{js|angular|react|vue}/jsdom-polyfill subpackage in order to have real working Porsche Design System components.jest, jsdom with TestBedjest, jsdom with @testing-library/angularcomponentOnReadycomponentsReadycomponentOnReady, resolving when all currently used PDS components are fully loaded and operational.whenDefinedwhenDefined function to track and wait for the definition of all custom
elements within the document's body. The function returns the number of elements that were initially undefined but have
now been defined.const whenDefined = async (el: HTMLElement = document.body): Promise<number> => {
// select all elements that are not yet defined as custom elements.
const undefinedElements = el.querySelectorAll(':not(:defined)');
// create a list of promises that resolve when each undefined element is defined.
const promises = Array.from(undefinedElements).map((el) => customElements.whenDefined(el.localName));
try {
// wait for all elements to be defined.
await Promise.all(promises);
// return the number of elements that were undefined but are now defined.
return promises.length;
} catch (err) {
console.error('[CustomElementRegistry: whenDefined()]', err); // eslint-disable-line no-console
// return 0 if an error occurs.
return 0;
}
};
const doSomeStuff = async () => {
// modify the DOM and add new custom elements to it.
await whenDefined();
// execute code that depends on the newly added custom elements being registered and available in the browser.
};
