My writings published in different sites and aggregated here.
npm install
.npm start
.Source: Svelte course by Rich Harris and Frontend Masters.
<img>
) does not have the alt
attribute by default, for example. In other words, Svelte provides accessibility warnings.<style>
) are scoped to the component (and the markup defined in it, not affecting nested components). You can use :global()
for global styles.{@html ...}
tag to render strings with HTML. However, Svelte does not perform any sanitization.$:
($: doubled = count * 2;
, for example) for reactive declarations.$: console.log(count);
.push
and splice
won’t (automatically) cause updates.export let variableName;
to declare properties/props.<Component {...obj}/>
to spread props.{#each things as thing (thing.id)}...{/each}
((thing.id)
is a unique identifier).{#await promise}...{:then number}...{:catch error}...{/await}
.on:
directive). The once
modifier is an example.message
, for example), broadcasts it, and then the outer component is able to handle it (on:message={handleMessage}
, for example).on:click
to the button of an inner component to handle it in the App.Svelte
file, for example.<input bind:value={variableName}>
.<input type=checkbox bind:checked={variableName}>
.<audio>
and <video>
as well.clientWidth
, clientHeight
, offsetWidth
, and offsetHeight
bindings (imagine adding this to a <div>
with a <span>
to know the dimensions of a piece of text).this
binding allows you to obtain a reference to rendered elements.onMount
lifecycle function. It runs after the component is first rendered to the DOM.{:else}
in an Each block to render something when the size of the array in question is zero, for example.fetch
in onMount
because of SSR (with the exception of onDestroy
, lifecycle functions don’t run during SSR).beforeUpdate
and afterUpdate
are useful for implementing auto scrolling.tick
lifecycle function returns a promise that resolves as soon as any pending state changes have been applied to the DOM.$
(auto-subscription).tweened
and spring
) for creating stores whose values change over time rather than immediately. Svelte also provides easing functions.transition:
directive (or the in:
and out:
directives) and the functions (e.g., fade
) of the svelte/transition
module. There are also transition events that can be listened to.crossfade
function returns a pair of transitions that talk to each other.<slot>
element is used to specify where these should be placed.<svelte:component>
special element is useful for dynamic components.<svelte:window>
special element lets you add event listeners to the window
object. It also supports bindings.<svelte:body>
special element lets you add event listeners to the document.body
object. It is useful for the mouseenter
and mouseleave
events, for example.<svelte:head>
special element allows you to insert elements inside the <head>
.<svelte:options>
special element allows you to specify compiler options.<script context=”module”>
block will run once, when the module first evaluates, rather than when a component is instantiated.{@debug variableName}
for debugging.UI Kits
Deployment
build
(assuming the FRAMEWORK PRESET is Svelte).npx create-snowpack-app svelte-snowpack --template @snowpack/app-template-minimal
.<header role="banner">…</header>
.<main role="main">…</main>
.<nav role="navigation">…</nav>
.<footer role="contentinfo">…</footer>
.Source: CSS for JavaScript Developers course by Josh Comeau.
em
unit is a relative unit. It is equal to the font size of the current element.em
means that a component will change depending on the font size of the container it is placed within.rem
unit is similar to the em
unit. However, it is relative to the root element (<html>
tag). The author suggests using this unit for font sizes/typography.em
or rem
, not px
(e.g., html { font-size: 1.2em; }
).px
.npm install @fontsource/alice
).