Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
07c0944
Update styled-components and add @styled-system/css
BinaryMuse Apr 13, 2020
fd5fbe5
Get styled-components from the root project
BinaryMuse Apr 13, 2020
39af9e7
Add withSx Hoc
BinaryMuse Apr 13, 2020
c98df7c
Wrap Heading in withSx
BinaryMuse Apr 13, 2020
c5145bd
Refactor sx prop
BinaryMuse Apr 14, 2020
bcaa4c0
Update components from withSx => sx.styled
BinaryMuse Apr 14, 2020
9ef246f
Remove temporary variable
BinaryMuse Apr 14, 2020
7bec957
:art:
BinaryMuse Apr 14, 2020
512df86
Revert styled-component update
BinaryMuse Apr 14, 2020
c5c398f
:art:
BinaryMuse Apr 14, 2020
44d33f9
Merge remote-tracking branch 'origin/release-19.0.0' into mkt/sx-prop
BinaryMuse Apr 21, 2020
bf285df
Add sx.propTypes
BinaryMuse Apr 21, 2020
96de6cb
Make all components sx-prop aware and convert test suites
BinaryMuse Apr 21, 2020
b421aba
Add sx prop info to CONTRIBUTING and fix spelling and linting errors
BinaryMuse Apr 22, 2020
272f7b2
Add initial pass at sx-prop docs
BinaryMuse Apr 22, 2020
03703ce
Remove weird pattern in SelectMenuFilter sx prop
BinaryMuse Apr 22, 2020
c862012
Update Dialog definition
BinaryMuse Apr 22, 2020
e0a165f
Add missing system props types to CONTRIBUTING
BinaryMuse Apr 22, 2020
b859e4d
Update sx props doc
BinaryMuse Apr 22, 2020
3c6bdf8
Add responsive sx prop example
BinaryMuse Apr 22, 2020
cff9e05
Move 'When to use the sx prop' section up
BinaryMuse Apr 27, 2020
fa3943e
Fix formatting and links in Primer Theme docs
BinaryMuse Apr 27, 2020
e9d9c33
Add note on custom theme to sx prop docs
BinaryMuse Apr 27, 2020
c4f78d0
Modify sx prop guidelines
BinaryMuse Apr 27, 2020
398706b
Use SystemStyleObject from @styled-system/css in TS sx prop typings
BinaryMuse Apr 27, 2020
72e2bfe
Merge remote-tracking branch 'origin/master' into mkt/sx-prop
BinaryMuse Apr 29, 2020
5d99108
Merge remote-tracking branch 'origin/release-19.0.0' into mkt/sx-prop
BinaryMuse Apr 29, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 75 additions & 35 deletions CONTRIBUTING.md

Large diffs are not rendered by default.

73 changes: 73 additions & 0 deletions docs/content/overriding-styles.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
title: Overriding styles with the sx prop
---

Our goal with Primer Components is to hit the sweet spot between providing too little and too much styling flexibility; too little and the design system is too rigid, and too much and it becomes too difficult to maintain a consistent style. Our components already support a standard set of [system props](/system-props), but sometimes a component just isn't *quite* flexible enough to look the way you need it to look. For those cases, we provide the `sx` prop.

The `sx` prop allows ad-hoc styling that is still theme aware. Declare the styles you want to apply in camel-cased object notation, and try to use theme values in appropriate CSS properties when possible. If you've passed a custom theme using `ThemeProvider` or a `theme` prop, the `sx` prop will honor the custom theme. For more information on theming in Primer Components, check out [the Primer Theme documentation](/primer-theme).

## When to use the `sx` prop

The `sx` prop provides a lot of power, which means it is an easy tool to abuse. To best make use of it, we recommend following these guidelines:

* Use the `sx` prop for small stylistic changes to components. For more substantial changes, consider abstracting your style changes into your own wrapper component.
* Use [system props](/system-props) instead of the `sx` prop whenever possible.
* Avoid nesting and pseudo-selectors in `sx` prop values when possible.

