-
Notifications
You must be signed in to change notification settings - Fork 32
feat(ThemeProvider): Adds GamutThemeProvider component and initial Global styles #1444
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
348b649
Add base serialization and theme provider
codecaaron af8d6c7
Use our provider
codecaaron db86d30
Whoops
codecaaron 512e32c
Import order
codecaaron 19a61cb
Fix it again
codecaaron a9314ff
Refactor types and simplify
codecaaron c504a03
even simpler
codecaaron 4aeaedb
Final Refactor
codecaaron 5b53899
Don't update random system things
codecaaron 6ff2c68
Add this
codecaaron a98f6fa
What the heck
codecaaron b54a3a9
Split to different files
codecaaron e1a95d7
Export variable creation
codecaaron 29590a7
reorganize
codecaaron 5f2f455
Rename
codecaaron 4ac7094
Less nesting
codecaaron c3d8394
Merge branch 'main' into ar-serialized-vars
codecaaron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,14 @@ | ||
| import '@emotion/react'; | ||
|
|
||
| import { Theme as GamutTheme } from './theme'; | ||
| import { theme } from './theme'; | ||
|
|
||
| export * from './cache'; | ||
| export * from './variables'; | ||
| export * from './utilities'; | ||
| export * from './system'; | ||
| export * from './theme'; | ||
|
|
||
| export type ThemeShape = typeof theme; | ||
|
|
||
| declare module '@emotion/react' { | ||
| export interface Theme extends GamutTheme {} | ||
| export interface Theme extends ThemeShape {} | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { css, Global, ThemeProvider } from '@emotion/react'; | ||
| import React from 'react'; | ||
|
|
||
| import { theme as rawTheme } from './theme'; | ||
| import { createThemeVariables } from './utils/createThemeVariables'; | ||
|
|
||
| export const { theme, cssVariables } = createThemeVariables(rawTheme, [ | ||
| 'elements', | ||
| ]); | ||
|
|
||
| export const GamutThemeProvider: React.FC = ({ children }) => { | ||
| return ( | ||
| <ThemeProvider theme={theme}> | ||
| <Global styles={css({ ':root': cssVariables })} /> | ||
| {children} | ||
| </ThemeProvider> | ||
| ); | ||
| }; |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export * from './GamutThemeProvider'; | ||
| export * from './props'; | ||
| export { createVariables } from './utils/createVariables'; | ||
| export * from './utils/shouldForwardProp'; |
This file contains hidden or 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
This file contains hidden or 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
75 changes: 75 additions & 0 deletions
75
packages/gamut-styles/src/theme/utils/createThemeVariables.ts
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { AbstractTheme } from '@codecademy/gamut-system'; | ||
| import { CSSObject } from '@emotion/react'; | ||
| import { hasIn, isObject, mapKeys, mapValues, merge } from 'lodash'; | ||
|
|
||
| import { createVariables } from './createVariables'; | ||
|
|
||
| /** | ||
| * Returns an type of any object with { key: 'var(--key) } | ||
| */ | ||
| export type KeyAsVariable<T extends Record<string, any>> = { | ||
| [V in keyof T]: `var(--${Extract<V, string>})`; | ||
| }; | ||
|
|
||
| /** | ||
| * Updates the theme type with the correct new values of css variable references | ||
| */ | ||
| export type ThemeWithVariables< | ||
| Theme extends AbstractTheme, | ||
| VariableKeys extends (keyof Theme)[] | ||
| > = { | ||
| [Key in keyof Theme]: Key extends VariableKeys[number] | ||
| ? KeyAsVariable<Theme[Key]> | ||
| : Theme[Key]; | ||
| }; | ||
|
|
||
| export interface CreateThemeVars { | ||
| <Theme extends Required<AbstractTheme>, VariableKeys extends (keyof Theme)[]>( | ||
| theme: Theme, | ||
| keys: VariableKeys | ||
| ): { | ||
| cssVariables: CSSObject; | ||
| theme: ThemeWithVariables<Theme, VariableKeys>; | ||
| }; | ||
| } | ||
|
|
||
| const isBreakpoint = ( | ||
| key: string, | ||
| breakpoints: Required<AbstractTheme>['breakpoints'] | ||
| ): key is keyof Required<AbstractTheme>['breakpoints'] => | ||
| Object.keys(breakpoints).includes(key); | ||
|
|
||
| export const createThemeVariables: CreateThemeVars = (theme, keys) => { | ||
| // Create an empty theme to merge with the base theme object | ||
| const updatedTheme = { ...theme }; | ||
| // Treat all CSS Variables as a plain emotion CSSObject to be added to. | ||
| const cssVariables: CSSObject = {}; | ||
|
|
||
| keys.forEach((key) => { | ||
| const tokensToSerialize = { ...theme[key] }; | ||
| // Update the theme object with the new tokens | ||
| for (const variable in tokensToSerialize) { | ||
| if (hasIn(tokensToSerialize, variable)) { | ||
| const variableReference = `var(--${variable})`; | ||
| updatedTheme[key][variable] = variableReference; | ||
| } | ||
| } | ||
| // Replace breakpoint aliases with the actual selector | ||
| const replacedBreakpointAliases = mapValues(tokensToSerialize, (val) => { | ||
| if (isObject(val)) { | ||
| return mapKeys(val, (val, key) => { | ||
| return isBreakpoint(key, theme.breakpoints) | ||
| ? theme.breakpoints[key] | ||
| : key; | ||
| }); | ||
| } | ||
|
|
||
| return val; | ||
| }); | ||
|
|
||
| // Create the variables and merge with the rest of the vars | ||
| merge(cssVariables, createVariables(replacedBreakpointAliases)); | ||
| }); | ||
|
|
||
| return { cssVariables, theme: updatedTheme }; | ||
| }; |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { CSSObject } from '@emotion/react'; | ||
| import { hasIn, merge } from 'lodash'; | ||
|
|
||
| export const createVariables = ( | ||
| tokens: Record<string, string | number | CSSObject> | ||
| ) => { | ||
| const cssVariables: CSSObject = {}; | ||
|
|
||
| for (const variable in tokens) { | ||
| if (!hasIn(tokens, variable)) continue; | ||
|
|
||
| const varName = `--${variable}`; | ||
| const valuesToRegister = tokens[variable]; | ||
|
|
||
| // For all variables in the theme scale add theme to the resulting CSS Object | ||
| switch (typeof valuesToRegister) { | ||
| // If the value is primitive just add it as is to the returned vars css object | ||
| case 'number': | ||
| case 'string': | ||
| cssVariables[varName] = valuesToRegister; | ||
| break; | ||
| // If the value is an object then attempt to parse it as a resposnive property | ||
| case 'object': | ||
| const { base, ...rest } = valuesToRegister; | ||
|
|
||
| // If base key is defined add it to the root values | ||
| if (base) { | ||
| cssVariables[varName] = base; | ||
| } | ||
|
|
||
| // If there are remaining selectors / queries that override the root value add them to style object | ||
| const selectors = Object.keys(rest); | ||
| if (selectors) { | ||
| const overridesBySelector: CSSObject = {}; | ||
| selectors.forEach((selector) => { | ||
| overridesBySelector[selector] = { | ||
| [varName]: valuesToRegister[selector], | ||
| }; | ||
| }); | ||
|
|
||
| // Merge with the base object. | ||
| merge(cssVariables, overridesBySelector); | ||
| } | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| } | ||
| return cssVariables; | ||
| }; |
13 changes: 13 additions & 0 deletions
13
packages/gamut-styles/src/theme/utils/shouldForwardProp.ts
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import isPropValid from '@emotion/is-prop-valid'; | ||
|
|
||
| import { properties } from '../props'; | ||
|
|
||
| const allProps = Object.keys(properties).reduce<string[]>( | ||
| (carry, prop: keyof typeof properties) => { | ||
| return [...carry, ...properties[prop].propNames]; | ||
| }, | ||
| [] | ||
| ); | ||
|
|
||
| export const shouldForwardProp = (prop: string) => | ||
| isPropValid(prop) && !allProps.includes(prop); |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export const elements = { | ||
| headerHeight: { base: '4rem', md: '5rem' }, | ||
| } as const; |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
packages/styleguide/.storybook/components/PropsTable/constants.ts
This file contains hidden or 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
This file contains hidden or 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
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.
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.
This removes new line characters from gunking up the strings. This is a no op.
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.
imo, a small comment/warning about that would be helpful for future us's