|
| 1 | +--- |
| 2 | +pageClass: 'rule-details' |
| 3 | +sidebarDepth: 0 |
| 4 | +title: 'svelte/no-inline-styles' |
| 5 | +description: 'disallow attributes and directives that produce inline styles' |
| 6 | +--- |
| 7 | + |
| 8 | +# svelte/no-inline-styles |
| 9 | + |
| 10 | +> disallow attributes and directives that produce inline styles |
| 11 | +
|
| 12 | +- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> |
| 13 | + |
| 14 | +## :book: Rule Details |
| 15 | + |
| 16 | +This rule reports all attributes and directives that would compile to inline styles. This is mainly useful when adding Content Security Policy to your app, as having inline styles requires the `style-src: 'unsafe-inline'` directive, which is generally discouraged and unsafe. |
| 17 | + |
| 18 | +<ESLintCodeBlock> |
| 19 | + |
| 20 | +<!--eslint-skip--> |
| 21 | + |
| 22 | +```svelte |
| 23 | +<script> |
| 24 | + /* eslint svelte/no-inline-styles: "error" */ |
| 25 | +
|
| 26 | + import { fade } from 'svelte/transition'; |
| 27 | +
|
| 28 | + export let classTwo; |
| 29 | + export let blockDisplay; |
| 30 | +</script> |
| 31 | +
|
| 32 | +<!-- ✓ GOOD --> |
| 33 | +<span class="one">Hello World!</span> |
| 34 | +
|
| 35 | +<span class:two={classTwo}>Hello World!</span> |
| 36 | +
|
| 37 | +<!-- ✗ BAD --> |
| 38 | +<span style="display: block;">Hello World!</span> |
| 39 | +
|
| 40 | +<span style:display={blockDisplay ? 'block' : 'inline'}>Hello World!</span> |
| 41 | +
|
| 42 | +<span transition:fade>Hello World!</span> |
| 43 | +``` |
| 44 | + |
| 45 | +</ESLintCodeBlock> |
| 46 | + |
| 47 | +## :wrench: Options |
| 48 | + |
| 49 | +```json |
| 50 | +{ |
| 51 | + "svelte/no-inline-styles": [ |
| 52 | + "error", |
| 53 | + { |
| 54 | + "allowTransitions": false |
| 55 | + } |
| 56 | + ] |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +- `allowTransitions` ... Most svelte transitions (including the built-in ones) use inline styles. However, it is theoretically possible to only use transitions that don't (see this [issue](https://github.com/sveltejs/svelte/issues/6662) about removing inline styles from built-in transitions). This option allows transitions to be used in such cases. Default `false`. |
| 61 | + |
| 62 | +## :books: Further Reading |
| 63 | + |
| 64 | +- [CSP documentation](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) |
| 65 | + |
| 66 | +## :mag: Implementation |
| 67 | + |
| 68 | +- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/src/rules/no-inline-styles.ts) |
| 69 | +- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/tests/src/rules/no-inline-styles.ts) |
0 commit comments