summaryrefslogtreecommitdiffstats
path: root/apps/web-shared/src/components/button.svelte
blob: 5eaf19f2e9f898e8ede113d14209763dc6248f62 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<script lang="ts">
    import Icon from "$shared/components/icon.svelte";

    export let text = "";
    export let title = "";
    export let href = "";
    export let variant: "primary"|"secondary"|"subtle" = "primary";
    export let type: "button"|"submit"|"reset" = "button";
    export let disabled = false;
    export let loading = false;
    export let icon = "";
    export let icon_right_aligned = false;
    export let icon_width = false;
    export let icon_height = false;
    export let id;
    export let tabindex;
    export let style;
    
    $: shared_props = {
        type: type,
        id: id || null,
        title: title || null,
        disabled: disabled || null,
        tabindex: tabindex || null,
        style: style || null,
        "aria-controls": ($$restProps["aria-controls"] ?? "") || null,
        class: [variant === "reset" ? "reset" : `btn btn--${variant} btn--preserve-width  ${loading ? "btn--state-b" : ""}`, $$restProps.class ?? ""].filter(Boolean).join(" "),
    };
</script>

<template>
    {#if href && !disabled}
        <a {href}
           {...shared_props}
           on:click>
            <span class="btn__content-a">
                {#if icon !== ""}
                    {#if icon_right_aligned}
                        {text}
                        <Icon class="{text ? 'margin-left-xxxs': ''}"
                              width={icon_width}
                              height={icon_height}
                              name={icon}/>
                    {:else}
                        <Icon class="{text ? 'margin-left-xxxs': ''}"
                              width={icon_width}
                              height={icon_height}
                              name={icon}/>
                        {text}
                    {/if}
                {:else}
                    {text}
                {/if}
            </span>
            {#if variant !== "reset" && loading}
                <span class="btn__content-b">
                    <svg class="icon icon--is-spinning"
                         aria-hidden="true"
                         viewBox="0 0 16 16">
                        <title>Loading</title>
                        <g stroke-width="1"
                           fill="currentColor"
                           stroke="currentColor">
                            <path d="M.5,8a7.5,7.5,0,1,1,1.91,5"
                                  fill="none"
                                  stroke="currentColor"
                                  stroke-linecap="round"
                                  stroke-linejoin="round"/>
                        </g>
                    </svg>
                </span>
            {/if}
        </a>
    {:else}
        <button {...shared_props}
                on:click>
            <span class="btn__content-a">
                {#if icon !== ""}
                    {#if icon_right_aligned}
                        {text}
                        <Icon class="{text ? 'margin-left-xxxs': ''}"
                              width={icon_width}
                              height={icon_height}
                              name={icon}/>
                    {:else}
                        <Icon class="{text ? 'margin-left-xxxs': ''}"
                              width={icon_width}
                              height={icon_height}
                              name={icon}/>
                        {text}
                    {/if}
                {:else}
                    {text}
                {/if}
            </span>
            {#if variant !== "reset" && loading}
                <span class="btn__content-b">
                    <svg class="icon icon--is-spinning"
                         aria-hidden="true"
                         viewBox="0 0 16 16">
                        <title>Loading</title>
                        <g stroke-width="1"
                           fill="currentColor"
                           stroke="currentColor">
                            <path d="M.5,8a7.5,7.5,0,1,1,1.91,5"
                                  fill="none"
                                  stroke="currentColor"
                                  stroke-linecap="round"
                                  stroke-linejoin="round"/>
                        </g>
                    </svg>
                </span>
            {/if}
        </button>
    {/if}
</template>