|
| 1 | +# use-styled-react-import |
| 2 | + |
| 3 | +💼 This rule is _disabled_ in the ✅ `recommended` config. |
| 4 | + |
| 5 | +🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). |
| 6 | + |
| 7 | +<!-- end auto-generated rule header --> |
| 8 | + |
| 9 | +Enforce importing components that use `sx` prop from `@primer/styled-react` instead of `@primer/react`, and vice versa. |
| 10 | + |
| 11 | +## Rule Details |
| 12 | + |
| 13 | +This rule detects when certain Primer React components are used with the `sx` prop and ensures they are imported from the temporary `@primer/styled-react` package instead of `@primer/react`. When the same components are used without the `sx` prop, it ensures they are imported from `@primer/react` instead of `@primer/styled-react`. |
| 14 | + |
| 15 | +When a component is used both with and without the `sx` prop in the same file, the rule will import the styled version with an alias (e.g., `StyledButton`) and update the JSX usage accordingly to avoid naming conflicts. |
| 16 | + |
| 17 | +It also moves certain types and utilities to the styled-react package. |
| 18 | + |
| 19 | +### Components that should be imported from `@primer/styled-react` when used with `sx`: |
| 20 | + |
| 21 | +- ActionList |
| 22 | +- ActionMenu |
| 23 | +- Box |
| 24 | +- Breadcrumbs |
| 25 | +- Button |
| 26 | +- Flash |
| 27 | +- FormControl |
| 28 | +- Heading |
| 29 | +- IconButton |
| 30 | +- Label |
| 31 | +- Link |
| 32 | +- LinkButton |
| 33 | +- PageLayout |
| 34 | +- Text |
| 35 | +- TextInput |
| 36 | +- Truncate |
| 37 | +- Octicon |
| 38 | +- Dialog |
| 39 | + |
| 40 | +### Types and utilities that should always be imported from `@primer/styled-react`: |
| 41 | + |
| 42 | +- `BoxProps` (type) |
| 43 | +- `SxProp` (type) |
| 44 | +- `BetterSystemStyleObject` (type) |
| 45 | +- `sx` (utility) |
| 46 | + |
| 47 | +## Examples |
| 48 | + |
| 49 | +### ❌ Incorrect |
| 50 | + |
| 51 | +```jsx |
| 52 | +import {Button, Link} from '@primer/react' |
| 53 | + |
| 54 | +const Component = () => <Button sx={{color: 'red'}}>Click me</Button> |
| 55 | +``` |
| 56 | + |
| 57 | +```jsx |
| 58 | +import {Box} from '@primer/react' |
| 59 | + |
| 60 | +const Component = () => <Box sx={{padding: 2}}>Content</Box> |
| 61 | +``` |
| 62 | + |
| 63 | +```jsx |
| 64 | +import {sx} from '@primer/react' |
| 65 | +``` |
| 66 | + |
| 67 | +```jsx |
| 68 | +import {Button} from '@primer/styled-react' |
| 69 | + |
| 70 | +const Component = () => <Button>Click me</Button> |
| 71 | +``` |
| 72 | + |
| 73 | +```jsx |
| 74 | +import {Button} from '@primer/react' |
| 75 | + |
| 76 | +const Component1 = () => <Button>Click me</Button> |
| 77 | +const Component2 = () => <Button sx={{color: 'red'}}>Styled me</Button> |
| 78 | +``` |
| 79 | + |
| 80 | +### ✅ Correct |
| 81 | + |
| 82 | +```jsx |
| 83 | +import {Link} from '@primer/react' |
| 84 | +import {Button} from '@primer/styled-react' |
| 85 | + |
| 86 | +const Component = () => <Button sx={{color: 'red'}}>Click me</Button> |
| 87 | +``` |
| 88 | + |
| 89 | +```jsx |
| 90 | +import {Box} from '@primer/styled-react' |
| 91 | + |
| 92 | +const Component = () => <Box sx={{padding: 2}}>Content</Box> |
| 93 | +``` |
| 94 | + |
| 95 | +```jsx |
| 96 | +import {sx} from '@primer/styled-react' |
| 97 | +``` |
| 98 | + |
| 99 | +```jsx |
| 100 | +// Components without sx prop can stay in @primer/react |
| 101 | +import {Button} from '@primer/react' |
| 102 | + |
| 103 | +const Component = () => <Button>Click me</Button> |
| 104 | +``` |
| 105 | + |
| 106 | +```jsx |
| 107 | +// Components imported from styled-react but used without sx prop should be moved back |
| 108 | +import {Button} from '@primer/react' |
| 109 | + |
| 110 | +const Component = () => <Button>Click me</Button> |
| 111 | +``` |
| 112 | + |
| 113 | +```jsx |
| 114 | +// When a component is used both ways, use an alias for the styled version |
| 115 | +import {Button} from '@primer/react' |
| 116 | +import {Button as StyledButton} from '@primer/styled-react' |
| 117 | + |
| 118 | +const Component1 = () => <Button>Click me</Button> |
| 119 | +const Component2 = () => <StyledButton sx={{color: 'red'}}>Styled me</StyledButton> |
| 120 | +``` |
| 121 | + |
| 122 | +## Options |
| 123 | + |
| 124 | +This rule has no options. |
| 125 | + |
| 126 | +## When Not To Use It |
| 127 | + |
| 128 | +This rule is specifically for migrating components that use the `sx` prop to the temporary `@primer/styled-react` package. If you're not using the `sx` prop or not participating in this migration, you can disable this rule. |
0 commit comments