@domicile-desktop/manganese-page-requests 1.1.1 → 1.2.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,7 +1,9 @@
1
1
  {
2
2
  "dependencies": {
3
- "@domicile-desktop/component-library": "2.0.1",
4
- "@domicile-desktop/sdk": "2.1.0",
3
+ "@cprussin/option-result": "^2.0.1",
4
+ "@domicile-desktop/component-library": "2.1.0",
5
+ "@domicile-desktop/sdk": "2.2.0",
6
+ "@domicile-desktop/system-apps": "1.2.0",
5
7
  "@pandacss/dev": "^1.12.1",
6
8
  "@phosphor-icons/react": "^2.1.10",
7
9
  "react": "^19.2.8",
@@ -21,18 +23,26 @@
21
23
  "typescript": "^7.0.2"
22
24
  },
23
25
  "engines": {
24
- "bun": ">=1.4.2",
25
- "node": ">=24"
26
+ "bun": ">=1.4.2"
26
27
  },
27
28
  "exports": {
28
29
  "./DeviceAsk": "./src/DeviceAsk.tsx",
30
+ "./ExternalAppAsk": "./src/ExternalAppAsk.tsx",
29
31
  "./PageDialog": "./src/PageDialog.tsx",
30
32
  "./PermissionAsk": "./src/PermissionAsk.tsx",
33
+ "./PickerAsk": "./src/PickerAsk.tsx",
34
+ "./PromptAsk": "./src/PromptAsk.tsx",
31
35
  "./permission-names": "./src/permission-names.tsx",
36
+ "./SignInAsk": "./src/SignInAsk.tsx",
32
37
  "./site-permissions": "./src/site-permissions.ts",
33
38
  "./useDeviceRequest": "./src/useDeviceRequest.ts",
34
39
  "./useDialogRequest": "./src/useDialogRequest.ts",
35
- "./usePermissionRequest": "./src/usePermissionRequest.ts"
40
+ "./useExternalAppRequest": "./src/useExternalAppRequest.ts",
41
+ "./useLinkOpener": "./src/useLinkOpener.ts",
42
+ "./usePermissionRequest": "./src/usePermissionRequest.ts",
43
+ "./usePickerRequest": "./src/usePickerRequest.ts",
44
+ "./usePromptRequest": "./src/usePromptRequest.ts",
45
+ "./useSignInRequest": "./src/useSignInRequest.ts"
36
46
  },
37
47
  "files": [
38
48
  "src/**",
@@ -57,5 +67,5 @@
57
67
  "test:unit": "bun test"
58
68
  },
59
69
  "type": "module",
60
- "version": "1.1.1"
70
+ "version": "1.2.0"
61
71
  }
