Skip to content

Script Examples

Communication between the Show UI and the Template UI

Show UI:
Download ezUI File

Template UI:
Download ezUI File

In this example, we demonstrate the communication process between the Show UI and the Template UI to dynamically update the graphical user interface based on user input.

By storing the user's selection in session storage and listening for changes, the Template UI can promptly reflect the user's choices, providing a responsive and interactive user experience.

ShowUI-TemplateUI-Communication

Info

sessionStorage in UI Builder is a shared, per-client store: a value written by one user interface is broadcast to every other user interface open in the same Studio window. sessionStorage.on(key, callback) receives { key, value } whenever that key changes, no matter which UI changed it. once(key, callback) and off(key, callback) are available as well.

Warning

Storage values must be strings. setItem silently ignores a non-string value, so wrap anything else in String(...) or JSON.stringify(...) before storing it.

Show ezUI:

1
2
3
4
5
// Event listener for the highlight button click event
btnHighlight.onClick = () => {
  // Store the selected value from the dropdown in session storage under the key 'highlight'
  sessionStorage.setItem('highlight', dropdown.value);
};

Template ezUI:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Function to set the highlight based on the provided value
function onSetHighlight(value: string) {
  let highlight = -1;  // Initialize highlight variable to -1

  // Determine the highlight value based on the input
  switch (value) {
    case 'Option 1':
      highlight = 1;
      break;

    case 'Option 2':
      highlight = 2;
      break;

    case 'Option 3':
      highlight = 3;
      break;
  }

  /* Loop through the options and update the border 
  color of the corresponding buttons*/
  for (let i = 1; i <= 3; i++) {
    const btn = findElement(`btnOption${i}`);  // Find the button element
    if (btn)
      btn.style.borderColor = (i === highlight ? 'red' : undefined);  /* Set 
      border color to red if highlighted, otherwise undefined */
  }
}

// Event listener for changes in session storage 'highlight' key
sessionStorage.on('highlight', e => onSetHighlight(e.value));

Tip

Assigning undefined to a style property clears it, which is what restores the default border colour on the buttons that are not highlighted.

See Storage for the other storage scopes and for values that have to survive longer than the current Studio window.