digdir / designsystemet

Designsystemet
https://designsystemet.no
MIT License
81 stars 37 forks source link

Use data attribute to choose component colors #2507

Open mimarz opened 1 month ago

mimarz commented 1 month ago

TLDR; Lift semantic color definition to a higher level and have components only use a generic --ds-color-* variable. Which semantic color i used for a component is then defined by the user using data-attributes ([data-color="accent|neutral|my-color"]). Users are free to add any number of colors and name them accordingly to their semantic needs.

Example code

/** In generated theme file from design-tokens */
:root, [data-ds-color-mode="light"] {

/** We probably need some default colors here. Neutral? */
--ds-color-surface-default: var(--ds-color-neutral-3);

[data-color="accent"] {
--ds-color-surface-default: var(--ds-color-accent-3);
}

[data-color="neutral"] {
--ds-color-surface-default: var(--ds-color-neutral-3);

}

[data-color="my-color"] {
--ds-color-surface-default: var(--ds-color-my-color-3);

}
}

/** In Button CSS */

.ds-button {
  background-color: var(--ds-color-surface-default);
}

<Button data-color="my-color" >…</Button>
<Accordion data-color="my-color">…</Accordion>

WIP Notes

mimarz commented 1 month ago

This needs to be done after #2287

unekinn commented 3 weeks ago

@mimarz & @Febakke Here's my step-by-step plan to implement this, based on my existing proof of concept. If this seems okay, should I add these as separate issues and add them to the board, or is it enough to track it here?

mimarz commented 1 week ago

After demo today we suggest the following.

Febakke commented 5 days ago

Figma testing with @unekinn

Color scales that will be included as Figma variables

Two new modes

Obs We need to document that if users use the color scales from semantic that are used in main and support directly they might have issues if the change names later. For instance if you set background color to primary-background instead of main-background. We have experienced that Token Studio might not always be able to make Figma understand that they are the same scales. The consequence is dead token references in Figma

Figma tasks