Skip to content
GitHub

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.
Live preview · Loader strategies Open in new tab ↗

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:

templates/demos/loader/load-eager.eager.ce.ts
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 (or main.js), so the theme needs that file.
  • visible registers Drupal.behaviors.customElementLoader, so elements in content added by AJAX are observed too.