Custom element loader
Register custom elements by file name: eager, idle, lazy or visible.
Name a file *.{eager|idle|lazy|visible}.ce.{vue,ts,tsx,js,jsx} and it becomes a custom element. You don't register it or attach a library. The file name picks the tag name and the loading strategy.
templates/demos/loader/load-idle.idle.ce.ts → <demo-load-idle>
templates/components/card/card.visible.ce.vue → <mytheme-card>
The tag name is <prefix>-<file name>. The prefix defaults to the theme name; this theme sets it to demo with the ce.prefix option.
Loading strategies
| Mode | When the element is imported and defined |
|---|---|
eager |
Part of the main bundle, defined immediately. You rarely want this. |
idle |
Dynamic import in requestIdleCallback (at the latest after 1 s). |
lazy |
Dynamic import right after the main bundle ran. |
visible |
Dynamic import once an instance is within 50% of the viewport. Elements that never show up are never loaded. |
Exports
idle, lazy and visible modules register their first export (or the default export of a .vue file). eager modules must have a default export:
import { LoaderDemo } from './_LoaderDemo'
/**
* Loaded with the "eager" strategy. Eager elements need a default export.
*/
export default class LoadEager extends LoaderDemo {
static mode = 'eager'
}
Requirements
- The loader is injected into
js/main.ts(ormain.js), so the theme needs that file. visibleregistersDrupal.behaviors.customElementLoader, so elements in content added by AJAX are observed too.