@marianmeres/stuic 3.178.0 → 3.179.0

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.
@@ -60,6 +60,29 @@
60
60
  /** Minimum message length. 0 (default) disables the check. */
61
61
  messageMinLength?: number;
62
62
 
63
+ /**
64
+ * Names of fields rendered **read-only**: the value is visible and still
65
+ * reaches `onSubmit`, but the user cannot change it. The typical case is a
66
+ * signed-in visitor whose name/email the server already knows — prefill
67
+ * them through `formData` and list them here.
68
+ *
69
+ * Accepts the built-in names (`"name"`, `"email"`, `"phone"`, `"subject"`,
70
+ * `"company"`, `"message"`) and any `extraFields` name. Unknown names are
71
+ * ignored. Listing a field does NOT show it — pair with the matching
72
+ * `show*` toggle.
73
+ *
74
+ * Read-only is not the same as `disabled`: the value keeps full contrast
75
+ * and stays focusable/selectable/copyable. A read-only Subject rendered
76
+ * from `subjectValues` falls back to a read-only text input, because
77
+ * `<select>` has no read-only counterpart.
78
+ *
79
+ * Prefill whatever you lock: a read-only control is barred from *native*
80
+ * constraint validation, so an empty required one is a dead end for the
81
+ * user. The form still reports it (see `validateContactForm`), it just
82
+ * cannot be fixed in the UI.
83
+ */
84
+ readonlyFields?: string[];
85
+
63
86
  /**
64
87
  * Declarative extra fields rendered as FieldInput entries.
65
88
  * Values bind into `formData.extra[name]`.
@@ -153,6 +176,7 @@
153
176
  showCompany = false,
154
177
  requireCompany = false,
155
178
  messageMinLength = 0,
179
+ readonlyFields,
156
180
  extraFields = [],
157
181
  extraFieldsSlot,
158
182
  useHoneypot = true,
@@ -184,12 +208,24 @@
184
208
  let topFields = $derived(extraFields.filter((f) => f.position === "top"));
185
209
  let bottomFields = $derived(extraFields.filter((f) => f.position !== "top"));
186
210
 
211
+ let readonlySet = $derived(new Set(readonlyFields ?? []));
212
+ function isReadonly(field: string): boolean {
213
+ return readonlySet.has(field);
214
+ }
215
+
187
216
  // Subject: render a <select> when subjectValues is non-empty (which also shows
188
217
  // the field regardless of showSubject); otherwise a free-text input gated by
189
218
  // showSubject. The select gets a prepended blank "prompt" option so the initial
190
219
  // empty subject isn't silently auto-selected to the first real value.
191
- let subjectAsSelect = $derived((subjectValues?.length ?? 0) > 0);
192
- let subjectShown = $derived(showSubject || subjectAsSelect);
220
+ //
221
+ // A read-only subject downgrades to a read-only text input — <select> has no
222
+ // readonly counterpart, and `disabled` would grey out the very value we mean
223
+ // to show. Hence `subjectShown` keys off *having* values rather than off
224
+ // rendering a select: sharing one flag would have made `subjectValues` +
225
+ // readonly hide the field outright.
226
+ let subjectHasValues = $derived((subjectValues?.length ?? 0) > 0);
227
+ let subjectAsSelect = $derived(subjectHasValues && !isReadonly("subject"));
228
+ let subjectShown = $derived(showSubject || subjectHasValues);
193
229
  let subjectOptions = $derived([
194
230
  { label: t("contact_form.subject_select_prompt"), value: "" },
195
231
  ...(subjectValues ?? []).map((v) => ({ label: v, value: v })),
@@ -280,6 +316,21 @@
280
316
  },
281
317
  });
282
318
 
319
+ // Single source for `validateContactForm`'s options — the submit path and
320
+ // `readonlyMissing()` below must agree on which fields are shown/required.
321
+ let validationOptions = $derived({
322
+ showName,
323
+ requireName,
324
+ showPhone,
325
+ requirePhone,
326
+ showSubject: subjectShown,
327
+ requireSubject,
328
+ showCompany,
329
+ requireCompany,
330
+ messageMinLength,
331
+ extraFields,
332
+ });
333
+
283
334
  // Merge internal + external errors; external takes precedence per field.
284
335
  let allErrors = $derived.by(() => {
285
336
  const map = new Map<string, string>();
@@ -289,7 +340,22 @@
289
340
  });
290
341
 
291
342
  function fieldError(field: string): string | undefined {
292
- return allErrors.find((e) => e.field === field)?.message;
343
+ return allErrors.find((e) => e.field === field)?.message ?? readonlyMissing(field);
344
+ }
345
+
346
+ // A read-only control is barred from *native* constraint validation:
347
+ // `validity.valueMissing` stays false however empty it is (the spec requires
348
+ // the control to be "mutable"). Without this the field walk behind the
349
+ // exported `validate()` would green-light an empty read-only required field,
350
+ // and the consumer would post it. Run the form's own validator for that one
351
+ // field instead — same rules, same messages, no duplicated logic. The
352
+ // built-in submit path reaches `validateContactForm` anyway; this only
353
+ // closes the pre-submit / imperative window, and only for read-only fields.
354
+ function readonlyMissing(field: string): string | undefined {
355
+ if (!readonlySet.size || !isReadonly(field)) return;
356
+ return validateContactForm(formData, t, validationOptions).find(
357
+ (e) => e.field === field
358
+ )?.message;
293
359
  }
294
360
 
295
361
  function extraValue(cfg: ContactFieldConfig): string {
@@ -321,18 +387,7 @@
321
387
  }
322
388
 
323
389
  function handleSubmitValid() {
324
- const validationErrors = validateContactForm(formData, t, {
325
- showName,
326
- requireName,
327
- showPhone,
328
- requirePhone,
329
- showSubject: subjectShown,
330
- requireSubject,
331
- showCompany,
332
- requireCompany,
333
- messageMinLength,
334
- extraFields,
335
- });
390
+ const validationErrors = validateContactForm(formData, t, validationOptions);
336
391
  internalErrors = validationErrors;
337
392
 
338
393
  // Report-only on bot signals: we still submit when field validation passes
@@ -452,6 +507,7 @@
452
507
  placeholder={cfg.placeholder}
453
508
  autocomplete={cfg.autocomplete}
454
509
  required={cfg.required}
510
+ readonly={isReadonly(cfg.name)}
455
511
  name={`contact-extra-${cfg.name}`}
456
512
  labelLeftBreakpoint={0}
457
513
  validate={{
@@ -481,6 +537,7 @@
481
537
  placeholder={t("contact_form.name_placeholder")}
482
538
  autocomplete="name"
483
539
  required={requireName}
540
+ readonly={isReadonly("name")}
484
541
  name="contact-name"
485
542
  labelLeftBreakpoint={0}
486
543
  validate={{
@@ -501,6 +558,7 @@
501
558
  placeholder={t("contact_form.email_placeholder")}
502
559
  autocomplete="email"
503
560
  required
561
+ readonly={isReadonly("email")}
504
562
  name="contact-email"
505
563
  labelLeftBreakpoint={0}
506
564
  validate={{
@@ -521,6 +579,7 @@
521
579
  placeholder={t("contact_form.phone_placeholder")}
522
580
  autocomplete="tel"
523
581
  required={requirePhone}
582
+ readonly={isReadonly("phone")}
524
583
  name="contact-phone"
525
584
  labelLeftBreakpoint={0}
526
585
  validate={{
@@ -542,6 +601,7 @@
542
601
  placeholder={t("contact_form.company_placeholder")}
543
602
  autocomplete="organization"
544
603
  required={requireCompany}
604
+ readonly={isReadonly("company")}
545
605
  name="contact-company"
546
606
  labelLeftBreakpoint={0}
547
607
  validate={{
@@ -579,6 +639,7 @@
579
639
  type="text"
580
640
  placeholder={t("contact_form.subject_placeholder")}
581
641
  required={requireSubject}
642
+ readonly={isReadonly("subject")}
582
643
  name="contact-subject"
583
644
  labelLeftBreakpoint={0}
584
645
  validate={{
@@ -598,6 +659,7 @@
598
659
  label={t("contact_form.message_label")}
599
660
  placeholder={t("contact_form.message_placeholder")}
600
661
  required
662
+ readonly={isReadonly("message")}
601
663
  name="contact-message"
602
664
  labelLeftBreakpoint={0}
603
665
  validate={{
@@ -619,6 +681,7 @@
619
681
  placeholder={cfg.placeholder}
620
682
  autocomplete={cfg.autocomplete}
621
683
  required={cfg.required}
684
+ readonly={isReadonly(cfg.name)}
622
685
  name={`contact-extra-${cfg.name}`}
623
686
  labelLeftBreakpoint={0}
624
687
  validate={{
@@ -47,6 +47,28 @@ export interface Props extends Omit<HTMLAttributes<HTMLFormElement>, "children">
47
47
  requireCompany?: boolean;
48
48
  /** Minimum message length. 0 (default) disables the check. */
