Skip to content

Commit f6f3b51

Browse files
committed
docs: better event phase demo
1 parent 6e3eb3b commit f6f3b51

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

site/src/routes/demo/common/_EventsAndModifiers.svelte

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@
2626
</div>
2727

2828
<div>
29-
<Button on:click$capture={addEventCapture} on:click={addEventBubble}>
29+
Try clicking and using the Enter key to activate this next button. (The click
30+
event's target is an element below the button while the key event's target is
31+
the button.)
32+
</div>
33+
34+
<div>
35+
<Button on:click$capture={addEventPhase} on:click={addEventPhase}>
3036
<Label>Capture and Bubble Phase Listeners</Label>
3137
</Button>
3238
</div>
@@ -40,17 +46,18 @@
4046
</div>
4147

4248
<div>
43-
<Button
49+
<A
4450
href="http://example.com"
4551
on:click$preventDefault={() =>
4652
console.log("You tried to go, but didn't make it.")}
4753
>
48-
<Label>A Link Button, with Default Prevented</Label>
49-
</Button>
54+
A Link, with Default Prevented
55+
</A>
5056
</div>
5157

5258
<script>
5359
import Button, { Label } from '@smui/button';
60+
import A from '@smui/common/A.svelte';
5461
5562
let eventOutput;
5663
let eventPhaseOutput;
@@ -65,16 +72,9 @@
6572
});
6673
}
6774
68-
function addEventCapture(event) {
69-
eventPhases.push([event, 'capture']);
70-
eventPhases = eventPhases;
71-
requestAnimationFrame(() => {
72-
eventPhaseOutput.scrollTop = eventPhaseOutput.scrollHeight;
73-
});
74-
}
75-
76-
function addEventBubble(event) {
77-
eventPhases.push([event, 'bubble']);
75+
function addEventPhase(event) {
76+
const phases = ['none', 'capturing', 'at-target', 'bubbling'];
77+
eventPhases.push([event, phases[event.eventPhase]]);
7878
eventPhases = eventPhases;
7979
requestAnimationFrame(() => {
8080
eventPhaseOutput.scrollTop = eventPhaseOutput.scrollHeight;

0 commit comments

Comments
 (0)