Component files

A component is one HTML file in components/, for example components/coffee-card.html. The file holds everything about the component: its markup, its props, its variants and its slots. A page holds only a small stub that names the component.

To make and edit components in the App, see Components.

The parts of the file

PartWhat it does
<template data-opt-component="…">The component. The value is the component ID.
data-name, data-slug, data-groupThe name, the slug and the group, on the same <template>. Components lists components by group, then by name. The default group is Components.
<style>Optional. CSS for variants and IDs of the component.
<template data-opt-component-locale="no">Optional. A full copy of the template for one language.
<script type="application/json" data-opt-schema>The props, the variants and the slots, as JSON

Example

<template data-opt-component="component_3f9a1c27b4d0" data-name="Coffee card"
          data-slug="coffee-card" data-group="Menu">
  <article class="coffee-card">
    <img class="coffee-card-image" src="/assets/cupping.svg" alt="Cupping bowls" data-prop-attr="src:image">
    <h3 class="coffee-card-title" data-prop-text="name">Harbour Blend</h3>
    <p class="coffee-card-notes" data-prop-text="notes">Chocolate, red apple, honey.</p>
    <span class="coffee-card-badge" data-prop-show="new">New</span>
    <div class="coffee-card-details" data-opt-slot-outlet="slot_details">
      <p>Roasted every week.</p>
    </div>
  </article>
</template>

<style>
  .coffee-card[data-tone="dark"] { background: var(--color-ink); color: var(--color-paper); }
</style>

<script type="application/json" data-opt-schema>
{
  "props": [
    { "id": "name", "key": "name", "label": "Name", "type": "text", "default": "Harbour Blend" },
    { "id": "notes", "key": "notes", "label": "Notes", "type": "text", "default": "Chocolate, red apple, honey." },
    { "id": "image", "key": "image", "label": "Image", "type": "image",
      "default": { "src": "/assets/cupping.svg", "alt": "Cupping bowls" } },
    { "id": "new", "key": "new", "label": "New", "type": "switch", "default": false }
  ],
  "variants": { "axes": [
    { "id": "ax_tone", "key": "tone", "label": "Tone", "options": [
      { "id": "opt_light", "key": "light", "label": "Light", "default": true },
      { "id": "opt_dark", "key": "dark", "label": "Dark" }
    ] }
  ] },
  "slots": [ { "id": "slot_details", "name": "details", "label": "Details" } ]
}
</script>

Rules of the template

  • The first element in the template is the component. The App drops other top-level elements.
  • Put class rules in styles/site.css. When the App saves a component, it moves class rules from the <style> block to styles/site.css.
  • The <style> block keeps rules for variants ([data-tone="dark"]) and for IDs.
  • The schema keeps only props, variants and slots. The App drops other keys on the next save.
  • A component can hold another component as a stub. Nesting goes 12 levels deep. A component cannot hold itself.

Caution: Do not change the component ID. Every stub on every page names the component by this ID.

Props

TypeValue
textA string
numberA number
switchtrue or false
selectOne of the strings in options
image{ "src": "/assets/hero.jpg", "alt": "…" }
link{ "href": "/about", "type": "internal" }, with target, rel and other link attributes if you need them
filters{ "op": "and", "rules": [ … ] }, the filter rules for a content list in the component

These markers in the template take a prop value:

MarkerValueWhat it does
data-prop-textProp IDSets the text of the element
data-prop-showSwitch prop IDShows or hides the element
data-prop-attrattr:propId pairs, joined by ;Sets attributes. An image prop on src also sets alt. A link prop on href also sets target and rel.
data-opt-prop-attr-templateJSON, for example {"aria-label":"Order ${name}"}Sets an attribute from text mixed with props
data-prop-filterFilters prop IDAdds the rules to the query of a content list
data-opt-slot-outletSlot IDMarks where slot content goes. Its children are the default content.

The prop IDs text, html, show, attr and filter are reserved.

A component on a page

A page holds a stub. The stub names the component and holds only what differs from the defaults.

<div data-opt-instance="component_3f9a1c27b4d0"
     data-prop-name="Night Shift"
     data-prop-new="true"
     data-tone="dark">
  <div data-opt-slot="slot_details"><p>Roasted on Mondays only.</p></div>
</div>
AttributeWhat it does
data-opt-instanceNames the component by its ID. The tag of the stub does not matter.
data-prop-<propId>The value for this instance. Leave it out to use the default. Image, link and filters values are JSON.
data-<axisKey>The variant option, for example data-tone="dark"
<div data-opt-slot="slotId">A child of the stub. Its inside fills the slot.
data-opt-propsJSON with values for props with a reserved ID, for example {"text":"Order now"}
class, id, aria-* and other attributesStay on the page. The classes come after the classes of the template.

What reaches the live site

A publish replaces each stub with the markup of the component. Then it removes every component marker. Only data-<axisKey> stays on the root element, so your variant CSS works.

The stub above becomes:

<article class="coffee-card" data-tone="dark">
  <img class="coffee-card-image" src="/assets/cupping.svg" alt="Cupping bowls">
  <h3 class="coffee-card-title">Night Shift</h3>
  …
</article>

The <style> blocks of all components go into one file, /styles/components.<hash>.css.

Edit a component file by hand

  1. Open components/<slug>.html in your editor.
  2. Make the change.
  3. Save the file.

The App reads the file again, and every instance in the Design panel updates. If you also changed the component in the App, the App merges the two changes. When both changed the same part, the App keeps its own copy. The disk version goes to Removed.

Note: A prop with a type that the App does not know is read as text. The component still publishes.