Conditions

How to show or hide elements by state

How to show or hide elements by state

In TeleportHQ, a state is a value the page remembers, such as isMenuOpen (true or false) or activeTab (a number), and a condition on a state shows an element only while the state has a given value. Create the state in Page settings → State, give the element a Conditional Rendering rule such as isMenuOpen = true, and let a button change the state with a Change State event. This is how a menu opens on a click, how a selected tab shows its content, and how a form moves from step 1 to step 2. It works on websites and web apps alike.

Before you start

  • Nothing to turn on. Page and component states, conditions and Change State events work in every TeleportHQ project. They don't turn a website into a web app.
  • Know where the element lives. A page state is listed for elements on that page; a component state for elements inside that component. See How to store and use values with States.
  • Role. You need a role that can edit the project, for example Designer.

Steps

The example is a menu panel that opens when a visitor clicks a burger button.

1. Create the state

  1. Open the page on the canvas. Above the top right of the canvas, click Page settings (Component settings inside a component).
  2. Click the State tab, then Create new state.
  3. In Name, type isMenuOpen. Keep Source on Static value.
  4. In Type, choose boolean. In Default, pick false: visitors start with the menu closed.
  5. Click Create.

2. Put the rule on the element

  1. On the canvas, select the menu panel.
  2. In the right panel → Visual tab, turn on the Conditional Rendering switch, near the top under Workflows.
  3. Click Bind. A small menu opens.
  4. Click Dynamic Properties Assignment Panel. The Select Dynamic Property panel opens.
  5. Under state, click isMenuOpen, then click Bind property.
  6. Keep = in the Operand list. In the value box (Pick a value), pick true.

The panel disappears from the canvas, because the state starts at false. The section says "Element not visible in the canvas! The current value of isMenuOpen is false." Click Set isMenuOpen to true to see the panel while you design it. That changes the state's Internal value, the value the editor uses, and nothing on your site.

3. Change the state from a click

  1. On the canvas, select the burger button.
  2. In the right panel, open the Events tab and add an event of type Change State.
  3. In Modifies, pick isMenuOpen. In To, pick the new value: true for a button that opens the menu, false for a close button inside the panel.

For one button that opens and closes, see How to use states in a component.

Three common states

  • A menu or panel: isMenuOpen, boolean, default false. Rule on the panel: isMenuOpen = true.
  • A selected tab: activeTab, number, default 1. Rule on the first tab's content: activeTab = 1; on the second: activeTab = 2. Each tab button sets activeTab to its number. The Tabs element in Elements → Primitives → Interactive builds this for you, with Current tab and Direction in its Tabs settings.
  • A step of a form: step, number, default 1. Rule on the first group of fields: step = 1; on the second: step = 2. The Next button sets step to 2, the Back button to 1. The submit button gets step = 2 as well, so it shows on the last step only.

When a state switches one section between several views, a small switch appears on the canvas while you point at that section. It lists every view; click one to see it.

Check that it worked

On the canvas, change the state's Internal value in Page settings → State, or click Set … to … in the Conditional Rendering section, and watch the element appear and disappear. Then click Preview in the bottom toolbar and click the button as a visitor would.

Troubleshooting

The state isn't listed under state in the panel The state belongs to another page or component. A page state is only listed for elements on that page, a component state only inside that component. Create the state where the element lives.

The element shows in the editor but not on my live site The canvas uses the state's Internal value; visitors start with its Default. Set the Default to the value visitors should start with.

The button does nothing in Preview Its Change State event is missing, or Modifies points at another state. Select the button, open the Events tab and check the event.

I can't find the Conditional Rendering section You selected a component's main (outermost) element, which can't hold a condition. Select an element inside it, or the copy of the component on the page.

Good to know

  • Several elements, one state. Any number of elements can read the same state. A menu panel and a dark overlay behind it both get isMenuOpen = true.
  • A state and a condition on the same page. A state is not bound to one element. To reuse it on another page, create a state there too, or use a global state in a web app.
  • Downloaded code. The HTML download keeps conditions on states, such as a menu that opens on a click. The Next.js download runs every condition. See How to download your site as code.
  • Ready-made widgets such as Tabs, Accordion and the pop-ups use states for you. See Condition recipes.
Share

Related Articles

What are conditions?

In TeleportHQ, a condition is a rule on one element, such as "show this badge while stock equals 0" or "show this menu while isMenuOpen is true"; you set it in the Conditional Rendering section of the right panel, it can read a state, a component setting, a list item, the signed-in visitor, the cart or a URL parameter, and on the published site the element is left out of the page while the rule is false.

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.

How to show or hide elements for signed-in visitors

In TeleportHQ, a web app with sign-up and login offers two built-in values under global in the Select Dynamic Property panel, User is logged in (true or false) and Current User (name, email, role and more); a condition such as User is logged in = true shows a Sign out button, = false a Sign in link, and Current User → role = admin a button for one role only.

How to show or hide elements by URL parameter

In TeleportHQ, declare the parameter on the page under Page Params in Page settings (a Key name such as category and a Default value), then give the element a Conditional Rendering rule on the URL Param, such as category = cakes; a visitor who opens /products?category=cakes sees that element, and the page without the parameter uses the default.

How to show or hide elements by cart and store values

In a TeleportHQ store, the Select Dynamic Property panel lists the visitor's cart under global → E-commerce → Cart (itemCount, total, shippingIsFree, freeDeliveryRemaining and more) and the store's Settings; a rule such as shippingIsFree = false shows a "Spend more for free delivery" banner, itemCount = 0 an empty-cart message, and a product's stock field a "Only 3 left" label.

How to use conditions in components and variants

In TeleportHQ, a condition inside a component can read one of the component's settings (props): click Bind in the Conditional Rendering section and choose Create Conditional Prop, and every copy of the component then turns that part on or off, or picks a look, under Component Properties → Conditional; one card component gets several looks without being duplicated.

Condition recipes

Six ready-to-copy TeleportHQ conditions, each with the state or value to create and the rule to set: an accordion and tabs built from a state (or the Accordion and Tabs elements that do it for you), an image slider with a slide state, a "no results" message with is_empty, a language-specific block on Current Language → short, and a fallback for a missing field.