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 basic theming to the @wordpress/components package #44116

Open
10 of 23 tasks
ciampo opened this issue Sep 13, 2022 · 4 comments
Open
10 of 23 tasks

Add basic theming to the @wordpress/components package #44116

ciampo opened this issue Sep 13, 2022 · 4 comments
Assignees
Labels
[Feature] UI Components Impacts or related to the UI component system [Package] Components /packages/components [Type] Enhancement A suggestion for improvement.

Comments

@ciampo
Copy link
Contributor

ciampo commented Sep 13, 2022

What

Allow the components package to be themed by its consumers

How

The components package would allow theming by exposing a few CSS variables (e.g "primary color", "text color", "background color", "border radius"...). The package would have default values for each variable, but any consumer of the package would be able to override those values.

These variables would be applied to all components in the package, regardless of how they are used within the editor. But given the nature of CSS and CSS variables, different overrides could be applied in different DOM subtrees (e.g the sidebar wrapper could set a different background colors from the navigation panel, etc etc).

Having the package expose its own variables (instead of using gutenberg-specific variables) would help to decouple the package from the rest of the editor, and make it more reusable.

Things to discuss / refine:

  • Which variables should we exposed? Ideally for the first iteration, as least as possible
  • How do we document these variables?
  • Should the package have any concepts of dark/light themes? (personally I don't think so, as that would being a lot of extra complexity).

Action plan

Context

@ciampo ciampo added [Feature] UI Components Impacts or related to the UI component system [Package] Components /packages/components labels Sep 13, 2022
@mirka
Copy link
Member

mirka commented Sep 13, 2022

I'm thinking of starting with something like (in order of priority):

  1. accent color
  2. background color
  3. foreground color

Very simple, three official variables. Everything else (borders, shadows, darkened accent colors, etc.) we can try and automatically generate internally based on the given colors. It might be possible that we can manage without even exposing the foreground color (i.e. text), because we could just infer a black or white scheme based on the background color contrast.

The approach diverges on how to handle the auto-generation. We could calculate everything in CSS only, by enforcing consumers to set the CSS variable in distinct RGB or HSL values (some variation on this technique). But I think it would be easier for everyone if we added a nestable theme provider component to handle the calculations in JS:

<ThemeProvider accent="#007cba" background="#1e1e1e">
  <MyHeader />
  <ThemeProvider background="#fff">
    <MyEditor />
  </ThemeProvider>
</ThemeProvider>

This way we can more easily do contrast-based manipulation with colord, and consumers can just provide hex colors if they want. The theme provider will calculate all the colors in the scheme and inject it as CSS variables.

I feel like this would cover most sensible use cases. And the very hardcore consumer could still override the generated CSS variables at the CSS level if they wanted to get more granular.

@ciampo
Copy link
Contributor Author

ciampo commented Sep 13, 2022

I'm thinking of starting with something like [...] Very simple, three official variables

Agreed. The simpler we start, the better IMO.

the auto-generation

Based on personal experience with implementing a similar system, I'm a bit skeptical that we'll manage to find a formula that auto-generates colors in a way that is pleasing and well "proportioned" to the human eye for all possible colors — but we should definitely give it a shot, see where we get, and iterate.

We could calculate everything in CSS only [...] But I think it would be easier for everyone if we added a nestable theme provider component to handle the calculations in JS

I liked the idea of using vanilla CSS variables, since it feels cleaner and "closer to the metal" (i.e. using CSS APIs for styling components).

At the same time, I agree that using React context will give us extra flexibility in handling and transforming the values, while still preserving the same "cascade" mechanism that we would have with CSS

@mirka
Copy link
Member

mirka commented Sep 13, 2022

At the same time, I agree that using React context will give us extra flexibility in handling and transforming the values, while still preserving the same "cascade" mechanism that we would have with CSS

I was thinking we don't even need context, just render a ThemeProvider as <div class="asdf"> and inject the generated CSS variables as CSS .asdf { --color-foo: #eee; --color-bar: #fff; } etc.

@ciampo
Copy link
Contributor Author

ciampo commented Oct 5, 2022

Edit: the plane of action originally written in this comment has been moved to the issue's description

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] UI Components Impacts or related to the UI component system [Package] Components /packages/components [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

3 participants