-
Notifications
You must be signed in to change notification settings - Fork 77
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 CSS Custom Properties to Protocol Themes #841
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What a long PR!
You did such a great job getting this done with almost zero nits and fixes!
Most of the nits I found were spelling errors in the markdown files, though.
GOOD JOB!!!!!!!!!!!!! 🕌
r+wc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 to echo Reem, this is a whole lot of great work. Really clean enhancement.
I did a code review (with a few todo notes to myself for tomorrow) and will do the local testing review tomorrow. Thought I'd put those comments out today for async discussion.
@@ -129,7 +169,7 @@ | |||
.mzp-c-split-body { | |||
@include border-box; | |||
@include bidi(((float, left, right),)); | |||
padding: 0 (get-theme('h-grid-lg') * 0.5); | |||
padding: 0 ($h-grid-lg * 0.5); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
curious: how is this working without calc
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol! no clue, going to add calc in to just be extra sure it works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤷♀️ it works in both cases, no idea why in the first, seeing calc
feels better for consistency 👍
@@ -6,6 +6,69 @@ | |||
// themes. Mixins and functions can draw from these variable maps, swapping to | |||
// a different map based on the value of the global $brand-theme variable. | |||
|
|||
// Default "theme" that will be served to legacy browsers - IE 11 etc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 great comment
@extend %mozilla-theme; | ||
@extend %mozilla-type-scale; | ||
|
||
@if $type-scale == 'condensed' { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏 I was thinking all the custom properties would be set on the :root
inside the individual theme files but that would make the type scale logic trickier. This is an effective, readable way of breaking them up.
@@ -44,5 +44,5 @@ $type-scale: 'standard' !default; | |||
// Import all the things! | |||
@import './node_modules/@mozilla-protocol/tokens/dist/index'; | |||
@import 'functions'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
curious: are we keeping the get-theme
and type-scale
functions in code for a while? I don't see them deleted (and I guess if they are not deleted, we don't have to make this a breaking change?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't want to delete them, since it would introduce a breaking change - but if we should go all out and remove them here I'd be happy to do so. @craigcook
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy to leave as-is in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be worth keeping them for a while and adding a deprecation notice like we've done with some components, to give some transition time for anyone using those functions. But I'm not sure how widely they're used outside of Protocol itself... we only use get-theme
in a few places in bedrock.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wonder how difficult it would be to write a migration script to do a search and replace (potentially with a flag setting for optional support query wrapping):
color: get-theme('title-text-color');
/* replace only */
color: var(--title-text-color);
@include bidi(((padding-left, get-theme('h-grid-sm'), padding-right, get-theme('h-grid-sm')),))
/* replace & use support query */
@support (--css: variables) {
@include bidi(((padding-left, var(--h-grid-sm), padding-right, var(--h-grid-sm)),))
}
I'm on the fence about including the support queries in end user code. I think it's important for clarity in Protocol, but since we've set all the IE defaults already, it should be safe for users to directly set custom property values on their style rules. Browser parsers that don't support custom properties will ignore those rules and use the Protocol defaults.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of minor bugs noted in the previous review
one more blocking issue from local testing of Protocol docs: the custom properties are not set in demos, i.e. http://localhost:3000/components/detail/paragraphs
I do think this counts as a breaking change if end users need to add a themes import.
If we want to try avoiding the duplicated :root
rule declarations we ran into with @import
, I started a POC here: f5b656b#diff-6da31b8d13d36754703085ab3c9ae106a4d80abb5f51e3a5db324042d4a9e63fR6
I ran into the "can't locate protocol tokens" error when trying to run a bedrock test. I think you had a solution for this, but I can't remember it. Happy to help try and debug that if we think it's worth pursuing.
|
||
// colors | ||
--background-color-tertiary-inverse: #{$color-dark-gray-40}; | ||
--background-color-tertiary: #{$color-marketing-gray-20}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (blocking): mismatched custom property value with original moz theme value
--background-color-tertiary: #{$color-marketing-gray-20}; | |
--background-color-tertiary: #{$color-light-gray-30}; |
https://github.com/mozilla/protocol/blob/main/assets/sass/protocol/includes/_themes.scss#L71
@maureenlholland made some fixes and tested in protocol docs and bedrock and things looked good to me |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏 👏 👏 r+wc (mozilla tertiary background value is out of sync with prod)
My two cents (I think we discussed similar before) is we should bundle this change with a sass module update and be loud about a breaking release that brings us up to date with sass internally and creates new theming flexibility for Protocol users externally
|
||
// colors | ||
--background-color-tertiary-inverse: #{$color-dark-gray-40}; | ||
--background-color-tertiary: #{$color-marketing-gray-20}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can have different values for different brands. | ||
the Mozilla and Firefox brands. Themes work by declaring a set of CSS Custom Properties | ||
on the document's root element (`:root`) in `/includes/_themes.scss` and are retrieved | ||
using the `var()` function. The custom properties will be updated depending on what theme is declared. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(non-blocking, just noting for a potential future docs update)
This is a very exciting change because we're not compiling away sass variables with internal functions anymore. We're using CSS inheritance. This allows end users more flexibility if the pre-defined Protocol themes don't apply (i.e. they are early days in the branding journey and want to experiment before creating an official Protocol theme).
We could provide a list of themeable CSS properties and demo how a user can define their own theme by declaring these properties after the theme declarations
/* user-lib.scss */
@use '~@mozilla-protocol/core/protocol/css/includes/themes';
/* user-defined theme values */
:root {
--background-color-inverse: #{$color-green-80};
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
such a good idea!
Agree on bundling this with use and forward updates! Thanks for all of your help sifting through this @maureenlholland @reemhamz |
as discussed internally, waiting on a few non-breaking changes to create one more published version before creating a breaking change version |
Had to also include an import in preview.scss since the demo's are inside a nested iFrame
a55c80a
to
074fda5
Compare
074fda5
to
ea2db68
Compare
ea2db68
to
edeb79b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't gone as far as testing the bedrock changes, but it looks like things aren't quite working right on the protocol docs pages. I'm happy to take another look once things are working there.
I think we should also do some testing in legacy browsers (in bedrock) to make sure that the default theming is working as expected.
dca3ec9
to
816ddd4
Compare
@alexgibson - I fixed the doc site issue you mentioned (i think the theme import was removed during rebase). I will test thoroughly in legacy browsers with bedrock. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code changes here look good to me and work well locally, nice work! r+
I do have one question though on something I'm not 100% clear on: at this point are we saying get-theme()
and type-scale()
should no longer be used?
If so, how are we planning to end of life their use? There are a couple of options I can think of:
- We could warn people that they are deprecated and provide a migration guide, later removing the functions in a future major version bump?
- We could remove the functions now and provide a guide, forcing a hard requirement for sites to remove their use of the functions before this next update.
Looking at mozilla/bedrock#13146 for example, I still see many calls to get-theme()
, throughout bedrock. Does that mean there's likely pages using a mixture of both the new and old method for theming?
@alexgibson - I think option two makes the most sense. And I will write up a migration guide in the changelog that we can add when we push the next major version. For bedrock, I would like to remove the refrences to the get-theme and get-type functions - just didn't do it in that PR. |
Sounds good @nathan-barrett 👍 |
Description
This PR updates the themes in Protocol to use CSS Custom Properties over sass variable maps. To support legacy browsers, sass variables were created that will be used as a fallback. I've also added my notes to the bottom of the description if anyone needs a refresher
CHANGELOG.md
.Testing
To test that these changes are working I suggest using
npm link
(article on npm-link) since we haven't pushed any of the changes in protocol to the npm package. Directions below:To test this works: (you will need a local version of both protocol and bedrock to test these changes)
In local protocol directory
npm run build-package
(this will build the package)cd package && npm link
In local bedrock
origin/protocol-update-test
npm link @mozilla-protocol/core
(This will use the linked protocol package that was just built instead of using the NPM package)@import '~@mozilla-protocol/core/protocol/css/includes/themes'; to
protocol-mozilla.scssand
protocol-firefox.scss`npm run start
Notes on Custom Properties
Blog Posts
How to create better themes with CSS variables - LogRocket Blog
Theming with CSS variables
Color Theme Switcher
Why we prefer CSS Custom Properties to SASS variables | CodyHouse
In SCSS:
In CSS:
Differences between the two:
Custom Properties can be accessed at run time and stay in CSS code using
--variable-name
syntax that browsers recognizeyou can preview all of the CSS variables in the browser
CSS custom properties can be accessed in JavaScript
CSS custom properties can accept a fallback value:
var(--variable-name, fallbackValue)
Custom Properties are scoped to an element, most commonly the
:root
elementTheming with Custom Properties
Currently in protocol we use this function to retrieve variables depending on the theme:
With Custom properties, the variables would be set up like this:
When used with
#{}
removes quotation marks from quoted strings, it’s sometimes necessary to wrap them in themeta.inspect()
function to preserve quotes: