Wikidown

Home / Site Templates / Web Components

Web Components

Use custom elements, such as a photo gallery, in wiki pages. _config.yml loads them on every page of the published site, so you can swap one implementation for another without editing the pages.

Web components are browser-standard custom HTML tags (<wikidown-gallery>, <wa-lightbox>), each defined by a JavaScript module. Wikidown's rules for them:

  1. They run on the exported site only. Pages hold plain HTML that still makes sense without JavaScript: real links, images, alt text. On github.com, in the web editor's preview, and in a PDF, readers see that fallback.
  2. Config loads them, not pages. A page never carries a <script> tag. _config.yml lists the modules and styles, and export-html adds them to every page, whatever its layout.
  3. Tags are contracts. Change which module defines a tag in _config.yml, and every page that uses the tag picks up the new implementation.

Loading components

Add a web_components section under wikidown: in _config.yml:

wikidown:
  web_components:
    modules:
      - /assets/components/wikidown-gallery.js
    styles:
      - /assets/components/wikidown-gallery.css
    tags:
      - wikidown-gallery
Key What export-html does
modules Adds <script type="module" src="…"> at the end of each page's <body>, in this order
styles Adds <link rel="stylesheet" href="…"> at the end of each page's <head>, in this order
tags Lets blocks that start with these tags contain blank lines (see Writing the markup)

Each URL is either:

A module runs on every page of your site with full access to it, so load only code you trust, from a source you trust. Use a local copy if the site must work offline or under a strict content security policy.

Custom elements must be served over HTTP. Opening an exported page straight from disk (file://) won't run module scripts. Use a local server, as in Publishing to GitHub Pages.

Writing the markup

Put the component's HTML straight into the page, as you would an inline SVG:

<wikidown-gallery label="Photos from the opening" columns="3">
  <figure>
    <a href="../.attachments/opening/rabbits-full.jpg" data-width="1600" data-height="1200">
      <img src="../.attachments/opening/rabbits.jpg" alt="Copper rabbit sculptures" width="400" height="300" loading="lazy">
    </a>
    <figcaption>Copper rabbits</figcaption>
  </figure>
</wikidown-gallery>

<wikidown-gallery> comes with the starter theme, so the config above works with no files to copy. For a step-by-step guide, see Photo Galleries. It shows a grid of thumbnails, and clicking one opens a full-screen viewer with previous and next buttons, arrow keys, and Escape to close. With JavaScript off, it is still a grid, and each thumbnail links to its full image.

Markup Meaning
label attribute The gallery's name for screen readers
columns attribute Columns on wide screens (default 3). Narrow screens fit as many as they can
<figure> One photo
<a href> The full-size image
data-width, data-height on the link The full image's size, so the viewer can reserve space for it
<img> The thumbnail. Its alt text describes the photo
<figcaption> Optional caption, also shown in the viewer

To restyle it, copy the theme with wikidown pages --theme and edit assets/components/wikidown-gallery.css. To replace it, point modules at another file that defines wikidown-gallery. The pages stay the same.

Using Web Awesome

Web Awesome is an MIT-licensed component library, published on a CDN. Its loader imports each component the first time a page uses its tag, so a page pulls in only what it uses:

wikidown:
  web_components:
    styles:
      - https://cdn.jsdelivr.net/npm/@awesome.me/webawesome@3.15.0/dist-cdn/styles/webawesome.css
    modules:
      - https://cdn.jsdelivr.net/npm/@awesome.me/webawesome@3.15.0/dist-cdn/webawesome.loader.js
    tags:
      - wa-lightbox

Then use its tags in a page. Its lightbox adds swipe, zoom and a strip of thumbnails:

<wa-lightbox class="wa-grid" label="Photos from the opening" with-thumbnails>
  <wa-lightbox-item src="../.attachments/opening/rabbits-full.jpg" caption="Copper rabbits" width="1600" height="1200">
    <img src="../.attachments/opening/rabbits.jpg" alt="Copper rabbit sculptures">
  </wa-lightbox-item>
</wa-lightbox>

<wa-lightbox> is marked experimental as of 3.15. Check its documentation before you change the pinned version. Its tags are specific to Web Awesome. Use <wikidown-gallery> if you want to be able to change the implementation later without editing pages.

The built-in gallery and Web Awesome can be loaded together. A tag can be defined only once per page, so never load two modules that both define the same tag.