## Basic example

This example demonstrates applying a bottom border to `Heading`, a component that does not receive `BORDER` system props. The `borderBottomWidth` value comes from `theme.borderWidths` and `borderBottomColor` comes from `theme.colors`.

```jsx live
<Heading pb={2}>Heading</Heading>

<Heading
pb={2}
sx={{
borderBottomWidth: 1,
borderBottomColor: 'border.gray',
borderBottomStyle: 'solid'
}}>
Heading with bottom border
</Heading>
```

## Responsive values

Just like [values passed to system props](https://styled-system.com/responsive-styles), values in the `sx` prop can be provided as arrays to provide responsive styling.

```jsx live
<BorderBox
p={2}
sx={{
bg: ['red.1', 'green.1', 'blue.1', 'purple.1', 'yellow.1']
}}>
Responsive background color
</BorderBox>
```

## Nesting, pseudo-classes, and pseudo-elements

The `sx` prop also allows for declaring styles based on media queries, psueudo-classes, and pseudo-elements. This example, though contrived, demonstrates the ability:

```jsx live
<Box sx={{
'> *': {
borderWidth: 1,
borderColor: 'border.gray',
borderStyle: 'solid',
borderBottomWidth: 0,
padding: 2,
':last-child': {
borderBottomWidth: 1
},
':hover': {
bg: 'gray.1'
}
}
}}>
<Box>First</Box>
<Box>Second</Box>
<Box>Third</Box>
</Box>
```
45 changes: 21 additions & 24 deletions docs/content/primer-theme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@ title: Primer Theme

import {theme} from '@primer/components'

Primer Components come with built-in access to our Primer theme. The [theme file](https://github.com/primer/components/blob/master/src/theme.js) contains an object which holds values
for common variables such as color, fonts, box shadows, and more. Our theme file pulls many of its color and typography values from [primer-primitives](https://github.com/primer/primer-primitives).
Primer Components come with built-in access to our Primer theme. The [theme file](https://github.com/primer/components/blob/master/src/theme.js) contains an object which holds values for common variables such as color, fonts, box shadows, and more. Our theme file pulls many of its color and typography values from [primer-primitives](https://github.com/primer/primer-primitives).

Many of our theme keys correspond to system props on our components. For example, if you'd like to set the max width on a `<Box>` set the `maxWidth` prop to `medium`:
`<Box maxWidth='medium'>`
Many of our theme keys correspond to system props on our components. For example, if you'd like to set the max width on a `<Box>` set the `maxWidth` prop to `medium`: `<Box maxWidth='medium'>`

In the background, [styled-system](https://github.com/jxnblk/styled-system) does the work of finding the `medium` value from `maxWidth` key in the theme file and applying the corresponding CSS.
In the background, [styled-system](https://github.com/styled-system/styled-system) does the work of finding the `medium` value from `maxWidth` key in the theme file and applying the corresponding CSS.

Our full theme can be found [here](https://github.com/primer/components/blob/master/src/theme.js).


### Custom Theming

Custom theming is an optional way to override the Primer values that control color, spacing, typography, and other aspects of our components.

There are two ways to change the theme of Primer components:

1. You can override the entire theme for an entire tree of components using the `<ThemeProvider>` from [styled-components]:

```jsx
```javascript
import {Block, Button, Text, theme as primer} from '@primer/components'
import {ThemeProvider} from 'styled-components'

Expand All @@ -46,29 +44,29 @@ There are two ways to change the theme of Primer components:
```

**⚠️ Note: [styled-components]'s `<ThemeProvider>` only allows exactly one child.**
2. You can merge the Primer theme with your custom theme using Object.assign:

```jsx
import {ThemeProvider} from `styled-components`
import {theme} from '@primer/components'
2. You can merge the Primer theme with your custom theme using Object.assign:

const customTheme = { ... }
```javascript
import {ThemeProvider} from `styled-components`
import {theme} from '@primer/components'

const customTheme = { ... }

const App = (props) => {
return (
<div>
<ThemeProvider theme={Object.assign({}, theme, customTheme)}> // matching keys in customTheme will override keys in the Primer theme
<div>your app here</div>
</ThemeProvider>
</div>
)
}
```
const App = (props) => {
return (
<div>
<ThemeProvider theme={Object.assign({}, theme, customTheme)}> // matching keys in customTheme will override keys in the Primer theme
<div>your app here</div>
</ThemeProvider>
</div>
)
}
```

3. You can theme individual components by passing the `theme` prop directly:

```jsx
```javascript
import {Text} from '@primer/components'

const theme = {
Expand All @@ -84,7 +82,6 @@ const App = (props) => {

**☝️ This is an intentionally convoluted example, since you can use `<Text color='#f0f'>` out of the box.**


Read the [styled-system docs](https://styled-system.com/#theming) for more information on theming in styled-system.

[styled-components]: https://styled-components.com/
2 changes: 1 addition & 1 deletion docs/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = {
options: {
alias: {
'@primer/components': path.resolve(__dirname, '../src'),
'styled-components': path.resolve(__dirname, 'node_modules', 'styled-components'),
'styled-components': path.resolve(__dirname, '..', 'node_modules', 'styled-components'),
'react': path.resolve(__dirname, 'node_modules', 'react'),
}
}
Expand Down
3 changes: 3 additions & 0 deletions docs/src/@primer/gatsby-theme-doctocat/nav.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
- title: Philosophy
url: /philosophy

- title: Overriding Styles
url: /overriding-styles

- title: Components
url: /
children:
Expand Down
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
declare module '@primer/components' {
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>
import * as StyledSystem from 'styled-system'
import {SystemStyleObject} from '@styled-system/css'
import * as StyledComponents from 'styled-components'
import * as History from 'history'

export interface BaseProps extends React.Props<any> {
as?: React.ReactType
className?: string
css?: string
sx?: SystemStyleObject
title?: string
// NOTE(@mxstbr): Necessary workaround to make <Component as={Link} to="/bla" /> work
to?: History.LocationDescriptor
Expand Down Expand Up @@ -80,6 +82,7 @@ declare module '@primer/components' {
export interface ButtonProps
extends BaseProps,
CommonProps,
LayoutProps,
StyledSystem.FontSizeProps,
Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'color'> {
variant?: 'small' | 'medium' | 'large'
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@
"author": "GitHub, Inc.",
"license": "MIT",
"dependencies": {
"@primer/octicons-react": "^9.2.0",
"@primer/octicons-react": "^9.6.0",
"@primer/primitives": "3.0.0",
"@reach/dialog": "0.3.0",
"@styled-system/css": "5.1.5",
"@styled-system/prop-types": "5.1.2",
"@styled-system/props": "5.1.4",
"@styled-system/theme-get": "5.1.2",
Expand Down
5 changes: 4 additions & 1 deletion src/Avatar.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import PropTypes from 'prop-types'
import styled from 'styled-components'
import PropTypes from 'prop-types'
import sx from './sx'
import {get} from './constants'
import {space} from 'styled-system'
import systemPropTypes from '@styled-system/prop-types'
Expand All @@ -22,6 +23,7 @@ const Avatar = styled.img.attrs(props => ({
vertical-align: middle;
${borderRadius};
${space};
${sx};
`

Avatar.defaultProps = {
Expand All @@ -35,6 +37,7 @@ Avatar.propTypes = {
size: PropTypes.number,
src: PropTypes.string,
...systemPropTypes.space,
...sx.propTypes,
theme: PropTypes.object
}

Expand Down
5 changes: 4 additions & 1 deletion src/AvatarStack.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import PropTypes from 'prop-types'
import styled, {css} from 'styled-components'
import sx from './sx'
import {get, COMMON} from './constants'
import theme from './theme'

Expand All @@ -22,6 +23,7 @@ const AvatarStackWrapper = styled.span`
min-width: ${props => (props.count === 1 ? '26px' : props.count === 2 ? '36px' : '46px')};
height: 20px;
${COMMON}
${sx};
`

const AvatarStackBody = styled.span`
Expand Down Expand Up @@ -152,6 +154,7 @@ AvatarStack.defaultProps = {

AvatarStack.propTypes = {
...COMMON.propTypes,
alignRight: PropTypes.bool
alignRight: PropTypes.bool,
...sx.propTypes
}
export default AvatarStack
11 changes: 8 additions & 3 deletions src/BorderBox.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import styled from 'styled-components'
import sx from './sx'
import PropTypes from 'prop-types'
import Box from './Box'
import theme from './theme'
import {BORDER} from './constants'

const BorderBox = styled(Box)(BORDER)
const BorderBox = styled(Box)`
${BORDER};
${sx};
`

BorderBox.defaultProps = {
theme,
Expand All @@ -15,9 +19,10 @@ BorderBox.defaultProps = {
}

BorderBox.propTypes = {
theme: PropTypes.object,
...Box.propTypes,
...BORDER.propTypes
...BORDER.propTypes,
...sx.propTypes,
theme: PropTypes.object
}

export default BorderBox
3 changes: 3 additions & 0 deletions src/Box.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import styled from 'styled-components'
import PropTypes from 'prop-types'
import sx from './sx'
import {COMMON, FLEX, LAYOUT} from './constants'
import theme from './theme'

const Box = styled.div`
${COMMON}
${FLEX}
${LAYOUT}
${sx};
`

Box.defaultProps = {theme}
Expand All @@ -15,6 +17,7 @@ Box.propTypes = {
...COMMON.propTypes,
...FLEX.propTypes,
...LAYOUT.propTypes,
...sx.propTypes,
theme: PropTypes.object
}

Expand Down
3 changes: 3 additions & 0 deletions src/BranchName.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import PropTypes from 'prop-types'
import styled from 'styled-components'
import sx from './sx'
import theme from './theme'
import {COMMON, get} from './constants'

Expand All @@ -12,6 +13,7 @@ const BranchName = styled.a`
background-color: #eaf5ff;
border-radius: 3px;
${COMMON};
${sx};
`

BranchName.defaultProps = {
Expand All @@ -21,6 +23,7 @@ BranchName.defaultProps = {
BranchName.propTypes = {
href: PropTypes.string,
...COMMON.propTypes,
...sx.propTypes,
theme: PropTypes.object
}

Expand Down
13 changes: 11 additions & 2 deletions src/Breadcrumbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'
import PropTypes from 'prop-types'
import classnames from 'classnames'
import styled from 'styled-components'
import sx from './sx'
import {COMMON, FLEX, get} from './constants'
import theme from './theme'
import Box from './Box'
Expand Down Expand Up @@ -46,6 +47,7 @@ const Breadcrumb = styled(BreadcrumbBase)`
justify-content: space-between;
${COMMON};
${FLEX};
${sx};
`

Breadcrumb.Item = styled.a.attrs(props => ({
Expand All @@ -65,25 +67,32 @@ Breadcrumb.Item = styled.a.attrs(props => ({
pointer-events: none;
}
${COMMON}
${sx};
`

Breadcrumb.defaultProps = {
theme
}

Breadcrumb.propTypes = {
...COMMON.propTypes
...COMMON.propTypes,
...sx.propTypes
}

Breadcrumb.displayName = 'Breadcrumb'

Breadcrumb.Item.defaultProps = {
theme
}

Breadcrumb.Item.propTypes = {
as: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
as: PropTypes.oneOfType([PropTypes.string, PropTypes.elementType]),
href: PropTypes.string,
selected: PropTypes.bool,
...sx.propTypes,
...COMMON.propTypes
}

Breadcrumb.Item.displayName = 'Breadcrumb.Item'

export default Breadcrumb
Loading