Show/hide formula
The show/hide formula allows you to conditionally render elements based on specific conditions.
See how the show/hide formula controls the visibility of the card based on the toggle button.
The show/hide formula controls whether an element is displayed or not:
- It evaluates a
Booleanexpression - If the expression is truthy, the element is shown
- If the expression is falsy, the element is hidden
- The formula is applied by setting the
showattribute on an element
The truthy concept in Nordcraft is similar to JavaScript but follows a more streamlined approach:
- Falsy values:
false,nullandundefined - Truthy values: all other values including empty strings, zero and empty arrays
Add a show/hide formula
To conditionally show or hide an element:
- 1Select the element you want to show and hide based on conditions
- 2In the attributes panel, click the fx button next to Show
- 3Enter a formula that evaluates to a
Booleanvalue; the element will only be visible when the formula evaluates totrue
How show/hide differs from CSS display
Using Nordcraft's show/hide formula is different from setting CSS display: none or visibility: hidden. Using show/hide, elements or components are completely removed or added to the page when conditions change.
- Show/hide formula:
- Completely removes the element from the page when hidden
- Adds a new instance of the component or element to the page and reinitializes the initial state when shown
- No APIs, events or workflows run on hidden elements
- CSS display/visibility:
- The element or component remains on the page but is not visible
- APIs, events and workflows continue to run
- No unmounting or remounting occurs when toggling visibility
Performance considerations
Given elements are removed (unmounted) and added (remounted) when the show/hide condition changes, be aware of these performance implications:
- Re-initialization: When an element is added to the page when the show/hide formula evalutes to truthy, all initializations, API calls and
onLoadevents will run again - DOM updates: Hiding and showing elements forces the browser to remove and recreate HTML, which can negatively affect page performance for complex components
- State loss: Any internal state not stored in variables will reset when the element is hidden
For most situations, this behavior is ideal. However, if you need to toggle visibility frequently while maintaining state, consider CSS alternatives:
- Use
display: noneto hide the element but preserve its state - Use
visibility: hiddento hide the element while maintaining its layout space
Hiding and showing elements and components using CSS styles means any HTML for those elements remain on the page. Use this approach when you need to preserve state during frequent visibility changes.
