-
Notifications
You must be signed in to change notification settings - Fork 674
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2385 from system-ui/2207-global-component
Add theme-aware Global component
- Loading branch information
Showing
18 changed files
with
346 additions
and
64 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
title: '@theme-ui/global' | ||
--- | ||
|
||
import Readme from '@theme-ui/global/README.md' | ||
|
||
<Readme /> |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,27 @@ | ||
# @theme-ui/global | ||
|
||
Wrapper around the Emotion `Global` component, made Theme UI theme-aware. | ||
|
||
**Note:** _This package is included in the main `theme-ui` package and a | ||
separate installation is not required unless you’re using `@theme-ui/core`._ | ||
|
||
```sh | ||
npm i @theme-ui/global @theme-ui/core @emotion/react | ||
``` | ||
|
||
```jsx | ||
import Global from '@theme-ui/global' | ||
|
||
export default (props) => ( | ||
<Global | ||
styles={{ | ||
button: { | ||
m: 0, | ||
bg: 'primary', | ||
color: 'background', | ||
border: 0, | ||
}, | ||
}} | ||
/> | ||
) | ||
``` |
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,27 @@ | ||
{ | ||
"name": "@theme-ui/global", | ||
"version": "0.15.4", | ||
"repository": "system-ui/theme-ui", | ||
"main": "dist/theme-ui-global.cjs.js", | ||
"module": "dist/theme-ui-global.esm.js", | ||
"source": "src/index.tsx", | ||
"author": "Lachlan Campbell", | ||
"license": "MIT", | ||
"sideEffects": false, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"@theme-ui/core": "workspace:^", | ||
"@theme-ui/css": "workspace:^" | ||
}, | ||
"peerDependencies": { | ||
"@emotion/react": "^11", | ||
"react": ">=18" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.0.0", | ||
"@types/react": "^18", | ||
"@theme-ui/test-utils": "workspace:^" | ||
} | ||
} |
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,16 @@ | ||
import { jsx, type ThemeUIStyleObject } from '@theme-ui/core' | ||
import { css, type Theme } from '@theme-ui/css' | ||
import { Global as EmotionGlobal } from '@emotion/react' | ||
|
||
export interface GlobalProps { | ||
styles: ThemeUIStyleObject | ||
} | ||
const Global = ({ styles }: GlobalProps): JSX.Element => | ||
jsx(EmotionGlobal, { | ||
styles: (emotionTheme: unknown) => { | ||
const theme = emotionTheme as Theme | ||
return css(styles)(theme) | ||
}, | ||
}) | ||
|
||
export default Global |
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,77 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`renders global and component styles 1`] = ` | ||
.emotion-0 { | ||
color: pink; | ||
} | ||
<html> | ||
<head> | ||
<style | ||
data-emotion="css" | ||
data-s="" | ||
> | ||
.css-2cx43c{} | ||
</style> | ||
<style | ||
data-emotion="css" | ||
data-s="" | ||
> | ||
.css-2cx43c html{background-color:hotpink;} | ||
</style> | ||
<style | ||
data-emotion="css" | ||
data-s="" | ||
> | ||
.emotion-0{color:pink;} | ||
</style> | ||
</head> | ||
<body> | ||
<div> | ||
<header> | ||
<div | ||
class="emotion-0" | ||
/> | ||
</header> | ||
</div> | ||
</body> | ||
</html> | ||
`; | ||
|
||
exports[`renders global styles 1`] = ` | ||
<html> | ||
<head> | ||
<style | ||
data-emotion="css-global" | ||
data-s="" | ||
> | ||
@font-face{font-family:some-name;} | ||
</style> | ||
<style | ||
data-emotion="css-global" | ||
data-s="" | ||
> | ||
body{font-family:Georgia,serif;margin:0;} | ||
</style> | ||
<style | ||
data-emotion="css-global" | ||
data-s="" | ||
> | ||
h1{color:salmon;} | ||
</style> | ||
</head> | ||
<body> | ||
<div> | ||
<h1> | ||
Hello | ||
</h1> | ||
</div> | ||
</body> | ||
</html> | ||
`; |
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,82 @@ | ||
/** | ||
* @jest-environment jsdom | ||
* @jsx jsx | ||
*/ | ||
|
||
import { jsx } from '@theme-ui/core' | ||
import { cleanup } from '@testing-library/react' | ||
import { render } from '@theme-ui/test-utils' | ||
import { matchers } from '@emotion/jest' | ||
|
||
import { ThemeProvider } from '@theme-ui/core' | ||
import Global from '../src' | ||
|
||
expect.extend(matchers) | ||
|
||
beforeEach(() => { | ||
document.head.innerHTML = '' | ||
jest.resetAllMocks() | ||
}) | ||
|
||
afterEach(cleanup) | ||
|
||
test.only('renders global styles', async () => { | ||
const root = ( | ||
<ThemeProvider | ||
theme={{ | ||
config: { | ||
useRootStyles: false, | ||
}, | ||
fonts: { | ||
body: 'Georgia,serif', | ||
}, | ||
colors: { | ||
primary: 'salmon', | ||
}, | ||
}} | ||
> | ||
<Global | ||
styles={{ | ||
'@font-face': { | ||
fontFamily: 'some-name', | ||
}, | ||
body: { | ||
fontFamily: 'body', | ||
margin: 0, | ||
}, | ||
h1: { | ||
color: 'primary', | ||
}, | ||
}} | ||
/> | ||
<h1>Hello</h1> | ||
</ThemeProvider> | ||
) | ||
|
||
const document = render(root) | ||
expect(document.baseElement.parentElement).toMatchSnapshot() | ||
|
||
const bodyStyle = global.getComputedStyle(document.baseElement) | ||
expect(bodyStyle.fontFamily).toBe('Georgia,serif') | ||
expect(bodyStyle.margin).toBe('0px') | ||
|
||
const h1 = document.baseElement.querySelector('h1')! | ||
expect(global.getComputedStyle(h1).color).toBe('salmon') | ||
}) | ||
|
||
test('renders global and component styles', () => { | ||
const root = ( | ||
<header> | ||
<Global | ||
styles={{ | ||
html: { | ||
backgroundColor: 'hotpink', | ||
}, | ||
}} | ||
/> | ||
<div sx={{ color: 'pink' }} /> | ||
</header> | ||
) | ||
const { baseElement } = render(root) | ||
expect(baseElement.parentElement).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
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.