How to use conditions in components and variants
In TeleportHQ, a condition inside a component can read one of the component's own settings (props), so one component gets several looks. Open the component, select the part to show or hide, turn on Conditional Rendering, click Bind and choose Create Conditional Prop: TeleportHQ makes the setting and binds the rule to it. Every copy of the component on your pages then turns the part on or off under Component Properties → Conditional, with a False | True switch. A product card can show a "Sale" ribbon on some copies, a price on others, and a wide layout on a third, all from one component.
Before you start
- A component. Props and conditional props exist inside components, not on pages. See How to create a component.
- Know the two kinds of rule. A condition on an element shows or hides a part. A rendering condition on a class turns a look on, such as a full-width button. See How to turn a part of a component on or off.
- Role. You need the Owner, Admin or Designer role.
Steps
The example is a ProductCard component with a "Sale" ribbon that only some copies show.
1. Make the setting and the rule in one go
- In the left sidebar, open Pages, Components & Tree View, and in the Components folder click
ProductCard. - On the component canvas, select the ribbon. Don't select the card's outermost element: it can't hold a condition.
- In the right panel → Visual tab, turn on the Conditional Rendering switch.
- Click Bind. Inside a component, the menu offers Create Conditional Prop and Dynamic Properties Assignment Panel.
- Click Create Conditional Prop. In the Create Conditional Prop window, type the name
onSale. Type isbooleanand Default is False: leave both. Click Create. - Keep
=in the Operand list, click the value box (Pick a value) and picktrue.
The ribbon disappears from the component canvas, and the section says "Element not visible in the canvas! The current value of onSale is false." Click Set onSale to true to see the ribbon while you design it. That changes the prop's Internal value, which the editor uses and your copies ignore.
2. Turn it on, copy by copy
- Go back to a page with copies of
ProductCardand click one. - In the right panel, under Component Properties, open the Conditional group.
onSalehas a False | True switch. - Click True on the copies that are on sale.
3. Use a setting you already have
If the component already has a setting, such as a text prop layout, bind the rule to it instead: click Bind → Dynamic Properties Assignment Panel, open props, click layout, then Bind property. Type the value in the value box, for example wide. A copy then picks wide from a list under Component Properties: the list offers the values your conditions compare against, so a second rule with = compact adds compact to it.
One card, several looks
Combine a few rules on ProductCard and each copy picks its look:
| Part of the card | Rule | Copy setting |
|---|---|---|
| "Sale" ribbon | onSale = true | the onSale switch set to True |
| Price | showPrice = true | the showPrice switch set to True |
| Image on top | layout = vertical | layout set to vertical |
| Image on the left | layout = horizontal | layout set to horizontal |
| Stock line | the list item's stock > 0 | none: it reads the product |
A look that changes styles rather than parts, such as a wider card, is a class with a rendering condition: on the class row in the Classes section, click Add rendering condition and bind it the same way. See How to build a button with four looks.
Check that it worked
- On the page, a copy with
onSaleon shows the ribbon; a copy with it off doesn't. - Click Preview in the bottom toolbar: the same cards show.
Troubleshooting
Create Conditional Prop isn't in the Bind menu You're on a page, not inside the component. Open the component from the Components folder of Pages, Components & Tree View, then select the part again.
The setting is listed under Unassigned, not Conditional Its rule has no value yet. Open the component, select the part, and pick a value in the value box.
The ribbon shows on every copy The ribbon's Conditional Rendering switch is off, or its rule reads another value. Select the ribbon inside the component and check the section.
Set onSale to true did nothing on my copies It isn't meant to. The button changes only the editor's Internal value for the component canvas. Copies keep their own switch.
Good to know
- A copy can have a rule too. Select the copy on the page and turn on Conditional Rendering there, to show the whole card only when, for example, the visitor is signed in.
- A rule can read the list the card is in. When the card repeats in a list, its props can be bound to the item's fields, so
onSalefollows the product'ssalefield instead of a switch. See How to show a list of items. - Downloaded code. Conditions on a component's settings are worked out for each copy when the HTML download is built, so every copy keeps the version you picked for it. The Next.js download runs them. See How to download your site as code.