@placeholderco/placeholder-ui 3.0.4 → 3.0.5

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.
@@ -26,7 +26,7 @@
26
26
  onchange
27
27
  }: SegmentedControlProps = $props();
28
28
 
29
- let containerRef: HTMLDivElement;
29
+ let containerRef = $state<HTMLDivElement>();
30
30
  let buttonElements = $state<HTMLButtonElement[]>([]);
31
31
  let backgroundStyle = $state('');
32
32
 
@@ -45,17 +45,25 @@
45
45
  }
46
46
 
47
47
  onMount(() => {
48
- // recalculate 10 times on startup to ensure background is positioned correctly
49
- for (let i = 0; i < 10; i++) {
50
- scheduleRecalculate(100 * i);
51
- }
52
-
53
48
  return () => {
54
49
  for (const id of pendingTimers) clearTimeout(id);
55
50
  pendingTimers.clear();
56
51
  };
57
52
  });
58
53
 
54
+ // Re-measure whenever the control or a segment changes size. This also covers
55
+ // mounting while hidden (e.g. inside a closed Dialog): the observer fires once
56
+ // the control is laid out, and again for late font loads or label changes
57
+ $effect(() => {
58
+ if (!containerRef) return;
59
+ const observer = new ResizeObserver(() => recalculateBackground());
60
+ observer.observe(containerRef);
61
+ for (const button of buttonElements) {
62
+ if (button) observer.observe(button);
63
+ }
64
+ return () => observer.disconnect();
65
+ });
66
+
59
67
  // Update background position and size based on selected element
