|
| 1 | +--- |
| 2 | +title: Stylelint Polaris |
| 3 | +description: A configuration of Stylelint rules that promote adoption and track coverage of the Polaris design system in consuming apps. |
| 4 | +icon: WandMajor |
| 5 | +collapseChildren: true |
| 6 | +keywords: |
| 7 | + - stylelint |
| 8 | + - dev tools |
| 9 | + - developer tools |
| 10 | + - tools |
| 11 | + - tooling |
| 12 | + - development |
| 13 | + - plugin |
| 14 | + - rules |
| 15 | + - linter |
| 16 | + - linting |
| 17 | + - css |
| 18 | +--- |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +```sh |
| 23 | +yarn add -D @shopify/stylelint-polaris stylelint |
| 24 | +``` |
| 25 | + |
| 26 | +> Note: `stylelint-polaris` requires a peer dependency of `stylelint@>=14.15.0` |
| 27 | +
|
| 28 | +[Source code](https://github.com/Shopify/polaris/tree/main/stylelint-polaris) |
| 29 | + |
| 30 | +## Usage |
| 31 | + |
| 32 | +Extend `@shopify/stylelint-polaris` in your [Stylelint config](https://stylelint.io/user-guide/configure/). Example in `package.json` |
| 33 | + |
| 34 | +```json |
| 35 | +{ |
| 36 | + "stylelint": { |
| 37 | + "extends": ["@shopify/stylelint-polaris"] |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +> IMPORTANT: `@shopify/stylelint-polaris` must be added to the end of the `extends` array |
| 43 | +
|
| 44 | +### Run the linter |
| 45 | + |
| 46 | +```sh |
| 47 | +npx stylelint '**/*.{css,scss}' |
| 48 | +``` |
| 49 | + |
| 50 | +### Run the linter and autofix failures |
| 51 | + |
| 52 | +```sh |
| 53 | +npx stylelint --fix '**/*.{css,scss}' |
| 54 | +``` |
| 55 | + |
| 56 | +### Ignoring existing failures |
| 57 | + |
| 58 | +Enabling the linter could result in a large amount of warnings and errors in existing codebases. It is important to fix as many failures upfront as possible, but that shouldn't block the linter from being added. The [styles-insert-stylelint-disable](https://github.com/Shopify/polaris/tree/main/polaris-migrator#v10) migration inserts [ignore comments](https://stylelint.io/user-guide/ignore-code/) so that enabling `stylelint-polaris` can be unblocked. |
| 59 | + |
| 60 | +The migration will insert comments as follows: |
| 61 | + |
| 62 | +```diff |
| 63 | ++ // stylelint-disable-next-line -- generated by polaris-migrator DO NOT COPY |
| 64 | +padding: 1rem; |
| 65 | +``` |
| 66 | + |
| 67 | +Run the following command substituting `<path>` with a glob pattern of files to run against: |
| 68 | + |
| 69 | +```sh |
| 70 | +npx @shopify/polaris-migrator styles-insert-stylelint-disable <path> |
| 71 | +``` |
| 72 | + |
| 73 | +## Rules |
| 74 | + |
| 75 | +There are over 40 rules configured in Stylelint Polaris to help you avoid errors and follow stylistic and non-stylistic conventions while building for the Shopify admin. |
| 76 | + |
| 77 | +- [Colors](/tools/stylelint-polaris/rules#colors) |
| 78 | +- [Conventions](/tools/stylelint-polaris/rules#conventions) |
| 79 | +- [Depth](/tools/stylelint-polaris/rules#depth) |
| 80 | +- [Layout](/tools/stylelint-polaris/rules#layout) |
| 81 | +- [Legacy](/tools/stylelint-polaris/rules#legacy) |
| 82 | +- [Media queries](/tools/stylelint-polaris/rules#media-queries) |
| 83 | +- [Motion](/tools/stylelint-polaris/rules#motion) |
| 84 | +- [Shape](/tools/stylelint-polaris/rules#shape) |
| 85 | +- [Spacing](/tools/stylelint-polaris/rules#spacing) |
| 86 | +- [Typography](/tools/stylelint-polaris/rules#typography) |
| 87 | +- [Z-index](/tools/stylelint-polaris/rules#z-index) |
| 88 | + |
| 89 | +## Development |
| 90 | + |
| 91 | +### Add new rules |
| 92 | + |
| 93 | +1. Navigate to the root [`stylelint-polaris` config](https://github.com/Shopify/polaris/blob/main/stylelint-polaris/index.js) |
| 94 | +1. Locate the `stylelint-polaris/coverage` options |
| 95 | +1. Identify the appropriate category for the new rule |
| 96 | +1. Insert the rule using standard Stylelint [rule configurations](https://stylelint.io/user-guide/configure#rules) |
| 97 | +1. Add documentation for the rule with examples of code that will be reported as a problem and code that will fix the problem |
| 98 | +1. The title should be the category + the stylelint rule name, for example `### colors/color-named` |
| 99 | + |
| 100 | +```js |
| 101 | +module.exports = { |
| 102 | + rules: { |
| 103 | + 'polaris/coverage': { |
| 104 | + colors: {...}, // Standard Stylelint rules config |
| 105 | + layout: {...}, // Standard Stylelint rules config |
| 106 | + motion: { |
| 107 | + 'new-rule': 'new-rule-options', |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | +}; |
| 112 | +``` |
| 113 | + |
| 114 | +### Build custom rules |
| 115 | + |
| 116 | +1. Refer to the [Writing plugins](https://stylelint.io/developer-guide/plugins) guide of the Stylelint documentation |
| 117 | +1. Create your rule in the [plugins](plugins) directory |
| 118 | +1. Validate your plugin with [tests](https://github.com/stylelint/jest-preset-stylelint#usage) (reference sibling plugins for examples) |
| 119 | +1. Refer to the [Add new rules](#add-new-rules) section to add your custom rule to the `stylelint-polaris` config |
| 120 | + |
| 121 | +### Add custom messages |
| 122 | + |
| 123 | +Custom messages are surfaced in the command line, CI, and supported editors along side the default `stylelint` rule messages. They are added to the root level config and aim to provide more insight on how to resolve rule violations. |
| 124 | + |
| 125 | +In a majority of cases, the default rule messages are clear and concise. However, they don't always guide developers to a desired outcome. Thus, there are two mechanisms we suggest for improving and providing custom rule messages: |
| 126 | + |
| 127 | +Set a generic custom message on the `message` property of the secondary options of a given `stylelint-polaris/coverage` category. This message is appended to the default rule message and we expect will cover most cases. |
| 128 | + |
| 129 | +```js |
| 130 | +module.exports = { |
| 131 | + rules: { |
| 132 | + 'polaris/coverage': { |
| 133 | + colors: [ |
| 134 | + { |
| 135 | + 'color-named': 'never' |
| 136 | + 'color-no-hex': true, |
| 137 | + }, |
| 138 | + {message: 'Please use a Polaris color token: https://polaris.shopify.com/tokens/colors'}, |
| 139 | + ], |
| 140 | + }, |
| 141 | + }, |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +Example failure message: |
| 146 | + |
| 147 | +```diff |
| 148 | +- Unexpected named color "red" (color-named) |
| 149 | ++ Unexpected named color "red" (color-named) Please use a Polaris color token |
| 150 | +``` |
| 151 | + |
| 152 | +Set a custom message on the `message` property in the [Stylelint rule config's secondary options](https://stylelint.io/user-guide/configure/#message) if supported. This message is appended to the default rule message instead of the generic category message when provided. |
| 153 | + |
| 154 | +```js |
| 155 | +module.exports = { |
| 156 | + rules: { |
| 157 | + 'polaris/coverage': { |
| 158 | + layout: [ |
| 159 | + { |
| 160 | + 'property-disallowed-list': [ |
| 161 | + ['position'], |
| 162 | + {message: 'Please use the Polaris "Sticky" component'}, |
| 163 | + ], |
| 164 | + }, |
| 165 | + {message: 'Please use a Polaris layout component'}, |
| 166 | + ], |
| 167 | + }, |
| 168 | + }, |
| 169 | +}; |
| 170 | +``` |
| 171 | + |
| 172 | +Example failure message: |
| 173 | + |
| 174 | +```diff |
| 175 | +- Unexpected value "sticky" for property "position" (declaration-property-value-disallowed-list) Please use a Polaris layout component |
| 176 | ++ Unexpected value "sticky" for property "position" (declaration-property-value-disallowed-list) Please use the Polaris "Sticky" component |
| 177 | +``` |
| 178 | + |
| 179 | +### Tophat `stylelint-polaris` updates in `polaris-react` |
| 180 | + |
| 181 | +> Open your terminal to the root of the `polaris` monorepo: |
| 182 | +
|
| 183 | +1. Install and symlink dependencies |
| 184 | + |
| 185 | +```sh |
| 186 | +yarn install |
| 187 | +``` |
| 188 | + |
| 189 | +2. Build `@shopify/polaris` dependencies, but not `@shopify/polaris` itself |
| 190 | + |
| 191 | +```sh |
| 192 | +yarn build -- --filter=@shopify/polaris^... |
| 193 | +``` |
| 194 | + |
| 195 | +> Note: Remove the `^` character if you do want to build `@shopify/polaris` |
| 196 | +
|
| 197 | +3. Run `stylelint` in `polaris-react` |
| 198 | + |
| 199 | +All files |
| 200 | + |
| 201 | +```sh |
| 202 | +yarn turbo run lint:styles --filter=@shopify/polaris |
| 203 | +``` |
| 204 | + |
| 205 | +Specific file |
| 206 | + |
| 207 | +```sh |
| 208 | +yarn run stylelint path/to/component.scss |
| 209 | + |
| 210 | +// yarn run stylelint polaris-react/src/components/TopBar/TopBar.scss |
| 211 | +``` |
0 commit comments