Drupal 10 has dropped support for Internet Explorer which means CSS custom properties are fully supported by all browsers that Drupal supports.
Passing backend settings to frontend is quite common task when you need content managers to be able to change site appearance.
Currently it is typically implemented through preprocess variables which is kind of tedious.
Proposed solution
Support attaching CSS properties same way as JS settings.
'#attached': [
'drupalProperties': [
':root': [
'bg-color': 'pink',
],
],
],
On FE side it should look like this.
<style>
:root {--bg-color: pink}
</style>
Issue fork drupal-3296202
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
Comment #2
chi commentedComment #3
chi commentedComment #4
andypostLooks like duplicate of #788332: Provide a parser for THEME.colors.yml
Comment #5
nod_That's a pretty neat idea. I don't think it's a duplicate, this is not just about colors.
I could probably be used by Olivero to simplify the color customization code that uses preprocess to do that in
olivero_preprocess_htmlFew things:
--in the names to avoid awkward situations where someone is searching for a variable name and doesn't know--should be removed to find a match in the codebase.<style>tag directly might cause some problems down the line for #2513356: Add a default Content-Security-Policy and clickjacking defence to core, with JS we went around it by changing the type attribute of the script tag and having code evaluate that at runtime. This is not possible/wanted here so that's one thing to keep in minddrupalPropertiesis not ideal, since this is not a Drupal thing, it's purely a CSS thing (see point above), so it might make sense to have acustomPropertieskey under thecsskey? It's different than what we do with JS settings but this CSS feature will not have Drupal specific code to evaluate it. On the other hand keeping that at the root might make some internal code and the whole alter pipeline easier to deal with.Despite all that, I'm overall +1 to the proposal :)
Comment #7
chi commentedRe #5-4. I am not happy with
drupalPropertiesname either. Furthermore, I thinkdrupalSettingsis not accurate name as well because it's used not just for settings but for any data that needs to be passed from backend to frontend.Comment #8
mherchelThis is really interesting. I really like this!
Agree. We currently inject the styles inline via preprocess.
Agree. Call it
cssCustomProperties, or have thecustomPropertiesunder thecsskey as @nod_ suggested.Comment #9
jwilson3+1 I love this. One thing that occurred to me is that the approach will need to gracefully consider the CSS cascade, and allow devs and themers to adjust the weight at which a group of CSS properties are injected into the page to determine conflict resolution (last item in the cascade wins). This would be similar to (maybe same as?) the SMACSS organization technique employed by core.
A contrived example would be to imagine (for the pure lulz of it):
root: { --toolbar-height: 39px;},:root { --toolbar-height: 2rem; },:root { --toolbar-height: calc(var(--base-font-size) * 2) },:root { --toolbar-height: 50px;}Revenge of the custom variables.
Comment #10
chi commentedI think variables from modules should go first. That would match the order of CSS files.
And to ajust the weight of a variable the one could increase CSS specificity. Something link this.