Skip to content

Commit

Permalink
Deprecate system props, but don't remove system props (#1336)
Browse files Browse the repository at this point in the history
* deprecate system props

* failing spec to combine sx props

* combine sx props when there is an existing sx prop in code mod

* respect spreads in system prop codemod

* run tests in codemods and in src

* changeset with codemod documentation

* respect string literal keys

* respect object accessors with spreads

* update documentations in changeset

* update documentations in changeset

* Update deprecation warning

* Update StyledOcticon docs

* Update system prop and sx prop docs

* Update changeset

Co-authored-by: Cole Bemis <colebemis@github.com>
  • Loading branch information
VanAnderson and colebemis committed Jul 22, 2021
1 parent 4c06331 commit 489a718
Show file tree
Hide file tree
Showing 44 changed files with 981 additions and 205 deletions.
26 changes: 26 additions & 0 deletions .changeset/tasty-laws-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
'@primer/components': minor
---

System props are deprecated in all components except `Box`. Move all system props into the [`sx` prop](https://primer.style/components/overriding-styles) instead. Example:

```diff
- <Button mr={2}>...</Button>
+ <Button sx={{mr: 2}}>...</Button>
```

There is a codemod available to migrate from system props to the `sx` prop:

- TypeScript example:

```shell
npx jscodeshift -t node_modules/@primer/components/codemods/removeSystemProps.js
--parser=tsx path/to/workspace/src/*.tsx
```

- Babel example:

```shell
npx jscodeshift -t node_modules/@primer/components/codemods/removeSystemProps.js
--parser=babel path/to/workspace/src/*.tsx
```
225 changes: 225 additions & 0 deletions codemods/__tests__/removeSystemProps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
import {defineInlineTest} from 'jscodeshift/dist/testUtils'
import removeSystemProps from '../removeSystemProps'

defineInlineTest(
removeSystemProps,
{},
`
import {Label} from '@primer/components'
const leftMargin = 2
export default () => (
<Label mr={1} ml={leftMargin}>
<Text>hi</Text>
</Label>
)
`.trim(),
`
import {Label} from '@primer/components'
const leftMargin = 2
export default () => (
<Label sx={{mr: 1, ml: leftMargin}}>
<Text>hi</Text>
</Label>
)
`.trim(),
'removeSystemProps'
)

defineInlineTest(
removeSystemProps,
{},
`
import {Button, StyledOcticon} from '@primer/components'
import {CheckIcon, ClippyIcon} from '@primer/octicons-react'
const ClipboardCopy = ({value}) => <Button px={2}>
{copied ? (
<StyledOcticon icon={CheckIcon} color="green.5" />
) : (
<StyledOcticon icon={ClippyIcon} color="gray.5" />
)}
</Button>
`.trim(),
`
import {Button, StyledOcticon} from '@primer/components'
import {CheckIcon, ClippyIcon} from '@primer/octicons-react'
const ClipboardCopy = ({value}) => (
<Button sx={{px: 2}}>
{copied ? (
<StyledOcticon icon={CheckIcon} sx={{color: 'green.5'}} />
) : (
<StyledOcticon icon={ClippyIcon} sx={{color: 'gray.5'}} />
)}
</Button>
)`.trim(),
'removeSystemProps'
)

defineInlineTest(
removeSystemProps,
{},
`
import {Link} from '@primer/components'
const siteMetadata = {shortName: 'inline-block'}
const link = <Link
display={[
// We only hide "Primer" on small viewports if a shortName is defined.
siteMetadata.shortName ? 'none' : 'inline-block',
null,
null,
'inline-block',
]}
href="https://primer.style"
color="blue.4"
fontFamily="mono"
>
Primer
</Link>
`.trim(),
`
import {Link} from '@primer/components'
const siteMetadata = {shortName: 'inline-block'}
const link = (
<Link
href="https://primer.style"
sx={{
display: [siteMetadata.shortName ? 'none' : 'inline-block', null, null, 'inline-block'],
color: 'blue.4',
fontFamily: 'mono'
}}
>
Primer
</Link>
)`.trim(),
'removeSystemProps'
)

defineInlineTest(
removeSystemProps,
{},
`
import {Label} from '@primer/components'
const leftMargin = 2
export default () => (
<Label mr={1} ml={leftMargin} sx={{p: 1}}>
<Text>hi</Text>
</Label>
)
`.trim(),
`
import {Label} from '@primer/components'
const leftMargin = 2
export default () => (
<Label sx={{p: 1, mr: 1, ml: leftMargin}}>
<Text>hi</Text>
</Label>
)
`.trim(),
'removeSystemProps'
)

defineInlineTest(
removeSystemProps,
{},
`
import {Label} from '@primer/components'
const leftMargin = 2
export default () => (
<Label mr={1} ml={leftMargin} sx={{p:3}}>
<Text>hi</Text>
</Label>
)
`.trim(),
`
import {Label} from '@primer/components'
const leftMargin = 2
export default () => (
<Label sx={{p: 3, mr: 1, ml: leftMargin}}>
<Text>hi</Text>
</Label>
)
`.trim(),
'removeSystemProps'
)

defineInlineTest(
removeSystemProps,
{},
`
import {Label} from '@primer/components'
const leftMargin = 2
const colorProps = {backgroundColor: 'red'}
const borderProps = {borderColor: 'red'}
export default () => (
<Label mr={1} ml={leftMargin} sx={{...colorProps, ...borderProps, p:3}}>
<Text>hi</Text>
</Label>
)
`.trim(),
`
import {Label} from '@primer/components'
const leftMargin = 2
const colorProps = {backgroundColor: 'red'}
const borderProps = {borderColor: 'red'}
export default () => (
<Label sx={{...colorProps, ...borderProps, p: 3, mr: 1, ml: leftMargin}}>
<Text>hi</Text>
</Label>
)
`.trim(),
'removeSystemProps'
)

defineInlineTest(
removeSystemProps,
{},
`
import {Label} from '@primer/components'
const leftMargin = 2
export default () => (
<Label mr={1} sx={{'&:hover': {textDecoration: 'none'}}}>
<Text>hi</Text>
</Label>
)
`.trim(),
`
import {Label} from '@primer/components'
const leftMargin = 2
export default () => (
<Label sx={{'&:hover': {textDecoration: 'none'}, mr: 1}}>
<Text>hi</Text>
</Label>
)
`.trim(),
'removeSystemProps'
)

defineInlineTest(
removeSystemProps,
{},
`
import {Label} from '@primer/components'
const leftMargin = 2
const colorProps = {dark: {backgroundColor: 'red'}}
export default () => (
<Label mr={1} ml={leftMargin} sx={{...colorProps.dark, p:3}}>
<Text>hi</Text>
</Label>
)
`.trim(),
`
import {Label} from '@primer/components'
const leftMargin = 2
const colorProps = {dark: {backgroundColor: 'red'}}
export default () => (
<Label sx={{...colorProps.dark, p: 3, mr: 1, ml: leftMargin}}>
<Text>hi</Text>
</Label>
)
`.trim(),
'removeSystemProps'
)
Loading

1 comment on commit 489a718

@vercel
Copy link

@vercel vercel bot commented on 489a718 Jul 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

Warning: primer-components.now.sh was not aliased since new "now.sh" domains are deprecated. Please use "vercel.app" instead. Learn More →

Please sign in to comment.