Skip to content

Commit b6fe490

Browse files
committed
chore: update doc
1 parent caa1513 commit b6fe490

File tree

1 file changed

+59
-4
lines changed

1 file changed

+59
-4
lines changed

docs/rules/no-dom-manipulating.md

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,81 @@ We don't recommend but If you intentionally manipulate the DOM, simply you can i
2424
```svelte
2525
<script>
2626
/* eslint svelte/no-dom-manipulating: "error" */
27-
let div
28-
let show
27+
let foo, bar, show
2928
3029
/* ✓ GOOD */
3130
const toggle = () => (show = !show)
3231
3332
/* ✗ BAD */
34-
const remove = () => div.remove()
33+
const remove = () => foo.remove()
34+
const update = () => (bar.textContent = "Update!")
3535
</script>
3636
3737
{#if show}
38-
<div bind:this={div}>div</div>
38+
<div bind:this={foo}>Foo</div>
3939
{/if}
40+
<div bind:this={bar}>
41+
{#if show}
42+
Bar
43+
{/if}
44+
</div>
4045
4146
<button on:click={() => toggle()}>Click Me (Good)</button>
4247
<button on:click={() => remove()}>Click Me (Bad)</button>
48+
<button on:click={() => update()}>Click Me (Bad)</button>
4349
```
4450

4551
</ESLintCodeBlock>
4652

53+
This rule only tracks and checks variables given with `bind:this={}`. In other words, it doesn't track things like function arguments given to `transition:` directives. These functions have been well tested and are often used more carefully.
54+
55+
<ESLintCodeBlock>
56+
57+
<!--eslint-skip-->
58+
59+
```svelte
60+
<script>
61+
/* eslint svelte/no-dom-manipulating: "error" */
62+
let visible = false
63+
64+
function typewriter(node, { speed = 1 }) {
65+
const valid =
66+
node.childNodes.length === 1 &&
67+
node.childNodes[0].nodeType === Node.TEXT_NODE
68+
69+
if (!valid) {
70+
throw new Error(
71+
`This transition only works on elements with a single text node child`,
72+
)
73+
}
74+
75+
const text = node.textContent
76+
const duration = text.length / (speed * 0.01)
77+
78+
return {
79+
duration,
80+
tick: (t) => {
81+
const i = Math.trunc(text.length * t)
82+
node.textContent = text.slice(0, i) // It does not report.
83+
},
84+
}
85+
}
86+
</script>
87+
88+
<label>
89+
<input type="checkbox" bind:checked={visible} />
90+
visible
91+
</label>
92+
93+
{#if visible}
94+
<p transition:typewriter>The quick brown fox jumps over the lazy dog</p>
95+
{/if}
96+
```
97+
98+
</ESLintCodeBlock>
99+
100+
See also <https://svelte.jp/examples/custom-js-transitions>.
101+
47102
## :wrench: Options
48103

49104
Nothing.

0 commit comments

Comments
 (0)