vitonsky / react-elegant-ui

Elegant UI components, made by BEM best practices for react
https://vitonsky.github.io/react-elegant-ui/
Apache License 2.0
2 stars 0 forks source link

Research a technologies for theming with static typing #270

Open vitonsky opened 10 months ago

vitonsky commented 10 months ago

Currently we use too complex theming approach. To update theme, user must do:

The problems

The potential solutions

CSS in JS way

https://github.com/vanilla-extract-css/vanilla-extract project uses this approach to define styles

// styles.css.ts

import { createTheme, style } from '@vanilla-extract/css';

export const [themeClass, vars] = createTheme({
  color: {
    brand: 'blue'
  },
  font: {
    body: 'arial'
  }
});

export const exampleStyle = style({
  backgroundColor: vars.color.brand,
  fontFamily: vars.font.body,
  color: 'white',
  padding: 10
});

We could try to use the same approach with createTheme builder, to generate a CSS properties names and theme class name.

Let's imagine that vars.color.brand will return --optionalProjectPrefix-color-brand or var(--optionalProjectPrefix-color-brand). Now we can bind a values defined in TS files to a values in CSS theme, because CSS properties names generates automatically and in case we will try use non defined values, a TypeScript compiller may catch us and generate error.

To use this approach we have to provide JS variables into CSS, so we have to use any CSS-in-JS library. We could use a https://github.com/callstack/linaria with zero runtime. We also may generate theme class with this tool.

The problem with derived styles

Not clear how to create derived styles for UI components.

We may want to have a button with a background color based on accent color but with changed HUE. How to implement it?