Component variants

How to turn a part of a component on or off

How to turn a part of a component on or off

In TeleportHQ, a setting that is only on or off is a boolean prop. It can do two things: switch a class on, for example a full-width look for a button, or show and hide an element, for example an icon inside the button. Every copy then turns the setting on or off with a False | True switch.

Before you start

What you're building

ActionButton                      (component)
  settings (props)
    buttonType   string     default "primary"
    size         string     default "medium"
    fullWidth    boolean    default false     ← new: stretch the button
    showIcon     boolean    default false     ← new: show the arrow
    label        richtext   default "Get started"
  button element
    .btn             always      (+ gap 8px between text and icon)
    .btn-<look>, .btn-<size>    as before
    .btn-full        when fullWidth = true    (new)
    label text
    arrow icon       24 × 24, shown when showIcon = true   (new)

A class turns a look on; Conditional Rendering turns a whole element on. Both read the same kind of setting.

Steps

1. Make the button stretch when fullWidth is on

  1. In the left sidebar, open Pages, Components & Tree View, and in the Components folder click ActionButton. On the component canvas, click the button to select it.
  2. In the right panel, in the Classes section, click Type to search or create new class, type btn-full and click Create class btn-full. In the Create class panel, turn off Extract element styles and add one row: width 100%.
  3. On the .btn-full row, click Add rendering condition (the eye with a small C). Click Bind, then Create Conditional Prop.
  4. In Name, replace the suggested name with fullWidth. Type is already boolean and Default is already False: leave both. Click Create.
  5. Click the value box (Pick a value) and pick true from the list. The condition is saved as soon as you pick it.

The .btn-full row shows Rendering condition is not met: the button only stretches on copies that turn fullWidth on.

2. Add an arrow that shows when showIcon is on

  1. Space the text and the icon: on the .btn row in the Classes section, click Edit class (the expand icon) and add the row gap 8px. The value box may show HalfUnit, the spacing token that equals 8px; the class keeps 8px.
  2. Select the button's text: in Pages, Components & Tree View, expand the button in the tree and click its text row.
  3. In the left sidebar, open Assets Manager, then the Icons tab. In Search, type arrow right: every section with a matching icon opens. In the Arrows section, click the icon named arrow right (point at an icon to see its name). The icon goes into the button, after the text.
  4. Select the new icon (click it in the tree). Icons arrive at 48 × 48, too big for a button: in the right panel, open the Attributes section and set width and height to 24.
  5. In the right panel, find Conditional Rendering and turn on its switch.
  6. Click Bind, then Create Conditional Prop. In Name, type showIcon; leave Type on boolean and Default on False. Click Create.
  7. Leave the operator on =, click the value box (Pick a value) and pick true.

The section now says Element not visible in the canvas! The current value of showIcon is false. That's expected: copies show the arrow only when they turn showIcon on. Click Set showIcon to true to see the arrow while you design the component. It changes only this preview: copies on your pages keep their own setting.

3. Turn the settings on, copy by copy

  1. Go back to the page with your copies and click a copy.
  2. In the right panel, under Component Properties, open the Conditional group. fullWidth and showIcon each have a False | True switch.
  3. Click True to turn a setting on for that copy.

Check that it worked

  • A copy with showIcon on shows the arrow after its text; the others don't.
  • A copy with fullWidth on fills the width of its container. In a grid, it fills its column, so it only looks wider when that column is wider than the button.
  • Click Preview in the bottom toolbar and wait for the preview to build (about a minute): the same buttons show.

Troubleshooting

The icon landed next to the button, not inside it. The button itself was selected when you clicked the icon. Delete the icon, select the button's text in the tree, and click the icon again.

The setting is listed under Unassigned, not Conditional. No value was picked in its condition. Open the component, select the element, and pick true in the condition's value box.

The arrow shows on every copy. The icon's Conditional Rendering switch is off, or its condition isn't bound to showIcon. Select the icon and check its Conditional Rendering section.

Good to know

  • On/off settings mix with looks and sizes: one copy can be a large outline button with an arrow, stretched to full width.
  • The arrow takes the look's text color only if the icon uses the current text color; otherwise set its color in the icon's Color section.
Share

Related Articles

How to build a button with four looks (primary, secondary, outline, link)

In TeleportHQ, a button with variants is one component whose button element wears a base class, btn, plus four look classes (btn-primary, btn-secondary, btn-outline, btn-link) that each apply only when a text prop called buttonType equals their name; every copy then picks its look from a list.

How to add sizes to a button (small, medium, large)

In TeleportHQ, a button gets sizes the same way it gets looks: a second text prop called size (default medium) and two size classes, btn-small and btn-large, that apply only when size equals their name; medium is the base class's own size, so it needs no class, and every copy picks a look and a size.

What are component variants?

In TeleportHQ, a component variant is one component whose copies can look or behave differently, picked per copy with a setting (a prop); the component keeps a base class with what every version shares and a class per version that switches on when the setting matches.

How to show or hide elements based on a condition

In TeleportHQ, you show an element only when a condition is true, such as "stock equals 0" or "the menu is open", by selecting it, turning on the Conditional Rendering switch near the top of the right panel and building the condition from a value, an operator and a comparison value; it works in every project, website or web app.