@glasshome/ui 1.29.1 → 1.31.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@glasshome/ui",
3
- "version": "1.29.1",
3
+ "version": "1.31.0",
4
4
  "description": "SolidJS component library for GlassHome, built on Kobalte",
5
5
  "keywords": [
6
6
  "components",
package/src/index.ts CHANGED
@@ -32,6 +32,7 @@ export {
32
32
  SECTION_ROW_SURFACE,
33
33
  TRACK_SURFACE,
34
34
  } from "./lib/card-classes.js";
35
+ export { copyImage, copyText } from "./lib/clipboard.js";
35
36
  export {
36
37
  CAROUSEL_DOTS,
37
38
  CAROUSEL_VIEWPORT,
@@ -0,0 +1,84 @@
1
+ /** The one door to the clipboard. Call it from the click: browsers refuse a
2
+ * copy more than a few seconds after it. Text still loading goes in as a
3
+ * promise so a ClipboardItem write keeps the gesture (Safari). */
4
+ export async function copyText(text: string | Promise<string>): Promise<boolean> {
5
+ if (typeof text !== "string") {
6
+ if (await copyPending(text)) return true;
7
+ const loaded = await text.catch(() => null);
8
+ return loaded === null ? false : copyText(loaded);
9
+ }
10
+ // Dashboards are reached over plain http on the LAN, where navigator.clipboard
11
+ // does not exist; the selection path is the only copy those installs have.
12
+ if (navigator.clipboard?.writeText) {
13
+ try {
14
+ await navigator.clipboard.writeText(text);
15
+ return true;
16
+ } catch {
17
+ // permission or focus denial, fall through to the selection path
18
+ }
19
+ }
20
+ return copyViaSelection(text);
21
+ }
22
+
23
+ /** An image that is still being made: the promise goes into the ClipboardItem
24
+ * so the write keeps the gesture (Safari). False where there is no image
25
+ * clipboard (plain http, Firefox by default). */
26
+ export async function copyImage(image: Promise<Blob>, type = "image/png"): Promise<boolean> {
27
+ if (!canWriteItems()) return false;
28
+ return writeItem(type, image);
29
+ }
30
+
31
+ async function copyPending(text: Promise<string>): Promise<boolean> {
32
+ if (!canWriteItems()) return false;
33
+ return writeItem(
34
+ "text/plain",
35
+ text.then((t) => new Blob([t], { type: "text/plain" })),
36
+ );
37
+ }
38
+
39
+ function canWriteItems(): boolean {
40
+ return typeof ClipboardItem !== "undefined" && typeof navigator.clipboard?.write === "function";
41
+ }
42
+
43
+ async function writeItem(type: string, data: Promise<Blob>): Promise<boolean> {
44
+ try {
45
+ await navigator.clipboard.write([new ClipboardItem({ [type]: data })]);
46
+ return true;
47
+ } catch {
48
+ return false;
49
+ }
50
+ }
51
+
52
+ function copyViaSelection(text: string): boolean {
53
+ const previous = deepActiveElement();
54
+ const area = document.createElement("textarea");
55
+ area.value = text;
56
+ area.setAttribute("readonly", "");
57
+ area.style.position = "fixed";
58
+ area.style.top = "0";
59
+ area.style.left = "0";
60
+ area.style.opacity = "0";
61
+ // A focus trap only lets the textarea take focus when it sits inside the
62
+ // trapped subtree, and a copy from an unfocused one reports success having
63
+ // copied nothing.
64
+ (previous?.parentElement ?? document.body).append(area);
65
+ try {
66
+ area.focus();
67
+ area.select();
68
+ area.setSelectionRange(0, text.length);
69
+ const root = area.getRootNode() as Document | ShadowRoot;
70
+ if (root.activeElement !== area) return false;
71
+ return document.execCommand("copy");
72
+ } catch {
73
+ return false;
74
+ } finally {
75
+ area.remove();
76
+ previous?.focus();
77
+ }
78
+ }
79
+
80
+ function deepActiveElement(): HTMLElement | null {
81
+ let element = document.activeElement;
82
+ while (element?.shadowRoot?.activeElement) element = element.shadowRoot.activeElement;
83
+ return element instanceof HTMLElement ? element : null;
84
+ }
@@ -1,8 +1,9 @@
1
- import { type Component, createSignal, Show } from "solid-js";
1
+ import { type Component, Show } from "solid-js";
2
2
  import { ICON_BUTTON_CLASS } from "../lib/button-variants.js";
3
3
  import { cn } from "../lib/utils.js";
4
4
  import { Icon } from "./icon.js";
5
5
  import { toast } from "./sonner.js";
6
+ import { useCopyText } from "./use-copy-text.js";
6
7
 
7
8
  interface CopyButtonProps {
8
9
  text: string;
@@ -10,17 +11,11 @@ interface CopyButtonProps {
10
11
  }
11
12
 
12
13
  const CopyButton: Component<CopyButtonProps> = (props) => {
13
- const [copied, setCopied] = createSignal(false);
14
+ const { state, copy } = useCopyText();
14
15
 
15
16
  const handleCopy = async () => {
16
- try {
17
- await navigator.clipboard.writeText(props.text);
18
- setCopied(true);
19
- toast.success("Copied to clipboard");
20
- setTimeout(() => setCopied(false), 2000);
21
- } catch {
22
- toast.error("Failed to copy");
23
- }
17
+ if (await copy(props.text)) toast.success("Copied to clipboard");
18
+ else toast.error("Couldn't copy", { description: "Select it and copy by hand." });
24
19
  };
25
20
 
26
21
  return (
@@ -29,10 +24,10 @@ const CopyButton: Component<CopyButtonProps> = (props) => {
29
24
  data-slot="copy-button"
30
25
  onClick={handleCopy}
31
26
  class={cn(ICON_BUTTON_CLASS, "size-8", props.class)}
32
- aria-label={copied() ? "Copied!" : "Copy to clipboard"}
27
+ aria-label={state() === "copied" ? "Copied!" : "Copy to clipboard"}
33
28
  >
34
29
  <Show
35
- when={copied()}
30
+ when={state() === "copied"}
36
31
  fallback={
37
32
  <Icon icon="lucide:copy" width={16} height={16} class="text-muted-foreground h-4 w-4" />
38
33
  }
@@ -5,6 +5,7 @@
5
5
  export { type Color, parseColor } from "@kobalte/core/colors";
6
6
  // Floating glass panel + its shared surface token.
7
7
  export { SECTION_ROW_SURFACE } from "../lib/card-classes.js";
8
+ export { copyImage, copyText } from "../lib/clipboard.js";
8
9
  export { HOLD_GRACE_MS, HOLD_MS, spendLongPress } from "../lib/hold.js";
9
10
  export { RISE_MOTION, SETTLE_MOTION, TRAVEL_MOTION } from "../lib/motion-classes.js";
10
11
  export { OVERLAY_SURFACE } from "../lib/overlay-classes.js";
@@ -114,6 +115,7 @@ export {
114
115
  ContextMenuTrigger,
115
116
  } from "./context-menu.js";
116
117
  export { CopyButton } from "./copy-button.js";
118
+ export { type CopyState, useCopyText } from "./use-copy-text.js";
117
119
  export { CountPill } from "./count-pill.js";
118
120
  export { DashboardPreview, type DashboardPreviewTile } from "./dashboard-preview.js";
119
121
  export {
@@ -397,4 +399,5 @@ export {
397
399
  type WidgetSummary,
398
400
  widgetHref,
399
401
  } from "./widget-identity.js";
402
+ export { SourceMarks } from "./source-marks.js";
400
403
  export { WidgetTrustBadge } from "./widget-trust-badge.js";
@@ -9,9 +9,10 @@ const COLUMNS = { 2: "grid-cols-2", 3: "grid-cols-3", 4: "grid-cols-4" } as cons
9
9
  const SHAPE = { wide: "aspect-[16/10]", tile: "aspect-[5/4]" } as const;
10
10
 
11
11
  /* The face is the affordance: the radio control is suppressed and a ring on
12
- * the face carries the picked state, the focus ring rides the same edge. */
12
+ * the face carries the picked state, the focus ring rides the same edge.
13
+ * Rings are drawn inside the face, above the picture: an outer ring is cut off where a scroll area clips the grid. */
13
14
  const TILE_FACE =
14
- "relative w-full overflow-hidden rounded-lg border border-border/60 transition-glass duration-200 group-active/preview-tile:scale-[0.97] group-data-[checked]/preview-tile:border-transparent group-data-[checked]/preview-tile:ring-2 group-data-[checked]/preview-tile:ring-primary group-data-[checked]/preview-tile:ring-offset-2 group-data-[checked]/preview-tile:ring-offset-transparent group-has-[:focus-visible]/preview-tile:ring-[3px] group-has-[:focus-visible]/preview-tile:ring-ring/50";
15
+ "relative w-full overflow-hidden rounded-lg border border-border/60 transition-glass duration-200 group-active/preview-tile:scale-[0.97] group-data-[checked]/preview-tile:border-transparent after:pointer-events-none after:absolute after:inset-0 after:z-10 after:rounded-[inherit] after:ring-inset after:transition-glass group-data-[checked]/preview-tile:after:ring-2 group-data-[checked]/preview-tile:after:ring-primary group-has-[:focus-visible]/preview-tile:after:ring-[3px] group-has-[:focus-visible]/preview-tile:after:ring-ring/50";
15
16
 
16
17
  export function PreviewTileGroup(props: {
17
18
  value: string | null;
@@ -43,6 +44,8 @@ export function PreviewTile(props: {
43
44
  /** Off for pictures that name themselves; the label still reaches assistive tech. */
44
45
  caption?: boolean;
45
46
  icon?: string;
47
+ /** Beside the caption, after the label: provenance like `SourceMarks`. */
48
+ marks?: JSX.Element;
46
49
  shape?: keyof typeof SHAPE;
47
50
  disabled?: boolean;
48
51
  class?: string;
@@ -58,7 +61,7 @@ export function PreviewTile(props: {
58
61
  showControl={false}
59
62
  onClick={() => !props.disabled && props.onPick?.()}
60
63
  class={cn(
61
- "group/preview-tile min-w-0 [&_[data-slot=radio-group-item-content]]:flex [&_[data-slot=radio-group-item-content]]:flex-col [&_[data-slot=radio-group-item-content]]:gap-1.5",
64
+ "group/preview-tile min-w-0 [&_[data-slot=radio-group-item-content]]:flex [&_[data-slot=radio-group-item-content]]:flex-col [&_[data-slot=radio-group-item-content]]:gap-1.5 [&_[data-slot=radio-group-item-label]]:min-w-0",
62
65
  props.class,
63
66
  )}
64
67
  >
@@ -68,7 +71,7 @@ export function PreviewTile(props: {
68
71
  <Show when={props.caption ?? true} fallback={<span class="sr-only">{props.label}</span>}>
69
72
  <span
70
73
  data-slot="preview-tile-caption"
71
- class="text-foreground flex min-w-0 items-center gap-1 text-xs font-medium"
74
+ class="text-foreground @container/source-marks flex min-w-0 items-center gap-1 text-xs font-medium"
72
75
  >
73
76
  <Show when={props.icon}>
74
77
  {(icon) => (
@@ -76,6 +79,7 @@ export function PreviewTile(props: {
76
79
  )}
77
80
  </Show>
78
81
  <span class="truncate">{props.label}</span>
82
+ {props.marks}
79
83
  </span>
80
84
  <Show when={props.meta}>
81
85
  <span
@@ -0,0 +1,24 @@
1
+ import { Show } from "solid-js";
2
+ import { cn } from "../lib/utils.js";
3
+ import { Badge } from "./badge.js";
4
+ import { WidgetTrustBadge } from "./widget-trust-badge.js";
5
+
6
+ /**
7
+ * Where a catalog item comes from: the official or community mark, plus Built in when it ships with GlassHome.
8
+ * Inside an `@container/source-marks` narrower than 12rem, Built in steps aside so the name keeps its room.
9
+ */
10
+ export function SourceMarks(props: { official: boolean; builtIn?: boolean; class?: string }) {
11
+ return (
12
+ <span
13
+ data-slot="source-marks"
14
+ class={cn("inline-flex shrink-0 items-center gap-1.5", props.class)}
15
+ >
16
+ <WidgetTrustBadge isOfficial={props.official} />
17
+ <Show when={props.builtIn}>
18
+ <Badge tone="var(--muted-foreground)" class="@max-[12rem]/source-marks:hidden">
19
+ Built in
20
+ </Badge>
21
+ </Show>
22
+ </span>
23
+ );
24
+ }
@@ -0,0 +1,22 @@
1
+ import { createSignal, onCleanup } from "solid-js";
2
+ import { copyText } from "../lib/clipboard.js";
3
+
4
+ export type CopyState = "idle" | "copied" | "failed";
5
+
6
+ /** Copy plus the state a control needs to say what happened. */
7
+ export function useCopyText(resetAfterMs = 2000) {
8
+ const [state, setState] = createSignal<CopyState>("idle");
9
+ let timer: ReturnType<typeof setTimeout> | undefined;
10
+
11
+ onCleanup(() => clearTimeout(timer));
12
+
13
+ const copy = async (text: string): Promise<boolean> => {
14
+ const copied = await copyText(text);
15
+ setState(copied ? "copied" : "failed");
16
+ clearTimeout(timer);
17
+ timer = setTimeout(() => setState("idle"), resetAfterMs);
18
+ return copied;
19
+ };
20
+
21
+ return { state, copy };
22
+ }