-
Notifications
You must be signed in to change notification settings - Fork 645
Add sx prop for theme-aware ad-hoc styles
#755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 fd5fbe5
Get styled-components from the root project
BinaryMuse 39af9e7
Add withSx Hoc
BinaryMuse c98df7c
Wrap Heading in withSx
BinaryMuse c5145bd
Refactor sx prop
BinaryMuse bcaa4c0
Update components from withSx => sx.styled
BinaryMuse 9ef246f
Remove temporary variable
BinaryMuse 7bec957
:art:
BinaryMuse 512df86
Revert styled-component update
BinaryMuse c5c398f
:art:
BinaryMuse 44d33f9
Merge remote-tracking branch 'origin/release-19.0.0' into mkt/sx-prop
BinaryMuse bf285df
Add sx.propTypes
BinaryMuse 96de6cb
Make all components sx-prop aware and convert test suites
BinaryMuse b421aba
Add sx prop info to CONTRIBUTING and fix spelling and linting errors
BinaryMuse 272f7b2
Add initial pass at sx-prop docs
BinaryMuse 03703ce
Remove weird pattern in SelectMenuFilter sx prop
BinaryMuse c862012
Update Dialog definition
BinaryMuse e0a165f
Add missing system props types to CONTRIBUTING
BinaryMuse b859e4d
Update sx props doc
BinaryMuse 3c6bdf8
Add responsive sx prop example
BinaryMuse cff9e05
Move 'When to use the sx prop' section up
BinaryMuse fa3943e
Fix formatting and links in Primer Theme docs
BinaryMuse e9d9c33
Add note on custom theme to sx prop docs
BinaryMuse c4f78d0
Modify sx prop guidelines
BinaryMuse 398706b
Use SystemStyleObject from @styled-system/css in TS sx prop typings
BinaryMuse 72e2bfe
Merge remote-tracking branch 'origin/master' into mkt/sx-prop
BinaryMuse 5d99108
Merge remote-tracking branch 'origin/release-19.0.0' into mkt/sx-prop
BinaryMuse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,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> | ||
| ``` | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.