# Conditional Visibility

With the `Visibility` property, you can control which UI elements you want to show in the app, based on the API data.&#x20;

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FyY4lIMaIqylbMLbIlFlC%2FScreen%20Recording%202024-05-07%20at%2009.51.39.gif?alt=media&#x26;token=d087ccec-fde0-4e0f-bb0d-d7f4e89e4015" alt=""><figcaption></figcaption></figure>

You could choose to hide certain elements within a list upon certain conditions, or display some UI elements only to specific users, among other cases.

## 📲 How to use it

The `Visibility` property must be bound to a data item containing a [**boolean value**](https://en.wikipedia.org/wiki/Boolean_data_type). This means, the value of the data item needs to be either **True** or **False**.

There are two ways to configure it:

* **Make it visible**: The UI element will show.
* **Make it hidden**: The UI element won't show.

The way to define a boolean data type will depend on the backend tool. For instance, in Airtable, these values can be defined with a checkbox. Then, these boolean data items can be bound to the `Visibility` property for a UI element. \
\
**When "Make it visible" is selected:**

* If the data item has a **False** value, the UI element won't show.&#x20;
* If it has a **True** value, the UI element will show.

**When "Make it hidden" is selected:**

* If the data item has a **False** value, the UI element will show.&#x20;
* If it has a **True** value, the UI element won't show.

{% hint style="info" %}
Remember that the value returned by the API needs to be a boolean (true/false) or a number (1/0), **never a string.**
{% endhint %}

Some examples are shown below. Note that there are more possible cases than the ones described on this page, and they can be combined in the way you wish. Feel free to be creative!

![Setting conditional visibility on a component with states.](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FrRPsagCHzaweW0U2v7cO%2FScreenshot%202024-05-07%20at%2015.51.39.png?alt=media\&token=1af56318-97aa-45c7-9ad6-d62c53d15efe)

{% hint style="info" %}
If you are using conditional visibility on a [stateful component](https://docs.bravostudio.app/bravo-tags/components/stateful-component-like-button), make sure you set the visibility at the group level.
{% endhint %}

### Hide some items in a list

You could choose to control from the backend which list items you want to display in the app in a specific moment. To do that, add a **boolean field** to your database items, and bind its corresponding data item to the `Visibility` property of the **list container element**.

{% embed url="<https://www.loom.com/share/c84f56280193438aa31d5b4c2de37dc0?sid=9196b0ad-0818-4bd1-a76b-2094a92ca645>" %}

You can also control the visibility of an item binding the `Visibility` property of the list container to a non-boolean field: if that field for a specific **data record is empty**, the list item won't be shown.&#x20;

For instance, you could hide all the list items that don't have an image in the database, as shown below.

{% embed url="<https://www.loom.com/share/177f1e6db0c144d8aa5cc3acf4723d1a?sid=c7a2d68d-738e-420c-aa12-036c7a39c40a>" %}

### Hide a UI element on some items in a list

It's also possible to hide a specific UI element inside a list data item. For instance, you could choose to display a piece of text, a badge or a button depending on a boolean value in the database. That UI element would be hidden for some of the list items, depending on that boolean field.

To do that, bind the `Visibility` property of the UI element inside the list container to the corresponding data item. In this example, we control the visibility of a text element inside the list item, while showing all the list elements. We could also control the visibility of the whole items separately, by binding the `Visibility` of the list container as shown in the previous examples.

{% embed url="<https://www.loom.com/share/b24b1e25707a45139b84eb2df092f516?sid=22e14572-6163-4d38-84ed-262c4de4074e>" %}

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FqTsHHmjEPYjt7uBZw0sY%2FScreen-Recording-2024-05-08-at-10.06.51_2%20(1).gif?alt=media&#x26;token=8f74a10d-93e7-43b4-a15e-c298de956587" alt=""><figcaption></figcaption></figure>

### Hide a single UI element

The Visibility property can also be used for single elements outside lists. For instance, you could control the visibility of some elements in a [detail page](https://docs.bravostudio.app/data-binding/display-data-in-your-app/data-biding-bind-data-to-ui-for-a-detail-page).

To do that, you'll need to select the boolean data item (in this case Show Description) in the [detail API request](https://docs.bravostudio.app/connect-api/set-up-api-requests/creating-an-api-collection/airtable/data-library-connect-to-any-api-get-request-for-detail-page) used for the detail page. Then, bind this data item to the `Visibility` property of the corresponding UI element.

In this example, we'll control the visibility of a description text inside a detail page.

{% embed url="<https://www.loom.com/share/ca2c6e5f5aa0436bb026420c01763142?sid=40b6471f-5619-4eb9-b29a-df1387674ec1>" %}

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FcoU4AjdemZZQG7MCEhvQ%2FScreen-Recording-2024-05-08-at-09.43.06.gif?alt=media&#x26;token=57311200-27cb-44a8-b59c-da5a3e4f3bc2" alt=""><figcaption></figcaption></figure>