60
68
  $effect(() => {
61
69
  if (selectedIndex >= 0 && buttonElements[selectedIndex] && containerRef) {
@@ -86,6 +94,10 @@
86
94
  const containerRect = containerRef.getBoundingClientRect();
87
95
  const buttonRect = selectedButton.getBoundingClientRect();
88
96
 
97
+ // Not rendered (display: none, e.g. a closed Dialog). Keep the last good
98
+ // position so reopening doesn't animate the background in from zero size
99
+ if (buttonRect.width === 0 && buttonRect.height === 0) return;
100
+
89
101
  // Absolute positioning is relative to the padding box, but getBoundingClientRect
90
102
  // measures from the border's outer edge — subtract the border (clientLeft/clientTop)
91
103
  const offsetLeft = buttonRect.left - containerRect.left - containerRef.clientLeft;
@@ -1,9 +1,18 @@
1
1
  <script module lang="ts">
2
2
  import type { Snippet } from 'svelte';
3
3
 
4
+ export type SwitchLabelPosition = 'right' | 'left' | 'top';
5
+
4
6
  export interface SwitchProps {
5
- /** Label text displayed above the switch */
7
+ /** Label text displayed next to the switch */
6
8
  label?: string;
9
+ /**
10
+ * Where the label sits relative to the switch.
11
+ * - `right` (default): switch then label, inline like a checkbox
12
+ * - `left`: label then switch, with the switch pushed to the far end (settings rows)
13
+ * - `top`: form-field style label above the switch, matching Textbox/Select
14
+ */
15
+ labelPosition?: SwitchLabelPosition;
7
16
  /** Mark field as required (shows asterisk) */
8
17
  required?: boolean;
9
18
  /** Whether the switch is on (bindable) */
@@ -22,6 +31,8 @@
22
31
  showError?: boolean;
23
32
  /** Error message to display */
24
33
  errorText?: string;
34
+ /** Accessible label for screen readers (use when no visible label) */
35
+ ariaLabel?: string;
25
36
  /** Simple tooltip text */
26
37
  tooltipText?: string;
27
38
  /** Position of the tooltip */
@@ -37,6 +48,7 @@
37
48
 
38
49
  let {
39
50
  label,
51
+ labelPosition = 'right',
40
52
  required = false,
41
53
  checked = $bindable(),
42
54
  disabled = false,
@@ -46,6 +58,7 @@
46
58
  onchange,
47
59
  showError = false,
48
60
  errorText = '',
61
+ ariaLabel = undefined,
49
62
  tooltipText = undefined,
50
63
  tooltipLocation = 'top',
51
64
  tooltipContent = undefined
@@ -53,7 +66,15 @@
53
66
 
54
67
  const nonce = Math.random().toString(36).substring(2, 15);
55
68
 
56
- const id = $derived(inputId ? inputId : label ? `switch-${label}` : `switch-${nonce}`);
69
+ const id = $derived(
70
+ inputId
71
+ ? inputId
72
+ : label
73
+ ? `switch-${label.toLowerCase().replace(/[^a-z0-9_-]+/g, '-')}`
74
+ : `switch-${nonce}`
75
+ );
76
+
77
+ const hasLabelText = $derived(!!label || required || !!tooltipContent || !!tooltipText);
57
78
 
58
79
  function handleClick() {
59
80
  if (disabled) return;
@@ -62,30 +83,35 @@
62
83
  }
63
84
  </script>
64
85
 
65
- <div class="switch-container {containerClass} {disabled ? 'disabled' : ''}">
86
+ <div class="switch-container {labelPosition} {containerClass}" class:disabled>
66
87
  <label for={id}>
67
- <span class="label-text"
68
- >{label}
69
- {#if required}
70
- <span class="required">*</span>
71
- {/if}
72
- {#if tooltipContent || tooltipText}
73
- <Tooltip {tooltipContent} location={tooltipLocation} text={tooltipText}>
74
- <Icon svg={iconInfoCircle} size="1em" stroke="var(--accent-color)" />
75
- </Tooltip>
76
- {/if}</span
77
- >
78
88
  <button
79
89
  {id}
80
90
  role="switch"
81
91
  type="button"
82
92
  aria-checked={checked}
83
- aria-label={label || 'Toggle switch'}
93
+ aria-label={label ? undefined : (ariaLabel ?? 'Toggle switch')}
94
+ aria-invalid={showError || undefined}
84
95
  onclick={handleClick}
85
- class="switch {switchClass} {showError ? 'error' : ''} {disabled ? 'disabled' : ''}"
96
+ class="switch {switchClass}"
97
+ class:error={showError}
86
98
  {disabled}
87
99
  >
100
+ <span class="thumb"></span>
88
101
  </button>
102
+ {#if hasLabelText}
103
+ <span class="label-text"
104
+ >{label}
105
+ {#if required}
106
+ <span class="required">*</span>
107
+ {/if}
108
+ {#if tooltipContent || tooltipText}
109
+ <Tooltip {tooltipContent} location={tooltipLocation} text={tooltipText}>
110
+ <Icon svg={iconInfoCircle} size="1em" stroke="var(--accent-color)" />
111
+ </Tooltip>
112
+ {/if}</span
113
+ >
114
+ {/if}
89
115
  </label>
90
116
  {#if showError && errorText}
91
117
  <div class="error-text">{errorText}</div>
@@ -93,72 +119,124 @@
93
119
  </div>
94
120
 
95
121
  <style>
96
- :root .switch-container {
97
- --accent-color: var(--ui-primary);
98
- --gray: #ccc;
122
+ .switch-container {
123
+ --switch-track-off: #c4c9d0;
124
+ --switch-track-off-hover: #adb3bc;
125
+ --switch-track-on: var(--ui-primary);
126
+ --switch-thumb: #fff;
127
+ --switch-thumb-on: var(--ui-primary-contrast-color);
99
128
  }
100
129
 
101
130
  :global(.dark) .switch-container {
102
- --accent-color: var(--ui-primary);
103
- --gray: #444;
131
+ --switch-track-off: #4a4d55;
132
+ --switch-track-off-hover: #5c606a;
133
+ --switch-track-on: var(--ui-accent);
134
+ --switch-thumb: #d4d6da;
135
+ --switch-thumb-on: var(--ui-accent-contrast-color);
104
136
  }
105
137
 
106
138
  label {
107
- display: flex;
108
- flex-direction: column;
109
- align-items: flex-start;
139
+ display: inline-flex;
140
+ align-items: center;
141
+ gap: 0.625em;
110
142
  user-select: none;
111
143
  cursor: pointer;
144
+ color: var(--text-color);
145
+ line-height: 1.25;
112
146
  }
113
147
 
114
- .disabled label {
115
- cursor: not-allowed;
148
+ .label-text {
149
+ display: inline-flex;
150
+ align-items: center;
151
+ gap: 0.25em;
152
+ }
153
+
154
+ /* Label first, switch pushed to the far end */
155
+ .left label {
156
+ display: flex;
157
+ flex-direction: row-reverse;
158
+ justify-content: space-between;
159
+ }
160
+
161
+ /* Form-field style: matches FormGroup labels above Textbox, Select, etc. */
162
+ .top label {
163
+ flex-direction: column-reverse;
164
+ align-items: flex-start;
165
+ gap: 0.25rem;
166
+ }
167
+
168
+ .top .label-text {
169
+ font-size: 0.875rem;
170
+ line-height: 1.25rem;
171
+ font-weight: 500;
172
+ color: var(--label-color, #6c757d);
116
173
  }
117
174
 
118
175
  .switch {
119
- width: 3em;
120
- height: 1.6em;
176
+ flex-shrink: 0;
177
+ box-sizing: border-box;
121
178
  position: relative;
122
- margin: 4px 0 0 0;
123
- background: var(--gray);
179
+ width: 2.25em;
180
+ height: 1.25em;
181
+ margin: 0;
182
+ padding: 0;
183
+ font-size: inherit;
184
+ background-color: var(--switch-track-off);
124
185
  border: none;
125
- border-radius: 1.5em;
186
+ border-radius: 999px;
126
187
  cursor: pointer;
188
+ transition:
189
+ background-color 0.2s ease,
190
+ box-shadow 0.2s ease;
127
191
  }
128
192
 
129
- .switch::before {
130
- content: '';
193
+ .thumb {
131
194
  position: absolute;
132
- width: 1.3em;
133
- height: 1.3em;
134
- background: #fff;
135
- top: 0.13em;
136
- right: 1.5em;
137
- border-radius: 100%;
138
- transition: transform 0.3s;
195
+ top: 0.125em;
196
+ left: 0.125em;
197
+ width: 1em;
198
+ height: 1em;
199
+ border-radius: 50%;
200
+ background-color: var(--switch-thumb);
201
+ box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
202
+ transition:
203
+ transform 0.2s ease,
204
+ background-color 0.2s ease;
139
205
  }
140
206
 
141
207
  .switch[aria-checked='true'] {
142
- background-color: var(--ui-primary);
208
+ background-color: var(--switch-track-on);
209
+ }
210
+
211
+ .switch[aria-checked='true'] .thumb {
212
+ transform: translateX(1em);
213
+ background-color: var(--switch-thumb-on);
143
214
  }
144
215
 
145
- .switch[aria-checked='true']::before {
146
- transform: translateX(1.3em);
147
- transition: transform 0.3s;
216
+ label:hover .switch:not(:disabled)[aria-checked='false'] {
217
+ background-color: var(--switch-track-off-hover);
148
218
  }
149
219
 
150
220
  .switch:focus {
151
- outline: 1px solid var(--ui-primary);
221
+ outline: none;
222
+ }
223
+
224
+ .switch:focus-visible {
225
+ outline: 2px solid var(--ui-focus-color);
152
226
  outline-offset: 2px;
153
227
  }
154
228
 
155
- .switch.disabled {
156
- cursor: not-allowed;
229
+ .switch.error {
230
+ box-shadow: inset 0 0 0 2px var(--danger-text);
231
+ }
232
+
233
+ .disabled {
157
234
  opacity: 0.5;
158
235
  }
159
236
 
160
- .switch.error {
161
- outline: 1px solid var(--danger-text);
237
+ .disabled label,
238
+ .switch:disabled {
239
+ cursor: not-allowed;
162
240
  }
163
241
 
164
242
  .required {
@@ -167,5 +245,14 @@
167
245
 
168
246
  .error-text {
169
247
  color: var(--danger-text);
248
+ font-size: 0.875rem;
249
+ margin-top: 0.25rem;
250
+ }
251
+
252
+ @media (prefers-reduced-motion: reduce) {
253
+ .switch,
254
+ .thumb {
255
+ transition: none;
256
+ }
170
257
  }
171
258
  </style>
@@ -1,7 +1,15 @@
1
1
  import type { Snippet } from 'svelte';
2
+ export type SwitchLabelPosition = 'right' | 'left' | 'top';
2
3
  export interface SwitchProps {
3
- /** Label text displayed above the switch */
4
+ /** Label text displayed next to the switch */
4
5
  label?: string;
6
+ /**
7
+ * Where the label sits relative to the switch.
8
+ * - `right` (default): switch then label, inline like a checkbox
9
+ * - `left`: label then switch, with the switch pushed to the far end (settings rows)
10
+ * - `top`: form-field style label above the switch, matching Textbox/Select
11
+ */
12
+ labelPosition?: SwitchLabelPosition;
5
13
  /** Mark field as required (shows asterisk) */
6
14
  required?: boolean;
7
15
  /** Whether the switch is on (bindable) */
@@ -20,6 +28,8 @@ export interface SwitchProps {
20
28
  showError?: boolean;
21
29
  /** Error message to display */
22
30
  errorText?: string;
31
+ /** Accessible label for screen readers (use when no visible label) */
32
+ ariaLabel?: string;
23
33
  /** Simple tooltip text */
24
34
  tooltipText?: string;
25
35
  /** Position of the tooltip */
package/dist/index.d.ts CHANGED
@@ -105,7 +105,7 @@ export type { TextboxProps } from './form/Textbox.svelte';
105
105
  export { default as Slider } from './form/Slider.svelte';
106
106
  export type { SliderProps } from './form/Slider.svelte';
107
107
  export { default as Switch } from './form/Switch.svelte';
108
- export type { SwitchProps } from './form/Switch.svelte';
108
+ export type { SwitchProps, SwitchLabelPosition } from './form/Switch.svelte';
109
109
  export { default as Chips } from './form/Chips.svelte';
110
110
  export type { ChipsProps } from './form/Chips.svelte';
111
111
  export { default as ColorPicker } from './form/ColorPicker.svelte';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@placeholderco/placeholder-ui",
3
- "version": "3.0.4",
3
+ "version": "3.0.5",
4
4
  "description": "A modern, customizable Svelte 5 UI component library with comprehensive theming support. Re-brand it with a single config object.",
5
5
  "license": "MIT",
6
6
  "author": "Placeholderco",
@@ -62,7 +62,7 @@
62
62
  "prettier-plugin-svelte": "^4.1.1",
63
63
  "publint": "^0.3.25",
64
64
  "rimraf": "^6.1.3",
65
- "svelte": "^5.57.2",
65
+ "svelte": "^5.57.3",
66
66
  "svelte-check": "^4.7.7",
67
67
  "typescript": "^6.0.3",
68
68
  "vite": "^8.3.5"