-
Notifications
You must be signed in to change notification settings - Fork 1
/
TextInput.svelte
191 lines (159 loc) · 4.88 KB
/
TextInput.svelte
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!--
Copyright © 2022 The Radicle Design System Contributors
This file is part of radicle-design-system, distributed under the GPLv3
with Radicle Linking Exception. For full terms see the included
LICENSE file.
-->
<script lang="ts">
import type { TextInputValidationState } from "./TextInput";
import CheckCircleIcon from "./icons/CheckCircle.svelte";
import ExclamationCircleIcon from "./icons/ExclamationCircle.svelte";
import KeyHint from "./KeyHint.svelte";
import Spinner from "./Spinner.svelte";
export let variant:
| { type: "text" }
| { type: "password" }
| { type: "number"; min: number } = {
type: "text",
};
export let autofocus: boolean = false;
export let disabled: boolean = false;
export let readonly: boolean = false;
export let showSuccessCheck: boolean = false;
export let inputStyle: string | undefined = undefined;
export let style: string | undefined = undefined;
export let value: string | number | undefined = undefined;
export let placeholder: string | undefined = undefined;
export let hint: string | undefined = undefined;
export let suffix: string | undefined = undefined;
export let validationState: TextInputValidationState = {
type: "unvalidated",
};
export const focus = (): void => {
inputElement && inputElement.focus();
};
let inputElement: HTMLInputElement | undefined = undefined;
// Can't use normal `autofocus` attribute on the `inputElement`: "Autofocus
// processing was blocked because a document's URL has a fragment".
// preventScroll is necessary for onboarding animations to work.
$: if (autofocus) {
inputElement && inputElement.focus({ preventScroll: true });
}
// We do it this way to work around the svelte-check error: 'type' attribute
// cannot be dynamic if input uses two-way binding (svelte).
$: if (inputElement) {
inputElement.type = variant.type;
}
let rightContainerWidth: number;
</script>
<style>
.wrapper {
display: flex;
flex-direction: column;
position: relative;
width: 100%;
}
input {
background-color: var(--color-background);
border-radius: 0.5rem;
border: 1px solid var(--color-foreground-level-3);
height: 2.5rem;
padding: 0.5rem 0.75rem;
width: 100%;
}
input[disabled] {
background-color: var(--color-foreground-level-1);
color: var(--color-foreground-level-4);
cursor: not-allowed;
}
input[disabled]::placeholder {
color: var(--color-foreground-level-4);
}
input[disabled]:hover {
background-color: var(--color-foreground-level-1);
}
input[readonly]:hover {
cursor: pointer;
}
.right-container {
align-items: center;
display: flex;
height: 2.5rem;
position: absolute;
right: 0;
top: 0;
}
.concealed {
color: var(--color-foreground-level-6);
}
input::placeholder {
color: var(--color-foreground-level-5);
}
input:focus,
input:hover {
background-color: var(--color-foreground-level-1);
border: 1px solid var(--color-foreground-level-3);
outline: none;
}
input.invalid:focus,
input.invalid {
background-position: right 0.875rem top 55%;
background: var(--color-background);
border: 1px solid var(--color-negative);
outline: none;
}
input.invalid:focus {
background: var(--color-foreground-level-1);
}
.validation-message {
align-items: center;
color: var(--color-negative);
display: flex;
margin-left: 0.75rem;
margin-top: 0.75rem;
text-align: left;
}
</style>
<div {style} class="wrapper">
<input
style={`${inputStyle}; padding-right: ${
rightContainerWidth ? `${rightContainerWidth}px` : "auto"
};`}
class:invalid={validationState.type === "invalid"}
class:concealed={variant.type === "password"}
min={variant.type === "number" ? variant.min : undefined}
{placeholder}
{disabled}
{readonly}
spellcheck={false}
bind:value
bind:this={inputElement}
on:change
on:click
on:input
on:keydown
on:keypress />
<div class="right-container" bind:clientWidth={rightContainerWidth}>
{#if hint && (validationState.type === "unvalidated" || validationState.type === "valid")}
<KeyHint style="margin: 0 0.5rem;">{hint}</KeyHint>
{/if}
{#if suffix}
<span style="color: var(--color-foreground-level-5); margin: 0 0.5rem;">
{suffix}
</span>
{/if}
{#if validationState.type === "pending"}
<Spinner style="margin: 0 0.5rem;" />
{:else if showSuccessCheck && validationState.type === "valid"}
<CheckCircleIcon style="fill: var(--color-positive); margin: 0 0.5rem;" />
{:else if validationState.type === "invalid"}
<ExclamationCircleIcon
style="fill: var(--color-negative); margin: 0 0.5rem;" />
{/if}
</div>
{#if validationState.type === "invalid"}
<div class="validation-message">
{validationState.message}
</div>
{/if}
</div>