diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index ecce2dd5fd3..b753be4cb7e 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -1,14 +1,16 @@
# Contribution guidelines
+
1. [Roadmap](#roadmap)
2. [Developing Components](#developing-components)
* [Tools we use](#tools-we-use)
* [Component patterns](#component-patterns)
* [Adding default theme](#adding-default-theme)
* [Adding system props](#adding-system-props)
+ * [Adding the sx prop](#adding-the-sx-prop)
* [Linting](#linting)
* [Testing](#testing)
* [TypeScript support](#typescript-support)
- * [Additonal resources](#additional-resources)
+ * [Additional resources](#additional-resources)
3. [Writing documentation](#writing-documentation)
4. [Creating a pull request](#creating-a-pull-request)
* [What to expect after opening a pull request](#what-to-expect-after-opening-a-pull-request)
@@ -22,8 +24,8 @@
* [System Props](#system-props)
## Roadmap
-If you're looking for something to work on, a great place to start is our issues labeled [up for grabs](https://github.com/primer/components/issues?q=is%3Aopen+is%3Aissue+label%3A%22up+for+grabs%22)! If you've got a feature that you'd like to implement, be sure to check out our [Primer Components Roadmap](https://github.com/primer/components/projects/3) to make sure it hasn't already been started on.
+If you're looking for something to work on, a great place to start is our issues labeled [up for grabs](https://github.com/primer/components/issues?q=is%3Aopen+is%3Aissue+label%3A%22up+for+grabs%22)! If you've got a feature that you'd like to implement, be sure to check out our [Primer Components Roadmap](https://github.com/primer/components/projects/3) to make sure it hasn't already been started on.
## Developing components
@@ -31,7 +33,7 @@ We primarily use our documentation site as a workspace to develop new components
To get the documentation site running locally run the following in your terminal:
-```
+```sh
yarn start
```
@@ -42,27 +44,31 @@ Navigate to http://localhost:8000/ to see the site in your browser ✨
1. We use [styled-components](https://www.styled-components.com/) to style our components.
2. We use style functions from [styled-system](https://styled-system.com/) whenever possible, and styled-systems' `style()` function to create new ones.
-
### Component patterns
With a couple of exceptions, all components should be created with the `styled` function from [styled-components] and should have the appropriate groups of system props attached.
Default values for system props can be set in `Component.defaultProps`.
-Prop Types from system props such as `COMMON` or `TYPOGRAPHY` as well as styled-system functions can be spread in the component's prop types declaration (see example below).
+Prop Types from system props such as `COMMON` or `TYPOGRAPHY` as well as styled-system functions can be spread in the component's prop types declaration (see example below). These need to go *after* any built-in styles that you want to be overridable.
⚠️ **Make sure to always set the default `theme` prop to our [theme](https://github.com/primer/components/blob/master/src/theme.js)! This allows consumers of our components to access our theme values without a ThemeProvider.**
+Additionally, every component should support [the `sx` prop](https://primer.style/components/overriding-styles); remember to add `${sx}` to the style literal and `...sx.propTypes` to the component's `propTypes`.
+
Here's an example of a basic component written in the style of Primer Components:
```jsx
import {TYPOGRAPHY, COMMON} from './constants'
import theme from './theme'
+import sx from './sx
const Component = styled.div`
- ${TYPOGRAPHY};
- ${COMMON};
// additional styles here
+
+ ${COMMON};
+ ${TYPOGRAPHY};
+ ${sx};
`
Component.defaultProps = {
@@ -73,13 +79,15 @@ Component.defaultProps = {
Component.propTypes = {
...COMMON.propTypes,
- ...TYPOGRAPHY.propTypes
+ ...TYPOGRAPHY.propTypes,
+ ...sx.propTypes
}
export default Component
```
### Adding default theme
+
Each component needs access to our default Primer Theme, so that users of the component can access theme values easily in their consuming applications.
To add the default theme to a component, import the theme and assign it to the component's defaultProps object:
@@ -94,6 +102,7 @@ Component.defaultProps = {
```
### Adding system props
+
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.
Categories of system props are exported from `src/constants.js`:
@@ -101,8 +110,9 @@ Categories of system props are exported from `src/constants.js`:
* `COMMON` includes color and spacing (margin and padding) props
* `TYPOGRAPHY` includes font family, font weight, and line-height props
* `POSITION` includes positioning props
-* `FLEX_CONTAINER` includes flexbox props for containers
-* `FLEX_ITEM` includes flexbox props for items in a flex container
+* `FLEX` includes flexbox props
+* `BORDER` includes border and box-shadow props
+* `GRID` includes grid props
To give the component access to a group of system props, import the system prop function from `./constants` and include the system prop function in your styled-component like so:
@@ -110,8 +120,8 @@ To give the component access to a group of system props, import the system prop
import {COMMON} from './constants'
const Component = styled.div`
- ${COMMON};
// additional styles here
+ ${COMMON};
`
// don't forget to also add it to your prop type declaration!
@@ -121,17 +131,42 @@ Component.propTypes = {
}
```
+Remember that the system prop function inside your style declaration needs to go *after* any built-in styles you want to be overridable.
+
+### Adding the `sx` prop
+
+Each component should provide access to a prop called `sx` that allows for setting theme-aware ad-hoc styles. See the [overriding styles](https://primer.style/components/overriding-styles) doc for more information on using the prop.
+
+Adding the `sx` prop is similar to adding system props; import the default export from the `sx` module, add it to your style definition, and add the appropriate prop types. **The `sx` prop should go at the *very end* of your style definition.**
+
+```jsx
+import {COMMON} from './constants'
+import sx from './sx'
+
+const Component = styled.div`
+ // additional styles here
+ ${COMMON};
+ ${sx};
+`
+
+// don't forget to also add it to your prop type declaration!
+
+Component.propTypes = {
+ ...COMMON.propTypes,
+ ...sx.propTypes
+}
+```
+
### Linting
We use the [React configuration](https://github.com/github/eslint-plugin-github/blob/master/lib/configs/react.js) from [GitHub's eslint plugin](https://github.com/github/eslint-plugin-github) to lint our JavaScript. To check your work before pushing, run:
-```
+```sh
yarn run lint
```
Or, you can use [npx] to run eslint on one or more specific files:
-
```sh
# lint the component and the tests in src/__tests__
npx eslint src/**/MyComponent.js
@@ -149,7 +184,7 @@ yarn run lint -- --fix
We test our components with [Jest](https://facebook.github.io/jest/) and [react-test-renderer](https://reactjs.org/docs/test-renderer.html). You can run the tests locally with `yarn test`. To run the tests as you work, run Jest in watch mode with:
-```
+```sh
yarn test -- --watch
```
@@ -159,15 +194,15 @@ See [`src/__tests__/example.js`](src/__tests__/example.js) for examples of ways
Several of the projects that consume Primer Components are written in TypeScript. Though Primer Components is not currently written in TS, we do export type definitions in order to make Primer Components compatible with other TS projects.
-Whenever adding new components or modifying the props of an existing component, **please make sure to update the type definition** in `index.d.ts`! This is super important to make sure we don't break compatibility :)
+Whenever adding new components or modifying the props of an existing component, **please make sure to update the type definition** in `index.d.ts`! This is super important to make sure we don't break compatibility :)
### Additional resources
-- [Primer Components Philosophy](https://primer.style/components/philosophy)
-- [Primer Components Core Concepts](https://primer.style/components/core-concepts)
-- [Primer Components System Props](https://primer.style/components/system-props)
-- [Styled Components docs](https://styled-components.com/)
-- [Styled System docs](https://styled-system.com/)
+* [Primer Components Philosophy](https://primer.style/components/philosophy)
+* [Primer Components Core Concepts](https://primer.style/components/core-concepts)
+* [Primer Components System Props](https://primer.style/components/system-props)
+* [Styled Components docs](https://styled-components.com/)
+* [Styled System docs](https://styled-system.com/)
## Writing documentation
@@ -180,37 +215,41 @@ To add a new component to our documentation site, create a new file with the `.m
When creating a new pull request, please follow the guidelines in the auto-populated pull request template. Be sure to add screenshots of any relevant work and a thoughtful description.
### What to expect after opening a pull request
-After opening a pull request, a member of the design systems team will add the appropriate labels (major, minor, patch release labels) and update the base branch to the correct release branch. Usually, you'll receive a response from the design systems team within a day or two. The design systems team member will review the pull request keeping the following items in mind:
+After opening a pull request, a member of the design systems team will add the appropriate labels (major, minor, patch release labels) and update the base branch to the correct release branch. Usually, you'll receive a response from the design systems team within a day or two. The design systems team member will review the pull request keeping the following items in mind:
### What we look for in reviews
-- 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 Componets!)
-- Does the component follow our [Primer Components code style](#component-patterns)?
-- Does the component use theme values for most CSS values?
-- Does the component have access to the [default theme](#adding-default-theme)?
-- Does the component have the [correct system props implemented](#adding-system-props)?
-- Is the component API intuitive?
-- Does the component have the appropriate [type definitions in `index.d.ts`](#typescript-support)?
-- Is the component documented accurately?
-- Does the component have appropriate tests?
-- Does the pull request increase the bundle size significantly?
-
-If everything looks great, the design systems team member will approve the pull request and merge when appropriate. Minor and patch changes are released frequently, and we try to bundle up breaking changes and avoid shipping major versions too often. If your pull request is time-senstive, please let a design systems team member know. You do not need to worry about merging pull requests on your own, we'll take care of that for you :)
+
+* 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!)
+* Does the component follow our [Primer Components code style](#component-patterns)?
+* Does the component use theme values for most CSS values?
+* Does the component have access to the [default theme](#adding-default-theme)?
+* Does the component have the [correct system props implemented](#adding-system-props)?
+* Is the component API intuitive?
+* Does the component have the appropriate [type definitions in `index.d.ts`](#typescript-support)?
+* Is the component documented accurately?
+* Does the component have appropriate tests?
+* Does the pull request increase the bundle size significantly?
+
+If everything looks great, the design systems team member will approve the pull request and merge when appropriate. Minor and patch changes are released frequently, and we try to bundle up breaking changes and avoid shipping major versions too often. If your pull request is time-sensitive, please let a design systems team member know. You do not need to worry about merging pull requests on your own, we'll take care of that for you :)
## Deploying and publishing
### Deploying
-All of our documentation sites use the [Now integration](https://github.com/organizations/primer/settings/installations/1007619) to deploy documentation changes whenever code is merged into master. The integration also creates a preview site every time you commit code to a branch. To view the preview site, navigate to the PR and find the comment from the `now` bot. This will include a link to the preview site for your branch.
+
+All of our documentation sites use the [Now integration](https://github.com/organizations/primer/settings/installations/1007619) to deploy documentation changes whenever code is merged into master. The integration also creates a preview site every time you commit code to a branch. To view the preview site, navigate to the PR and find the comment from the `now` bot. This will include a link to the preview site for your branch.
Once you merge your branch into master, any changes to the docs will automatically deploy. No further action is necessary.
### Path aliasing
+
This site is served as a subdirectory of [primer.style] using a [path alias](https://zeit.co/docs/features/path-aliases) configured in that repo's [`rules.json`](https://github.com/primer/primer.style/tree/master/rules.json). If you change the production deployment URL for this app, you will also need to change it there and re-deploy that app; otherwise, Now will automatically route requests from [primer.style/components](https://primer.style/components/) to the new deployment whenever you alias this one to `primer-components.now.sh`.
### Publishing
+
We use a custom GitHub Actions to handle all of our processes relating to publishing to NPM. This includes release candidates, canary releases, and publishing the final release.
-The [publish GitHub Action](https://github.com/primer/publish) will automatically publish a canary release for each commit to a branch. If the branch is prefixed with `release-` it will publish a release candidate. To find the canary release or release candidtate, navigate to the PR and find the `publish` check in the merge box. Clicking on the `details` link for the check will navigate you to the unpkg page for that canary release/release candidate. For more documentation on our publish GitHub Action and workflows, please refer to the [`@primer/publish` repo](https://github.com/primer/publish).
+The [publish GitHub Action](https://github.com/primer/publish) will automatically publish a canary release for each commit to a branch. If the branch is prefixed with `release-` it will publish a release candidate. To find the canary release or release candidate, navigate to the PR and find the `publish` check in the merge box. Clicking on the `details` link for the check will navigate you to the unpkg page for that canary release/release candidate. For more documentation on our publish GitHub Action and workflows, please refer to the [`@primer/publish` repo](https://github.com/primer/publish).
## Troubleshooting
@@ -225,6 +264,7 @@ Ensure you are using the latest minor of Node.js for the major version specified
## Glossary
### System props
+
System props are style functions that provide one or more props, and can be passed directly the return value of [styled-components]'s `styled()` function:
```jsx
diff --git a/docs/content/overriding-styles.mdx b/docs/content/overriding-styles.mdx
new file mode 100644
index 00000000000..5d227a4c7ff
--- /dev/null
+++ b/docs/content/overriding-styles.mdx
@@ -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
+
+
+ Heading with bottom border
+
+```
+
+## 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
+
+ Responsive background color
+
+```
+
+## 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
+ *': {
+ borderWidth: 1,
+ borderColor: 'border.gray',
+ borderStyle: 'solid',
+ borderBottomWidth: 0,
+ padding: 2,
+ ':last-child': {
+ borderBottomWidth: 1
+ },
+ ':hover': {
+ bg: 'gray.1'
+ }
+ }
+}}>
+ First
+ Second
+ Third
+
+```
diff --git a/docs/content/primer-theme.md b/docs/content/primer-theme.md
index d29173ce227..0a5e58a0a9a 100644
--- a/docs/content/primer-theme.md
+++ b/docs/content/primer-theme.md
@@ -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 `` set the `maxWidth` prop to `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 `` set the `maxWidth` prop to `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 `` from [styled-components]:
- ```jsx
+ ```javascript
import {Block, Button, Text, theme as primer} from '@primer/components'
import {ThemeProvider} from 'styled-components'
@@ -46,29 +44,29 @@ There are two ways to change the theme of Primer components:
```
**⚠️ Note: [styled-components]'s `` 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 (
-
- // matching keys in customTheme will override keys in the Primer theme
-