|
1 | 1 | <script lang="ts"> |
2 | | - import type { Snippet } from 'svelte'; |
3 | 2 | import { twMerge } from 'tailwind-merge'; |
4 | 3 | import { idGenerator } from '../../uiHelpers.svelte'; |
5 | | - import type { HTMLInputAttributes } from 'svelte/elements'; |
6 | | -
|
7 | | - interface Props extends HTMLInputAttributes { |
8 | | - children: Snippet; |
9 | | - id?: string | undefined | null; |
10 | | - aria_describedby?: string | undefined | null; |
11 | | - style?: 'filled' | 'outlined' | 'standard'; |
12 | | - inputSize?: 'small' | 'default'; |
13 | | - color?: 'base' | 'green' | 'red'; |
14 | | - divClass?: string | undefined | null; |
15 | | - inputClass?: string | undefined | null; |
16 | | - labelClass?: string | undefined | null; |
17 | | - } |
| 4 | + import { type FloatingLabelInputProps as Props, floatingLabelInput } from '.'; |
| 5 | + |
18 | 6 | let { |
19 | 7 | children, |
20 | 8 | id = idGenerator(), |
21 | 9 | aria_describedby, |
22 | 10 | style = 'standard', |
23 | | - inputSize = 'default', |
24 | | - color = 'base', |
| 11 | + size = 'default', |
| 12 | + color = 'gray', |
25 | 13 | divClass, |
26 | 14 | inputClass, |
27 | 15 | labelClass, |
28 | 16 | ...attributes |
29 | 17 | }: Props = $props(); |
30 | 18 |
|
31 | | - const divClasses = { |
32 | | - filled: 'relative', |
33 | | - outlined: 'relative', |
34 | | - standard: 'relative z-0' |
35 | | - }; |
| 19 | + const { base, input, label } = $derived(floatingLabelInput({ style, size, color })); |
| 20 | +
|
| 21 | + // const divClasses = { |
| 22 | + // filled: 'relative', |
| 23 | + // outlined: 'relative', |
| 24 | + // standard: 'relative z-0' |
| 25 | + // }; |
36 | 26 |
|
37 | | - const inputSizes = { |
38 | | - filled: { |
39 | | - small: 'px-2.5 pb-1.5 pt-4', |
40 | | - default: 'px-2.5 pb-2.5 pt-5' |
41 | | - }, |
42 | | - outlined: { |
43 | | - small: 'px-2.5 pb-1.5 pt-3', |
44 | | - default: 'px-2.5 pb-2.5 pt-4' |
45 | | - }, |
46 | | - standard: { |
47 | | - small: 'py-2 px-0', |
48 | | - default: 'py-2.5 px-0' |
49 | | - } |
50 | | - }; |
| 27 | + // const inputSizes = { |
| 28 | + // filled: { |
| 29 | + // small: 'px-2.5 pb-1.5 pt-4', |
| 30 | + // default: 'px-2.5 pb-2.5 pt-5' |
| 31 | + // }, |
| 32 | + // outlined: { |
| 33 | + // small: 'px-2.5 pb-1.5 pt-3', |
| 34 | + // default: 'px-2.5 pb-2.5 pt-4' |
| 35 | + // }, |
| 36 | + // standard: { |
| 37 | + // small: 'py-2 px-0', |
| 38 | + // default: 'py-2.5 px-0' |
| 39 | + // } |
| 40 | + // }; |
51 | 41 |
|
52 | | - const labelSizes = { |
53 | | - filled: { |
54 | | - small: 'top-3', |
55 | | - default: 'top-4' |
56 | | - }, |
57 | | - outlined: { |
58 | | - small: 'top-1', |
59 | | - default: 'top-2' |
60 | | - }, |
61 | | - standard: { |
62 | | - small: 'top-3', |
63 | | - default: 'top-3' |
64 | | - } |
65 | | - }; |
| 42 | + // const labelSizes = { |
| 43 | + // filled: { |
| 44 | + // small: 'top-3', |
| 45 | + // default: 'top-4' |
| 46 | + // }, |
| 47 | + // outlined: { |
| 48 | + // small: 'top-1', |
| 49 | + // default: 'top-2' |
| 50 | + // }, |
| 51 | + // standard: { |
| 52 | + // small: 'top-3', |
| 53 | + // default: 'top-3' |
| 54 | + // } |
| 55 | + // }; |
66 | 56 |
|
67 | | - const inputClasses = { |
68 | | - filled: |
69 | | - 'block rounded-t-lg w-full text-sm text-gray-900 bg-gray-50 dark:bg-gray-700 border-0 border-b-2 appearance-none dark:text-white focus:outline-none focus:ring-0 peer', |
70 | | - outlined: |
71 | | - 'block w-full text-sm text-gray-900 bg-transparent rounded-lg border appearance-none dark:text-white focus:outline-none focus:ring-0 peer', |
72 | | - standard: |
73 | | - 'block w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 appearance-none dark:text-white focus:outline-none focus:ring-0 peer' |
74 | | - }; |
| 57 | + // const inputClasses = { |
| 58 | + // filled: |
| 59 | + // 'block rounded-t-lg w-full text-sm text-gray-900 bg-gray-50 dark:bg-gray-700 border-0 border-b-2 appearance-none dark:text-white focus:outline-none focus:ring-0 peer', |
| 60 | + // outlined: |
| 61 | + // 'block w-full text-sm text-gray-900 bg-transparent rounded-lg border appearance-none dark:text-white focus:outline-none focus:ring-0 peer', |
| 62 | + // standard: |
| 63 | + // 'block w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 appearance-none dark:text-white focus:outline-none focus:ring-0 peer' |
| 64 | + // }; |
75 | 65 |
|
76 | | - const labelClasses = { |
77 | | - filled: |
78 | | - 'absolute text-sm duration-300 transform -translate-y-4 scale-75 top-4 z-10 origin-left rtl:origin-right start-2.5 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-4', |
79 | | - outlined: |
80 | | - 'absolute text-sm duration-300 transform -translate-y-4 scale-75 top-2 z-10 origin-left rtl:origin-right bg-white dark:bg-gray-900 px-2 peer-focus:px-2 peer-placeholder-shown:scale-100 peer-placeholder-shown:-translate-y-1/2 peer-placeholder-shown:top-1/2 peer-focus:top-2 peer-focus:scale-75 peer-focus:-translate-y-4 start-1', |
81 | | - standard: |
82 | | - 'absolute text-sm duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-left rtl:origin-right peer-focus:start-0 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6' |
83 | | - }; |
| 66 | + // const labelClasses = { |
| 67 | + // filled: |
| 68 | + // 'absolute text-sm duration-300 transform -translate-y-4 scale-75 top-4 z-10 origin-left rtl:origin-right start-2.5 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-4', |
| 69 | + // outlined: |
| 70 | + // 'absolute text-sm duration-300 transform -translate-y-4 scale-75 top-2 z-10 origin-left rtl:origin-right bg-white dark:bg-gray-900 px-2 peer-focus:px-2 peer-placeholder-shown:scale-100 peer-placeholder-shown:-translate-y-1/2 peer-placeholder-shown:top-1/2 peer-focus:top-2 peer-focus:scale-75 peer-focus:-translate-y-4 start-1', |
| 71 | + // standard: |
| 72 | + // 'absolute text-sm duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-left rtl:origin-right peer-focus:start-0 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6' |
| 73 | + // }; |
84 | 74 |
|
85 | | - const inputColorClasses = { |
86 | | - base: 'border-gray-300 dark:border-gray-600 dark:focus:border-primary-500 focus:border-primary-600', |
87 | | - green: |
88 | | - 'border-green-600 dark:border-green-500 dark:focus:border-green-500 focus:border-green-600', |
89 | | - red: 'border-red-600 dark:border-red-500 dark:focus:border-red-500 focus:border-red-600' |
90 | | - }; |
| 75 | + // const inputColorClasses = { |
| 76 | + // base: 'border-gray-300 dark:border-gray-600 dark:focus:border-primary-500 focus:border-primary-600', |
| 77 | + // green: |
| 78 | + // 'border-green-600 dark:border-green-500 dark:focus:border-green-500 focus:border-green-600', |
| 79 | + // red: 'border-red-600 dark:border-red-500 dark:focus:border-red-500 focus:border-red-600' |
| 80 | + // }; |
91 | 81 |
|
92 | | - const labelColorClasses = { |
93 | | - base: 'text-gray-500 dark:text-gray-400 peer-focus:text-primary-600 peer-focus:dark:text-primary-500', |
94 | | - green: 'text-green-600 dark:text-green-500', |
95 | | - red: 'text-red-600 dark:text-red-500' |
96 | | - }; |
| 82 | + // const labelColorClasses = { |
| 83 | + // base: 'text-gray-500 dark:text-gray-400 peer-focus:text-primary-600 peer-focus:dark:text-primary-500', |
| 84 | + // green: 'text-green-600 dark:text-green-500', |
| 85 | + // red: 'text-red-600 dark:text-red-500' |
| 86 | + // }; |
97 | 87 | </script> |
98 | 88 |
|
99 | | -<div class={twMerge(divClasses[style], divClass)}> |
| 89 | +<div class={base({ class: divClass})}> |
100 | 90 | <input |
101 | 91 | {id} |
| 92 | + placeholder=" " |
102 | 93 | {...attributes} |
103 | 94 | aria-describedby={aria_describedby} |
104 | | - class={twMerge( |
105 | | - inputClasses[style], |
106 | | - inputColorClasses[color], |
107 | | - inputSizes[style][inputSize], |
108 | | - inputClass |
109 | | - )} |
| 95 | + class={input({ class: inputClass })} |
110 | 96 | /> |
111 | 97 |
|
112 | 98 | <label |
113 | 99 | for={id} |
114 | | - class={twMerge( |
115 | | - labelClasses[style], |
116 | | - labelColorClasses[color], |
117 | | - labelSizes[style][inputSize], |
118 | | - labelClass |
119 | | - )} |
| 100 | + class={label({ class: labelClass})} |
120 | 101 | > |
121 | 102 | {@render children()} |
122 | 103 | </label> |
|
0 commit comments