49
49
  messageMinLength?: number;
50
+ /**
51
+ * Names of fields rendered **read-only**: the value is visible and still
52
+ * reaches `onSubmit`, but the user cannot change it. The typical case is a
53
+ * signed-in visitor whose name/email the server already knows — prefill
54
+ * them through `formData` and list them here.
55
+ *
56
+ * Accepts the built-in names (`"name"`, `"email"`, `"phone"`, `"subject"`,
57
+ * `"company"`, `"message"`) and any `extraFields` name. Unknown names are
58
+ * ignored. Listing a field does NOT show it — pair with the matching
59
+ * `show*` toggle.
60
+ *
61
+ * Read-only is not the same as `disabled`: the value keeps full contrast
62
+ * and stays focusable/selectable/copyable. A read-only Subject rendered
63
+ * from `subjectValues` falls back to a read-only text input, because
64
+ * `<select>` has no read-only counterpart.
65
+ *
66
+ * Prefill whatever you lock: a read-only control is barred from *native*
67
+ * constraint validation, so an empty required one is a dead end for the
68
+ * user. The form still reports it (see `validateContactForm`), it just
69
+ * cannot be fixed in the UI.
70
+ */
71
+ readonlyFields?: string[];
50
72
  /**
51
73
  * Declarative extra fields rendered as FieldInput entries.
52
74
  * Values bind into `formData.extra[name]`.
@@ -75,6 +75,7 @@ interface ContactFieldConfig {
75
75
  | `showCompany` | `boolean` | `false` | Render the Company field. |
76
76
  | `requireCompany` | `boolean` | `false` | Require Company (only applies when shown). |
77
77
  | `messageMinLength` | `number` | `0` | Minimum message length. `0` disables the check. |
78
+ | `readonlyFields` | `string[]` | - | Field names rendered read-only — prefilled, visible, submitted, not editable (see below). |
78
79
  | `extraFields` | `ContactFieldConfig[]` | `[]` | Declarative extra fields, positioned top or bottom. |
79
80
  | `extraFieldsSlot` | `Snippet<[{ formData, fieldError }]>` | - | Escape hatch for non-FieldInput extras (consent checkbox, captcha widget). |
80
81
  | `useHoneypot` | `boolean` | `true` | Render the hidden honeypot trap. |
@@ -182,6 +183,54 @@ automatically; the bound value is still the chosen string in `formData.subject`)
182
183
  </ContactUsForm>
183
184
  ```
184
185
 
186
+ ### Prefilled, read-only fields (signed-in visitor)
187
+
188
+ When the server already knows who is writing, prefill `formData` and list those
189
+ field names in `readonlyFields`. The values stay visible, keep full contrast, are
190
+ selectable/copyable, and still reach `onSubmit` — the user simply cannot change
191
+ them.
192
+
193
+ ```svelte
194
+ <script lang="ts">
195
+ import { ContactUsForm, createEmptyContactFormData } from "@marianmeres/stuic";
196
+
197
+ let { user } = $props();
198
+
199
+ let formData = $state({
200
+ ...createEmptyContactFormData(),
201
+ name: user.name,
202
+ email: user.email,
203
+ });
204
+ </script>
205
+
206
+ <ContactUsForm
207
+ bind:formData
208
+ onSubmit={send}
209
+ showName
210
+ readonlyFields={["name", "email"]}
211
+ />
212
+ ```
213
+
214
+ Notes:
215
+
216
+ - `readonlyFields` does **not** show a field — pair it with the matching `show*` toggle.
217
+ - It accepts the built-in names (`name`, `email`, `phone`, `subject`, `company`,
218
+ `message`) and any `extraFields` name. Unknown names are ignored.
219
+ - Read-only is **not** `disabled`. `disabled` reads as "unavailable", greys the
220
+ value out and drops it from the tab order; read-only says "this is your value,
221
+ it just isn't yours to change here".
222
+ - A read-only Subject falls back to a read-only **text input** even when
223
+ `subjectValues` is set, because `<select>` has no read-only counterpart and
224
+ disabling it would grey out the very value you meant to show.
225
+ - **Prefill whatever you lock.** A read-only control is barred from _native_
226
+ constraint validation, so an empty required one is a dead end for the user.
227
+ The form still reports it (`validateContactForm` runs on submit, and
228
+ `validate()` covers the same ground for read-only fields), but nobody can fix
229
+ it from the UI.
230
+ - Read-only is a **UI** affordance, not a security boundary: anything the browser
231
+ holds can be edited from devtools. Re-derive the trusted values server-side
232
+ from the session instead of trusting the posted ones.
233
+
185
234
  ### Declarative extra field
186
235
 
187
236
  ```svelte
@@ -314,6 +314,25 @@ Override globally in `:root` or locally via `style` prop:
314
314
  | `--stuic-input-text` | `--stuic-color-foreground` | Text color |
315
315
  | `--stuic-input-placeholder` | `--stuic-color-muted-foreground` | Placeholder color |
316
316
 
317
+ ### Readonly Tokens
318
+
319
+ Any `Field*` control that reaches the DOM with a `readonly` attribute (via the
320
+ `readonly` prop passthrough) gets a muted wrapper — a "shown, but not yours to
321
+ change" treatment. Deliberately **not** the `disabled` treatment: `disabled`
322
+ fades the whole wrap to `opacity: 0.5` and leaves the tab order, while readonly
323
+ keeps full text contrast and stays focusable, selectable and copyable. Applies
324
+ to `<input>` and `<textarea>` only — `<select>` has no readonly counterpart.
325
+
326
+ | Variable | Default | Description |
327
+ | ------------------------------- | ------------------------------------------------------------------------ | -------------------------------- |
328
+ | `--stuic-input-bg-readonly` | `--stuic-color-input` mixed 10% toward `--stuic-color-muted-foreground` | Wrapper background when readonly |
329
+ | `--stuic-input-border-readonly` | `--stuic-input-border` mixed 15% toward `--stuic-color-muted-foreground` | Wrapper border when readonly |
330
+
331
+ Both are _mixed off_ the editable values rather than pointing at
332
+ `--stuic-color-muted`: in several themes (stone, for one) `muted` and `input`
333
+ sit ~2% apart, which is no visual cue at all. Mixing toward the muted
334
+ _foreground_ also reverses direction on its own in dark themes.
335
+
317
336
  ### Size Tokens
318
337
 
319
338
  Each size (sm, md, lg) has corresponding tokens:
@@ -24,6 +24,15 @@
24
24
  --stuic-input-text: var(--stuic-color-foreground);
25
25
  --stuic-input-placeholder: color-mix(in oklab, var(--stuic-color-muted-foreground) 75%, transparent);
26
26
 
27
+ /* Readonly: a value shown but not editable. Deliberately NOT the disabled
28
+ treatment (opacity 0.5) — the text must stay fully legible. Mixed off the
29
+ input bg rather than set to --stuic-color-muted: in several themes (stone,
30
+ the demo default) muted and input sit ~2% apart, which is no cue at all.
31
+ Mixing toward the muted *foreground* also flips direction on its own in
32
+ dark themes, so the field reads as "settled" either way. */
33
+ --stuic-input-bg-readonly: color-mix(in oklab, var(--stuic-color-muted-foreground) 10%, var(--stuic-color-input));
34
+ --stuic-input-border-readonly: color-mix(in oklab, var(--stuic-color-muted-foreground) 15%, var(--stuic-input-border));
35
+
27
36
  /* Size: sm */
