mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-08 21:20:45 +08:00
168 lines
4.3 KiB
Svelte
168 lines
4.3 KiB
Svelte
<script lang="ts" strictEvents>
|
|
import debounce from "lodash/debounce";
|
|
import { createEventDispatcher } from "svelte";
|
|
import { onMount } from "svelte";
|
|
|
|
import IconSmall from "@app/components/IconSmall.svelte";
|
|
import KeyHint from "@app/components/KeyHint.svelte";
|
|
import Loading from "@app/components/Loading.svelte";
|
|
|
|
export let name: string | undefined = undefined;
|
|
export let placeholder: string | undefined = undefined;
|
|
export let value: string | undefined = undefined;
|
|
|
|
export let autofocus: boolean = false;
|
|
export let disabled: boolean = false;
|
|
export let loading: boolean = false;
|
|
export let valid: boolean = true;
|
|
export let showKeyHint: boolean = true;
|
|
|
|
const dispatch = createEventDispatcher<{
|
|
blur: FocusEvent;
|
|
focus: FocusEvent;
|
|
submit: null;
|
|
}>();
|
|
|
|
let rightContainerWidth: number;
|
|
let inputElement: HTMLInputElement | undefined = undefined;
|
|
let isFocused = false;
|
|
let success = false;
|
|
|
|
onMount(() => {
|
|
if (autofocus && inputElement) {
|
|
// We set preventScroll to true for Svelte animations to work.
|
|
inputElement.focus({ preventScroll: true });
|
|
}
|
|
});
|
|
|
|
const restoreIcon = debounce(() => {
|
|
success = false;
|
|
}, 800);
|
|
|
|
function handleKeydown(event: KeyboardEvent) {
|
|
if (event.key === "Enter" && valid) {
|
|
success = true;
|
|
dispatch("submit");
|
|
restoreIcon();
|
|
}
|
|
|
|
if (event.key === "Escape") {
|
|
inputElement?.blur();
|
|
}
|
|
}
|
|
|
|
function handleFocusEvent(e: FocusEvent) {
|
|
if (isFocused) {
|
|
dispatch("blur", e);
|
|
} else {
|
|
dispatch("focus", e);
|
|
}
|
|
isFocused = !isFocused;
|
|
}
|
|
</script>
|
|
|
|
<style>
|
|
.wrapper {
|
|
display: flex;
|
|
flex-direction: column;
|
|
margin: 0;
|
|
position: relative;
|
|
flex: 1;
|
|
align-items: center;
|
|
height: var(--button-small-height);
|
|
background: var(--color-background-dip);
|
|
}
|
|
input {
|
|
background: var(--color-background-dip);
|
|
font-size: var(--font-size-small);
|
|
color: var(--color-foreground-contrast);
|
|
border: 1px solid var(--color-border-hint);
|
|
border-radius: var(--border-radius-tiny);
|
|
line-height: 1.6;
|
|
outline: none;
|
|
text-overflow: ellipsis;
|
|
width: 100%;
|
|
height: 100%;
|
|
padding-left: 0.75rem;
|
|
margin: 0;
|
|
}
|
|
input::placeholder {
|
|
font-family: var(--font-family-sans-serif);
|
|
color: var(--color-foreground-dim);
|
|
opacity: 1 !important;
|
|
}
|
|
input:hover:not(.invalid) {
|
|
border: 1px solid var(--color-border-default);
|
|
}
|
|
input:hover:not(.invalid) + .right-container {
|
|
border-top: 1px solid var(--color-border-default);
|
|
border-right: 1px solid var(--color-border-default);
|
|
border-bottom: 1px solid var(--color-border-default);
|
|
color: var(--color-fill-contrast);
|
|
}
|
|
input:focus:not(.invalid) + .right-container {
|
|
border-top: 1px solid var(--color-fill-secondary);
|
|
border-right: 1px solid var(--color-fill-secondary);
|
|
border-bottom: 1px solid var(--color-fill-secondary);
|
|
color: var(--color-fill-contrast);
|
|
}
|
|
input:focus:not(.invalid) {
|
|
border: 1px solid var(--color-fill-secondary);
|
|
}
|
|
input[disabled] {
|
|
cursor: not-allowed;
|
|
}
|
|
.right-container {
|
|
border: 1px solid transparent;
|
|
color: var(--color-fill-gray);
|
|
position: absolute;
|
|
right: 0;
|
|
top: 0;
|
|
display: flex;
|
|
align-items: center;
|
|
padding-left: 0.5rem;
|
|
overflow: hidden;
|
|
height: 100%;
|
|
border-top-right-radius: var(--border-radius-tiny);
|
|
border-bottom-right-radius: var(--border-radius-tiny);
|
|
gap: 0.25rem;
|
|
}
|
|
.invalid {
|
|
border: 1px solid var(--color-border-error);
|
|
}
|
|
</style>
|
|
|
|
<div class="wrapper">
|
|
<input
|
|
class:invalid={!valid && value}
|
|
style:padding-right={rightContainerWidth
|
|
? `${rightContainerWidth}px`
|
|
: "auto"}
|
|
bind:this={inputElement}
|
|
type="text"
|
|
{name}
|
|
{placeholder}
|
|
{disabled}
|
|
bind:value
|
|
on:input
|
|
on:focus={handleFocusEvent}
|
|
on:blur={handleFocusEvent}
|
|
on:keydown|stopPropagation={handleKeydown}
|
|
on:click
|
|
on:change />
|
|
|
|
<div class="right-container" bind:clientWidth={rightContainerWidth}>
|
|
{#if loading}
|
|
<Loading small noDelay />
|
|
{/if}
|
|
|
|
{#if valid && !loading && isFocused && showKeyHint}
|
|
{#if success}
|
|
<IconSmall name="checkmark" />
|
|
{:else}
|
|
<KeyHint>⏎</KeyHint>
|
|
{/if}
|
|
{/if}
|
|
</div>
|
|
</div>
|