(
288 | element: Element,
289 | type: Type,
290 | handler: (this: Element, event: MediaQueryListEventMap[Type]) => any,
291 | options?: AddEventListenerOptions
292 | ): () => void;
293 |
294 | // Generic event attachment
295 | function on(
296 | element: EventTarget,
297 | type: string,
298 | handler: EventListener,
299 | options?: AddEventListenerOptions
300 | ): () => void;
301 |
302 | ---
303 | ---
304 |
305 | Use `on:eventname={handler}` to attach handlers (e.g., `onclick={() => ...}`).
306 | - Handlers can mutate `$state` directly; UI updates automatically.
307 | - Shorthand `{eventname}` works if the handler is a variable or function.
308 | - Events like `click`, `input`, etc., are delegated; assume bubbling unless stopped.
309 | - Events fire after bindings (e.g., `oninput` after `bind:value`).
310 | - Case-sensitive: `onclick` ≠ `onClick` (custom events may use uppercase).
311 |
312 |
320 |
321 |
322 |
325 |
326 |
327 |
330 |
331 |
332 | text = e.target.value}
334 | bind:value={text}
335 | placeholder="Type here"
336 | />
337 | You typed: {text}
338 |
339 |
340 | e.key === 'Enter' && count++}
342 | placeholder="Press Enter to increment"
343 | />
344 |
345 | Note: For other events (e.g., `onmouseover`, `onfocus`), follow the same pattern:
346 | `on:eventname={() => ...}` or `{eventname}`. Adjust the handler logic based on the event’s purpose.
--------------------------------------------------------------------------------
/svelte5_runes.txt:
--------------------------------------------------------------------------------
1 | # Svelte 5 Runes Overview
2 |
3 |
4 |
5 | ---
6 | ## $State
7 | ---
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 | ---
16 |
17 |
18 |
21 |
22 |
25 |
26 | ---
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 | ---
39 |
40 |
41 |
51 |
52 |
60 |
61 |
64 |
65 | ---
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 | ---
78 |
79 |
80 |
97 |
98 | {todo.text} - {todo.done ? 'Done' : 'Not done'}
99 |
100 |
101 |
102 | ---
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 | ---
115 |
116 |
117 |
131 |
132 | {person.name} is {person.age} years old.
133 |
134 |
135 | ---
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 | ---
148 |
149 |
150 |
157 |
158 | Count: {counter.count}
159 |
160 |
161 |
162 | ---
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 | ---
174 |
175 |
176 |
185 |
186 | {a} + {b} = {total()}
187 |
188 |
189 |
190 | ---
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
206 |
207 | name,
209 | (v) => name = v.trimStart()
210 | } />
211 |
212 | Hello, {name}!
213 |
214 |
218 |
219 |
220 | ---
221 | ## $Derived
222 | ---
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 | ---
231 |
232 |
233 |
237 |
238 |
241 |
242 | {count} doubled is {doubled}
243 |
244 | ---
245 |
246 |
247 | - `$derived(count * 2)` ensures `doubled` always reflects the latest `count`.
248 | - You **cannot** modify state inside `$derived`, e.g., `count++` inside the expression.
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 | ---
257 |
258 |
259 |
271 |
272 |
275 |
276 | {numbers.join(' + ')} = {total}
277 |
278 | ---
279 |
280 |
281 | - `$derived.by(() => { ... })` is useful when the derived state requires loops or multiple steps.
282 | - The derived state (`total`) automatically updates when `numbers` change.
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 | ---
291 |
292 |
293 |
297 |
298 |
301 |
302 | {count} is {isEven ? 'Even' : 'Odd'}
303 |
304 | ---
305 |
306 |
307 | - `$derived` tracks all **synchronously accessed** state inside its expression.
308 | - If `count` changes, `isEven` will automatically update.
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 | ---
317 |
318 |
319 |
323 |
324 |
327 |
328 | Is count large? {large}
329 |
330 | ---
331 |
332 |
333 | - Svelte **only updates** when `large` changes (`false` → `true` or vice versa).
334 | - If `count` changes **within the same threshold** (e.g., `5 → 6`), no update occurs.
335 |
336 | ---
337 |
338 |
339 | ---
340 | ## $Effect
341 | ---
342 |
343 |
344 |
345 |
346 |
347 |
348 |
349 | ---
350 |
351 |
352 |
366 |
367 |
368 |
369 |
370 |
371 |
372 | ---
373 |
374 |
379 |
380 |
381 |
382 |
383 |
384 |
385 |
386 | ---
387 |
388 |
389 |
404 |
405 | {count}
406 |
407 |
408 |
409 |
410 | ---
411 |
412 |
416 |
417 |
418 |
419 |
420 |
421 |
422 |
423 | ---
424 |
425 |
426 |
443 |
444 |
445 |
446 |
447 |
448 |
449 | ---
450 |
451 |
455 |
456 |
457 |
458 |
459 |
460 |
461 |
462 | ---
463 |
464 |
465 |
485 |
486 |
487 | {#each messages as message}
488 |
{message}
489 | {/each}
490 |
491 |
492 |
493 |
494 | ---
495 |
496 |
500 |
501 |
502 |
503 |
504 |
505 |
506 |
507 | ---
508 |
509 |
510 |
519 |
520 | {count} doubled is {doubled}
521 |
522 |
523 |
524 | ---
525 |
526 |
530 |
531 | ---
532 |
533 |
534 | ---
535 | ## $Bindable
536 | ---
537 |
538 |
539 |
540 |
541 |
542 |
543 |
544 | ---
545 |
546 |
547 |
550 |
551 |
552 |
553 | ---
554 |
555 |
556 |
561 |
562 |
563 | {message}
564 |
565 | ---
566 |
567 |
568 |
569 |
570 |
571 |
572 |
573 |
574 |
575 |
576 |
577 |
578 | ---
579 |
580 |
581 |
584 |
585 |
586 |
587 | ---
588 |
589 |
590 |
593 |
594 |
595 |
596 | ---
597 |
598 |
599 |
600 |
601 |
602 |
603 |
604 |
605 |
606 |
607 |
608 | ---
609 |
610 |
611 |
614 |
615 |
616 |
617 | ---
618 |
619 |
620 |
626 |
627 |
628 |
631 |
632 | ---
633 |
634 |
635 | - The input's `value` and `disabled` states can both be bound to the parent.
636 | - The button toggles the `disabled` state dynamically.
637 | - Useful for forms and UI elements requiring controlled states.
638 |
639 |
640 | ---
641 | ## $Props
642 | ---
643 |
644 |
645 |
646 |
647 |
648 |
649 |
650 | ---
651 |
652 |
653 |
656 |
657 |
658 |
659 | ---
660 |
661 |
662 |
665 |
666 | This component is {props.adjective}
667 |
668 | ---
669 |
670 |
675 |
676 |
677 |
678 |
679 |
680 |
681 |
682 | ---
683 |
684 |
685 |
688 |
689 | This component is {adjective}
690 |
691 | ---
692 |
693 |
697 |
698 |
699 |
700 |
701 |
702 |
703 |
704 | ---
705 |
706 |
707 |
710 |
711 | This component is {adjective}
712 |
713 | ---
714 |
715 |
719 |
720 |
721 |
722 |
723 |
724 |
725 |
726 | ---
727 |
728 |
729 |
732 |
733 | Song lyric: {trouper}
734 |
735 | ---
736 |
737 |
742 |
743 |
744 |
745 |
746 |
747 |
748 |
749 | ---
750 |
751 |
752 |
755 |
756 | a: {a}, b: {b}, c: {c}
757 | Other props: {JSON.stringify(others)}
758 |
759 | ---
760 |
761 |
766 |
767 |
768 |
769 |
770 |
771 |
772 |
773 | ---
774 |
775 |
776 |
781 |
782 |
785 |
786 |
787 |
788 | ---
789 |
790 |
791 |
794 |
795 |
798 |
799 | ---
800 |
801 |
806 |
807 |
808 |
809 |
810 |
811 |
812 |
813 | ---
814 |
815 |
816 |
819 |
820 |
821 |
822 | ---
823 |
824 |
825 |
828 |
829 |
832 |
833 | ---
834 |
835 |
840 |
841 |
842 |
843 |
844 |
845 |
846 |
847 | ---
848 |
849 |
850 |
855 |
856 |
857 |
858 | ---
859 |
860 |
861 |
864 |
865 |
868 |
869 | ---
870 |
871 |
876 |
877 |
878 |
879 |
880 |
881 |
882 |
883 | ---
884 |
885 |
886 |
889 |
890 |
897 |
898 | ---
899 |
900 |
905 |
906 |
907 | ---
908 | ## $Host
909 | ---
910 |
911 |
912 |
913 |
914 |
915 |
916 |
917 | ---
918 |
919 |
920 |
921 |
922 |
929 |
930 |
931 |
932 |
933 | ---
934 |
935 |
936 |
941 |
942 | count -= 1}
944 | onincrement={() => count += 1}
945 | >
946 |
947 | count: {count}
948 |
949 | ---
950 |
951 |
952 | - `$host()` only works in components compiled as **custom elements** (``).
953 | - This approach allows dispatching custom events from a **shadow DOM encapsulated component**.
954 | - Ensure the parent component listens to the dispatched events (`ondecrement`, `onincrement`).
955 |
956 |
957 | ---
958 | ## $Inspect
959 | ---
960 |
961 |
962 |
963 |
964 |
965 |
966 |
967 | ---
968 |
969 |
970 |
976 |
977 |
978 |
979 |
980 | ---
981 |
982 |
987 |
988 | ---
989 |
990 |
991 |
992 |
993 |
994 |
995 |
996 | ---
997 |
998 |
999 |
1008 |
1009 |
1010 |
1011 | ---
1012 |
1013 |
1018 |
1019 | ---
1020 |
1021 |
1022 |
1023 |
1024 |
1025 |
1026 |
1027 | ---
1028 |
1029 |
1030 |
1035 |
1036 |
1037 |
1038 | ---
1039 |
1040 |
1044 |
1045 | ---
1046 |
1047 |
1048 |
1049 |
1050 |
1051 |
1052 |
1053 | ---
1054 |
1055 |
1056 |
1064 |
1065 | ---
1066 |
1067 |
1072 |
--------------------------------------------------------------------------------
/svelte5_full_context.txt:
--------------------------------------------------------------------------------
1 | # Svelte 5 Runes Overview
2 |
3 | ---
4 | ## $State
5 | ---
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | ---
14 |
15 |
16 |
19 |
20 |
23 |
24 | ---
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 | ---
37 |
38 |
39 |
49 |
50 |
58 |
59 |
62 |
63 | ---
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | ---
76 |
77 |
78 |
95 |
96 | {todo.text} - {todo.done ? 'Done' : 'Not done'}
97 |
98 |
99 |
100 | ---
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 | ---
113 |
114 |
115 |
129 |
130 | {person.name} is {person.age} years old.
131 |
132 |
133 | ---
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 | ---
146 |
147 |
148 |
155 |
156 | Count: {counter.count}
157 |
158 |
159 |
160 | ---
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 | ---
172 |
173 |
174 |
183 |
184 | {a} + {b} = {total()}
185 |
186 |
187 |
188 | ---
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
204 |
205 | name,
207 | (v) => name = v.trimStart()
208 | } />
209 |
210 | Hello, {name}!
211 |
212 |
216 |
217 |
218 | ---
219 | ## $Derived
220 | ---
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 | ---
229 |
230 |
231 |
235 |
236 |
239 |
240 | {count} doubled is {doubled}
241 |
242 | ---
243 |
244 |
245 | - `$derived(count * 2)` ensures `doubled` always reflects the latest `count`.
246 | - You **cannot** modify state inside `$derived`, e.g., `count++` inside the expression.
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 | ---
255 |
256 |
257 |
269 |
270 |
273 |
274 | {numbers.join(' + ')} = {total}
275 |
276 | ---
277 |
278 |
279 | - `$derived.by(() => { ... })` is useful when the derived state requires loops or multiple steps.
280 | - The derived state (`total`) automatically updates when `numbers` change.
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 | ---
289 |
290 |
291 |
295 |
296 |
299 |
300 | {count} is {isEven ? 'Even' : 'Odd'}
301 |
302 | ---
303 |
304 |
305 | - `$derived` tracks all **synchronously accessed** state inside its expression.
306 | - If `count` changes, `isEven` will automatically update.
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 | ---
315 |
316 |
317 |
321 |
322 |
325 |
326 | Is count large? {large}
327 |
328 | ---
329 |
330 |
331 | - Svelte **only updates** when `large` changes (`false` → `true` or vice versa).
332 | - If `count` changes **within the same threshold** (e.g., `5 → 6`), no update occurs.
333 |
334 | ---
335 |
336 |
337 | ---
338 | ## $Effect
339 | ---
340 |
341 |
342 |
343 |
344 |
345 |
346 |
347 | ---
348 |
349 |
350 |
364 |
365 |
366 |
367 |
368 |
369 |
370 | ---
371 |
372 |
377 |
378 |
379 |
380 |
381 |
382 |
383 |
384 | ---
385 |
386 |
387 |
402 |
403 | {count}
404 |
405 |
406 |
407 |
408 | ---
409 |
410 |
414 |
415 |
416 |
417 |
418 |
419 |
420 |
421 | ---
422 |
423 |
424 |
441 |
442 |
443 |
444 |
445 |
446 |
447 | ---
448 |
449 |
453 |
454 |
455 |
456 |
457 |
458 |
459 |
460 | ---
461 |
462 |
463 |
483 |
484 |
485 | {#each messages as message}
486 |
{message}
487 | {/each}
488 |
489 |
490 |
491 |
492 | ---
493 |
494 |
498 |
499 |
500 |
501 |
502 |
503 |
504 |
505 | ---
506 |
507 |
508 |
517 |
518 | {count} doubled is {doubled}
519 |
520 |
521 |
522 | ---
523 |
524 |
528 |
529 | ---
530 |
531 |
532 | ---
533 | ## $Bindable
534 | ---
535 |
536 |
537 |
538 |
539 |
540 |
541 |
542 | ---
543 |
544 |
545 |
548 |
549 |
550 |
551 | ---
552 |
553 |
554 |
559 |
560 |
561 | {message}
562 |
563 | ---
564 |
565 |
566 |
567 |
568 |
569 |
570 |
571 |
572 |
573 |
574 |
575 |
576 | ---
577 |
578 |
579 |
582 |
583 |
584 |
585 | ---
586 |
587 |
588 |
591 |
592 |
593 |
594 | ---
595 |
596 |
597 |
598 |
599 |
600 |
601 |
602 |
603 |
604 |
605 |
606 | ---
607 |
608 |
609 |
612 |
613 |
614 |
615 | ---
616 |
617 |
618 |
624 |
625 |
626 |
629 |
630 | ---
631 |
632 |
633 | - The input's `value` and `disabled` states can both be bound to the parent.
634 | - The button toggles the `disabled` state dynamically.
635 | - Useful for forms and UI elements requiring controlled states.
636 |
637 |
638 | ---
639 | ## $Props
640 | ---
641 |
642 |
643 |
644 |
645 |
646 |
647 |
648 | ---
649 |
650 |
651 |
654 |
655 |
656 |
657 | ---
658 |
659 |
660 |
663 |
664 | This component is {props.adjective}
665 |
666 | ---
667 |
668 |
673 |
674 |
675 |
676 |
677 |
678 |
679 |
680 | ---
681 |
682 |
683 |
686 |
687 | This component is {adjective}
688 |
689 | ---
690 |
691 |
695 |
696 |
697 |
698 |
699 |
700 |
701 |
702 | ---
703 |
704 |
705 |
708 |
709 | This component is {adjective}
710 |
711 | ---
712 |
713 |
717 |
718 |
719 |
720 |
721 |
722 |
723 |
724 | ---
725 |
726 |
727 |
730 |
731 | Song lyric: {trouper}
732 |
733 | ---
734 |
735 |
740 |
741 |
742 |
743 |
744 |
745 |
746 |
747 | ---
748 |
749 |
750 |
753 |
754 | a: {a}, b: {b}, c: {c}
755 | Other props: {JSON.stringify(others)}
756 |
757 | ---
758 |
759 |
764 |
765 |
766 |
767 |
768 |
769 |
770 |
771 | ---
772 |
773 |
774 |
779 |
780 |
783 |
784 |
785 |
786 | ---
787 |
788 |
789 |
792 |
793 |
796 |
797 | ---
798 |
799 |
804 |
805 |
806 |
807 |
808 |
809 |
810 |
811 | ---
812 |
813 |
814 |
817 |
818 |
819 |
820 | ---
821 |
822 |
823 |
826 |
827 |
830 |
831 | ---
832 |
833 |
838 |
839 |
840 |
841 |
842 |
843 |
844 |
845 | ---
846 |
847 |
848 |
853 |
854 |
855 |
856 | ---
857 |
858 |
859 |
862 |
863 |
866 |
867 | ---
868 |
869 |
874 |
875 |
876 |
877 |
878 |
879 |
880 |
881 | ---
882 |
883 |
884 |
887 |
888 |
895 |
896 | ---
897 |
898 |
903 |
904 |
905 | ---
906 | ## $Host
907 | ---
908 |
909 |
910 |
911 |
912 |
913 |
914 |
915 | ---
916 |
917 |
918 |
919 |
920 |
927 |
928 |
929 |
930 |
931 | ---
932 |
933 |
934 |
939 |
940 | count -= 1}
942 | onincrement={() => count += 1}
943 | >
944 |
945 | count: {count}
946 |
947 | ---
948 |
949 |
950 | - `$host()` only works in components compiled as **custom elements** (``).
951 | - This approach allows dispatching custom events from a **shadow DOM encapsulated component**.
952 | - Ensure the parent component listens to the dispatched events (`ondecrement`, `onincrement`).
953 |
954 |
955 | ---
956 | ## $Inspect
957 | ---
958 |
959 |
960 |
961 |
962 |
963 |
964 |
965 | ---
966 |
967 |
968 |
974 |
975 |
976 |
977 |
978 | ---
979 |
980 |
985 |
986 | ---
987 |
988 |
989 |
990 |
991 |
992 |
993 |
994 | ---
995 |
996 |
997 |
1006 |
1007 |
1008 |
1009 | ---
1010 |
1011 |
1016 |
1017 | ---
1018 |
1019 |
1020 |
1021 |
1022 |
1023 |
1024 |
1025 | ---
1026 |
1027 |
1028 |
1033 |
1034 |
1035 |
1036 | ---
1037 |
1038 |
1042 |
1043 | ---
1044 |
1045 |
1046 |
1047 |
1048 |
1049 |
1050 |
1051 | ---
1052 |
1053 |
1054 |
1062 |
1063 | ---
1064 |
1065 |
1070 |
1071 |
1072 | # Svelte 5 Event Handling
1073 |
1074 |
1075 |
1076 |
1077 |
1078 | **Svelte 5 replaces `on:` directives with direct property bindings for event handlers.**
1079 |
1080 | ---
1081 |
1082 |
1083 | ```svelte
1084 |
1091 |
1092 |
1095 | ```
1096 |
1097 | **Key changes**:
1098 | - Removed `on:click`, replacing it with `{onclick}` as a property.
1099 | - Uses a named function (`onclick`) instead of inline arrow functions.
1100 |
1101 | ---
1102 |
1103 |
1104 |
1105 |
1106 |
1107 | **In Svelte 5, components no longer use `createEventDispatcher()`. Instead, they accept callback props.**
1108 |
1109 | ---
1110 |
1111 |
1112 | ```svelte
1113 |
1117 |
1118 |
1119 |
1120 |
1121 | Pump power: {power}
1122 |
1123 | ```
1124 |
1125 | ---
1126 |
1127 |
1128 | ```svelte
1129 |
1149 |
1150 |
1151 |
1152 | {#if burst}
1153 |
1154 | 💥
1155 | {:else}
1156 |
1157 | 🎈
1158 |
1159 | {/if}
1160 | ```
1161 |
1162 | **Key changes**:
1163 | - Removed `createEventDispatcher()`, instead using callback props (`inflate`, `deflate`).
1164 | - Events are now just function calls inside `Pump.svelte`.
1165 | - **Parent (App.svelte)** passes handlers like `inflateHandler` to `Pump`.
1166 |
1167 | ---
1168 |
1169 |
1170 |
1171 |
1172 |
1173 | **To forward an event from an element to a component, use callback props instead of `on:`.**
1174 |
1175 | ---
1176 |
1177 |
1178 | ```svelte
1179 |
1182 |
1183 |
1186 | ```
1187 |
1188 | **Key changes**:
1189 | - No need to manually forward events like `on:click` → `{onclick}` does the job.
1190 |
1191 | ---
1192 |
1193 |
1194 |
1195 |
1196 |
1197 | **Instead of manually listing every event, just spread props.**
1198 |
1199 | ---
1200 |
1201 |
1202 | ```svelte
1203 |
1206 |
1207 |
1210 | ```
1211 |
1212 | **Key changes**:
1213 | - `{...props}` automatically applies all attributes, including event handlers like `onclick`.
1214 |
1215 | ---
1216 |
1217 |
1218 |
1219 |
1220 |
1221 | **Instead of duplicate event bindings, combine handlers manually.**
1222 |
1223 | ---
1224 |
1225 |
1226 | ```svelte
1227 |
1236 |
1237 |
1245 | ```
1246 |
1247 | **Key changes**:
1248 | - Instead of multiple `on:click={one} on:click={two}`, handlers are manually merged.
1249 |
1250 | ---
1251 |
1252 |
1253 |
1254 |
1255 |
1256 | **Ensure that when spreading props, local event handlers still execute.**
1257 |
1258 | ---
1259 |
1260 |
1261 | ```svelte
1262 |
1269 |
1270 |
1279 | ```
1280 |
1281 | **Key changes**:
1282 | - The local `onclick` executes **before** the `props.onclick`, preventing overwrites.
1283 |
1284 | ---
1285 |
1286 |
1287 |
1288 |
1289 |
1290 | **Modifiers like `once` and `preventDefault` should now be implemented manually.**
1291 |
1292 | ---
1293 |
1294 |
1295 | ```svelte
1296 |
1315 |
1316 |
1319 | ```
1320 |
1321 | **Key changes**:
1322 | - Used **higher-order functions** to replace `on:click|once|preventDefault={handler}`.
1323 |
1324 | ---
1325 | ---
1326 |
1327 | // svelte/events - `on` function
1328 | // `on` attaches an event handler and returns a cleanup function.
1329 | // It ensures correct handler order compared to `addEventListener`.
1330 |
1331 | import { on } from 'svelte/events';
1332 |
1333 | // Attach an event to `window`
1334 | function on(
1335 | window: Window,
1336 | type: Type,
1337 | handler: (this: Window, event: WindowEventMap[Type]) => any,
1338 | options?: AddEventListenerOptions
1339 | ): () => void;
1340 |
1341 | // Attach an event to `document`
1342 | function on(
1343 | document: Document,
1344 | type: Type,
1345 | handler: (this: Document, event: DocumentEventMap[Type]) => any,
1346 | options?: AddEventListenerOptions
1347 | ): () => void;
1348 |
1349 | // Attach an event to an `HTMLElement`
1350 | function on(
1351 | element: Element,
1352 | type: Type,
1353 | handler: (this: Element, event: HTMLElementEventMap[Type]) => any,
1354 | options?: AddEventListenerOptions
1355 | ): () => void;
1356 |
1357 | // Attach an event to a `MediaQueryList`
1358 | function on(
1359 | element: Element,
1360 | type: Type,
1361 | handler: (this: Element, event: MediaQueryListEventMap[Type]) => any,
1362 | options?: AddEventListenerOptions
1363 | ): () => void;
1364 |
1365 | // Generic event attachment
1366 | function on(
1367 | element: EventTarget,
1368 | type: string,
1369 | handler: EventListener,
1370 | options?: AddEventListenerOptions
1371 | ): () => void;
1372 |
1373 | ---
1374 | ---
1375 |
1376 | Use `on:eventname={handler}` to attach handlers (e.g., `onclick={() => ...}`).
1377 | - Handlers can mutate `$state` directly; UI updates automatically.
1378 | - Shorthand `{eventname}` works if the handler is a variable or function.
1379 | - Events like `click`, `input`, etc., are delegated; assume bubbling unless stopped.
1380 | - Events fire after bindings (e.g., `oninput` after `bind:value`).
1381 | - Case-sensitive: `onclick` ≠ `onClick` (custom events may use uppercase).
1382 |
1383 |
1391 |
1392 |
1393 |
1396 |
1397 |
1398 |
1401 |
1402 |
1403 | text = e.target.value}
1405 | bind:value={text}
1406 | placeholder="Type here"
1407 | />
1408 | You typed: {text}
1409 |
1410 |
1411 | e.key === 'Enter' && count++}
1413 | placeholder="Press Enter to increment"
1414 | />
1415 |
1416 | Note: For other events (e.g., `onmouseover`, `onfocus`), follow the same pattern:
1417 | `on:eventname={() => ...}` or `{eventname}`. Adjust the handler logic based on the event’s purpose.
1418 |
1419 | # Svelte 5 Snippets
1420 |
1421 |
1422 |
1423 |
1424 |
1425 |
1426 |
1427 | ---
1428 |
1429 |
1430 |
1436 |
1437 | {#snippet figure(image)}
1438 |
1439 |
1440 | {image.caption}
1441 |
1442 | {/snippet}
1443 |
1444 | {#each images as image}
1445 | {#if image.href}
1446 |
1447 | {@render figure(image)}
1448 |
1449 | {:else}
1450 | {@render figure(image)}
1451 | {/if}
1452 | {/each}
1453 |
1454 | ---
1455 |
1456 |
1457 |
1458 |
1459 |
1460 |
1461 |
1462 |
1463 |
1464 | ---
1465 |
1466 |
1467 |
1468 | {#snippet x()}
1469 | {#snippet y()}
1470 |
Inside y
1471 | {/snippet}
1472 |
1473 |
1474 | {@render y()}
1475 | {/snippet}
1476 |
1477 |
1478 | {@render y()}
1479 |
1480 |
1481 |
1482 | {@render x()}
1483 |
1484 | ---
1485 |
1486 |
1487 |
1488 |
1489 |
1490 |
1491 |
1492 |
1493 |
1494 | ---
1495 |
1496 |
1497 | {#snippet blastoff()}
1498 | 🚀
1499 | {/snippet}
1500 |
1501 | {#snippet countdown(n)}
1502 | {#if n > 0}
1503 | {n}...
1504 | {@render countdown(n - 1)}
1505 | {:else}
1506 | {@render blastoff()}
1507 | {/if}
1508 | {/snippet}
1509 |
1510 | {@render countdown(5)}
1511 |
1512 | ---
1513 |
1514 |
1515 |
1516 |
1517 |
1518 |
1519 |
1520 |
1521 |
1522 | ---
1523 |
1524 |
1525 |
1528 |
1529 |
1530 |
1531 | {@render header()}
1532 |
1533 |
1534 | {#each data as item}
1535 | {@render row(item)}
1536 | {/each}
1537 |
1538 |
1539 |
1540 | ---
1541 |
1542 |
1543 |
1552 |
1553 | {#snippet header()}
1554 | Fruit |
1555 | Qty |
1556 | Price |
1557 | Total |
1558 | {/snippet}
1559 |
1560 | {#snippet row(d)}
1561 | {d.name} |
1562 | {d.qty} |
1563 | {d.price} |
1564 | {d.qty * d.price} |
1565 | {/snippet}
1566 |
1567 |
1568 |
1569 | ---
1570 |
1571 |
1572 |
1573 |
1574 |
1575 |
1576 |
1577 |
1578 |
1579 | ---
1580 |
1581 |
1582 |
1585 |
1586 |
1587 |
1588 | ---
1589 |
1590 |
1591 |
1592 |
1593 | ---
1594 |
1595 |
1596 |
1597 |
1598 |
1599 |
1600 |
1601 |
1602 |
1603 | ---
1604 |
1605 |
1606 |
1609 |
1610 | {#if children}
1611 | {@render children()}
1612 | {:else}
1613 | Fallback content
1614 | {/if}
1615 |
1616 | ---
1617 |
1618 |
1619 |
1620 |
1621 | {#snippet children()}
1622 | Custom content
1623 | {/snippet}
1624 |
1625 |
1626 | ---
1627 |
1628 |
1629 |
1630 |
1631 |
1632 |
1633 |
1634 |
1635 |
1636 | ---
1637 |
1638 |
1639 |
1642 |
1643 | {#snippet add(a, b)}
1644 | {a} + {b} = {a + b}
1645 | {/snippet}
1646 |
1647 | ---
1648 |
1649 |
1650 |
1653 |
1654 | {@render add(2, 3)}
1655 |
1656 | ---
1657 |
1658 |
1659 |
1660 |
1661 |
1662 |
1663 |
1664 |
1665 |
1666 | ---
1667 |
1668 |
1669 |
1672 |
1673 |
1674 | {@render header?.()}
1675 |
1676 |
1677 |
1678 | {@render main?.()}
1679 |
1680 |
1681 |
1684 |
1685 | ---
1686 |
1687 |
1688 |
1691 |
1692 |
1693 | {#snippet header()}
1694 | My Website
1695 | {/snippet}
1696 |
1697 | {#snippet main()}
1698 | Welcome to my website!
1699 | {/snippet}
1700 |
1701 | {#snippet footer()}
1702 | © 2023 My Website
1703 | {/snippet}
1704 |
1705 |
1706 | ---
1707 |
1708 |
1709 |
1710 |
1711 |
1712 |
1713 |
1714 |
1715 | ---
1716 |
1717 |
1718 |
1721 |
1722 | {#if items.length}
1723 |
1724 | {#each items as entry}
1725 | -
1726 | {@render item(entry)}
1727 |
1728 | {/each}
1729 |
1730 | {:else}
1731 | {@render empty?.()}
1732 | {/if}
1733 |
1734 | ---
1735 |
1736 |
1737 |
1742 |
1743 |
1744 | {#snippet item(fruit)}
1745 | {fruit}
1746 | {/snippet}
1747 |
1748 | {#snippet empty()}
1749 | No fruits available
1750 | {/snippet}
1751 |
1752 |
1753 | ---
1754 |
1755 |
1759 |
1760 | # Svelte 5 + TypeScript
1761 |
1762 |
1763 |
1764 |
1765 |
1766 |
1767 |
1768 | ---
1769 |
1770 |
1771 |
1778 |
1779 |
1780 |
1781 | ---
1782 |
1783 | ### Notes:
1784 | - Adding `lang="ts"` enables TypeScript.
1785 | - Type annotations (`string`) help with static checking.
1786 | - No runtime overhead since TypeScript removes type annotations at compile time.
1787 |
1788 | ---
1789 |
1790 |
1791 |
1792 |
1793 |
1794 |
1795 |
1796 | ---
1797 |
1798 |
1799 |
1806 |
1807 | Hello, {name}!
1808 |
1809 | ---
1810 |
1811 |
1812 |
1815 |
1816 |
1817 |
1818 | ---
1819 |
1820 | ### Notes:
1821 | - `Props` interface ensures `name` is always a string.
1822 | - `$props()` extracts the component’s props with proper typing.
1823 |
1824 | ---
1825 |
1826 |
1827 |
1828 |
1829 |
1830 |
1831 |
1832 | ---
1833 |
1834 |
1835 |
1843 |
1844 | {#each items as item}
1845 |
1846 | {/each}
1847 |
1848 | ---
1849 |
1850 |
1851 |
1860 |
1861 |
1862 |
1863 | ---
1864 |
1865 | ### Notes:
1866 | - `generics="Item"` allows the component to accept any item type.
1867 | - Ensures `items` and `select` function operate on the same type.
1868 |
1869 | ---
1870 |
1871 |
1872 |
1873 |
1874 |
1875 |
1876 |
1877 | ---
1878 |
1879 |
1880 |
1887 |
1888 |
1889 |
1890 | ---
1891 |
1892 | ### Notes:
1893 | - `$state(0)` initializes a reactive variable with a type.
1894 | - Without an initial value, TypeScript infers `number | undefined`.
1895 |
1896 | ---
1897 |
1898 |
1899 |
1900 |
1901 |
1902 |
1903 |
1904 | ---
1905 |
1906 |
1907 |
1912 |
1913 |
1914 |
1915 | ---
1916 |
1917 |
1918 |
1921 |
1922 |
1923 |
1924 | ---
1925 |
1926 | ### Notes:
1927 | - `HTMLButtonAttributes` ensures `Button.svelte` supports all standard button attributes.
1928 | - `...rest` spreads remaining props onto the `