@glasshome/ui 1.29.1 → 1.30.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.
@@ -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 {
@@ -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
+ }