svelte-multistyle-ui 0.0.13-alpha → 0.0.14-alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -55,15 +55,14 @@
55
55
  const uid = Math.random().toString(36).slice(2, 9);
56
56
  const internalId = $derived(id || `input-${uid}`);
57
57
 
58
- const floatingStyles = [
59
- "material",
60
- "material3",
61
- "fluent",
62
- "carbon",
63
- "bootstrap",
64
- ];
65
- const hasFloatingLabel = $derived(
66
- floatingStyles.includes(style) && (label || placeholder),
58
+ // Floating label (animates inside the field) — Material / Material 3 only.
59
+ // Every other style renders a static label above the field.
60
+ const floatingLabelStyles = ["material", "material3"];
61
+ const useFloatingLabel = $derived(
62
+ floatingLabelStyles.includes(style) && (label || placeholder),
63
+ );
64
+ const useAboveLabel = $derived(
65
+ !floatingLabelStyles.includes(style) && (label || placeholder),
67
66
  );
68
67
  const displayLabel = $derived(label || placeholder);
69
68
  </script>
@@ -76,48 +75,53 @@
76
75
  class:floated
77
76
  class:has-icon-start={hasIconStart}
78
77
  class:has-icon-end={hasIconEnd}
78
+ class:above-label={useAboveLabel}
79
79
  >
80
- {#if iconStart}
81
- <span class="s-input-icon s-input-icon-start {iconClass}">{iconStart}</span>
80
+ {#if useAboveLabel}
81
+ <label class="s-input-label-above" for={internalId}>{displayLabel}</label>
82
82
  {/if}
83
- {#if hasFloatingLabel}
83
+ {#if useFloatingLabel}
84
84
  <label class="s-input-floating-label" for={internalId}>{displayLabel}</label
85
85
  >
86
86
  {/if}
87
- <input
88
- {type}
89
- bind:value
90
- placeholder={hasFloatingLabel ? "" : placeholder}
91
- {disabled}
92
- {readonly}
93
- {required}
94
- {name}
95
- id={internalId}
96
- {min}
97
- {max}
98
- {minlength}
99
- {maxlength}
100
- {step}
101
- {pattern}
102
- {autocomplete}
103
- {autofocus}
104
- {multiple}
105
- {accept}
106
- {list}
107
- {size}
108
- {tabindex}
109
- {title}
110
- {form}
111
- {inputmode}
112
- {spellcheck}
113
- onfocus={() => (focused = true)}
114
- onblur={() => (focused = false)}
115
- {...rest}
116
- />
117
- {#if iconEnd}
118
- <span class="s-input-icon s-input-icon-end {iconClass}">{iconEnd}</span>
119
- {/if}
120
- {#if style === "fluent"}
121
- <span class="s-input-fluent-border-indicator"></span>
122
- {/if}
87
+ <div class="s-input-field">
88
+ {#if iconStart}
89
+ <span class="s-input-icon s-input-icon-start {iconClass}">{iconStart}</span>
90
+ {/if}
91
+ <input
92
+ {type}
93
+ bind:value
94
+ placeholder={useFloatingLabel || (useAboveLabel && !label)
95
+ ? ""
96
+ : placeholder}
97
+ {disabled}
98
+ {readonly}
99
+ {required}
100
+ {name}
101
+ id={internalId}
102
+ {min}
103
+ {max}
104
+ {minlength}
105
+ {maxlength}
106
+ {step}
107
+ {pattern}
108
+ {autocomplete}
109
+ {autofocus}
110
+ {multiple}
111
+ {accept}
112
+ {list}
113
+ {size}
114
+ {tabindex}
115
+ {title}
116
+ {form}
117
+ {inputmode}
118
+ {spellcheck}
119
+ onfocus={() => (focused = true)}
120
+ onblur={() => (focused = false)}
121
+ {...rest}
122
+ />
123
+ {#if iconEnd}
124
+ <span class="s-input-icon s-input-icon-end {iconClass}">{iconEnd}</span>
125
+ {/if}
126
+ </div>
123
127
  </div>
@@ -29,17 +29,19 @@
29
29
  const styleClass = $derived(`s-mselect-${style}`);
30
30
  const themeClass = $derived(`theme-${theme}`);
31
31
 
32
- const floatingStyles = [
33
- "material",
34
- "material3",
35
- "fluent",
36
- "carbon",
37
- "bootstrap",
38
- ];
39
- const hasFloatingLabel = $derived(
40
- floatingStyles.includes(style) && (label || placeholder),
32
+ // Floating label (animates inside the field) — Material / Material 3 only.
33
+ // Every other style renders a static label above the field.
34
+ const floatingLabelStyles = ["material", "material3"];
35
+ const useFloatingLabel = $derived(
36
+ floatingLabelStyles.includes(style) && (label || placeholder),
37
+ );
38
+ const useAboveLabel = $derived(
39
+ !floatingLabelStyles.includes(style) && (label || placeholder),
41
40
  );
42
41
  const displayLabel = $derived(label || placeholder);
42
+ const showInFieldPlaceholder = $derived(
43
+ !(useFloatingLabel || (useAboveLabel && !label)),
44
+ );
43
45
 
44
46
  function toggle(val) {
45
47
  if (selected.includes(val)) {
@@ -72,9 +74,16 @@
72
74
  }
73
75
 
74
76
  function handleBlur(e) {
77
+ const related = e.relatedTarget;
78
+ // Clicking a non-focusable element (e.g. the option label text) leaves
79
+ // relatedTarget null. Don't close here — the document mousedown handler
80
+ // below handles outside clicks. Closing now would unmount the checkbox
81
+ // before the label-forwarded click toggles it, so the option wouldn't
82
+ // select when its text is clicked.
83
+ if (related === null) return;
75
84
  if (
76
- wrapperEl && !wrapperEl.contains(e.relatedTarget) &&
77
- !(dropdownEl && dropdownEl.contains(e.relatedTarget))
85
+ wrapperEl && !wrapperEl.contains(related) &&
86
+ !(dropdownEl && dropdownEl.contains(related))
78
87
  ) {
79
88
  open = false;
80
89
  focused = false;
@@ -84,12 +93,23 @@
84
93
  $effect(() => {
85
94
  if (!open) return;
86
95
  const reposition = () => positionDropdown();
96
+ const onDocMouseDown = (e) => {
97
+ if (
98
+ wrapperEl && !wrapperEl.contains(e.target) &&
99
+ !(dropdownEl && dropdownEl.contains(e.target))
100
+ ) {
101
+ open = false;
102
+ focused = false;
103
+ }
104
+ };
87
105
  window.addEventListener("scroll", reposition, true);
88
106
  window.addEventListener("resize", reposition);
107
+ document.addEventListener("mousedown", onDocMouseDown);
89
108
  queueMicrotask(reposition);
90
109
  return () => {
91
110
  window.removeEventListener("scroll", reposition, true);
92
111
  window.removeEventListener("resize", reposition);
112
+ document.removeEventListener("mousedown", onDocMouseDown);
93
113
  };
94
114
  });
95
115
  </script>
@@ -102,6 +122,7 @@
102
122
  class:open
103
123
  class:has-value={hasValue}
104
124
  class:floated
125
+ class:above-label={useAboveLabel}
105
126
  tabindex="-1"
106
127
  role="listbox"
107
128
  aria-label={displayLabel}
@@ -109,7 +130,10 @@
109
130
  onfocusout={handleBlur}
110
131
  {...rest}
111
132
  >
112
- {#if hasFloatingLabel}
133
+ {#if useAboveLabel}
134
+ <label class="s-mselect-label-above">{displayLabel}</label>
135
+ {/if}
136
+ {#if useFloatingLabel}
113
137
  <label class="s-mselect-floating-label">{displayLabel}</label>
114
138
  {/if}
115
139
  <div
@@ -141,7 +165,7 @@
141
165
  </span>
142
166
  {/each}
143
167
  </div>
144
- {:else if !hasFloatingLabel}
168
+ {:else if showInFieldPlaceholder}
145
169
  <span class="mselect-placeholder">{placeholder}</span>
146
170
  {/if}
147
171
  <span class="mselect-arrow">
@@ -1,178 +1,183 @@
1
- <script>
2
- import "./select-styles.css";
3
- import { portal } from "../actions/portal.js";
4
- import { defaults } from "../config.js";
5
- import { cn } from "../utils/cn.js";
6
-
7
- let {
8
- style: styleProp,
9
- theme: themeProp,
10
- value = $bindable(""),
11
- options = [],
12
- placeholder = "Select...",
13
- label = "",
14
- disabled = false,
15
- class: className = "",
16
- ...rest
17
- } = $props();
18
-
19
- const style = $derived(styleProp ?? defaults.style);
20
- const theme = $derived(themeProp ?? defaults.theme);
21
-
22
- let open = $state(false);
23
- let focused = $state(false);
24
- let wrapperEl;
25
- let dropdownEl;
26
- let hasValue = $derived(
27
- value !== "" && value !== null && value !== undefined,
28
- );
29
- let floated = $derived(focused || hasValue);
30
-
31
- const styleClass = $derived(`s-select-${style}`);
32
- const themeClass = $derived(`theme-${theme}`);
33
-
34
- const floatingStyles = [
35
- "material",
36
- "material3",
37
- "fluent",
38
- "carbon",
39
- "bootstrap",
40
- ];
41
- const hasFloatingLabel = $derived(
42
- floatingStyles.includes(style) && (label || placeholder),
43
- );
44
- const displayLabel = $derived(label || placeholder);
45
-
46
- function getLabel(val) {
47
- const opt = options.find((o) => o.value === val);
48
- return opt ? opt.label : val;
49
- }
50
-
51
- function selectOption(val) {
52
- value = val;
53
- open = false;
54
- focused = false;
55
- }
56
-
57
- function handleBlur() {
58
- setTimeout(() => {
59
- open = false;
60
- focused = false;
61
- }, 150);
62
- }
63
-
64
- function handleKeydown(e) {
65
- if (e.key === "Enter" || e.key === " ") {
66
- e.preventDefault();
67
- open = !open;
68
- focused = true;
69
- } else if (e.key === "Escape") {
70
- open = false;
71
- focused = false;
72
- }
73
- }
74
-
75
- function positionDropdown() {
76
- if (!wrapperEl || !dropdownEl) return;
77
- const rect = wrapperEl.getBoundingClientRect();
78
- const ddHeight = dropdownEl.offsetHeight || 200;
79
- let top = rect.bottom + 4;
80
- // Flip above the field if it would overflow the bottom of the viewport.
81
- if (top + ddHeight > window.innerHeight - 8 && rect.top - ddHeight - 4 > 8) {
82
- top = rect.top - ddHeight - 4;
83
- }
84
- dropdownEl.style.left = `${rect.left}px`;
85
- dropdownEl.style.top = `${top}px`;
86
- dropdownEl.style.width = `${rect.width}px`;
87
- }
88
-
89
- $effect(() => {
90
- if (open) {
91
- const handler = (e) => {
92
- if (
93
- wrapperEl && !wrapperEl.contains(e.target) &&
94
- !(dropdownEl && dropdownEl.contains(e.target))
95
- ) {
96
- open = false;
97
- focused = false;
98
- }
99
- };
100
- document.addEventListener("pointerdown", handler);
101
- const reposition = () => positionDropdown();
102
- window.addEventListener("scroll", reposition, true);
103
- window.addEventListener("resize", reposition);
104
- queueMicrotask(reposition);
105
- return () => {
106
- document.removeEventListener("pointerdown", handler);
107
- window.removeEventListener("scroll", reposition, true);
108
- window.removeEventListener("resize", reposition);
109
- };
110
- }
111
- });
112
- </script>
113
-
114
- <div
115
- bind:this={wrapperEl}
116
- class={cn("s-select-wrapper", styleClass, themeClass, className)}
117
- class:disabled
118
- class:focused
119
- class:has-value={hasValue}
120
- class:floated
121
- class:open
122
- tabindex="-1"
123
- role="listbox"
124
- aria-label={displayLabel}
125
- onfocusin={() => (focused = true)}
126
- onfocusout={handleBlur}
127
- {...rest}
128
- >
129
- {#if hasFloatingLabel}
130
- <label class="s-select-floating-label"
131
- >{displayLabel}</label
132
- >
133
- {/if}
134
- <div
135
- class="s-select-control"
136
- role="button"
137
- tabindex="0"
138
- onclick={() => (open = !open)}
139
- onkeydown={handleKeydown}
140
- >
141
- {#if hasValue && !hasFloatingLabel}
142
- <span class="s-select-value">{getLabel(value)}</span>
143
- {:else if !hasFloatingLabel}
144
- <span class="s-select-placeholder">{placeholder}</span>
145
- {/if}
146
- {#if hasFloatingLabel && hasValue}
147
- <span class="s-select-value">{getLabel(value)}</span>
148
- {/if}
149
- <span class="s-select-arrow">
150
- <svg width="12" height="12" viewBox="0 0 12 12" fill="none">
151
- <path
152
- d="M2.5 4.5L6 8L9.5 4.5"
153
- stroke="currentColor"
154
- stroke-width="1.5"
155
- stroke-linecap="round"
156
- stroke-linejoin="round"
157
- />
158
- </svg>
159
- </span>
160
- </div>
161
- {#if open}
162
- <div
163
- class="s-select-dropdown {styleClass} {themeClass}"
164
- bind:this={dropdownEl}
165
- use:portal
166
- >
167
- {#each options as opt}
168
- <div
169
- class="s-select-option"
170
- class:selected={value === opt.value}
171
- onclick={() => selectOption(opt.value)}
172
- >
173
- {opt.label}
174
- </div>
175
- {/each}
176
- </div>
177
- {/if}
1
+ <script>
2
+ import "./select-styles.css";
3
+ import { portal } from "../actions/portal.js";
4
+ import { defaults } from "../config.js";
5
+ import { cn } from "../utils/cn.js";
6
+
7
+ let {
8
+ style: styleProp,
9
+ theme: themeProp,
10
+ value = $bindable(""),
11
+ options = [],
12
+ placeholder = "Select...",
13
+ label = "",
14
+ disabled = false,
15
+ class: className = "",
16
+ ...rest
17
+ } = $props();
18
+
19
+ const style = $derived(styleProp ?? defaults.style);
20
+ const theme = $derived(themeProp ?? defaults.theme);
21
+
22
+ let open = $state(false);
23
+ let focused = $state(false);
24
+ let wrapperEl;
25
+ let dropdownEl;
26
+ let hasValue = $derived(
27
+ value !== "" && value !== null && value !== undefined,
28
+ );
29
+ let floated = $derived(focused || hasValue);
30
+
31
+ const styleClass = $derived(`s-select-${style}`);
32
+ const themeClass = $derived(`theme-${theme}`);
33
+
34
+ // Floating label (animates inside the field) — Material / Material 3 only.
35
+ // Every other style renders a static label above the field.
36
+ const floatingLabelStyles = ["material", "material3"];
37
+ const useFloatingLabel = $derived(
38
+ floatingLabelStyles.includes(style) && (label || placeholder),
39
+ );
40
+ const useAboveLabel = $derived(
41
+ !floatingLabelStyles.includes(style) && (label || placeholder),
42
+ );
43
+ const displayLabel = $derived(label || placeholder);
44
+ // Show the in-field placeholder hint only when it isn't lifted to the
45
+ // above-label (label set) and there's no floating label acting as placeholder.
46
+ const showInFieldPlaceholder = $derived(
47
+ !(useFloatingLabel || (useAboveLabel && !label)),
48
+ );
49
+
50
+ function getLabel(val) {
51
+ const opt = options.find((o) => o.value === val);
52
+ return opt ? opt.label : val;
53
+ }
54
+
55
+ function selectOption(val) {
56
+ value = val;
57
+ open = false;
58
+ focused = false;
59
+ }
60
+
61
+ function handleBlur() {
62
+ setTimeout(() => {
63
+ open = false;
64
+ focused = false;
65
+ }, 150);
66
+ }
67
+
68
+ function handleKeydown(e) {
69
+ if (e.key === "Enter" || e.key === " ") {
70
+ e.preventDefault();
71
+ open = !open;
72
+ focused = true;
73
+ } else if (e.key === "Escape") {
74
+ open = false;
75
+ focused = false;
76
+ }
77
+ }
78
+
79
+ function positionDropdown() {
80
+ if (!wrapperEl || !dropdownEl) return;
81
+ const rect = wrapperEl.getBoundingClientRect();
82
+ const ddHeight = dropdownEl.offsetHeight || 200;
83
+ let top = rect.bottom + 4;
84
+ // Flip above the field if it would overflow the bottom of the viewport.
85
+ if (top + ddHeight > window.innerHeight - 8 && rect.top - ddHeight - 4 > 8) {
86
+ top = rect.top - ddHeight - 4;
87
+ }
88
+ dropdownEl.style.left = `${rect.left}px`;
89
+ dropdownEl.style.top = `${top}px`;
90
+ dropdownEl.style.width = `${rect.width}px`;
91
+ }
92
+
93
+ $effect(() => {
94
+ if (open) {
95
+ const handler = (e) => {
96
+ if (
97
+ wrapperEl && !wrapperEl.contains(e.target) &&
98
+ !(dropdownEl && dropdownEl.contains(e.target))
99
+ ) {
100
+ open = false;
101
+ focused = false;
102
+ }
103
+ };
104
+ document.addEventListener("pointerdown", handler);
105
+ const reposition = () => positionDropdown();
106
+ window.addEventListener("scroll", reposition, true);
107
+ window.addEventListener("resize", reposition);
108
+ queueMicrotask(reposition);
109
+ return () => {
110
+ document.removeEventListener("pointerdown", handler);
111
+ window.removeEventListener("scroll", reposition, true);
112
+ window.removeEventListener("resize", reposition);
113
+ };
114
+ }
115
+ });
116
+ </script>
117
+
118
+ <div
119
+ bind:this={wrapperEl}
120
+ class={cn("s-select-wrapper", styleClass, themeClass, className)}
121
+ class:disabled
122
+ class:focused
123
+ class:has-value={hasValue}
124
+ class:floated
125
+ class:open
126
+ class:above-label={useAboveLabel}
127
+ tabindex="-1"
128
+ role="listbox"
129
+ aria-label={displayLabel}
130
+ onfocusin={() => (focused = true)}
131
+ onfocusout={handleBlur}
132
+ {...rest}
133
+ >
134
+ {#if useAboveLabel}
135
+ <label class="s-select-label-above">{displayLabel}</label>
136
+ {/if}
137
+ {#if useFloatingLabel}
138
+ <label class="s-select-floating-label"
139
+ >{displayLabel}</label
140
+ >
141
+ {/if}
142
+ <div
143
+ class="s-select-control"
144
+ role="button"
145
+ tabindex="0"
146
+ onclick={() => (open = !open)}
147
+ onkeydown={handleKeydown}
148
+ >
149
+ {#if hasValue}
150
+ <span class="s-select-value">{getLabel(value)}</span>
151
+ {:else if showInFieldPlaceholder}
152
+ <span class="s-select-placeholder">{placeholder}</span>
153
+ {/if}
154
+ <span class="s-select-arrow">
155
+ <svg width="12" height="12" viewBox="0 0 12 12" fill="none">
156
+ <path
157
+ d="M2.5 4.5L6 8L9.5 4.5"
158
+ stroke="currentColor"
159
+ stroke-width="1.5"
160
+ stroke-linecap="round"
161
+ stroke-linejoin="round"
162
+ />
163
+ </svg>
164
+ </span>
165
+ </div>
166
+ {#if open}
167
+ <div
168
+ class="s-select-dropdown {styleClass} {themeClass}"
169
+ bind:this={dropdownEl}
170
+ use:portal
171
+ >
172
+ {#each options as opt}
173
+ <div
174
+ class="s-select-option"
175
+ class:selected={value === opt.value}
176
+ onclick={() => selectOption(opt.value)}
177
+ >
178
+ {opt.label}
179
+ </div>
180
+ {/each}
181
+ </div>
182
+ {/if}
178
183
  </div>
@@ -34,15 +34,14 @@
34
34
  const uid = Math.random().toString(36).slice(2, 9);
35
35
  const internalId = `textarea-${uid}`;
36
36
 
37
- const floatingStyles = [
38
- "material",
39
- "material3",
40
- "fluent",
41
- "carbon",
42
- "bootstrap",
43
- ];
44
- const hasFloatingLabel = $derived(
45
- floatingStyles.includes(style) && (label || placeholder),
37
+ // Floating label (animates inside the field) — Material / Material 3 only.
38
+ // Every other style renders a static label above the field.
39
+ const floatingLabelStyles = ["material", "material3"];
40
+ const useFloatingLabel = $derived(
41
+ floatingLabelStyles.includes(style) && (label || placeholder),
42
+ );
43
+ const useAboveLabel = $derived(
44
+ !floatingLabelStyles.includes(style) && (label || placeholder),
46
45
  );
47
46
  const displayLabel = $derived(label || placeholder);
48
47
 
@@ -61,15 +60,21 @@
61
60
  class:focused
62
61
  class:has-value={hasValue}
63
62
  class:floated
63
+ class:above-label={useAboveLabel}
64
64
  >
65
- {#if hasFloatingLabel}
65
+ {#if useAboveLabel}
66
+ <label class="s-textarea-label-above" for={internalId}>{displayLabel}</label>
67
+ {/if}
68
+ {#if useFloatingLabel}
66
69
  <label class="s-textarea-floating-label" for={internalId}
67
70
  >{displayLabel}</label
68
71
  >
69
72
  {/if}
70
73
  <textarea
71
74
  bind:value
72
- placeholder={hasFloatingLabel ? "" : placeholder}
75
+ placeholder={useFloatingLabel || (useAboveLabel && !label)
76
+ ? ""
77
+ : placeholder}
73
78
  {disabled}
74
79
  {readonly}
75
80
  {required}