-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathtext-field.svelte
158 lines (139 loc) · 3.63 KB
/
text-field.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
<script lang="ts">
import { commonMessages as t } from '@slowreader/core'
import { nanoid } from 'nanoid/non-secure'
import { onMount } from 'svelte'
let {
controls,
error,
errorId,
hideLabel = false,
label,
onchange,
oninput,
placeholder = '',
required = false,
spellcheck = true,
type = 'text',
value = $bindable('')
}: {
controls?: string
error?: string
errorId?: string
hideLabel?: boolean
label: string
onchange?: (v: string, valid: boolean) => void
oninput?: (v: string, valid: boolean) => void
placeholder?: string
required?: boolean
spellcheck?: boolean
type?: 'email' | 'password' | 'text' | 'url'
value?: string
} = $props()
let id = nanoid()
let inputError = $state<string | undefined>(error)
function validate(): void {
if (required && !value) {
inputError = t.get().empty
} else if (type === 'url' && !URL.canParse(value)) {
inputError = t.get().noUrl
}
}
function isValid(): boolean {
return !inputError || !error || !errorId
}
function onInput(e: { currentTarget: HTMLInputElement } & Event): void {
value = e.currentTarget.value
if (oninput) oninput(value, isValid())
}
function onChange(e: { currentTarget: HTMLInputElement } & Event): void {
value = e.currentTarget.value
validate()
if (onchange) onchange(value, isValid())
}
function onKeyUp(): void {
if (required) {
if (!value) {
inputError = t.get().empty
}
}
}
function onBlur(): void {
validate()
}
onMount(() => {
if (value && type === 'url') {
if (!URL.canParse(value)) inputError = t.get().noUrl
}
})
</script>
<div class="text-field">
{#if label && !hideLabel}
<label class="text-field_label" for={id}>{label}</label>
{/if}
<input
id={label ? id : null}
class="text-field_input"
aria-controls={controls}
aria-errormessage={errorId || (inputError || error ? `${id}-error` : null)}
aria-invalid={inputError || error || errorId ? true : null}
aria-label={hideLabel ? label : null}
onblur={onBlur}
onchange={onChange}
oninput={onInput}
onkeyup={onKeyUp}
{placeholder}
{required}
spellcheck={spellcheck ? null : 'false'}
{type}
{value}
/>
{#if inputError}
<div id={`${id}-error`} class="text-field_error">{inputError}</div>
{:else if error}
<div id={`${id}-error`} class="text-field_error">{error}</div>
{/if}
</div>
<style global>
:global {
.text-field {
position: relative;
flex-shrink: 1;
width: 100%;
margin-top: var(--padding-l);
}
.card > .text-field:first-child {
margin-top: calc(var(--card-text-fix) - 4px);
}
.text-field:first-child:not(:has(.text-field_label)) {
margin-top: 0;
}
.text-field_label {
padding: 0 var(--padding-m) var(--padding-m) var(--padding-m);
font-weight: bold;
}
.text-field_input {
box-sizing: border-box;
width: 100%;
padding: var(--padding-m);
background-color: var(--field-color);
border: 1px solid var(--border-color);
border-radius: var(--radius);
box-shadow: var(--field-shadow);
&[aria-invalid='true'] {
border-color: var(--error-color);
}
&:focus-visible {
border-color: var(--focus-color);
outline: 3px solid oklch(from var(--focus-color) l c h / 50%);
outline-offset: 0;
}
}
.text-field_error {
padding: 0 var(--padding-m);
color: var(--error-color);
}
.card > .text-field_error:last-child {
margin-bottom: var(--card-text-fix);
}
}
</style>