Learn Svelte in 3 minutes
Written by Rico Sta. Cruz ( @rstacruz) · 5 Aug 2022 I’ve been learning Svelte. Here are my notes as I learned it! If you’re already familiar with JavaScript and other frameworks, I’m hoping this guide can help you learn Svelte quickly. 👇
Svelte is a web framework for writing rich user interfaces. Here’s a quick guide on Svelte 3.0.
Examples
The easiest way to learn Svelte (in my opinion) is the examples documentation. It’s comprehensive and straight-to-the-point.
Basic component
Svelte components store the markup, script and CSS styles in one .svelte
file.
div { color : dodgerblue ; }
Nested components
Using export let
will define a component property.
import Nested from './Nested.svelte' ;
< Nested message = "Hello there" />
Styling
Styles are scoped per component, but :global(…)
will allow styling elements outside the component.
p :global( a ) { color : blue ; }
Attributes markup
Attributes are in HTML syntax, and can contain JavaScript expressions inside {…}
brackets.
< img src = "https://{ src }" >
< img { src } > <!-- shorthand -->
< img { ... props } > <!-- spread -->
State & events
Updating variables declared with let
will trigger DOM updates.
< button on : click = { () => { count ++ } }>
I was clicked {count} times
Computed state
Prefixing statements with $:
will make it run with changes to the states it depends on.
// reactive state derived from `count`
< button on : click = {() => { count ++ }}>
< p >{count} * 2 = {doubled}</ p >
Reactive statements
$:
works with blocks too. Only values which directly appear within the $: block will become dependencies of the reactive statement.
// runs when the `title` prop changes
$ : document .title = title;
console . log ( 'title is' , title);
Else-if and else (:else
)
{# if answer === 42 && a !== b}
{ : else if answer === 74 || b > c}
Loops (#each
)
< li >{ item .name} x { item .qty}</ li >
{ # each items as item , index}
< li >{i + 1 }: { item .name} x { item .qty}</ li >
Loops with keys
The key expression allows for efficient adding and removing of items.
{# each users as user ( user .id) }
Text markup
Text expressions are automatically escaped unless @html
is used.
Text inputs
bind:value
forms a two-way binding to a state variable.
< input bind : value = {name} />
< p >Hello { name || 'stranger' }!</ p >
Checkboxes
bind:checked
binds the checked state of a checkbox to a state variable.
< input bind : checked = {subscribed} />
Subscribe to the newsletter
< p >Thank you for subscribing!</ p >
bind:group
binds a variable to a group of elements, such as a radio button group.
bind : group = {flavor} value = 'vanilla' >
bind : group = {flavor} value = 'mint' >
Head elements
<svelte:head>
allows inserting elements into the document head.
< meta name = "robots" content = "noindex" >
Body events
<svelte:body>
allows binding DOM events to the body element.
on : mouseenter = {handleMouseenter}
on : mouseleave = {handleMouseleave}
Window props
svelte:window
allows manipulating window properties.
Properties
innerWidth
innerHeight
outerWidth
outerHeight
scrollX
(rw)
scrollY
(rw)
online
Window events
<svelte:window>
also allows binding DOM events to the window
object.
on : keydown = {handleKeydown}
Slots
Slots allow passing content to a component (“children”).
import Notice from './Notice.svelte'
There will be rain today.
Named slots
Use named slots to pass more than 1 group of children elements.
< h1 slot = 'header' >Forecast:</ h1 >
There will be rain today.
< div slot = 'footer' >23°C, Rain</ div >
< dialog class = 'my-dialog' >
< slot name = 'header' ></ slot >
< slot name = 'footer' ></ slot >
Fragments in named slots
< svelte : fragment slot = "items" >
< slot name = " items " ></ slot >
Slot props
< slot prop = {value}></ slot >
bind:this
import { onMount } from 'svelte' ;
drawOn ( canvasEl . getContext ( '2d' ));
< canvas bind : this = {canvasEl}></ canvas >
Docs: bind:this
Written by Rico Sta. Cruz
I am a web developer helping make the world a better place through JavaScript, Ruby, and UI design. I write articles like these often. If
you'd like to stay in touch, subscribe to my list .
Comments