@glasshome/ui 1.29.0 → 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.
- package/SPEC.md +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -3
- package/dist/lib/clipboard.d.ts +8 -0
- package/dist/solid/index.d.ts +2 -0
- package/dist/solid/index.js +4050 -4030
- package/dist/solid/use-copy-text.d.ts +6 -0
- package/dist/{utils-DcRd0bwQ.js → utils-C0qKxkyh.js} +171 -123
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/lib/clipboard.ts +84 -0
- package/src/solid/copy-button.tsx +7 -12
- package/src/solid/dialog-parts.tsx +11 -2
- package/src/solid/index.ts +2 -0
- package/src/solid/use-copy-text.ts +22 -0
|
@@ -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,
|
|
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
|
|
14
|
+
const { state, copy } = useCopyText();
|
|
14
15
|
|
|
15
16
|
const handleCopy = async () => {
|
|
16
|
-
|
|
17
|
-
|
|
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={
|
|
27
|
+
aria-label={state() === "copied" ? "Copied!" : "Copy to clipboard"}
|
|
33
28
|
>
|
|
34
29
|
<Show
|
|
35
|
-
when={
|
|
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
|
}
|
|
@@ -86,7 +86,7 @@ const MODAL_HEADER_ACTION = "flex shrink-0 items-center gap-2";
|
|
|
86
86
|
* - The reserved scrollbar lives inside the right inset, so the content column
|
|
87
87
|
* ends where the Header's and Footer's do. */
|
|
88
88
|
const MODAL_BODY =
|
|
89
|
-
"gh-scroll gh-stagger flex min-h-0 flex-1 flex-col gap-4 overflow-y-auto overscroll-contain [touch-action:pan-y_pinch-zoom] [scrollbar-gutter:stable] pl-6 pr-[calc(var(--spacing)*6-var(--scrollbar-w))] pt-6 pb-6 [[data-slot$='-header']~&]:pt-3 [&:has(~[data-slot$='-footer'])]:pb-3 in-data-[slot=bottom-sheet-content]:pl-4 in-data-[slot=bottom-sheet-content]:pr-[calc(var(--spacing)*4-var(--scrollbar-w))] [[data-slot=bottom-sheet-handle]+&]:pt-1";
|
|
89
|
+
"gh-scroll gh-stagger flex min-h-0 flex-1 flex-col gap-4 overflow-y-auto overscroll-contain [touch-action:pan-y_pinch-zoom] [scrollbar-gutter:stable] pl-6 pr-[calc(var(--spacing)*6-var(--scrollbar-gutter,var(--scrollbar-w)))] pt-6 pb-6 [[data-slot$='-header']~&]:pt-3 [&:has(~[data-slot$='-footer'])]:pb-3 in-data-[slot=bottom-sheet-content]:pl-4 in-data-[slot=bottom-sheet-content]:pr-[calc(var(--spacing)*4-var(--scrollbar-gutter,var(--scrollbar-w)))] [[data-slot=bottom-sheet-handle]+&]:pt-1";
|
|
90
90
|
|
|
91
91
|
const MODAL_FOOTER =
|
|
92
92
|
"gh-stagger flex shrink-0 flex-col-reverse gap-2 px-6 pt-3 pb-6 sm:flex-row sm:justify-end in-data-[slot=bottom-sheet-content]:px-4";
|
|
@@ -143,10 +143,19 @@ export function createModalParts(slot: string): ModalParts {
|
|
|
143
143
|
* inside a sheet must yield the gesture to its own scroll, whichever family
|
|
144
144
|
* rendered it. */
|
|
145
145
|
const Body: Component<ModalBodyProps> = (props) => {
|
|
146
|
-
const [local, rest] = splitProps(props, ["class", "as"]);
|
|
146
|
+
const [local, rest] = splitProps(props, ["class", "as", "ref"]);
|
|
147
|
+
let el: HTMLElement | undefined;
|
|
148
|
+
// Overlay scrollbars reserve no gutter, so the right inset subtracts what the platform actually reserved.
|
|
149
|
+
onMount(() => {
|
|
150
|
+
if (el) el.style.setProperty("--scrollbar-gutter", `${el.offsetWidth - el.clientWidth}px`);
|
|
151
|
+
});
|
|
147
152
|
return (
|
|
148
153
|
<Dynamic
|
|
149
154
|
component={local.as ?? "div"}
|
|
155
|
+
ref={(node: HTMLElement) => {
|
|
156
|
+
el = node;
|
|
157
|
+
if (typeof local.ref === "function") local.ref(node as HTMLDivElement);
|
|
158
|
+
}}
|
|
150
159
|
data-slot={`${slot}-body`}
|
|
151
160
|
data-sheet-scroll=""
|
|
152
161
|
class={cn(MODAL_BODY, local.class)}
|
package/src/solid/index.ts
CHANGED
|
@@ -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
|
+
}
|