about summary refs log tree commit diff
path: root/client/src/Components/Form.svelte
blob: 327d25da5b2c4e45a3c27d848669164573750d85 (plain)
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
<script lang="ts">
    import shorten from "$actions/shorten";
    import { links } from "$data/links";
    import checkUrl from "$utils/checkUrl";
    import debounce from "$utils/debounce";
    import ArrowIcon from "./icons/ArrowIcon.svelte";
    import CrossIcon from "./icons/CrossIcon.svelte";

    let value = "";
    let valid = false;

    function submit() {
        const url = checkUrl(value);
        if (url !== null) {
            links.add(shorten(url));
        }
    }

    const check = debounce(() => valid = !!checkUrl(value), 100);

    // @ts-ignore: Value is a dependency
    $: value, check();
</script>

<style>
    form {
        position: relative;
        border: 1px solid #aaaabb;
        box-shadow: 0 4px 6px #aaaabb30;
        box-sizing: border-box;
        border-radius: 5px;
    }

    .field {
        box-sizing: border-box;
        width: 100%;
        border: none;
        padding: 15px 50px 15px 20px;
        background: transparent;
        font-size: 1rem;
    }

    .button {
        position: absolute;
        right: 10px;
        margin: auto;
        top: 0;
        bottom: 0;
        background: transparent;
        border: none;
        cursor: pointer;
    }
</style>

<form on:submit|preventDefault={submit}>
    <input class="field" bind:value type="text"/>
    <button class="button" type="submit">
        {#if valid}
            <ArrowIcon/>
        {:else}
            <CrossIcon/>
        {/if}
    </button>
</form>