2023-01-23 16:38:43 +01:00
|
|
|
<script>
|
2023-02-07 13:11:25 +01:00
|
|
|
import { createEventDispatcher, onMount } from "svelte"
|
2023-01-23 16:38:43 +01:00
|
|
|
import FancyField from "./FancyField.svelte"
|
|
|
|
import FancyFieldLabel from "./FancyFieldLabel.svelte"
|
2023-01-25 16:55:57 +01:00
|
|
|
import { fade } from "svelte/transition"
|
2023-01-23 16:38:43 +01:00
|
|
|
|
|
|
|
export let label
|
|
|
|
export let value
|
|
|
|
export let type = "text"
|
|
|
|
export let disabled = false
|
|
|
|
export let error = null
|
|
|
|
export let validate = null
|
2023-01-25 16:55:57 +01:00
|
|
|
export let suffix = null
|
2023-01-23 16:38:43 +01:00
|
|
|
|
|
|
|
const dispatch = createEventDispatcher()
|
|
|
|
|
2023-02-07 13:11:25 +01:00
|
|
|
let ref
|
2023-01-23 16:38:43 +01:00
|
|
|
let focused = false
|
2023-02-07 13:11:25 +01:00
|
|
|
let autofilled = false
|
|
|
|
|
|
|
|
$: placeholder = !autofilled && !focused && !value
|
2023-01-23 16:38:43 +01:00
|
|
|
|
|
|
|
const onChange = e => {
|
|
|
|
const newValue = e.target.value
|
|
|
|
dispatch("change", newValue)
|
|
|
|
value = newValue
|
|
|
|
if (validate) {
|
|
|
|
error = validate(newValue)
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 13:11:25 +01:00
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
// Start watching for autofill every 100ms
|
|
|
|
const interval = setInterval(() => {
|
|
|
|
autofilled = ref?.matches(":-webkit-autofill")
|
|
|
|
if (autofilled) {
|
|
|
|
clearInterval(interval)
|
|
|
|
}
|
|
|
|
}, 100)
|
|
|
|
|
|
|
|
// Give up after 2 seconds and assume autofill has not been used
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
clearInterval(interval)
|
|
|
|
}, 2000)
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
return () => {
|
|
|
|
clearInterval(interval)
|
|
|
|
clearTimeout(timeout)
|
|
|
|
}
|
|
|
|
})
|
2023-01-23 16:38:43 +01:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<FancyField {error} {value} {validate} {disabled} {focused}>
|
|
|
|
{#if label}
|
|
|
|
<FancyFieldLabel {placeholder}>{label}</FancyFieldLabel>
|
|
|
|
{/if}
|
|
|
|
<input
|
|
|
|
{disabled}
|
|
|
|
value={value || ""}
|
|
|
|
type={type || "text"}
|
|
|
|
on:input={onChange}
|
|
|
|
on:focus={() => (focused = true)}
|
|
|
|
on:blur={() => (focused = false)}
|
|
|
|
class:placeholder
|
2023-02-07 13:11:25 +01:00
|
|
|
bind:this={ref}
|
2023-01-23 16:38:43 +01:00
|
|
|
/>
|
2023-01-25 16:55:57 +01:00
|
|
|
{#if suffix && !placeholder}
|
|
|
|
<div in:fade|local={{ duration: 130 }} class="suffix">{suffix}</div>
|
|
|
|
{/if}
|
2023-01-23 16:38:43 +01:00
|
|
|
</FancyField>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
input {
|
|
|
|
width: 100%;
|
|
|
|
transition: transform 130ms ease-out;
|
|
|
|
transform: translateY(9px);
|
|
|
|
background: transparent;
|
|
|
|
font-size: 15px;
|
|
|
|
line-height: 17px;
|
|
|
|
font-family: var(--font-sans);
|
|
|
|
color: var(--spectrum-global-color-gray-900);
|
|
|
|
outline: none;
|
|
|
|
border: none;
|
2023-01-25 16:57:47 +01:00
|
|
|
padding: 0;
|
|
|
|
margin: 0;
|
2023-01-23 16:38:43 +01:00
|
|
|
}
|
|
|
|
input.placeholder {
|
|
|
|
transform: translateY(0);
|
|
|
|
position: absolute;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
height: 100%;
|
|
|
|
}
|
2023-01-25 16:55:57 +01:00
|
|
|
.suffix {
|
|
|
|
color: var(--spectrum-global-color-gray-600);
|
|
|
|
transform: translateY(9px);
|
|
|
|
font-size: 15px;
|
|
|
|
line-height: 17px;
|
|
|
|
font-family: var(--font-sans);
|
|
|
|
}
|
2023-02-07 13:11:25 +01:00
|
|
|
input:-webkit-autofill {
|
|
|
|
border-radius: 2px;
|
|
|
|
-webkit-box-shadow: 0 0 0 100px var(--spectrum-global-color-gray-300) inset;
|
|
|
|
-webkit-text-fill-color: var(--spectrum-global-color-gray-900);
|
|
|
|
transition: -webkit-box-shadow 130ms 200ms, background-color 0s 86400s;
|
|
|
|
padding: 3px 8px 4px 8px;
|
|
|
|
}
|
2023-01-23 16:38:43 +01:00
|
|
|
</style>
|