Skip to content
GitHub

Vanilla, Lit, Vue & TSX

Every flavour of custom element the loader understands.

The loader doesn't care how an element is written. Plain classes, Lit, Vue single file components and TSX all work side by side.

  • 🎉
  • 🎉
Live preview · Four flavours, one loader Open in new tab ↗

Vanilla

hello-vanilla.idle.ce.ts
/**
 * A vanilla custom element.
 *
 * @element demo-hello-vanilla
 *
 * @attr {string} name - Who to greet.
 */
export class HelloVanilla extends HTMLElement {
  connectedCallback() {
    this.innerHTML = `<p>Hello, <strong>${this.getAttribute('name') ?? 'Somebody'}</strong>! (vanilla)</p>`
  }
}

Lit

hello-lit.idle.ce.ts
import { css, html, LitElement } from 'lit'

/**
 * A Lit element.
 *
 * @element demo-hello-lit
 *
 * @attr {string} name - Who to greet.
 * @slot - Appended after the greeting.
 */
export class HelloLit extends LitElement {
  static properties = {
    name: { type: String },
  }

  static styles = css`p { color: #2563eb; margin: 0 }`

  declare name: string

  constructor() {
    super()
    this.name = 'Somebody'
  }

  render() {
    return html`<p>Hello, <strong>${this.name}</strong>! (lit) <slot></slot></p>`
  }
}

Vue

.ce.vue files are wrapped with the plugin's own defineCustomElement. Props become attributes, <slot> works as usual. See Vue custom elements for styling.

hello-vue.idle.ce.vue
<docs lang="ts">
/**
 * A Vue single file component as custom element.
 *
 * @element demo-hello-vue
 *
 * @attr {string} name - Who to greet.
 *
 * @slot - Appended after the greeting.
 */
</docs>

<script setup lang="ts">
const props = defineProps<{
  name?: string
}>()
</script>

<template>
  <p>Hello, <strong>{{ props.name ?? 'Somebody' }}</strong>! (vue) <slot /></p>
</template>

<style>
p {
  color: #16a34a;
  margin: 0;
}
</style>

TSX

JSX in .ce.tsx files renders with Preact. The plugin sets the JSX import source for the build and in the generated .uebertool/tsconfig.json.

hello-tsx.idle.ce.tsx
import { render } from 'preact'

/**
 * A custom element that renders JSX with Preact.
 *
 * @element demo-hello-tsx
 *
 * @attr {string} name - Who to greet.
 */
export class HelloTsx extends HTMLElement {
  connectedCallback() {
    const name = this.getAttribute('name') ?? 'Somebody'

    render(
      <p>
        Hello,
        {' '}
        <strong>{name}</strong>
        ! (tsx)
      </p>,
      this,
    )
  }

  disconnectedCallback() {
    render(null, this)
  }
}

Use in Twig

templates/demos/custom-element-types.html.twig
<ul class="grid gap-4">
  <li><demo-hello-vanilla name="Drupal"></demo-hello-vanilla></li>
  <li><demo-hello-lit name="Drupal">&#127881;</demo-hello-lit></li>
  <li><demo-hello-vue name="Drupal">&#127881;</demo-hello-vue></li>
  <li><demo-hello-tsx name="Drupal"></demo-hello-tsx></li>
</ul>