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
- Open the page on the canvas. Above the top right of the canvas, click Page settings (Component settings inside a component).
- Click the State tab, then Create new state.
- In Name, type
isMenuOpen. Keep Source on Static value. - In Type, choose boolean. In Default, pick
false: visitors start with the menu closed. - Click Create.
2. Put the rule on the element
- On the canvas, select the menu panel.
- In the right panel → Visual tab, turn on the Conditional Rendering switch, near the top under Workflows.
- Click Bind. A small menu opens.
- Click Dynamic Properties Assignment Panel. The Select Dynamic Property panel opens.
- Under state, click
isMenuOpen, then click Bind property. - Keep
=in the Operand list. In the value box (Pick a value), picktrue.
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
- On the canvas, select the burger button.
- In the right panel, open the Events tab and add an event of type Change State.
- In Modifies, pick
isMenuOpen. In To, pick the new value:truefor a button that opens the menu,falsefor 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, defaultfalse. Rule on the panel:isMenuOpen=true. - A selected tab:
activeTab, number, default1. Rule on the first tab's content:activeTab=1; on the second:activeTab=2. Each tab button setsactiveTabto 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, default1. Rule on the first group of fields:step=1; on the second:step=2. The Next button setsstepto2, the Back button to1. The submit button getsstep=2as 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.