The language

Backticks

Backticks (`…`) let you put live data in the middle of a string, and make classes depend on your data. They work as they do in JavaScript, with a few limits that keep every page editable on the canvas.

Where they work

WhereWhat goes in ${…}Example
<Text> and text propsWords and data{`Only ${product.stock} left`}
Any other string propWords and datahref={`/product/${product.slug}`}
classNameClasses, switched on dataclassName={`p-4 ${product.onSale ? "bg-red-600" : "bg-white"}`}

Words and data

Write the sentence in backticks and put each piece of data in ${…}:

<Text>{`Only ${product.stock} left`}</Text>

Between <Text> tags you can also skip the backticks and write Only {product.stock} left. That's how the editor prints it back, and the two mean the same.

Props take them too, which is handy for building addresses and labels from data:

<Link href={`/product/${product.slug}?ref=sale`}>
  <Image src={`https://images.example.com/${product.slug}.jpg`} className="object-cover" />
</Link>

Inside these, ${…} holds data only: no sums, function calls or conditions. To choose between two pieces of text, choose between two widgets instead.

Won't parse
<Text data={`Total ${product.price * 2}`} />

ErrorTemplate literals may only contain data paths like ${product.name}

Long class lists

With no ${…}, backticks are simply a string that can span lines, so you can group a long className:

<Container
  className={`
    p-4 md:p-8
    bg-white rounded-xl
    shadow-md
  `}
/>

Classes that change with data

In a className, ${…} switches classes on your data. Use ? : to choose between two sets of classes:

A red card when the product is on sale, white otherwise
<Container className={`p-4 rounded-xl ${product.onSale ? "bg-red-600" : "bg-white"}`} />

Use && to add classes only when something is true, and ! to test the opposite:

A border only on featured products
<Container className={`rounded-xl ${product.featured && "border-2 border-gray-900"}`} />
A grey card when the product is out of stock
<Container className={`p-4 ${!product.inStock ? "bg-gray-100" : ""}`} />

Each branch can hold several classes, including breakpoint classes like md:p-8. Classes apply in order as usual, so a branch wins over the classes before it, and only at the breakpoints it names.

In a list, each item is styled from its own data:

<Column className="gap-2">
  {products({ limit: 4 }).map((item) => (
    <Container className={`p-2 ${item.onSale ? "bg-red-600" : "bg-white"}`}>
      <Text>{`${item.name} is here`}</Text>
    </Container>
  ))}
</Column>

The rules

  • Each branch is whole, quoted classes. Use "" for no classes.
  • Put a space on either side of ${…}. Part of a class, like bg-${…}, won't work.
  • A condition tests one data value, as in Lists & conditions. Comparisons like product.price > 100 aren't allowed.
  • A className can switch on up to 4 conditions.
  • The usual class rules still apply, so hover: isn't supported and a class must suit its widget.
Won't parse
<Container className={`bg-${product.onSale ? "white" : "black"}`} />

ErrorPut whole classes in each branch and spaces around ${…}, e.g. ${product.onSale ? "bg-red-600" : "bg-white"}

If the editor flags something

MessageWhat it means
Unknown class "foo-bar"A class inside the backticks doesn't exist. The editor underlines exactly that class.
In a className, ${…} can only switch classes on dataYou wrote a value on its own, like ${product.color}. Use ? : or && with quoted classes.
Put whole classes in each branch and spaces around ${…}You built part of a class, like bg-${…}. Write the whole class in each branch.
Each branch must be quoted classesA branch isn't a quoted string. Use "" for no classes.
A className can switch on at most 4 conditionsSplit the element in two, or use a condition around it.
Conditions may only test dataYou compared or calculated, like product.price > 100. Test a true/false field instead.
Template literals may only contain data pathsA prop's ${…} holds something other than data, like a sum.