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
| Where | What goes in ${…} | Example |
|---|---|---|
| <Text> and text props | Words and data | {`Only ${product.stock} left`} |
| Any other string prop | Words and data | href={`/product/${product.slug}`} |
| className | Classes, switched on data | className={`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.
<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:
<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:
<Container className={`rounded-xl ${product.featured && "border-2 border-gray-900"}`} /><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, likebg-${…}, won't work. - A condition tests one data value, as in Lists & conditions. Comparisons like
product.price > 100aren't allowed. - A
classNamecan switch on up to 4 conditions. - The usual class rules still apply, so
hover:isn't supported and a class must suit its widget.
<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
| Message | What 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 data | You 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 classes | A branch isn't a quoted string. Use "" for no classes. |
| A className can switch on at most 4 conditions | Split the element in two, or use a condition around it. |
| Conditions may only test data | You compared or calculated, like product.price > 100. Test a true/false field instead. |
| Template literals may only contain data paths | A prop's ${…} holds something other than data, like a sum. |