-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(theming): add global style ability (#5825)
Co-authored-by: Caleb Pollman <[email protected]>
- Loading branch information
1 parent
c855f47
commit 3a677a1
Showing
14 changed files
with
190 additions
and
86 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,18 @@ | ||
--- | ||
"@aws-amplify/ui-react": patch | ||
"@aws-amplify/ui": patch | ||
--- | ||
|
||
feat(theming): add global style ability (experimental) | ||
|
||
Adding the ability to create global styles with the experimental theming APIs | ||
|
||
```jsx | ||
<GlobalStyle styles={{ | ||
'body': { | ||
backgroundColor: 'purple' | ||
// supports design tokens! | ||
color: theme.tokens.colors.font.primary | ||
} | ||
}} /> | ||
``` |
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
32 changes: 32 additions & 0 deletions
32
packages/react/src/components/ThemeProvider/GlobalStyle.tsx
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,32 @@ | ||
import * as React from 'react'; | ||
import { createGlobalCSS } from '@aws-amplify/ui'; | ||
import { Style } from './Style'; | ||
|
||
interface GlobalStyleProps extends React.ComponentProps<'style'> { | ||
/** | ||
* Provide a server generated nonce which matches your CSP `style-src` rule. | ||
* This will be attached to the generated <style> tag. | ||
* @see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/style-src | ||
*/ | ||
nonce?: string; | ||
styles: Parameters<typeof createGlobalCSS>[0]; | ||
} | ||
|
||
/** | ||
* @experimental | ||
* [📖 Docs](https://ui.docs.amplify.aws/react/components/theme) | ||
*/ | ||
export const GlobalStyle = ({ | ||
styles, | ||
...rest | ||
}: GlobalStyleProps): JSX.Element | null => { | ||
if (!styles) { | ||
return null; | ||
} | ||
|
||
const cssText = createGlobalCSS(styles); | ||
|
||
return <Style {...rest} cssText={cssText} />; | ||
}; | ||
|
||
GlobalStyle.displayName = 'GlobalStyle'; |
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
29 changes: 29 additions & 0 deletions
29
packages/react/src/components/ThemeProvider/__tests__/GlobalStyle.test.tsx
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,29 @@ | ||
import { render } from '@testing-library/react'; | ||
import * as React from 'react'; | ||
|
||
import { GlobalStyle } from '../GlobalStyle'; | ||
|
||
describe('GlobalStyle', () => { | ||
it('does not render anything if no theme is passed', async () => { | ||
// @ts-expect-error - missing props | ||
const { container } = render(<GlobalStyle />); | ||
|
||
const styleTag = container.querySelector(`style`); | ||
expect(styleTag).toBe(null); | ||
}); | ||
|
||
it('renders a style tag if styles are passed', async () => { | ||
const { container } = render( | ||
<GlobalStyle | ||
styles={{ | ||
'.foo': { | ||
backgroundColor: 'red', | ||
}, | ||
}} | ||
/> | ||
); | ||
|
||
const styleTag = container.querySelector(`style`); | ||
expect(styleTag).toBeInTheDocument(); | ||
}); | ||
}); |
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
19 changes: 19 additions & 0 deletions
19
packages/ui/src/theme/createTheme/__tests__/__snapshots__/createGlobalCSS.test.ts.snap
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,19 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`@aws-amplify/ui createGlobalCSS should handle psuedo-states 1`] = ` | ||
".bar:active { background-color:#ff9900; } | ||
.bar { background-color:#bada55; } | ||
" | ||
`; | ||
|
||
exports[`@aws-amplify/ui createGlobalCSS should work with design tokens 1`] = ` | ||
".bar:active { background-color:var(--amplify-colors-blue-40); } | ||
.bar { background-color:var(--amplify-colors-blue-20); } | ||
" | ||
`; | ||
|
||
exports[`@aws-amplify/ui createGlobalCSS should work with regular styles 1`] = ` | ||
".foo { background-color:red; } | ||
button > .icon { color:blueviolet; } | ||
" | ||
`; |
45 changes: 45 additions & 0 deletions
45
packages/ui/src/theme/createTheme/__tests__/createGlobalCSS.test.ts
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,45 @@ | ||
import { createGlobalCSS } from '../createGlobalCSS'; | ||
import { createTheme } from '../createTheme'; | ||
|
||
const { tokens } = createTheme(); | ||
|
||
describe('@aws-amplify/ui', () => { | ||
describe('createGlobalCSS', () => { | ||
it('should work with regular styles', () => { | ||
expect( | ||
createGlobalCSS({ | ||
'.foo': { | ||
backgroundColor: 'red', | ||
}, | ||
'button > .icon': { | ||
color: 'blueviolet', | ||
}, | ||
}) | ||
).toMatchSnapshot(); | ||
}); | ||
it('should handle psuedo-states', () => { | ||
expect( | ||
createGlobalCSS({ | ||
'.bar': { | ||
backgroundColor: '#bada55', | ||
':active': { | ||
backgroundColor: '#ff9900', | ||
}, | ||
}, | ||
}) | ||
).toMatchSnapshot(); | ||
}); | ||
it('should work with design tokens', () => { | ||
expect( | ||
createGlobalCSS({ | ||
'.bar': { | ||
backgroundColor: tokens.colors.blue[20], | ||
':active': { | ||
backgroundColor: tokens.colors.blue[40], | ||
}, | ||
}, | ||
}) | ||
).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ComponentStyles } from '../components/utils'; | ||
import { recursiveComponentCSS } from './createComponentCSS'; | ||
|
||
type GlobalCSS = Record<string, ComponentStyles>; | ||
|
||
export function createGlobalCSS(css: GlobalCSS) { | ||
let cssText = ``; | ||
|
||
for (const [selector, styles] of Object.entries(css)) { | ||
cssText += recursiveComponentCSS(selector, styles); | ||
} | ||
|
||
return cssText; | ||
} |
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