Skip to content

Commit d155389

Browse files
authored
Merge pull request #1094 from primer/cb/remove-default-theme-prop
Remove default theme prop
2 parents 709cf89 + da668d4 commit d155389

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1998
-387
lines changed

.changeset/tricky-humans-appear.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
"@primer/components": major
3+
---
4+
5+
Components no longer have a default `theme` prop. To ensure components still render correctly, you'll need pass the Primer theme to a [styled-components](https://styled-components.com/) `<ThemeProvider>` at the root of your application:
6+
7+
```jsx
8+
import {ThemeProvider} from 'styled-components'
9+
import {theme} from '@primer/components'
10+
11+
funciton App(props) {
12+
return (
13+
<div>
14+
<ThemeProvider theme={theme}>
15+
<div>your app here</div>
16+
</ThemeProvider>
17+
</div>
18+
)
19+
}
20+
```

contributor-docs/CONTRIBUTING.md

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
4. [Developing Components](#developing-components)
77
- [Tools we use](#tools-we-use)
88
- [Component patterns](#component-patterns)
9-
- [Adding default theme](#adding-default-theme)
109
- [Adding system props](#adding-system-props)
1110
- [Adding the sx prop](#adding-the-sx-prop)
1211
- [Linting](#linting)
@@ -72,15 +71,14 @@ With a couple of exceptions, all components should be created with the `styled`
7271

7372
Default values for system props can be set in `Component.defaultProps`.
7473

75-
⚠️ **Make sure to always set the default `theme` prop to our [theme](https://github.com/primer/components/blob/main/src/theme-preval.js)! This allows consumers of our components to access our theme values without a ThemeProvider.**
74+
⚠️ **Do not set the default `theme` prop! This can sometimes override the theme provided by the ThemeProvider and cause unexpected theming issues.**
7675

7776
Additionally, every component should support [the `sx` prop](https://primer.style/components/overriding-styles); remember to add `${sx}` to the style literal.
7877

7978
Here's an example of a basic component written in the style of Primer Components:
8079

8180
```jsx
8281
import {TYPOGRAPHY, COMMON} from './constants'
83-
import theme from './theme'
8482
import sx from './sx
8583
8684
const Component = styled.div`
@@ -92,28 +90,13 @@ const Component = styled.div`
9290
`
9391
9492
Component.defaultProps = {
95-
theme, // make sure to always set the default theme!
9693
m: 0,
9794
fontSize: 5,
9895
}
9996
10097
export default Component
10198
```
10299
103-
### Adding default theme
104-
105-
Each component needs access to our default Primer Theme, so that users of the component can access theme values easily in their consuming applications.
106-
107-
To add the default theme to a component, import the theme and assign it to the component's defaultProps object:
108-
109-
```jsx
110-
import theme from './theme'
111-
112-
Component.defaultProps = {
113-
theme // make sure to always set the default theme!
114-
}
115-
```
116-
117100
### Adding system props
118101
119102
Each component should have access to the appropriate system props. Every component has access to `COMMON`. For **most** components added, you'll only need to give the component to `COMMON`. If you are unsure, ping a DS team member on your PR.
@@ -223,7 +206,6 @@ After opening a pull request, a member of the design systems team will add the a
223206
- If it's a new component, does the component make sense to add to Primer Components? (Ideally this is discussed before the pull request stage, please reach out to a DS member if you aren't sure if a component should be added to Primer Components!)
224207
- Does the component follow our [Primer Components code style](#component-patterns)?
225208
- Does the component use theme values for most CSS values?
226-
- Does the component have access to the [default theme](#adding-default-theme)?
227209
- Does the component have the [correct system props implemented](#adding-system-props)?
228210
- Is the component API intuitive?
229211
- Does the component have the appropriate [type definitions in `index.d.ts`](#typescript-support)?

docs/content/getting-started.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,27 @@ This will apply the same `color`, `font-family`, and `line-height` styles to the
7979

8080
## Theming
8181

82-
Components are styled using Primer's [theme](https://github.com/primer/components/blob/main/src/theme-preval.js) by default, but you can provide your own theme by using [styled-component's](https://styled-components.com/) `<ThemeProvider>`. If you'd like to fully replace the Primer [theme](https://github.com/primer/components/blob/main/src/theme-preval.js) with your custom theme, pass your theme to the `<ThemeProvider>` in the root of your application like so:
82+
For Primer Components to render correcly, you must pass the Primer theme to a [styled-components](<(https://styled-components.com/)>) `<ThemeProvider>` at the root of your application:
8383

8484
```jsx
8585
import {ThemeProvider} from 'styled-components'
86+
import {theme} from '@primer/components'
87+
88+
const App = props => {
89+
return (
90+
<div>
91+
<ThemeProvider theme={theme}>
92+
<div>your app here</div>
93+
</ThemeProvider>
94+
</div>
95+
)
96+
}
97+
```
8698

99+
If you'd like to fully replace the Primer [theme](https://github.com/primer/components/blob/main/src/theme-preval.js) with your custom theme, pass your theme to the `<ThemeProvider>` instead:
100+
101+
```jsx
102+
import {ThemeProvider} from 'styled-components'
87103
const theme = { ... }
88104

89105
const App = (props) => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
"rollup-plugin-terser": "5.3.0",
124124
"rollup-plugin-visualizer": "4.0.4",
125125
"semver": "7.3.2",
126-
"styled-components": "4.4.0",
126+
"styled-components": "4.4.1",
127127
"typescript": "4.1.3"
128128
},
129129
"peerDependencies": {

src/Avatar.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import styled from 'styled-components'
22
import {COMMON, get, SystemCommonProps} from './constants'
33
import sx, {SxProp} from './sx'
4-
import theme from './theme'
54
import {ComponentProps} from './utils/types'
65

76
type StyledAvatarProps = {
@@ -32,7 +31,6 @@ const Avatar = styled.img.attrs<StyledAvatarProps>(props => ({
3231
`
3332

3433
Avatar.defaultProps = {
35-
theme,
3634
size: 20,
3735
alt: '',
3836
square: false

src/AvatarPair.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import styled from 'styled-components'
33
import Avatar from './Avatar'
44
import {get} from './constants'
55
import {Relative, RelativeProps} from './Position'
6-
import theme from './theme'
76

87
const ChildAvatar = styled(Avatar)`
98
position: absolute;
@@ -29,6 +28,4 @@ const AvatarPair = ({children, ...rest}: AvatarPairProps) => {
2928
// styled() changes this
3029
AvatarPair.displayName = 'AvatarPair'
3130

32-
AvatarPair.defaultProps = {theme}
33-
3431
export default AvatarPair

src/AvatarStack.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import styled from 'styled-components'
44
import {COMMON, get, SystemCommonProps} from './constants'
55
import {Absolute} from './Position'
66
import sx, {SxProp} from './sx'
7-
import theme from './theme'
87
import {ComponentProps} from './utils/types'
98

109
type StyledAvatarStackWrapperProps = {
@@ -159,8 +158,4 @@ const AvatarStack = ({children, alignRight, ...rest}: AvatarStackProps) => {
159158
)
160159
}
161160

162-
AvatarStack.defaultProps = {
163-
theme
164-
}
165-
166161
export default AvatarStack

src/BaseStyles.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React from 'react'
22
import styled, {createGlobalStyle} from 'styled-components'
33
import {COMMON, SystemCommonProps, SystemTypographyProps, TYPOGRAPHY} from './constants'
44
import useMouseIntent from './hooks/useMouseIntent'
5-
import theme from './theme'
65
import {ComponentProps} from './utils/types'
76

87
const GlobalStyle = createGlobalStyle`
@@ -47,8 +46,7 @@ function BaseStyles(props: BaseStylesProps) {
4746
BaseStyles.defaultProps = {
4847
color: 'text.primary',
4948
fontFamily: 'normal',
50-
lineHeight: 'default',
51-
theme
49+
lineHeight: 'default'
5250
}
5351

5452
export default BaseStyles

src/BorderBox.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import styled from 'styled-components'
22
import Box from './Box'
33
import {BORDER, SystemBorderProps} from './constants'
44
import sx from './sx'
5-
import theme from './theme'
65
import {ComponentProps} from './utils/types'
76

87
const BorderBox = styled(Box)<SystemBorderProps>`
@@ -11,7 +10,6 @@ const BorderBox = styled(Box)<SystemBorderProps>`
1110
`
1211

1312
BorderBox.defaultProps = {
14-
theme,
1513
borderWidth: '1px',
1614
borderStyle: 'solid',
1715
borderColor: 'border.primary',

src/Box.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import styled from 'styled-components'
22
import {COMMON, FLEX, LAYOUT, SystemCommonProps, SystemFlexProps, SystemLayoutProps} from './constants'
33
import sx, {SxProp} from './sx'
4-
import theme from './theme'
54
import {ComponentProps} from './utils/types'
65

76
const Box = styled.div<SystemCommonProps & SystemFlexProps & SystemLayoutProps & SxProp>`
@@ -11,7 +10,5 @@ const Box = styled.div<SystemCommonProps & SystemFlexProps & SystemLayoutProps &
1110
${sx};
1211
`
1312

14-
Box.defaultProps = {theme}
15-
1613
export type BoxProps = ComponentProps<typeof Box>
1714
export default Box

0 commit comments

Comments
 (0)