Skip to content

Commit f9c7f93

Browse files
authored
Merge pull request #2385 from system-ui/2207-global-component
Add theme-aware Global component
2 parents 9fbd1fc + ac36c53 commit f9c7f93

File tree

18 files changed

+346
-64
lines changed

18 files changed

+346
-64
lines changed

examples/next/components/Header.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { Button, useColorMode } from 'theme-ui'
22

33
const Header = () => {
44
const [colorMode, setColorMode] = useColorMode()
5+
56
return (
67
<header>
78
<Button
9+
suppressHydrationWarning
810
onClick={() => setColorMode(colorMode === 'light' ? 'dark' : 'light')}
911
>
1012
Toggle {colorMode === 'light' ? 'Dark' : 'Light'}

examples/next/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@mdx-js/loader": "^2.1.2",
1717
"@mdx-js/react": "^2.1.2",
1818
"@next/mdx": "^12.0.7",
19+
"@theme-ui/css": "workspace:^",
1920
"next": "^12.1.0",
2021
"react": "^18",
2122
"react-dom": "^18",

examples/next/pages/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import Head from 'next/head'
22
import About from '../components/about.mdx'
3+
import { Global } from 'theme-ui'
34

45
export default function Page() {
56
return (
67
<>
78
<Head>
89
<title>Next.js Theme UI</title>
910
</Head>
11+
<Global styles={{ h1: { color: 'salmon !important' } }} />
1012
<About />
1113
</>
1214
)

packages/docs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"@theme-ui/core": "workspace:^",
2727
"@theme-ui/components": "workspace:^",
2828
"@theme-ui/css": "workspace:^",
29+
"@theme-ui/global": "workspace:^",
2930
"@theme-ui/match-media": "workspace:^",
3031
"@theme-ui/mdx": "workspace:^",
3132
"@theme-ui/presets": "workspace:^",

packages/docs/src/pages/guides/global-styles.mdx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,34 @@ title: 'Global Styles'
44

55
# Global Styles
66

7-
Use the Emotion `Global` component to add global CSS with theme-based values.
7+
Theme UI offers a `Global` component (that wraps Emotion’s) for adding global
8+
CSS with theme-based values.
89

9-
By default, the `ThemeProvider` component will apply styles in `theme.styles.root` to the `<html>` element.
10-
It will also apply `color` and `background-color` styles based on `theme.colors.text` and `theme.colors.background` respectively.
10+
By default (or, unless the
11+
[`useRootStyles` configuration option](/theming#configuration-flags)is
12+
disabled), the `ThemeProvider` component will apply styles in
13+
`theme.styles.root` to the `<html>` element. It will also apply `color` and
14+
`background-color` styles based on `theme.colors.text` and
15+
`theme.colors.background` respectively.
1116

1217
<Note>
1318
Generally, you should try to avoid adding CSS to global scope. Many styles can
1419
be safely encapsulated into a component without the need for global styles.
1520
</Note>
1621

1722
```jsx
18-
import { Global } from '@emotion/react'
23+
import { Global } from 'theme-ui'
1924

2025
export default (props) => (
2126
<Global
22-
styles={(theme) => ({
23-
'*': {
24-
boxSizing: 'border-box',
27+
styles={{
28+
button: {
29+
m: 0,
30+
bg: 'primary',
31+
color: 'background',
32+
border: 0,
2533
},
26-
})}
34+
}}
2735
/>
2836
)
2937
```
30-
31-
<Note>
32-
33-
If you are upgrading from a version of theme-ui older that v0.6.0, be aware the import
34-
package has changed from `@emotion/core` to `@emotion/react`. For more information see
35-
the [Migration Notes for 0.6](https://theme-ui.com/migrating/#breaking-changes).
36-
37-
</Note>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: '@theme-ui/global'
3+
---
4+
5+
import Readme from '@theme-ui/global/README.md'
6+
7+
<Readme />

packages/docs/src/sidebar.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
- [@theme-ui/core](/packages/core)
5959
- [@theme-ui/components](/packages/components)
6060
- [@theme-ui/mdx](/packages/mdx)
61+
- [@theme-ui/global](/packages/global)
6162
- [@theme-ui/presets](/packages/presets)
6263
- [@theme-ui/prism](/packages/prism)
6364
- [@theme-ui/color](/packages/color)

packages/docs/static/card.svg

Lines changed: 1 addition & 1 deletion
Loading

packages/docs/static/logo.svg

Lines changed: 1 addition & 1 deletion
Loading

packages/global/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# @theme-ui/global
2+
3+
Wrapper around the Emotion `Global` component, made Theme UI theme-aware.
4+
5+
**Note:** _This package is included in the main `theme-ui` package and a
6+
separate installation is not required unless you’re using `@theme-ui/core`._
7+
8+
```sh
9+
npm i @theme-ui/global @theme-ui/core @emotion/react
10+
```
11+
12+
```jsx
13+
import Global from '@theme-ui/global'
14+
15+
export default (props) => (
16+
<Global
17+
styles={{
18+
button: {
19+
m: 0,
20+
bg: 'primary',
21+
color: 'background',
22+
border: 0,
23+
},
24+
}}
25+
/>
26+
)
27+
```

0 commit comments

Comments
 (0)