{shadow}

The shadow root of a custom element, rendered on the server as declarative shadow DOM.

Layouts and components Tag pairs

Syntax

{shadow} ... {/shadow}
{shadow css='file.css, www:other.css', mode='closed', focus} ... {/shadow}

Options

css
Stylesheets inlined as one <style> at the start of the root, separated by commas: a .css file of the application, or www: one of its www/ directory.
mode
open (the default) or closed.
focus
Bare option: adds shadowrootdelegatesfocus.

Description

{shadow} writes <template shadowrootmode="open"> around its rendered content. Placed inside a custom element, it is that element's shadow root: the browser builds the root while it parses the page, so the element shows - with encapsulated styles and slotted light DOM - before any script has run, and a module that defines the element later finds the root ready in this.shadowRoot. Like {tidy}, the tag runs twice: the content renders first, then the template tags go around it.

A shadow root takes no styles from the page, and a <style> written into the template would have its braces read as PAD tags. css= therefore inlines stylesheet files instead, as one <style> at the start of the root: a .css file of the application, or with www: one in its www/ directory - the file rule of {source}. Each file is read once a request. mode='closed' makes a closed root, focus adds shadowrootdelegatesfocus.

The usual home of {shadow} is a custom tag in _tags/ that writes a custom element, combined with {parms}; the first example uses _tags/shadowCard.pad with its stylesheet _tags/shadowCard.css. Inside the pair, {#name} is {shadow}'s own parameter - in a custom tag, put the tag's parameters to use outside the pair, or carry one in with {set $value = {#value}} before it.

A stylesheet that is not there or is no .css file, a mode that is neither open nor closed, and a pair that never closes are errors.

Examples

A card with a shadow root

A custom tag writes a custom element with its shadow root, the stylesheet inlined, the title slotted.

Templateexamples/shadow/1-card.pad
{shadowCard title='Revenue'}
  <p>€ 12,400 this month.</p>
{/shadowCard}
Resultopen alone
Revenue

€ 12,400 this month.

A closed root that delegates focus

The pair straight in a page - mode='closed' and focus, the light DOM slotted.

Templateexamples/shadow/2-closed.pad
<search-box>
  {shadow mode='closed', focus}
    <label><slot name="label"></slot> <input type="search"></label>
  {/shadow}
  <span slot="label">Search the shop</span>
</search-box>
Resultopen alone
Search the shop

See also

  • {slot} A named place in a custom tag's template - and the fill a caller gives it.
  • {parms} Declares the parameters of a custom tag, with defaults, at the top of its template.
  • {source} Shows the files behind a page as tabs, each coloured as highlight colours it - the code shown is the code that runs.