DEV Community

Elid
Elid

Posted on

Svelte Counter I made to understand runes

The final result.
Image description

This is the Svelte code I wrote to create a counter.

Link to code

<script>
  let number = $state(0);
  let currentValue = $derived(number);

  const addByOne = () => (number += 1);
  const decreaseByOne = () => (number > 0 ? (number -= 1) : 0);
  const reset = () => (number = 0);
  const multiplyByTwo = () => (number *= 2);
</script>

<div class="container">
  <h1>Counter: {number}</h1>
  <button class="addBtn" onclick={addByOne}>Add By One</button>
  <button class="decreaseBtn" onclick={decreaseByOne}>Decrease By One</button>
  <button onclick={reset}>Reset</button>
  <button onclick={multiplyByTwo}>Multiply By Two</button>
  <p>Current Value is: {currentValue}</p>
</div>

<style>
  :root {
    --backgound_color: #1e293b;
    --font_family: fantasy;
    --btn_color: #18181b;
    --btn_add_color: #047857;
    --btn_decrease_color: #dc2626;
  }

  .container {
    background-color: var(--backgound_color);
    font-family: var(--font_family);
    text-align: center;
    max-width: 100%;
    height: 100vh;
    padding: 3rem;
  }

  h1 {
    font-size: 3rem;
  }

  button {
    background-color: var(--btn_color);
    border: none;
    border-radius: 5px;
    font-family: var(--font_family);
    font-size: 18px;
    padding: 10px;
  }

  .addBtn {
    background-color: var(--btn_add_color);
  }

  .decreaseBtn {
    background-color: var(--btn_decrease_color);
  }

  p {
    font-size: 2rem;
  }
</style>
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (1)

Collapse
 
lionelrowe profile image
lionel-rowe

ᛞᛁᛞ ᛁᛏ ᚹᛟᚱᚲ? ᛞᛟ ᚢᛖ ᚢᚾᛞᛖᚱᛋᛏᚨᚾᛞ ᚱᚢᚾᛖᛋ ᚾᚨᚹ?

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay