Drupal is a registered trademark of Dries Buytaert
Release: Drupal 10.3.6 Update released for Drupal core (10.3.6)! Release: Drupal 11.0.5 Update released for Drupal core (11.0.5)! Release: Drupal 10.2.9 Update released for Drupal core (10.2.9)! Release: Drupal 10.2.10 Update released for Drupal core (10.2.10)! Release: Drupal 10.3.7 Update released for Drupal core (10.3.7)! Release: Drupal 11.0.6 Update released for Drupal core (11.0.6)! Release: Drupal 10.3.8 Update released for Drupal core (10.3.8)! Release: Drupal 11.0.7 Update released for Drupal core (11.0.7)! Module Revived: Translation Management Tool 8.x-1.19 Module tmgmt updated after 7 months of inactivity (8.x-1.19). Usage Milestone: Account field split Module account_field_split crossed 10,000 active installs.

Islands

No security coverage
View on drupal.org

Introduction

The Islands module provides an implementation of Islands Architecture for Drupal. Islands Architecture was popularized by meta-frameworks Astro and 11ty.

Using the provided drupal-island element in twig templates, developers will be able to:

  • Load JavaScript modules based on various conditions, including visibility, an idle callback, user interaction, and more.
  • Hydrate client-side components using a variety of frameworks (Preact, Vue, Svelte, Web Components), with or without bundling.
  • Provide server rendered fallback content prior to hydrating an island as interactive.
  • Easily reference JavaScript modules using import maps.

This module is currently experimental and there will almost certainly be breaking changes to the API in an upcoming release. For example, when Drupal supports import maps in core this module will adopt the core approach to import maps.

Requirements

The drupal-island element currently extends @11ty/is-land, although this may change in a future version of this module.

Installation

Install as you would normally install a contributed Drupal module. See: https://www.drupal.org/node/895232 for further information.

Usage

Enabling the submodule Islands Examples will provide examples of how to use the drupal-island element in twig templates. Examples can be seen at the path /islands-examples

The drupal-island element currently matches the API of @11ty/is-land, which it extends. The following is a brief overview of some options:

Directives

The on directive is used to specify when an island should be hydrated. The following are some examples of how to use the on directive with a custom element:

on:visible

<drupal-island on:visible>
  <example-component>This component will load when visible</example-component>
</drupal-island>

on:interaction

<drupal-island on:interaction>
  This component will only load on interaction, but replaces this pre-js markup.
  <template data-island="replace">
    <example-component>This is an example component</example-component>
  </template>
</drupal-island>

Framework Components

Islands can also load framework components. Files in the template directory of a module or theme ending with .islands.js will be included in an import map. You can import the related module by referencing a key with the format module_or_theme_name/filename.

Twig template:

<drupal-island on:visible
  autoinit="preact"
  import="islands_examples/preact-component.island.js"
  >
</drupal-island>

preact-component.island.js:

import { html, render } from "https://unpkg.com/htm/preact/index.mjs?module";
import { useState } from "https://unpkg.com/preact@latest/hooks/dist/hooks.module.js?module";

function App() {
  const [count, setCount] = useState(0);

  const increment = () => {
    setCount((prevState) => prevState + 1);
  };
  const decrement = () => {
    setCount((prevState) => prevState - 1);
  };

  return html`
    <span>Preact Counter: </span>
    <button onClick=${increment}>⬆️</button>
    <span> ${count} </span>
    <button onClick=${decrement}>⬇️</button>
  `;
}

export default function (el) {
  render(html`<${App} />`, el);
}

Or components using frameworks like Vue can optionally be defined directly in your twig template.

<drupal-island
  on:visible autoinit="petite-vue"
  import="https://unpkg.com/[email protected]/dist/petite-vue.es.js"
  v-scope="{ count: 0 }">
	Petite Vue
	<template data-island>
		<button @click="count++">⬆️</button>
		<button @click="count--">⬇️</button>
		<span v-html="count">0</span>
	</template>
</drupal-island>

Maintainers

Current maintainers:

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
1
Tracked since
Nov 2024
Latest release
1 year ago
Releases (12 mo)
0 ▼ from 1
Maintenance
Dormant

Releases

Version Type Core Release date
0.x-dev Dev Nov 15, 2024