svelte-switch-case Svelte Themes

Svelte Switch Case

Switch case syntax for Svelte ⚡️

Svelte switch case

Switch case syntax for your Svelte components.

Demo · StackBlitz · NPM Package


:zap: 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>

:mag: 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}

:raised_hands: Contribute

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

Top categories

Loading Svelte Themes