-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add theming feature to elements (#3821)
* feat: add theming to feature to elements * fix: undefined property * fix: ignore flow check * fix: simplify import path * feat: remove theme generator docs --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
6655372
commit f39a65e
Showing
14 changed files
with
154 additions
and
289 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import useCustomTheming from './useCustomTheming'; | ||
import { ThemingProps } from './types'; | ||
|
||
const ThemingStyles = ({ selector, theme }: ThemingProps) => { | ||
useCustomTheming({ selector, theme }); | ||
|
||
return null; | ||
}; | ||
|
||
export default ThemingStyles; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { convertTokensToCustomProperties } from '../utils'; | ||
|
||
describe('elements/common/theming/utils', () => { | ||
describe('convertTokensToCustomProperties()', () => { | ||
test('returns correct mapping of tokens to custom properties', () => { | ||
const tokens = { | ||
Label: { | ||
Bold: { | ||
fontSize: '0.625rem', | ||
fontWeight: '700', | ||
letterSpacing: '0.0375rem', | ||
lineHeight: '1rem', | ||
paragraphSpacing: '0', | ||
textCase: 'none', | ||
textDecoration: 'none', | ||
}, | ||
Default: { | ||
fontSize: '0.625rem', | ||
fontWeight: '400', | ||
letterSpacing: '0.0375rem', | ||
lineHeight: '1rem', | ||
paragraphSpacing: '0', | ||
textCase: 'none', | ||
textDecoration: 'none', | ||
}, | ||
}, | ||
}; | ||
const output = { | ||
'--label-bold-font-size': '0.625rem', | ||
'--label-bold-font-weight': '700', | ||
'--label-bold-letter-spacing': '0.0375rem', | ||
'--label-bold-line-height': '1rem', | ||
'--label-bold-paragraph-spacing': '0', | ||
'--label-bold-text-case': 'none', | ||
'--label-bold-text-decoration': 'none', | ||
'--label-default-font-size': '0.625rem', | ||
'--label-default-font-weight': '400', | ||
'--label-default-letter-spacing': '0.0375rem', | ||
'--label-default-line-height': '1rem', | ||
'--label-default-paragraph-spacing': '0', | ||
'--label-default-text-case': 'none', | ||
'--label-default-text-decoration': 'none', | ||
}; | ||
const result = convertTokensToCustomProperties(tokens); | ||
expect(result).toEqual(output); | ||
}); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default } from './ThemingStyles'; | ||
export * from './types'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export interface Theme { | ||
tokens?: Record<string, unknown>; | ||
} | ||
|
||
export interface ThemingProps { | ||
selector?: string; | ||
theme?: Theme; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useEffect } from 'react'; | ||
import { convertTokensToCustomProperties } from './utils'; | ||
import { ThemingProps } from './types'; | ||
|
||
const useCustomTheming = ({ selector, theme = {} }: ThemingProps) => { | ||
const { tokens } = theme; | ||
|
||
const customProperties = convertTokensToCustomProperties(tokens); | ||
const styles = Object.entries(customProperties) | ||
.map(([token, value]) => `${token}: ${value}`) | ||
.join(';'); | ||
|
||
useEffect(() => { | ||
const styleEl = document.createElement('style'); | ||
document.head.appendChild(styleEl); | ||
|
||
styleEl.sheet.insertRule(`${selector ?? ':root'} { ${styles} }`); | ||
|
||
return () => { | ||
document.head.removeChild(styleEl); | ||
}; | ||
}, [selector, styles]); | ||
}; | ||
|
||
export default useCustomTheming; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import isObject from 'lodash/isObject'; | ||
import kebabCase from 'lodash/kebabCase'; | ||
|
||
const convertTokensToCustomProperties = (tokens = {}, prefix = '') => { | ||
const customProperties = {}; | ||
|
||
Object.entries(tokens).forEach(([level, value]) => { | ||
const levelName = `${prefix}${kebabCase(level)}`; | ||
|
||
if (isObject(value)) { | ||
const properties = convertTokensToCustomProperties(value, `${levelName}-`); | ||
Object.entries(properties).forEach(([tokenName, tokenValue]) => { | ||
customProperties[tokenName] = tokenValue; | ||
}); | ||
} else { | ||
customProperties[`--${levelName}`] = value; | ||
} | ||
}); | ||
|
||
return customProperties; | ||
}; | ||
|
||
export { convertTokensToCustomProperties }; |
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
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
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
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
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
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
Oops, something went wrong.