Skip to main content

Svelte Overview

Svelte Overview.

Svelte is a modern JavaScript framework that allows you to build user interfaces (UI) by compiling your code at build time. It differs from other frameworks like React or Angular, which rely on a virtual DOM and perform updates at runtime. Instead, Svelte converts your code into highly efficient JavaScript that directly manipulates the DOM, resulting in faster and more performant applications.

History of Svelte

Svelte was created by Rich Harris and first released in 2016. It was designed to address the limitations of traditional frameworks and provide a more efficient way to build web applications. Since its release, Svelte has gained popularity among developers due to its simplicity, performance, and small bundle size.

Features of Svelte

  1. Declarative Syntax: Svelte uses declarative syntax to describe the UI components, making it easy to understand and maintain your code.

  2. Reactive Bindings: Svelte provides reactive bindings that allow you to create dynamic and responsive UI components. You can bind variables and expressions to elements, and they automatically update whenever the underlying data changes.

    <script>
    let count = 0;
    </script>

    <button on:click={() => count += 1}>
    Clicked {count} times
    </button>

    In the above example, the count variable is bound to the text content of the button. Whenever the button is clicked, the count variable is incremented, and the text updates accordingly.

  3. Component-based Architecture: Svelte promotes a component-based architecture, allowing you to create reusable UI components. Components can have their own state, properties, and lifecycle methods.

    <script>
    export let name;
    </script>

    <h1>Hello {name}!</h1>

    In this example, the name property is passed to the component, and it is rendered inside the <h1> tag.

  4. Scoped CSS: Svelte supports scoped CSS, which means the styles defined within a component only apply to that component. This prevents style conflicts across different components.

    <style>
    h1 {
    color: blue;
    }
    </style>

    <h1>Hello Svelte!</h1>

    The CSS styles defined inside the <style> tag will only apply to the <h1> tag within the same component.

  5. Animation: Svelte provides built-in support for animations. You can easily animate elements using keyframes, transitions, and CSS properties.

    <script>
    let visible = false;
    </script>

    <button on:click={() => visible = !visible}>
    Toggle
    </button>

    {#if visible}
    <div transition:fade>
    This element fades in and out
    </div>
    {/if}

    In this example, the <div> element fades in and out whenever the button is clicked. The transition:fade directive handles the animation.

  6. Server-side Rendering: Svelte supports server-side rendering (SSR), allowing you to render your components on the server and send HTML to the client. This improves performance and search engine optimization (SEO).

Examples of Svelte

  1. Todo List: Here's an example of a simple todo list using Svelte:

    <script>
    let todos = [];
    let newTodo = '';

    function addTodo() {
    todos = [...todos, newTodo];
    newTodo = '';
    }
    </script>

    <input bind:value={newTodo} placeholder="Add a new todo">
    <button on:click={addTodo}>Add</button>

    {#each todos as todo}
    <div>{todo}</div>
    {/each}

    This code allows you to add new todos to the list. The todos array holds the list of todos, and the addTodo function is called when the "Add" button is clicked.

  2. Image Gallery: Here's an example of an image gallery using Svelte:

    <script>
    let images = ['image1.jpg', 'image2.jpg', 'image3.jpg'];
    let currentIndex = 0;

    function nextImage() {
    currentIndex = (currentIndex + 1) % images.length;
    }
    </script>

    <img src={images[currentIndex]} alt="Gallery Image">
    <button on:click={nextImage}>Next</button>

    This code displays an image from the images array and allows you to cycle through the images using the "Next" button.

You can find more examples and detailed documentation on the official Svelte website: https://svelte.dev/

Svelte provides a powerful and efficient way to build web applications. Its declarative syntax, reactive bindings, and component-based architecture make it a popular choice among developers. With its focus on performance and simplicity, Svelte offers an excellent alternative to traditional JavaScript frameworks.