davips96 / theme-importer-poc

Quick POC and demo for importing themes based on env vars with Vite.
1 stars 1 forks source link

Optimize ThemeImporter Implementation #4

Open davips96 opened 1 month ago

davips96 commented 1 month ago

In the initial POC, ThemeImporter was used to add CSS files to UI elements depending on the theme. While this approach was helpful to validate the POC, it doesn't scale and will require each UI element to have additional code to implement the styles. We could instead move our themed UI styles to their own directory as common CSS variables, then use these CSS variables in each component's dedicated style files. At build time, ThemeImporter would then select the appropriate variables file to load into the app.

We might encounter a situation where a theme requires a custom style. ThemeImporter could also be used to modify CSS files before React transformations occur. We could create a fake CSS @ line (forgot what it's called tbh) that are included only if the theme matches.

This is an example of this functionality:

.my-element-class {
  my-property: my-css-var;
}

@theme (theme-1) {
  .my-element-class {
    other-property: css-value;
  }
}

Using this, ThemeImporter can detect the @theme and remove the line and squiggly brackets to make the class override take effect. We can also add error handling for property overrides in the class, because in-css-themes should never override the properties of the css class this way, it should be done through CSS variables.

ACs:

davips96 commented 1 month ago

I think CSS might already have @theme, so we can use another name, but the idea is the same.