28
37
  --stuic-input-padding-x-sm: calc(var(--spacing) * 2.5);
29
38
  --stuic-input-padding-y-sm: calc(var(--spacing) * 2);
@@ -254,6 +263,22 @@
254
263
  cursor: not-allowed;
255
264
  }
256
265
 
266
+ /* Readonly state — "this value is known, you just can't change it".
267
+ Scoped to the wrap that directly contains the control (not the whole
268
+ .stuic-input) so a nested readonly control can't mute an editable parent.
269
+ Unlike disabled it keeps full text contrast and the focus ring: a readonly
270
+ control is still focusable, and hiding that would cost keyboard users. */
271
+ .stuic-input .input-wrap:has(input[readonly]),
272
+ .stuic-input .input-wrap:has(textarea[readonly]) {
273
+ background: var(--stuic-input-bg-readonly);
274
+ border-color: var(--stuic-input-border-readonly);
275
+ }
276
+
277
+ .stuic-input input[readonly],
278
+ .stuic-input textarea[readonly] {
279
+ cursor: default;
280
+ }
281
+
257
282
  /* Transparent wrapper utility - for components like Switch that don't need wrapper styling */
258
283
  .stuic-input .input-wrap.input-wrap-transparent,
259
284
  .stuic-input.invalid .input-wrap.input-wrap-transparent,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marianmeres/stuic",
3
- "version": "3.178.0",
3
+ "version": "3.179.0",
4
4
  "packageManager": "pnpm@11.5.0",
5
5
  "scripts": {
6
6
  "dev": "vite dev",
@@ -177,7 +177,7 @@
177
177
  "tailwindcss": "^4.3.3",
178
178
  "tsx": "^4.23.13",
179
179
  "typescript": "^5.9.3",
180
- "typescript-eslint": "^8.69.0",
180
+ "typescript-eslint": "^8.70.0",
181
181
  "vite": "^7.3.6",
182
182
  "vitest": "^4.1.11",
183
183
  "vitest-browser-svelte": "^2.2.1"