Problem/Motivation

When a component is selected, the Design tokens contextual panel should show that component's own CSS variables. Today it shows global ones only.

Current state of the tokens island:

  • It lists every UI Skins variable with a :root default, most of the 393 declared by UI Suite Bootstrap.
  • Values are saved on the node and applied as an inline style attribute. The list looks global but the change acts on one element.
  • alterElement() overwrites #attributes['style'] instead of merging it.
  • The island does not implement ThirdPartySettingsInterface, so the summary never shows it.
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mogtofu33 created an issue. See original summary.

pdureau’s picture

In UI Skins 1.x, there is no differences between "global CSS variables" and "component CSS variables".

In UI Skins 2.x, we will have su h difference thanks to the work on Core's #3531854: Add a Design Tokens & CSS variables API but it will work the opposite way:

  • when selecting a component, we will have both the global variables and the component variables
  • outside the component, we will have only the global variables
pdureau’s picture

pdureau’s picture

Beta4 has already been released.

mogtofu33’s picture

mogtofu33’s picture

Title: UI Skins for selected component » Design tokens panel: show only variables scoped to the selected component
Issue summary: View changes
Issue tags: -RC blocker
Parent issue: #3561282: Design tokens panel: keep existing inline styles, list overrides in the summary »

Updated the issue summary to keep this issue on its original ask: scoped variables for the selected component. Global tokens moved to #3624054: Global design tokens panel, with live preview in the builder, with the new core Design Tokens API in mind.

About #3561282-2: Design tokens panel: keep existing inline styles, list overrides in the summary: no need to wait for a new UI Skins version. The scopes declared in default_values already tell component variables apart from global ones.

pdureau’s picture

Issue tags: +Novice

I am having a look on the updated issue summary.

CSS variables

(just a reminder, for the ones not familiar with the concept which would want to help with the ticket)

When used, a CSS variable is the combination of 3 parts:

  • a design token (the variable name, UI SKins 1.x doesn't use the official Design Tokens format yet, but it is the same principle)
  • a scope (a CSS selector to target specific parts of the page)
  • an override value, the change applied to the scope

When used from UI Skins' /admin/appearance/css-variables, the CSS variables are printed in page body like that:

.my .scope {
  --my-design-token: my-overriden-value;
 }

Display Builder tokens panel does that a but differently, it uses an inline style attribute

style="--my-design-token: my-overriden-value"

The scope is implicit, it is the HTML node where the style attribute is added.

In theory, there is no "global" or "local" design tokens, the scope make them global or local. For example, we don't expect global-primary-color, card-primary-color, button-primary-color... We only expect primary-color, which can be scoped to :root, .card, .btn...

Bootstrap is the problem here

Bootstrap 5 did the mistake to convert all their internal Sass Variables to CSS Variables, and we now have a mess.

It is even worth than hardcoding the scope in token names ( card-primary-color, button-primary-color...). They hardcode the token name in the scope:

bs-alert-color:
    ".alert": "#212529"
    ".alert-primary": "#084298"
    ".alert-secondary": "#41464b"

It makes CSS variables nearly unusable in Bootstrap projects. It was already discussed in #3389752: UI Skins: Reduce the number of CSS variables but we didn't find a solution.

So, it is better to not take Bootstrap as an example when we try to improve the token panel.

What can we do?

It lists every UI Skins variable with a :root default, most of the 393 declared by UI Suite Bootstrap.

I agree it is a fragile mechanism, but there was an intent here: a global variable (so, with :root scope) will be more prone to be applicable anywhere than a variable without the global scope.

In other words, I trust :root { --color-primary: red } more to be applicable to .card scope than .fab { --degree: 0 }.

🤔 However, an alternative proposal is always welcomed.

Values are saved on the node and applied as an inline style attribute. The list looks global but the change acts on one element.

That's the purpose of scoping: a global list, a local override.

So, the title of the ticket ("Design tokens panel: show only variables scoped to the selected component") is not something we would want to do.

It is also not something we could really do if we would want, because we don't know what would be the scope of a component. We can guess it, but it will be a fragile and risky guess. For example:

Design system Component ID Scope selector
Bootstrap 5 button btn
Bulma button button
DaisyUI button btn
DSFR button fr-btn
Material 3 button mdc-button
Protocol button mzp-c-button

❌ So, I would vise to not go to this direction. "Normal" scoping already works kind of well outside of Boottsrap.

alterElement() overwrites #attributes['style'] instead of merging it.

👌 Let's fix that.

The island does not implement ThirdPartySettingsInterface, so the summary never shows it.

👌 Let's fix that.

So, I would advise to focus on the alterElement() and ThirdPartySettingsInterface changes.

Don't use the ticket title as the commit message if we don't show only variables scoped to the selected component ;)

pdureau’s picture

Assigned: Unassigned » pdureau
Issue tags: -Novice

pdureau’s picture

Assigned: pdureau » Unassigned
Status: Active » Needs review

