aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Amount.svelte
blob: 7dea7cf163724d4129f6c51c6eaa6b8f7abe7695 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<script>
  import { copy, s, f, toFiat } from "$lib/utils";
  import { fiat } from "$lib/store";
  const {
    align,
    amount,
    locale,
    tip,
    rate,
    currency,
    locked = false,
  } = $props();

  const icon = $derived(locked ? "ph:lock-fill" : "ph:lightning-fill");
  const canShowFiat = $derived($fiat && rate && currency);

  const toggleFiat = (e) => {
    e.preventDefault();
    e.stopPropagation();
    $fiat = !$fiat;
  };

</script>

{#if typeof amount !== "undefined"}
  <div>
    <h2
      class="text-2xl md:text-3xl font-semibold flex items-end"
      class:justify-center={align !== "left"}
    >
      <button
        type="button"
        class="flex items-center"
        onclick={toggleFiat}
        aria-label="Toggle currency display"
      >
        {#if !canShowFiat}
          <iconify-icon
            noobserver
            icon={icon}
            class="text-yellow-300"
          ></iconify-icon>
        {/if}
        {#if canShowFiat}
          {f(toFiat(amount, rate), currency, locale)}
        {:else}
          {s(amount, locale)}
        {/if}
      </button>

      {#if tip}
        <div class="flex items-center text-lg ml-2">
          <div>+</div>
          <div class="font-semibold">
            {#if canShowFiat}
              {f(toFiat(tip, rate), currency, locale)}
            {:else}
              {s(tip, locale)}
            {/if}
          </div>
        </div>
      {/if}
    </h2>
  </div>
{/if}