Skip to content

Commit 8d3f451

Browse files
[docs] Fix the speed of the typewriter example to scale correctly (#6568)
1 parent 3c5cea1 commit 8d3f451

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

site/content/docs/02-template-syntax.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -963,20 +963,22 @@ A custom transition function can also return a `tick` function, which is called
963963
<script>
964964
export let visible = false;
965965
966-
function typewriter(node, { speed = 50 }) {
966+
function typewriter(node, { speed = 1 }) {
967967
const valid = (
968968
node.childNodes.length === 1 &&
969969
node.childNodes[0].nodeType === Node.TEXT_NODE
970970
);
971971
972-
if (!valid) return {};
972+
if (!valid) {
973+
throw new Error(`This transition only works on elements with a single text node child`);
974+
}
973975
974976
const text = node.textContent;
975-
const duration = text.length * speed;
977+
const duration = text.length / (speed * 0.01);
976978
977979
return {
978980
duration,
979-
tick: (t, u) => {
981+
tick: t => {
980982
const i = ~~(text.length * t);
981983
node.textContent = text.slice(0, i);
982984
}
@@ -985,7 +987,7 @@ A custom transition function can also return a `tick` function, which is called
985987
</script>
986988
987989
{#if visible}
988-
<p in:typewriter="{{ speed: 20 }}">
990+
<p in:typewriter="{{ speed: 1 }}">
989991
The quick brown fox jumps over the lazy dog
990992
</p>
991993
{/if}

site/content/examples/09-transitions/04-custom-js-transitions/App.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script>
22
let visible = false;
33
4-
function typewriter(node, { speed = 50 }) {
4+
function typewriter(node, { speed = 1 }) {
55
const valid = (
66
node.childNodes.length === 1 &&
77
node.childNodes[0].nodeType === Node.TEXT_NODE
@@ -12,7 +12,7 @@
1212
}
1313
1414
const text = node.textContent;
15-
const duration = text.length * speed;
15+
const duration = text.length / (speed * 0.01);
1616
1717
return {
1818
duration,
@@ -30,7 +30,7 @@
3030
</label>
3131

3232
{#if visible}
33-
<p in:typewriter>
33+
<p in:typewriter out:typewriter>
3434
The quick brown fox jumps over the lazy dog
3535
</p>
3636
{/if}

site/content/tutorial/10-transitions/05-custom-js-transitions/app-b/App.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script>
22
let visible = false;
33
4-
function typewriter(node, { speed = 50 }) {
4+
function typewriter(node, { speed = 1 }) {
55
const valid = (
66
node.childNodes.length === 1 &&
77
node.childNodes[0].nodeType === Node.TEXT_NODE
@@ -12,7 +12,7 @@
1212
}
1313
1414
const text = node.textContent;
15-
const duration = text.length * speed;
15+
const duration = text.length / (speed * 0.01);
1616
1717
return {
1818
duration,
@@ -30,7 +30,7 @@
3030
</label>
3131

3232
{#if visible}
33-
<p in:typewriter>
33+
<p in:typewriter out:typewriter>
3434
The quick brown fox jumps over the lazy dog
3535
</p>
3636
{/if}

site/content/tutorial/10-transitions/05-custom-js-transitions/text.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: Custom JS transitions
55
While you should generally use CSS for transitions as much as possible, there are some effects that can't be achieved without JavaScript, such as a typewriter effect:
66

77
```js
8-
function typewriter(node, { speed = 50 }) {
8+
function typewriter(node, { speed = 1 }) {
99
const valid = (
1010
node.childNodes.length === 1 &&
1111
node.childNodes[0].nodeType === Node.TEXT_NODE
@@ -16,7 +16,7 @@ function typewriter(node, { speed = 50 }) {
1616
}
1717

1818
const text = node.textContent;
19-
const duration = text.length * speed;
19+
const duration = text.length / (speed * 0.01);
2020

2121
return {
2222
duration,

0 commit comments

Comments
 (0)