Skip to content

Instantly share code, notes, and snippets.

@arcdev1
Last active June 24, 2021 17:38
Show Gist options
  • Save arcdev1/824f164dde5127ce12b98ca8ab70a14a to your computer and use it in GitHub Desktop.
Save arcdev1/824f164dde5127ce12b98ca8ab70a14a to your computer and use it in GitHub Desktop.
Register WET Component
/**
* Lets WET-BOEW know about a new component that was added to the page so that
* WET-BOEW can apply the WET-BOEW JavaScript to it.
*
* @param {string} selector an HTML selector, usually the class
* name prefixed with a period.
*/
async function registerWbComponent(selector: string): Promise<void> {
if (typeof window !== "undefined") {
const wet: WetBoew = (window as any).wb as WetBoew;
const selectors = wet?.selectors ?? [];
if (wet && !selectors.includes(selector)) {
// tell WET there's a new element on the page.
selectors.push(selector);
// let WET do its magic.
wet.start();
}
}
// BUG: WET's imperative DOM manipulation interferes with
// HMR (hot module replacement) during development. Sometimes we get
// multiple component elements rendering. This is only affecting HMR so
// it shouldn't be an issue when deployed.
}
interface WetBoew {
selectors: string[];
start: () => void;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment