This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
github-actions
bot
added
Accordion
Changes to the accordion component
Button
Changes to the button component
Checkbox
Changes to the checkbox component
Choice card
Changes to the choice card component
Footer
Changes to the footer component
foundations
Affects @guardian/source-foundations
Label
Changes to the label component
Link
Changes to the link component
Radio
Changes to the radio button component
Select
Changes to the select component
Text input
Changes to the text input component
User feedback
Changes to user feedback components
labels
Nov 29, 2021
ob6160
added
Work in progress ⚙️
We're still working on it. Please don't merge!
and removed
🥒 WiP
labels
Nov 29, 2021
ob6160
removed
the
Work in progress ⚙️
We're still working on it. Please don't merge!
label
Nov 29, 2021
SiAdcock
reviewed
Dec 2, 2021
SiAdcock
reviewed
Dec 2, 2021
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.
Thanks @ob6160, this is a much tidier pattern: it's easier to read, sticks in your head and is simpler to maintain. Awesome work! 🏅
I really enjoyed reading the PR description too, an absolute gem 💎
I have one small comment, but other than that I'd say it's good to go!
@ob6160 wins Jimmy’s Award for Best Described and Most Informative PRs 2021! Congratulations on behalf of The Committee 🥇 |
…adio, theme.select, theme.textInput and theme.userFeedback
ob6160
force-pushed
the
ob/refactor-theming-functions
branch
2 times, most recently
from
December 3, 2021 16:12
33468a7
to
cd490d4
Compare
SiAdcock
approved these changes
Dec 3, 2021
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.
Amazing, thanks again @ob6160 💎 💎 💎
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
Accordion
Changes to the accordion component
Button
Changes to the button component
Checkbox
Changes to the checkbox component
Choice card
Changes to the choice card component
Footer
Changes to the footer component
foundations
Affects @guardian/source-foundations
Label
Changes to the label component
Link
Changes to the link component
Radio
Changes to the radio button component
Select
Changes to the select component
Text input
Changes to the text input component
User feedback
Changes to user feedback components
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Setting the scene 🖼
Across Source, we use some nifty individual component-level theming functions.
They accept the
theme
object passed to us from emotion. They then destructure that object into the single component level theme object that it needs. If we pass inundefined
, a default theming object is returned.Here's an example of how that works right now to help illustrate:
.. and the consumer uses it like this:
As you might have spotted, this implementation comes with the caveat that we must check that
theme.accordion
is defined before we pass intheme
.This makes a lot of sense! It's saying, "if accordion is defined on our
theme
object, go ahead and passtheme
in"... it then follows that: "if accordion is not defined, pass in
undefined
and fall back on the defaultaccordionDefault
value fortheme.accordion
".To conclude this bit: This logic handily avoids circumstances where
theme.accordion
is not defined on the custom theme. This happens often, because not everybody wants to customise everything that Source offers!So what needs doing here? 🔧
Simon 👨🎨 , our developer, is sitting down to make a shiny new variation of the Accordion component. Simon sets about using the
accordion
theming function defined above like this:As far as Simon is concerned, everything is fine.. it all makes sense! The
accordion
method should destructure the theme into theaccordion
theming object and fall back to the default:accordionDefault
if it's not found.....
even the type checking passes!
...
Everything is looking great, this component will be ready to ship in no time ! 🥇 ☕️
Sadly, without the
theme.accordion && theme
check, poor Simon could easily hit a runtime error.Let's look at why this is!
The error we just described will happen if
accordion
has not been defined on the users' custom theme. In this scenario, theaccordion
method is unable to fall back on its default value, because an object is being passed instead of undefined.To illustrate, here's an example of a common case that could happen at runtime:
...culminating in this delightful error, ruining Simons' otherwise tranquil Monday morning:
A Proposed Solution 🚙
The solution proposed here reworks our theming functions a little, making a few key changes. Instead of accepting the whole theme object, they have been refactored so that they only accept the theming object that they need to render.
With this approach,
accordion
only accepts thetheme.accordion
object ORundefined
(in that case falling back toaccordionDefault.accordion
):and Simon is able to call the
accordion
theming function like this:This approach leaves no room for ambiguity!
theme.accordion
will be either defined or undefined 1This is great, it means that we can't pass in a
theme
wheretheme.accordion
could possibly beundefined
as we could before without the type checker erroring. 🎉So, following this approach, the core changes made in this pull request change all of our theming functions to accept only the single theming object that is relevant to them as an optional parameter, which defaults to the default already defined.
Notes
This builds upon #1161
Footnotes
This is supported thanks to this change: https://github.com/guardian/source/pull/1195/files#diff-06f78fe3b5cad62762cc5b85c0f25c385f5065842bb33f4b3b44de3066e4b57fR17-R29 which builds upon Add as const type assertion on palette #1161 ↩