@glasshome/ui 1.12.0 → 1.14.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.
@@ -20,6 +20,7 @@ import { useMediaStore } from "./media-store.js";
20
20
  import { NumberField } from "./number-field.js";
21
21
  import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./select.js";
22
22
  import { Switch } from "./switch.js";
23
+ import { ToggleGroup, ToggleGroupItem } from "./toggle-group.js";
23
24
 
24
25
  export interface ExtendedJSONSchema extends JSONSchema7 {
25
26
  /* Nested schemas carry the same extensions, so a whole tree types as one. */
@@ -105,6 +106,10 @@ function isStringArray(prop: ExtendedJSONSchema): boolean {
105
106
  return prop.type === "array" && itemsOf(prop).type === "string";
106
107
  }
107
108
 
109
+ function isChoiceArray(prop: ExtendedJSONSchema): boolean {
110
+ return prop.type === "array" && itemsOf(prop).enum !== undefined;
111
+ }
112
+
108
113
  function isObjectGroup(prop: ExtendedJSONSchema): boolean {
109
114
  return prop.type === "object" && prop.properties !== undefined && prop.formType === undefined;
110
115
  }
@@ -168,7 +173,7 @@ export function switchVariantValue(
168
173
  }
169
174
 
170
175
  interface SchemaFormProps {
171
- schema: JSONSchema7;
176
+ schema: ExtendedJSONSchema;
172
177
  data: Record<string, unknown>;
173
178
  onChange: (data: Record<string, unknown>) => void;
174
179
  errors?: string[];
@@ -181,7 +186,7 @@ export function SchemaForm(props: SchemaFormProps) {
181
186
  const properties = () => {
182
187
  const schema = props.schema;
183
188
  if (schema.type !== "object" || !schema.properties) return [];
184
- return propertiesOf(schema as ExtendedJSONSchema);
189
+ return propertiesOf(schema);
185
190
  };
186
191
 
187
192
  // Controlled: props.data is the single source of truth, so parent-driven
@@ -251,6 +256,7 @@ type ControlKind =
251
256
  | "enum"
252
257
  | "boolean"
253
258
  | "number"
259
+ | "choices"
254
260
  | "strings"
255
261
  | "group"
256
262
  | "text";
@@ -268,6 +274,7 @@ const CONTROL_KINDS: ReadonlyArray<
268
274
  ["enum", (prop) => prop.enum !== undefined],
269
275
  ["boolean", (prop) => prop.type === "boolean"],
270
276
  ["number", (prop) => prop.type === "number" || prop.type === "integer"],
277
+ ["choices", (prop) => isChoiceArray(prop)],
271
278
  ["strings", (prop) => isStringArray(prop)],
272
279
  ["group", (prop) => isObjectGroup(prop)],
273
280
  ];
@@ -290,13 +297,14 @@ const LABEL_TARGET: Record<ControlKind, "for" | "labelledby" | "none"> = {
290
297
  boolean: "labelledby",
291
298
  list: "labelledby",
292
299
  variants: "labelledby",
300
+ choices: "labelledby",
293
301
  strings: "labelledby",
294
302
  unknown: "none",
295
303
  group: "none",
296
304
  };
297
305
 
298
- /* A control's own hint sits below the control; a group carries its legend and
299
- * explanation above its rows, so the group branch renders its own caption. */
306
+ /* A hint belongs to the label above it, not to the control below: under the
307
+ * control it reads as the next field's. Groups carry their own caption. */
300
308
  function LabeledField(props: FieldProps) {
301
309
  const kind = () => controlKind(props.prop, props.name);
302
310
  const labelId = () => `${props.id}-label`;
@@ -307,10 +315,10 @@ function LabeledField(props: FieldProps) {
307
315
  <FieldLabel id={labelId()} for={target() === "for" ? props.id : undefined}>
308
316
  {props.prop.title || props.name}
309
317
  </FieldLabel>
310
- <FieldControl {...props} labelledBy={target() === "labelledby" ? labelId() : undefined} />
311
318
  <Show when={props.prop.description}>
312
319
  {(description) => <FieldDescription>{description()}</FieldDescription>}
313
320
  </Show>
321
+ <FieldControl {...props} labelledBy={target() === "labelledby" ? labelId() : undefined} />
314
322
  </Field>
315
323
  </Show>
316
324
  );
@@ -319,6 +327,7 @@ function LabeledField(props: FieldProps) {
319
327
  /** The one recursive dispatch: every nesting level renders through here. */
320
328
  function FieldControl(props: FieldProps) {
321
329
  const current = () => props.value ?? props.prop.default;
330
+ const enumLabel = (value: string) => props.prop.labels?.[value] ?? value;
322
331
  const kind = () => controlKind(props.prop, props.name);
323
332
  return (
324
333
  <SwitchFlow
@@ -382,11 +391,13 @@ function FieldControl(props: FieldProps) {
382
391
  }}
383
392
  options={(props.prop.enum ?? []).map(String)}
384
393
  itemComponent={(itemProps) => (
385
- <SelectItem item={itemProps.item}>{String(itemProps.item.rawValue)}</SelectItem>
394
+ <SelectItem item={itemProps.item}>
395
+ {enumLabel(String(itemProps.item.rawValue))}
396
+ </SelectItem>
386
397
  )}
387
398
  >
388
399
  <SelectTrigger class="w-full" aria-labelledby={props.labelledBy}>
389
- <SelectValue<string>>{(state) => state.selectedOption()}</SelectValue>
400
+ <SelectValue<string>>{(state) => enumLabel(state.selectedOption())}</SelectValue>
390
401
  </SelectTrigger>
391
402
  <SelectContent />
392
403
  </Select>
@@ -411,6 +422,9 @@ function FieldControl(props: FieldProps) {
411
422
  onInput={(e) => props.onChange(Number(e.currentTarget.value))}
412
423
  />
413
424
  </Match>
425
+ <Match when={kind() === "choices"}>
426
+ <ChoicesControl {...props} />
427
+ </Match>
414
428
  <Match when={kind() === "strings"}>
415
429
  <StringListField
416
430
  labelledBy={props.labelledBy}
@@ -493,19 +507,22 @@ function ListControl(props: FieldProps) {
493
507
  return schema.labels?.[kind] ?? kind;
494
508
  };
495
509
 
496
- // The kind badge carries the variant label, so the caption never repeats it:
497
- // labelField value, else "Item N".
498
- const caption = (item: unknown, index: number) => {
499
- const record = recordOf(item);
510
+ const labelFieldValue = (item: unknown): string | undefined => {
500
511
  const labelField = props.prop.labelField;
501
- if (labelField !== undefined) {
502
- const value = record[labelField];
503
- if (typeof value === "string" && value.trim() !== "") return value;
504
- if (typeof value === "number") return String(value);
505
- }
506
- return `Item ${index + 1}`;
512
+ if (labelField === undefined) return undefined;
513
+ const value = recordOf(item)[labelField];
514
+ if (typeof value === "string" && value.trim() !== "") return value;
515
+ if (typeof value === "number") return String(value);
516
+ return undefined;
507
517
  };
508
518
 
519
+ const caption = (item: unknown, index: number) =>
520
+ labelFieldValue(item) ?? kindLabel(item) ?? `Item ${index + 1}`;
521
+
522
+ // Only a row the labelField already named has anything left for the badge.
523
+ const badgeLabel = (item: unknown) =>
524
+ labelFieldValue(item) === undefined ? undefined : kindLabel(item);
525
+
509
526
  const replaceAt = (index: number, value: unknown) => {
510
527
  props.onChange(items().map((item, i) => (i === index ? value : item)));
511
528
  };
@@ -630,7 +647,7 @@ function ListControl(props: FieldProps) {
630
647
  <span class="pointer-events-none relative min-w-0 flex-1 truncate text-left text-sm">
631
648
  {caption(item(), index)}
632
649
  </span>
633
- <Show when={kindLabel(item())}>
650
+ <Show when={badgeLabel(item())}>
634
651
  {(label) => (
635
652
  <Badge class="pointer-events-none relative shrink-0">{label()}</Badge>
636
653
  )}
@@ -774,6 +791,29 @@ function VariantsControl(props: FieldProps) {
774
791
  );
775
792
  }
776
793
 
794
+ function ChoicesControl(props: FieldProps) {
795
+ const current = () => props.value ?? props.prop.default;
796
+ const enumValues = () => (itemsOf(props.prop).enum ?? []).map(String);
797
+ const labelFor = (v: string) => props.prop.labels?.[v] ?? v;
798
+
799
+ return (
800
+ <ToggleGroup
801
+ multiple
802
+ aria-labelledby={props.labelledBy}
803
+ value={(current() as string[] | undefined) ?? []}
804
+ onChange={(vals) => {
805
+ const order = enumValues();
806
+ props.onChange((vals ?? []).slice().sort((a, b) => order.indexOf(a) - order.indexOf(b)));
807
+ }}
808
+ class="flex flex-wrap gap-1"
809
+ >
810
+ <For each={enumValues()}>
811
+ {(v) => <ToggleGroupItem value={v}>{labelFor(v)}</ToggleGroupItem>}
812
+ </For>
813
+ </ToggleGroup>
814
+ );
815
+ }
816
+
777
817
  function StringListField(props: {
778
818
  value: string[];
779
819
  onChange: (value: string[]) => void;
@@ -43,6 +43,7 @@ type SectionCardProps = {
43
43
  children?: JSX.Element;
44
44
  };
45
45
 
46
+ // The section owns every gap; the header, toolbar and rows inside it add none.
46
47
  export function SectionCard(props: SectionCardProps) {
47
48
  const glass = () => props.glass ?? NOOP_GLASS;
48
49
  const active = () => glass().active?.() ?? false;
@@ -103,6 +103,7 @@ function WidgetCardTile(props: InternalProps) {
103
103
  padding="md"
104
104
  class={cn("group h-full", props.class)}
105
105
  >
106
+ {/* flex-1 body: the meta stays pinned to the tile's foot whether the description is long, missing or off. */}
106
107
  <div data-slot="widget-card-body" class="flex flex-1 flex-col gap-3">
107
108
  <WidgetIdentity widget={props.widget} iconSize="md" showVersionInline={false} />
108
109
  <Show when={showDescription() && props.widget.description}>
@@ -213,6 +213,46 @@
213
213
  pointer-events: none;
214
214
  }
215
215
 
216
+ /* A mode's rim: the masked ring of .glass-edge-gradient, but the gradient orbits,
217
+ * so a surface can say a mode is on for as long as it lasts. The mask sits on the
218
+ * parent and the gradient rotates as a child, or the ring would rotate with it. */
219
+ :where(.glass-edge-orbit) {
220
+ position: absolute;
221
+ inset: 0;
222
+ border-radius: inherit;
223
+ padding: 1px;
224
+ overflow: hidden;
225
+ mask:
226
+ linear-gradient(#fff 0 0) content-box,
227
+ linear-gradient(#fff 0 0);
228
+ mask-composite: exclude;
229
+ pointer-events: none;
230
+ /* A conic wedge crosses a wide pill's ends fast and its long edges slowly, so the
231
+ * ring keeps a constant floor and the orbit only adds the travelling highlight. */
232
+ background: color-mix(in oklab, var(--primary) 30%, transparent);
233
+ }
234
+ :where(.glass-edge-orbit) > * {
235
+ position: absolute;
236
+ top: 50%;
237
+ left: 50%;
238
+ width: 150%;
239
+ aspect-ratio: 1;
240
+ translate: -50% -50%;
241
+ background: conic-gradient(
242
+ from 0deg,
243
+ transparent 0deg,
244
+ color-mix(in oklab, var(--primary) 95%, transparent) 40deg,
245
+ transparent 100deg,
246
+ transparent 360deg
247
+ );
248
+ animation: spin-slow 8s linear infinite;
249
+ }
250
+ @media (prefers-reduced-motion: reduce) {
251
+ :where(.glass-edge-orbit) > * {
252
+ animation: none;
253
+ }
254
+ }
255
+
216
256
  /**
217
257
  * Consistent, theme-aware overlay scrollbar across browsers. Firefox uses
218
258
  * scrollbar-width/color; Chromium/WebKit use the ::-webkit-scrollbar pseudos
@@ -6,3 +6,4 @@
6
6
  export * from "./hex.js";
7
7
  export * from "./presets.js";
8
8
  export * from "./theme-colors.js";
9
+ export { contrastRatio, parseThemeBlock } from "./theme-css.js";
@@ -0,0 +1,34 @@
1
+ import { oklchToHex } from "./hex.js";
2
+
3
+ /** Every custom property declared in one theme.css block (`:root`, `.dark`). */
4
+ export function parseThemeBlock(css: string, block: string): Record<string, string> {
5
+ const match = css.match(
6
+ new RegExp(`${block.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\s*\\{([^}]*)\\}`),
7
+ );
8
+ if (!match) throw new Error(`block not found in theme.css: ${block}`);
9
+ const vars: Record<string, string> = {};
10
+ for (const line of (match[1] ?? "").split("\n")) {
11
+ const m = line.match(/^\s*(--[\w-]+):\s*(.+?);\s*$/);
12
+ const [, name, value] = m ?? [];
13
+ if (name && value) vars[name] = value;
14
+ }
15
+ return vars;
16
+ }
17
+
18
+ function luminance(oklch: string): number {
19
+ const hex = oklchToHex(oklch);
20
+ const [r, g, b] = [1, 3, 5]
21
+ .map((i) => Number.parseInt(hex.slice(i, i + 2), 16) / 255)
22
+ .map((c) => (c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4)) as [
23
+ number,
24
+ number,
25
+ number,
26
+ ];
27
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b;
28
+ }
29
+
30
+ /** WCAG contrast between two oklch() literals. */
31
+ export function contrastRatio(a: string, b: string): number {
32
+ const [x, y] = [luminance(a), luminance(b)];
33
+ return (Math.max(x, y) + 0.05) / (Math.min(x, y) + 0.05);
34
+ }