@@ -0,0 +1,112 @@
1
+ import type { Option } from "@cprussin/option-result";
2
+ import { Button } from "@domicile-desktop/component-library/Button";
3
+ import { Switch } from "@domicile-desktop/component-library/Switch";
4
+ import type { DescribedApp } from "@domicile-desktop/system-apps/describe-apps";
5
+ import { AppWindowIcon } from "@phosphor-icons/react/dist/ssr/AppWindow";
6
+ import type { RefCallback } from "react";
7
+ import { useState } from "react";
8
+
9
+ import { css } from "../styled-system/css";
10
+ import { PageModal } from "./PageModal";
11
+ import { hostOf } from "./site-host";
12
+ import type { ExternalAppRequest } from "./useExternalAppRequest";
13
+
14
+ /** The application's icon beside the title, as the app chooser draws it. */
15
+ const TITLE_ICON_SIZE = 32;
16
+
17
+ type Props = {
18
+ /** The application that opens the link; nothing when none does. */
19
+ app: Option<DescribedApp>;
20
+ /** Gets the element to focus: the answer button. */
21
+ ref?: RefCallback<HTMLElement> | undefined;
22
+ request: ExternalAppRequest;
23
+ };
24
+
25
+ /**
26
+ * Asks whether to open a link for an application, such as `zoommtg:`, over
27
+ * the page that opened it, as Chrome's "Open Zoom?" does, naming the
28
+ * application from its desktop entry. A site that may be remembered gets a
29
+ * switch to stop asking.
30
+ *
31
+ * A `PageModal`: Enter opens and Escape cancels. Takes no focus itself: the
32
+ * caller focuses `ref`'s element as its own focus rules allow.
33
+ */
34
+ export const ExternalAppAsk = ({ app, ref, request }: Props) => {
35
+ const [remember, setRemember] = useState(false);
36
+ const host = hostOf(request.origin);
37
+ return app.match({
38
+ None: () => (
39
+ <PageModal
40
+ footer={
41
+ <Button ref={ref} type="submit">
42
+ Close
43
+ </Button>
44
+ }
45
+ message={`Nothing on this computer opens ${schemeOf(request.url)} links.`}
46
+ onDismiss={request.dismiss}
47
+ onSubmit={request.dismiss}
48
+ title="No app opens this link"
49
+ />
50
+ ),
51
+ Some: ({ icon, name }) => (
52
+ <PageModal
53
+ footer={
54
+ <>
55
+ <Button onClick={request.dismiss} variant="outline">
56
+ Cancel
57
+ </Button>
58
+ <Button ref={ref} type="submit" variant="accent">
59
+ {`Open ${name}`}
60
+ </Button>
61
+ </>
62
+ }
63
+ message={`${host ?? "This page"} wants to open this link in ${name}.`}
64
+ onDismiss={request.dismiss}
65
+ onSubmit={() => {
66
+ request.open(remember);
67
+ }}
68
+ title={
69
+ <>
70
+ {icon === undefined ? (
71
+ <AppWindowIcon size={TITLE_ICON_SIZE} />
72
+ ) : (
73
+ <img
74
+ alt=""
75
+ className={iconStyles}
76
+ height={TITLE_ICON_SIZE}
77
+ src={icon}
78
+ width={TITLE_ICON_SIZE}
79
+ />
80
+ )}
81
+ {`Open ${name}?`}
82
+ </>
83
+ }
84
+ >
85
+ <p className={addressStyles}>{request.url}</p>
86
+ {request.mayRemember && host !== undefined && (
87
+ <Switch
88
+ checked={remember}
89
+ label={`Always let ${host} open these links in ${name}`}
90
+ onCheckedChange={setRemember}
91
+ />
92
+ )}
93
+ </PageModal>
94
+ ),
95
+ });
96
+ };
97
+
98
+ /** The link's scheme, as people name its links: `zoommtg`. */
99
+ const schemeOf = (url: string): string => url.slice(0, url.indexOf(":"));
100
+
101
+ const iconStyles = css({ flexShrink: 0, objectFit: "contain" });
102
+
103
+ // The link on one line, as the app chooser shows the address it opens.
104
+ const addressStyles = css({
105
+ color: "muted",
106
+ fontFamily: "mono",
107
+ fontSize: "xs",
108
+ margin: 0,
109
+ overflow: "hidden",
110
+ textOverflow: "ellipsis",
111
+ whiteSpace: "nowrap",
112
+ });
@@ -1,10 +1,10 @@
1
1
  import { Button } from "@domicile-desktop/component-library/Button";
2
2
  import { Input } from "@domicile-desktop/component-library/Input";
3
- import type { KeyboardEvent, RefCallback } from "react";
3
+ import type { RefCallback } from "react";
4
4
  import { useId, useState } from "react";
5
5
 
6
- import { css } from "../styled-system/css";
7
- import { center, flex } from "../styled-system/patterns";
6
+ import { PageModal } from "./PageModal";
7
+ import { hostOf } from "./site-host";
8
8
  import type { DialogRequest } from "./useDialogRequest";
9
9
 
