My gaming profiles across different platforms.
The Net Ninja (Shaun Pelling)
Svelte
App.svelte
: root component.event
object.bind:property
directive is a two-way data binding under the hood. So, <input type="text" bind:value={colorVariable}>
is similar to <input type="text" on:input={handleInputFunction} value={colorVariable}>
. In practice, the input element changes the variable and the changes will be reflected on the page. If the variable is changed elsewhere (via a button, for example), the value will also be updated in the input element.$: console.log(colorVariable);
is an example of a reactive statement. Use $: {...}
for a code block.{#each array as arrayElement (arrayElement.key)}...{/each}
(key
is important if you need to manipulate the data). This block can also have an {:else}
clause, which is rendered if array
is empty.<button on:click={() => handleClick(object.id)}>Delete</button>
or <button on:click={(event) => handleClick(event, object.id)}>Delete</button>
(inline event handler). This is useful for deleting an element from an array by its identifier within an each block (assuming there is a button for each element), for example. In other words, this is useful if the handler function needs to take arguments in addition to event
.on:
directive (e.g., on:click
) is used without a value, the component will forward the event. In this way, a consumer (parent component) of the component (child component) can listen for it. In other words, you can define a handler function in App.svelte
, assuming the component is used here, as there is another element, such as a button, that is used to show/hide this component. Example:App.svelte
file: <Modal ... on:click={toggleModal} />
and <button on:click={toggleModal}>...</button>
.Modal.svelte
file: <div ... on:click>...</div>
.self
event modifier (e.g., on:click|self
) is useful for a modal since the handler is only triggered if event.target
is the element itself. In practice, this allows the modal to be closed only if there is a click outside the message container (a child <div>
), on the part that is faded, for example.<input type="number" bind:value={numVariable}>
), the input value is coerced. In this way, the associated variable is a number, not a string.let arrayVariable = [];
+ bind:group={arrayVariable}
for radio and checkbox inputs (example).createEventDispatcher
instead).<ChildComponent on:name={handlerFunction} />
). The data is available on the detail
property of the event object.CSS
crimson
instead of red
(color keywords).main { max-width: 960px; margin: 40px auto; }
.Misc