Proposal pushed.

Implementation

The "ThirdPartySettingsInterface and summary" is straightforward.

The "alterElement() and #attributes['style']" part is more complex than expected: alterElement() adds a step to the component's #pre_render list, just before the ui_patterns merge (ComponentElementAlter). That step writes everything into #props['attributes']['style'], in this order: the element's own #attributes style, the prop's style, then the tokens.

This would be better fixed on UI Patterns side according to Claude LLM:

In ComponentElementAlter::processAttributesRenderProperty(), combine the two style values instead of letting the prop's replace the element's. class already merges because both sides are lists, so this handles style the same way. The island then goes back to the plain #attributes merge, and the pre-render callback, element_info and TrustedCallbackInterface all go away. Any other module that sets an inline style through #attributes on a component benefits too.

Tests

Tested with UI Suite DaisyUI theme and with the Design Tokens panel activated on the profile.

ThirdPartySettingsInterface and summary:

Step Expectation Result
I add a Button, "Success" variant, to the display. Only "Config" dropdown in Scaffold ✅
In the Design Tokens panel, i put "yellow" value on "Success color" and i change focus 2 dropdowns in Scaffold, with "Success color: yellow" in the token one ✅
In the Design Tokens panel, i remove "Success color" value and i change focus Only "Config" dropdown in Scaffold ✅

alterElement() and #attributes['style']:

Step Expectation Result
Still on the button component, in the Config panel, I add style="foo: bar" to the attributes prop The attributes is added to the component HTML in Builder panel ✅
In the Design Tokens panel, i put "yellow" value on "Success color" and i change focus Now I see style="foo: bar; --color-success: yellow;" Builder panel ✅
In the Design Tokens panel, i remove "Success color" value and i change focus Now I see style="foo: bar;" Builder panel ✅

This was also the opportunity to rename the panel from "Design tokens (CSS variables)" to "Design tokens"

What's next?

Review the changes.

And it is not too late to propose an alternative to only showing global CSS variables in the panel 😉

csakiistvan’s picture

Assigned: Unassigned » csakiistvan
csakiistvan’s picture

Assigned: csakiistvan » Unassigned
Status: Needs review » Reviewed & tested by the community

✅ Tested and works — on a component whose attributes prop sets style="foo: bar", a Design tokens value was saved but missing from the rendered element; after applying MR !523 and rebuilding caches, the element renders style="foo: bar; --card-accent: #aa7941;". The token no longer gets lost next to an attributes prop style.

Notes
  • Before the fix, it is the token that disappears, not foo: bar: the attributes prop style wins the merge.
  • Non-blocking: if ui_patterns.component_element_alter:alter is not in the element's #pre_render, alterElement() falls back to #attributes and the token is lost again without notice; no test covers that path.
  • Non-blocking: getSummary() skips variables without a definition, but alterElement() still renders them, and an empty value renders as an empty declaration (--var:).
pdureau’s picture

Status: Reviewed & tested by the community » Needs work

Non-blocking: getSummary() skips variables without a definition, but alterElement() still renders them, and an empty value renders as an empty declaration (--var:).

Interesting. I will address that and move back to RTBC.

pdureau’s picture

Status: Needs work » Needs review

Interesting. I will address that and move back to RTBC.

Moved back to "Needs Review" instead of RTBC, because this little change was the opportunity of sharing the same filtering rules between ::alterElement() and ::getSummary(), and it "forced" us to alter one phpunit test.

csakiistvan’s picture

Status: Needs review » Reviewed & tested by the community

✅ Tested and works — ran the same five cases through alterElement() (on an element with style="foo: bar;") and getSummary(), before and after da78176: empty, blank and unknown values no longer render, and the rendered variables now match the summary. RTBC from my side.

         before (cf1ab93)                                          after (da78176)
normal   foo: bar; --card-accent: red;            [Card accent: red]   foo: bar; --card-accent: red;  [Card accent: red]
empty    foo: bar; --card-accent:;                [Card accent: ]      foo: bar;                      []
blank    foo: bar; --card-accent:;                [Card accent:  ]     foo: bar;                      []
unknown  foo: bar; --no-such-variable: blue;      []                   foo: bar;                      []
mixed    foo: bar; --card-accent: red; --no-such-variable: blue;  [Card accent: red]   foo: bar; --card-accent: red;  [Card accent: red]
pdureau’s picture

Assigned: Unassigned » mogtofu33
mogtofu33’s picture

Title: Design tokens panel: show only variables scoped to the selected component » Design tokens panel: keep existing inline styles, list overrides in the summary

  • mogtofu33 committed b9309b93 on 1.0.x authored by pdureau
    task: #3561282 Design tokens panel: keep existing inline styles, list...
mogtofu33’s picture

Assigned: mogtofu33 » Unassigned
Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

pdureau’s picture

Status: Fixed » Closed (fixed)

Shipped in beta10 release.