Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to execute actions when clicking on sidebar items #290

Merged
merged 3 commits into from
Jan 18, 2025

Conversation

elchininet
Copy link
Owner

@elchininet elchininet commented Jan 18, 2025

Added a new property in order items

This pull request adds a new feature to make it possible to execute actions when clicking on sidebar items. With custom-sidebar it is possible to add an href property to each sidebar item making them redirect to an internal dashboard or to an external URL. From now on, it is also possible to execute certain actions when clicking on sidebar items. These actions can be executed at the same time that you are navigating to a dashboard or an external URL or can be exclusively executed if one omits the href property on new items or change it to # in existing ones.

Order items properties

Property Type Required Description
on_click OnClickAction no Specifies the onClick property of the sidebar item.
It allows two types of actions, ServiceCallAction
or JavaScriptAction. Take into account that setting this property
will not stop the href option for working. If you want to avoid
navigating to a page, you should set the href option
as # (in new items you can just omit it)
Service call actions

This action allows you to call a service clicking on an item of the sidebar

order:
  - new_item: true
    item: Example
    icon: mdi:gesture-tap
    on_click:
      action: call-service
      service: light.toggle
      data:
        entity_id: light.woonkamer
JavaScript actions

This action allows you to execute a JavaScript code (you don't need to wrap the code block between three square brackets)

order:
  - new_item: true
    item: Example
    icon: mdi:gesture-tap
    on_click:
      action: javascript
      code: |
        location.reload();

Use reactive variables in the templates

With this release, after the update of the home-assistant-javascript-templates package, it is also possible to make use of reactive variables in JavaScript templates. This will make your templates be reactive to variables only in the device in which you are working. For example, let's show or hide an item of the sidebar clicking on another item:

order:
  - new_item: true
    item: Example
    icon: mdi:gesture-tap
    on_click:
      action: javascript
      code: |
        const hide = ref('hide');
        hide.value = !hide.value;
  - item: history
    hide: |
      [[[
         const hide = ref('hide');
         if (hide.value === undefined) {
           hide.value = true;
         }
         return hide.value;
      ]]]

Reactive variables can also be declared in the js_variables property giving them an initial value. Let's refactor the previous code to declare the reactive variable hide in the js_variables property giving it an initial value there instead of giving the initial value in the item hide template:

js_variables:
  hide: ref(true)
order:
  - new_item: true
    item: Example
    icon: mdi:gesture-tap
    on_click:
      action: javascript
      code: |
        const hide = ref('hide');
        hide.value = !hide.value;
  - item: history
    hide: |
      [[[ ref('hide').value ]]]

IMPORTANT

  1. To make the templates detect that a reactive variable has been mutated, one needs to assign a new value to the reactive variable. For example, changing the items or an array using push or pop will not make the remplates using that variable to be reevaluated. You need to assign a new array to the value of the reactive variable to make the change been detected.
  2. To make the template aware that it contains a reactive variable, the value property of the variable should be accesed when the template is rendered. If the code accesing the value property is not executed when the template renders, then the reactive variable will not be tracked. That is why is recomendable to access the value property of the reactive variable outside any condition and build the logic using the retrieved value. In this way the template will track that the reactive variable is being used and any time that the variable changes, the template will get re-evaluated.

@elchininet elchininet added documentation Improvements or additions to documentation feature tests labels Jan 18, 2025
@coveralls
Copy link

coveralls commented Jan 18, 2025

Coverage Status

coverage: 100.0%. remained the same
when pulling 1d8f110 on add_an_option_to_execute_clicks_on_items
into 287de86 on master.

@elchininet elchininet force-pushed the add_an_option_to_execute_clicks_on_items branch from 61b36ab to b0c4815 Compare January 18, 2025 20:08
@elchininet elchininet force-pushed the add_an_option_to_execute_clicks_on_items branch from b0c4815 to 1d8f110 Compare January 18, 2025 20:19
@elchininet elchininet marked this pull request as ready for review January 18, 2025 20:35
@elchininet elchininet merged commit bb1d6b5 into master Jan 18, 2025
3 checks passed
@elchininet elchininet deleted the add_an_option_to_execute_clicks_on_items branch January 18, 2025 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation feature tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants