Switch case syntax for Svelte ✨

Overview

Svelte switch case

Switch case syntax for your Svelte components.

Demo · StackBlitz · NPM Package


Getting started

Step 1: Add the preprocessor to your Svelte project

# Install it:
npm i -D svelte-switch-case
// Then, in your svelte.config.js
import switchCase from 'svelte-switch-case';

const config = {
  preprocess: [switchCase()],
};

export default config;

Step 2: Start using it in your Svelte components

<!-- Component.svelte -->
<script>
  let animal = 'dog';
</script>

<section>
  {#switch animal}
    {:case "cat"}
      <p>meow</p>
    {:case "dog"}
      <p>woof</p>
    {:default}
      <p>oink?</p>
  {/switch}
</section>

🔍 How it works

svelte-switch-case transpiles the following code

{#switch animal}
  {:case "cat"}
    <p>meow</p>
  {:case "dog"}
    <p>woof</p>
  {:default}
    <p>oink?</p>
{/switch}

into if/else statements

<!-- Injected by svelte-switch-case -->
{#if animal === "cat"}
  <p>meow</p>
{:else if animal === "dog"}
  <p>woof</p>
{:else}
  <p>oink?</p>
{/if}

🙌 Contribute

Found a bug or just had a cool idea? Feel free to open an issue or submit a PR.

You might also like...

🌸 It's a frontend case for CicekSepeti

Ciceksepeti Frontend Case When we change the category all filters are reset byself. You can make empty search for listing all items again. Getting Sta

Sep 13, 2022

A leetcode workspace template with test case runner for JavaScript/TypeScript programmers.

leetcode-typescript-workspace English | 简体中文 A vscode workspace template with test case runner script for JavaScript/TypeScript programmers using exte

Dec 13, 2022

API for the Baby Food Introduction Application. Keep your baby's food introductions in check with this application and backtrack in case of allergies!

API for the Baby Food Introduction Application. Keep your baby's food introductions in check with this application and backtrack in case of allergies!

Baby food introduction API This API is part of the Baby Food Introduction application, which aims to help technological parents keep track of the food

Nov 25, 2022

KWin Script to switch to the next or previous non empty virtual desktop

kwin-cycle-non-empty-desktops KWin Script to switch to the next or previous non empty virtual desktop. Installation Method 1: From the KDE Store Go to

Dec 5, 2022

❤️ A heart-shaped toggle switch component for React.

❤️ A heart-shaped toggle switch component for React.

heart-switch A heart-shaped toggle switch component for React. Inspired by Tore Bernhoft's I heart toggle Dribbble shot. 📖 Table of Contents 🚀 Getti

Dec 15, 2022

Automatically switch Terminal profile based on dark / light mode

Automatically switch Terminal profile based on dark / light mode

auto-terminal-profile Automatically switch the macOS Terminal profile based on the system-wide dark / light appearance mode Prerequisites Node.js 14-1

Jan 2, 2023

Pure CSS toggle switch

Pure CSS toggle switch demo install npm i toggle-switch-css or yarn add toggle-switch-css use label class="toggle-switch my-toggle-switch" in

Sep 14, 2021

switch Alternate Style Sheets

alternate-stylesheets.js switch Alternate Style Sheets Installation npm $ npm install alternate-stylesheets yarn $ yarn add alternate-stylesheets Usag

Dec 11, 2021
Comments
  • feat!: support multi conditions

    feat!: support multi conditions

    This PR implements inline support of multi conditions using the following syntax:

    <script>
      let animal = 'dog';
    </script>
    
    <section>
      Can fly ?
      {#switch animal}
        {:case "cat" || "dog"}
          <p>No</p>
        {:case "bird"}
          <p>Yes</p>
      {/switch}
    </section>
    

    Closes #1

    opened by l-portet 1
  • Support multi conditions

    Support multi conditions

    RFC: Support multi conditions

    This feature would allow to use multiple conditions in one case statement.

    Motivation

    This is the current syntax when we have multiple conditions rendering the same result:

    <script>
      let animal = 'dog';
    </script>
    
    <section>
      Can fly ?
      {#switch animal}
        {:case "cat"}
          <p>No</p>
        {:case "dog"}
          <p>No</p>
        {:case "bird"}
          <p>Yes</p>
      {/switch}
    </section>
    

    Issues:

    • Code is unnecessarily repeated.
    • The same result is re-rendered if the value of animal switches from "cat" to "dog".

    Detailed design

    The current proposal implements an inline multi conditions syntax.

    <script>
      let animal = 'dog';
    </script>
    
    <section>
      Can fly ?
      {#switch animal}
        {:case "cat" || "dog"}
          <p>No</p>
        {:case "bird"}
          <p>Yes</p>
      {/switch}
    </section>
    

    Alternatives

    The "native" switch syntax which implements the {:break} statement has been considered. It would allow to combine multiple {:case} falling back to the same result.

    However, we are skeptical about its cleanliness and it seems to lengthen the code. Compare the proposed design to the following:

    <script>
      let animal = 'dog';
    </script>
    
    <section>
      Can fly ?
      {#switch animal}
        {:case "cat"}
        {:case "dog"}
          <p>No</p>
        {:break}
        {:case "bird"}
          <p>Yes</p>
        {:break}
      {/switch}
    </section>
    

    It would also require refactoring all the projects using the current syntax and adding a {:break} statement to each condition.

    Unresolved questions

    There aren't really any unresolved questions at this point. I'm just opening this RFC if anyone wants to express themselves towards the proposed changes.

    In any case, if we decide to implement this feature, it will be in the next major version so we ensure it's non-breaking for the current projects using this package.

    Credits

    Thanks to those Reddit comments for raising the code repetition problem.

    RFC 
    opened by l-portet 0
Owner
Lucas Portet
lbsu student by day, co-founder at macas.studio by night 🇫🇷
Lucas Portet
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
A Svelte parser that compiles to Mitosis JSON, allowing you to write Svelte components once and compile to every framework.

Sveltosis Still in development A Svelte parser that compiles to Mitosis JSON, allowing you to write Svelte components once and compile to every framew

sveltosis 70 Nov 24, 2022
Get discord application's assets in case you don't have them on your PC

get-discord-app-assets Get discord application's assets in case you don't have them on your PC (this is also the reason why I made this script) I came

Narcis B. 7 Dec 25, 2022
🚨 Make the case correct, PLEASE!

?? CasePolice GitHub, not Github TypeScript, not Typescript macOS, not MacOS VS Code, not Vscode ... Make the case correct, PLEASE! Usage Make sure yo

Anthony Fu 944 Dec 26, 2022
A TypeScript namespace declaration for KeyboardEvent.key strings, just in case your code is allergic to enums.

ts-key-namespace A TypeScript namespace declaration for KeyboardEvent.key strings, just in case you prefer namespaces to enums. Largely based on ts-ke

Daniel Soohan Park 3 Apr 5, 2022
This project demonstrates single transaction and batch transaction use case

Batch Transaction Fullstack ( Localhost:8545 ) This project demonstrates single transaction and batch transaction use case. It comes with a transactio

Lemonde Shazai  39 Dec 27, 2022
A webpack plugin to enforce case-sensitive paths when resolving module

@umijs/case-sensitive-paths-webpack-plugin A webpack plugin to enforce case-sensitive paths when resolving module, similar to the well-known case-sens

UmiJS 13 Jul 25, 2022
a simple wrapper nestjs dynamic module on top of surrealdb.js driver, with a consumer app to show case library in action, nothing fancy

README README Project Components Dynamic Module Consumer App Install SurrealDb Starts SurrealDb Init surrealDb Database Run App from Source Code Launc

Mário Monteiro 0 Oct 3, 2022