2026-02-07 18:01:48 +03:00
|
|
|
<!--
|
|
|
|
|
Component: Input
|
2026-02-24 17:58:00 +03:00
|
|
|
design-system input. Zero border-radius, Space Grotesk, precise states.
|
2026-02-07 18:01:48 +03:00
|
|
|
-->
|
2026-02-24 17:58:00 +03:00
|
|
|
<script lang="ts">
|
2026-04-23 09:48:32 +03:00
|
|
|
import { cn } from '$shared/lib';
|
2026-02-24 17:58:00 +03:00
|
|
|
import XIcon from '@lucide/svelte/icons/x';
|
|
|
|
|
import type { Snippet } from 'svelte';
|
|
|
|
|
import { cubicOut } from 'svelte/easing';
|
|
|
|
|
import type { HTMLInputAttributes } from 'svelte/elements';
|
|
|
|
|
import { scale } from 'svelte/transition';
|
2026-04-16 16:32:41 +03:00
|
|
|
import {
|
|
|
|
|
inputSizeConfig,
|
|
|
|
|
inputVariantConfig,
|
|
|
|
|
} from './config';
|
2026-02-24 17:58:00 +03:00
|
|
|
import type {
|
|
|
|
|
InputSize,
|
|
|
|
|
InputVariant,
|
|
|
|
|
} from './types';
|
2026-02-15 23:00:12 +03:00
|
|
|
|
2026-02-24 17:58:00 +03:00
|
|
|
interface Props extends Omit<HTMLInputAttributes, 'size'> {
|
2026-03-02 22:19:35 +03:00
|
|
|
/**
|
|
|
|
|
* Visual style variant
|
|
|
|
|
* @default 'default'
|
|
|
|
|
*/
|
2026-02-24 17:58:00 +03:00
|
|
|
variant?: InputVariant;
|
2026-03-02 22:19:35 +03:00
|
|
|
/**
|
|
|
|
|
* Input size
|
|
|
|
|
* @default 'md'
|
|
|
|
|
*/
|
2026-02-24 17:58:00 +03:00
|
|
|
size?: InputSize;
|
2026-03-02 22:19:35 +03:00
|
|
|
/**
|
|
|
|
|
* Invalid state
|
|
|
|
|
*/
|
2026-02-24 17:58:00 +03:00
|
|
|
error?: boolean;
|
2026-03-02 22:19:35 +03:00
|
|
|
/**
|
|
|
|
|
* Helper text
|
|
|
|
|
*/
|
2026-02-24 17:58:00 +03:00
|
|
|
helperText?: string;
|
2026-03-02 22:19:35 +03:00
|
|
|
/**
|
|
|
|
|
* Show clear button
|
|
|
|
|
* @default false
|
|
|
|
|
*/
|
2026-02-24 17:58:00 +03:00
|
|
|
showClearButton?: boolean;
|
2026-03-02 22:19:35 +03:00
|
|
|
/**
|
|
|
|
|
* Clear button callback
|
|
|
|
|
*/
|
2026-02-24 17:58:00 +03:00
|
|
|
onclear?: () => void;
|
2026-02-07 18:01:48 +03:00
|
|
|
/**
|
2026-03-02 22:19:35 +03:00
|
|
|
* Left icon snippet
|
2026-02-07 18:01:48 +03:00
|
|
|
*/
|
2026-02-24 17:58:00 +03:00
|
|
|
leftIcon?: Snippet<[InputSize]>;
|
2026-02-07 18:01:48 +03:00
|
|
|
/**
|
2026-03-02 22:19:35 +03:00
|
|
|
* Right icon snippet
|
2026-02-07 18:01:48 +03:00
|
|
|
*/
|
2026-02-24 17:58:00 +03:00
|
|
|
rightIcon?: Snippet<[InputSize]>;
|
2026-03-02 22:19:35 +03:00
|
|
|
/**
|
|
|
|
|
* Full width
|
|
|
|
|
* @default false
|
|
|
|
|
*/
|
2026-02-24 17:58:00 +03:00
|
|
|
fullWidth?: boolean;
|
2026-03-02 22:19:35 +03:00
|
|
|
/**
|
|
|
|
|
* Input value
|
|
|
|
|
*/
|
2026-02-24 17:58:00 +03:00
|
|
|
value?: string | number | readonly string[];
|
2026-03-02 22:19:35 +03:00
|
|
|
/**
|
|
|
|
|
* CSS classes
|
|
|
|
|
*/
|
2026-02-07 18:01:48 +03:00
|
|
|
class?: string;
|
2026-02-24 17:58:00 +03:00
|
|
|
}
|
2026-02-07 18:01:48 +03:00
|
|
|
|
|
|
|
|
let {
|
2026-02-24 17:58:00 +03:00
|
|
|
variant = 'default',
|
|
|
|
|
size = 'md',
|
|
|
|
|
error = false,
|
|
|
|
|
helperText,
|
|
|
|
|
showClearButton = false,
|
|
|
|
|
onclear,
|
|
|
|
|
leftIcon,
|
|
|
|
|
rightIcon,
|
|
|
|
|
fullWidth = false,
|
2026-02-07 18:01:48 +03:00
|
|
|
value = $bindable(''),
|
|
|
|
|
class: className,
|
|
|
|
|
...rest
|
2026-02-24 17:58:00 +03:00
|
|
|
}: Props = $props();
|
|
|
|
|
|
|
|
|
|
const hasValue = $derived(value !== undefined && value !== '');
|
|
|
|
|
const showClear = $derived(showClearButton && hasValue && !!onclear);
|
|
|
|
|
const hasRightSlot = $derived(!!rightIcon || showClearButton);
|
2026-04-16 16:32:41 +03:00
|
|
|
const cfg = $derived(inputSizeConfig[size]);
|
|
|
|
|
const styles = $derived(inputVariantConfig[variant]);
|
2026-02-24 17:58:00 +03:00
|
|
|
|
2026-04-23 09:48:32 +03:00
|
|
|
const inputClasses = $derived(cn(
|
2026-02-25 09:56:59 +03:00
|
|
|
'font-primary rounded-none outline-none transition-all duration-200',
|
2026-02-24 17:58:00 +03:00
|
|
|
'text-neutral-900 dark:text-neutral-100',
|
|
|
|
|
'placeholder:text-neutral-400 dark:placeholder:text-neutral-600',
|
|
|
|
|
'disabled:opacity-50 disabled:cursor-not-allowed',
|
|
|
|
|
cfg.input,
|
|
|
|
|
cfg.text,
|
|
|
|
|
cfg.height,
|
|
|
|
|
styles.base,
|
|
|
|
|
error ? styles.error : styles.focus,
|
|
|
|
|
!!leftIcon && 'pl-10',
|
|
|
|
|
hasRightSlot && 'pr-10',
|
|
|
|
|
fullWidth && 'w-full',
|
|
|
|
|
className,
|
|
|
|
|
));
|
2026-02-07 18:01:48 +03:00
|
|
|
</script>
|
|
|
|
|
|
2026-04-23 09:48:32 +03:00
|
|
|
<div class={cn('flex flex-col gap-1', fullWidth && 'w-full')}>
|
|
|
|
|
<div class={cn('relative group', fullWidth && 'w-full')}>
|
2026-02-24 17:58:00 +03:00
|
|
|
<!-- Left icon slot -->
|
|
|
|
|
{#if leftIcon}
|
|
|
|
|
<div class="absolute left-3 top-1/2 -translate-y-1/2 text-neutral-400 dark:text-neutral-600 pointer-events-none z-10 flex items-center">
|
|
|
|
|
{@render leftIcon(size)}
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
|
|
<!-- Input -->
|
|
|
|
|
<input class={inputClasses} bind:value {...rest} />
|
|
|
|
|
|
|
|
|
|
<!-- Right slot: clear button + rightIcon -->
|
|
|
|
|
{#if hasRightSlot}
|
|
|
|
|
<div class="absolute right-3 top-1/2 -translate-y-1/2 flex items-center gap-2 z-10">
|
|
|
|
|
{#if showClear}
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
tabindex={-1}
|
|
|
|
|
onclick={onclear}
|
|
|
|
|
class="text-neutral-400 hover:text-brand transition-colors p-0.5 flex items-center"
|
|
|
|
|
in:scale={{ duration: 150, start: 0.8, easing: cubicOut }}
|
|
|
|
|
out:scale={{ duration: 100, start: 0.8, easing: cubicOut }}
|
|
|
|
|
>
|
|
|
|
|
<XIcon size={cfg.clearIcon} />
|
|
|
|
|
</button>
|
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
|
|
{#if rightIcon}
|
|
|
|
|
<div class="text-neutral-400 dark:text-neutral-600 flex items-center">
|
|
|
|
|
{@render rightIcon(size)}
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<!-- Helper / error text -->
|
|
|
|
|
{#if helperText}
|
|
|
|
|
<span
|
2026-04-23 09:48:32 +03:00
|
|
|
class={cn(
|
2026-04-17 09:41:55 +03:00
|
|
|
'text-2xs font-mono tracking-wide px-1',
|
2026-04-16 16:32:41 +03:00
|
|
|
error ? 'text-brand ' : 'text-secondary',
|
2026-02-24 17:58:00 +03:00
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{helperText}
|
|
|
|
|
</span>
|
|
|
|
|
{/if}
|
|
|
|
|
</div>
|