10
10
  type Props = {
@@ -22,55 +22,17 @@ type Props = {
22
22
  * A page's `alert()`, `confirm()`, `prompt()` or `beforeunload`, over the page
23
23
  * it pauses.
24
24
  *
25
- * Covers only its page, as Chrome's tab-modal dialogs do: the rest of the
26
- * desktop stays usable. Styled as the library's `ModalDialog` popup. Enter is
27
- * OK and Escape is Cancel.
28
- *
29
- * Takes no focus itself: the caller focuses `ref`'s element as its own focus
30
- * rules allow.
25
+ * A `PageModal`: Enter is OK and Escape is Cancel. Takes no focus itself: the
26
+ * caller focuses `ref`'s element as its own focus rules allow.
31
27
  */
32
28
  export const PageDialog = ({ asker, ref, request }: Props) => {
33
- const titleId = useId();
34
- const messageId = useId();
35
29
  const [value, setValue] = useState(request.defaultValue);
30
+ const messageId = useId();
36
31
  const words = wordsFor(request, asker ?? hostOf(request.origin));
37
32
  return (
38
- <div className={scrimStyles}>
39
- {/* biome-ignore lint/a11y/noNoninteractiveElementInteractions: Escape cancels the dialog wherever focus is in it, as base-ui's dialogs do */}
40
- <form
41
- aria-describedby={messageId}
42
- aria-labelledby={titleId}
43
- aria-modal="true"
44
- className={popupStyles}
45
- onKeyDown={(event: KeyboardEvent) => {
46
- if (event.key === "Escape") {
47
- event.preventDefault();
48
- request.dismiss();
49
- }
50
- }}
51
- onSubmit={(event) => {
52
- event.preventDefault();
53
- request.accept(request.type === "prompt" ? value : undefined);
54
- }}
55
- role="alertdialog"
56
- >
57
- <h2 className={titleStyles} id={titleId}>
58
- {words.title}
59
- </h2>
60
- <p className={messageStyles} id={messageId}>
61
- {words.message}
62
- </p>
63
- {request.type === "prompt" && (
64
- <Input
65
- aria-labelledby={messageId}
66
- onChange={(event) => {
67
- setValue(event.target.value);
68
- }}
69
- ref={ref}
70
- value={value}
71
- />
72
- )}
73
- <footer className={footerStyles}>
33
+ <PageModal
34
+ footer={
35
+ <>
74
36
  {request.type !== "alert" && (
75
37
  <Button onClick={request.dismiss} variant="outline">
76
38
  Cancel
@@ -83,9 +45,26 @@ export const PageDialog = ({ asker, ref, request }: Props) => {
83
45
  >
84
46
  {words.accept}
85
47
  </Button>
86
- </footer>
87
- </form>
88
- </div>
48
+ </>
49
+ }
50
+ message={<span id={messageId}>{words.message}</span>}
51
+ onDismiss={request.dismiss}
52
+ onSubmit={() => {
53
+ request.accept(request.type === "prompt" ? value : undefined);
54
+ }}
55
+ title={words.title}
56
+ >
57
+ {request.type === "prompt" && (
58
+ <Input
59
+ aria-labelledby={messageId}
60
+ onChange={(event) => {
61
+ setValue(event.target.value);
62
+ }}
63
+ ref={ref}
64
+ value={value}
65
+ />
66
+ )}
67
+ </PageModal>
89
68
  );
90
69
  };
91
70
 
@@ -111,72 +90,3 @@ const wordsFor = (
111
90
  };
112
91
  }
113
92
  };
114
-
115
- /**
116
- * The host of `origin`, or nothing for a frame with no site (`null`).
117
- *
118
- * Uses `URL.parse`, which does not throw, for `ConnectionIndicator`'s reason.
119
- */
120
- const hostOf = (origin: string): string | undefined => {
121
- const parsed = URL.parse(origin);
122
- return parsed === null || parsed.host === "" ? undefined : parsed.host;
123
- };
124
-
125
- // Dims the page it pauses, as `ModalDialog`'s backdrop dims the screen.
126
- const scrimStyles = center({
127
- _starting: { opacity: 0 },
128
- alignItems: "flex-start",
129
- backgroundColor: "backdrop",
130
- inset: 0,
131
- opacity: 1,
132
- paddingBlockStart: 10,
133
- paddingInline: 4,
134
- position: "absolute",
135
- transition: "opacity {durations.normal} {easings.out}",
136
- });
137
-
138
- // `ModalDialog`'s popup, at the top of the page as Chrome places these.
139
- const popupStyles = flex({
140
- _starting: {
141
- opacity: 0,
142
- transform: "translateY({spacing.3}) scale(0.96)",
143
- },
144
- backgroundColor: "card",
145
- border: "1px solid {colors.border}",
146
- borderRadius: "lg",
147
- boxShadow: "modal",
148
- direction: "column",
149
- gap: 4,
150
- inlineSize: "min({spacing.120}, 100%)",
151
- opacity: 1,
152
- outlineStyle: "none",
153
- padding: 5,
154
- transform: "translateY(0) scale(1)",
155
- transition:
156
- "opacity {durations.normal} {easings.out}, transform {durations.normal} {easings.out}",
157
- });
158
-
159
- const titleStyles = css({
160
- color: "foreground",
161
- fontSize: "md",
162
- fontWeight: "semibold",
163
- lineHeight: "snug",
164
- margin: 0,
165
- overflowWrap: "anywhere",
166
- });
167
-
168
- // A page's message keeps its line breaks; a long one scrolls.
169
- const messageStyles = css({
170
- color: "foreground",
171
- fontSize: "sm",
172
- margin: 0,
173
- maxBlockSize: 80,
174
- overflowWrap: "anywhere",
175
- overflowY: "auto",
176
- whiteSpace: "pre-wrap",
177
- });
178
-
179
- const footerStyles = flex({
180
- gap: 2,
181
- justifyContent: "flex-end",
182
- });
@@ -0,0 +1,133 @@
1
+ import type { KeyboardEvent, ReactNode } from "react";
2
+ import { useId } from "react";
3
+
4
+ import { css } from "../styled-system/css";
5
+ import { center, flex } from "../styled-system/patterns";
6
+
7
+ type Props = {
8
+ /** The question, which names the dialog. */
9
+ title: ReactNode;
10
+ /** The text under it, which describes the dialog. */
11
+ message: ReactNode;
12
+ /** Anything between the text and the buttons. */
13
+ children?: ReactNode;
14
+ /** The buttons; a `submit` one answers yes. */
15
+ footer: ReactNode;
16
+ /** Escape, as Cancel. */
17
+ onDismiss: () => void;
18
+ /** Enter or the `submit` button. */
19
+ onSubmit: () => void;
20
+ };
21
+
22
+ /**
23
+ * A question over the page it is about, as Chrome's tab-modal dialogs are:
24
+ * the rest of the desktop stays usable. Styled as the library's `ModalDialog`
25
+ * popup. Enter submits and Escape dismisses.
26
+ *
27
+ * Takes no focus itself: the caller focuses an element in it as its own focus
28
+ * rules allow.
29
+ */
30
+ export const PageModal = ({
31
+ children,
32
+ footer,
33
+ message,
34
+ onDismiss,
35
+ onSubmit,
36
+ title,
37
+ }: Props) => {
38
+ const titleId = useId();
39
+ const messageId = useId();
40
+ return (
41
+ <div className={scrimStyles}>
42
+ {/* biome-ignore lint/a11y/noNoninteractiveElementInteractions: Escape cancels the dialog wherever focus is in it, as base-ui's dialogs do */}
43
+ <form
44
+ aria-describedby={messageId}
45
+ aria-labelledby={titleId}
46
+ aria-modal="true"
47
+ className={popupStyles}
48
+ onKeyDown={(event: KeyboardEvent) => {
49
+ if (event.key === "Escape") {
50
+ event.preventDefault();
51
+ onDismiss();
52
+ }
53
+ }}
54
+ onSubmit={(event) => {
55
+ event.preventDefault();
56
+ onSubmit();
57
+ }}
58
+ role="alertdialog"
59
+ >
60
+ <h2 className={titleStyles} id={titleId}>
61
+ {title}
62
+ </h2>
63
+ <p className={messageStyles} id={messageId}>
64
+ {message}
65
+ </p>
66
+ {children}
67
+ <footer className={footerStyles}>{footer}</footer>
68
+ </form>
69
+ </div>
70
+ );
71
+ };
72
+
73
+ // Dims the page it pauses, as `ModalDialog`'s backdrop dims the screen.
74
+ const scrimStyles = center({
75
+ _starting: { opacity: 0 },
76
+ alignItems: "flex-start",
77
+ backgroundColor: "backdrop",
78
+ inset: 0,
79
+ opacity: 1,
80
+ paddingBlockStart: 10,
81
+ paddingInline: 4,
82
+ position: "absolute",
83
+ transition: "opacity {durations.normal} {easings.out}",
84
+ });
85
+
86
+ // `ModalDialog`'s popup, at the top of the page as Chrome places these.
87
+ const popupStyles = flex({
88
+ _starting: {
89
+ opacity: 0,
90
+ transform: "translateY({spacing.3}) scale(0.96)",
91
+ },
92
+ backgroundColor: "card",
93
+ border: "1px solid {colors.border}",
94
+ borderRadius: "lg",
95
+ boxShadow: "modal",
96
+ direction: "column",
97
+ gap: 4,
98
+ inlineSize: "min({spacing.120}, 100%)",
99
+ opacity: 1,
100
+ outlineStyle: "none",
101
+ padding: 5,
102
+ transform: "translateY(0) scale(1)",
103
+ transition:
104
+ "opacity {durations.normal} {easings.out}, transform {durations.normal} {easings.out}",
105
+ });
106
+
107
+ const titleStyles = css({
108
+ alignItems: "center",
109
+ color: "foreground",
110
+ display: "flex",
111
+ fontSize: "md",
112
+ fontWeight: "semibold",
113
+ gap: 3,
114
+ lineHeight: "snug",
115
+ margin: 0,
116
+ overflowWrap: "anywhere",
117
+ });
118
+
119
+ // A page's message keeps its line breaks; a long one scrolls.
120
+ const messageStyles = css({
121
+ color: "foreground",
122
+ fontSize: "sm",
123
+ margin: 0,
124
+ maxBlockSize: 80,
125
+ overflowWrap: "anywhere",
126
+ overflowY: "auto",
127
+ whiteSpace: "pre-wrap",
128
+ });
129
+
130
+ const footerStyles = flex({
131
+ gap: 2,
132
+ justifyContent: "flex-end",
133
+ });