New-UDButton OnClick variables

Hi,

I am working with a UDTable and have UDButtons in someone columns.
With these buttons i am trying to change some variables with the OnClick action, but it seems that the scope within the OnClick action is different. This seems to only work with session variables.
Due to the table natures and its multiple rows, i am using dynamic.
a) i don’t think i can use dynamic session variables
b) i am trying to avoid sessions variables

One simply example i am trying to do is, changing the UDButton color after it’s being clicked.
Which works fine when using session variables, but doesn’t when using dynamic non-session variables.

E.g.:
This is simplified button using session variables, which is working fine.
When converting the $session:button1Style into $button1Style, the color won’t change and the variable is null within the OnClick.

    $session:button1Style = @{'background-color' = "transparent";}
    New-UDElement -id "Button1Element" -Tag "div" -Endpoint {
        New-UDButton  -Variant "outlined" -Text "Button1" -Style $session:button1Style -OnClick {
            $session:button1Style = @{'background-color' = "lightgreen";}
            Sync-UDElement -id "Button1Element"
        }
    }

So my questions here:

  1. How can i pass variables into the OnClick action
  2. How can i retrieve/return variable which were changed or defined within the OnClick action
  3. Which is the correct variable scope to use here?
  4. Is there a better way to achieve this?
Product: PowerShell Universal
Version: 2.2.1