@protobase/ui 0.55.4 → 0.55.5

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,13 @@
1
+ //#region src/chat/composer-files.ts
2
+ /** Whether a drag carries files, which the composer takes, rather than text, which the box takes as usual. */
3
+ const carriesFiles = (transfer) => transfer.types.includes("Files");
4
+ /**
5
+ * The files a paste gives, or none when it also holds text: a copy from a spreadsheet or a web page can hold an image
6
+ * of its text too, and text pasted into the box stays text.
7
+ */
8
+ const pastedFiles = (transfer) => {
9
+ if (transfer.types.includes("text/plain")) return [];
10
+ return [...transfer.files];
11
+ };
12
+ //#endregion
13
+ export { carriesFiles, pastedFiles };
@@ -0,0 +1,8 @@
1
+ //#region src/chat/composer-send.ts
2
+ /** Whether the composer may send `message`, its trimmed text: never while busy, and empty text only when allowed, such as for a message of files alone. */
3
+ const canSend = (message, { busy, canSendEmpty }) => {
4
+ if (busy) return false;
5
+ return message !== "" || canSendEmpty === true;
6
+ };
7
+ //#endregion
8
+ export { canSend };
@@ -1,6 +1,6 @@
1
1
  import { PeopleSearch } from "../mentions/use-people-search.js";
2
2
  import "../mentions/index.js";
3
- import { Ref } from "react";
3
+ import { ReactNode, Ref } from "react";
4
4
  //#region src/chat/composer.d.ts
5
5
  export type ComposerProps = {
6
6
  /** Gets the trimmed text and the ids of the people it mentions, each once. */
@@ -11,10 +11,19 @@ export type ComposerProps = {
11
11
  inputRef?: Ref<HTMLTextAreaElement>;
12
12
  /** Finds the people `@` lists by the name typed after it, such as a channel's members; without it, `@` is plain text. */
13
13
  searchPeople?: PeopleSearch;
14
+ /** Drawn inside the composer above the box, such as a tray of the files picked for the message. */
15
+ attachments?: ReactNode;
16
+ /** Drawn left of the box, such as a button that picks files. */
17
+ actions?: ReactNode;
18
+ /** Enter and the button send even with no text, `onSend('', [])`, such as when files are picked. */
19
+ canSendEmpty?: boolean;
20
+ /** Gets the files pasted into the box or dropped on the composer; the composer itself uploads and sends nothing. A paste that holds text stays text. */
21
+ onFiles?: (files: File[]) => void;
14
22
  };
15
23
  /**
16
24
  * The text box under a chat, which grows with its text. Enter or the button sends the trimmed text and clears the box,
17
- * Shift+Enter starts a new line; while the list of people to mention is open, Enter picks from it instead.
25
+ * Shift+Enter starts a new line; while the list of people to mention is open, Enter picks from it instead. With `canSendEmpty`,
26
+ * such as while files are picked, it sends with no text too.
18
27
  */
19
- export declare const Composer: ({ onSend, busy, placeholder, inputRef, searchPeople }: ComposerProps) => import("react").JSX.Element;
28
+ export declare const Composer: ({ onSend, busy, placeholder, inputRef, searchPeople, attachments, actions, canSendEmpty, onFiles }: ComposerProps) => import("react").JSX.Element;
20
29
  //#endregion
@@ -2,19 +2,26 @@ import { Button } from "../primitives/button.js";
2
2
  import { text } from "./messages.js";
3
3
  import { mentionedIds } from "../mentions/mention-text.js";
4
4
  import { MentionBox, emptyDraft } from "../mentions/mention-box.js";
5
+ import { carriesFiles, pastedFiles } from "./composer-files.js";
6
+ import { canSend } from "./composer-send.js";
5
7
  import { useState } from "react";
6
8
  import { SendHorizontal } from "lucide-react";
7
9
  import { jsx, jsxs } from "react/jsx-runtime";
8
10
  //#region src/chat/composer.tsx
9
11
  /**
10
12
  * The text box under a chat, which grows with its text. Enter or the button sends the trimmed text and clears the box,
11
- * Shift+Enter starts a new line; while the list of people to mention is open, Enter picks from it instead.
13
+ * Shift+Enter starts a new line; while the list of people to mention is open, Enter picks from it instead. With `canSendEmpty`,
14
+ * such as while files are picked, it sends with no text too.
12
15
  */
13
- const Composer = ({ onSend, busy, placeholder, inputRef, searchPeople }) => {
16
+ const Composer = ({ onSend, busy, placeholder, inputRef, searchPeople, attachments, actions, canSendEmpty, onFiles }) => {
14
17
  const [draft, setDraft] = useState(emptyDraft);
15
18
  const message = draft.text.trim();
19
+ const sendable = canSend(message, {
20
+ busy,
21
+ canSendEmpty
22
+ });
16
23
  const send = () => {
17
- if (!message || busy) return;
24
+ if (!sendable) return;
18
25
  onSend?.(message, mentionedIds(message, draft.picked));
19
26
  setDraft(emptyDraft);
20
27
  };
@@ -23,33 +30,60 @@ const Composer = ({ onSend, busy, placeholder, inputRef, searchPeople }) => {
23
30
  event.preventDefault();
24
31
  send();
25
32
  };
33
+ const onPaste = (event) => {
34
+ if (!onFiles) return;
35
+ const files = pastedFiles(event.clipboardData);
36
+ if (files.length === 0) return;
37
+ event.preventDefault();
38
+ onFiles(files);
39
+ };
40
+ const onDragOver = (event) => {
41
+ if (!onFiles || !carriesFiles(event.dataTransfer)) return;
42
+ event.preventDefault();
43
+ };
44
+ const onDrop = (event) => {
45
+ if (!onFiles || !carriesFiles(event.dataTransfer)) return;
46
+ event.preventDefault();
47
+ onFiles([...event.dataTransfer.files]);
48
+ };
26
49
  return /* @__PURE__ */ jsxs("form", {
27
- className: "flex items-end gap-2",
50
+ className: "flex flex-wrap items-end gap-2",
28
51
  onSubmit: (event) => {
29
52
  event.preventDefault();
30
53
  send();
31
54
  },
32
- children: [/* @__PURE__ */ jsx("div", {
33
- className: "min-w-0 flex-1",
34
- children: /* @__PURE__ */ jsx(MentionBox, {
35
- ref: inputRef,
36
- rows: 1,
37
- "aria-label": text("message"),
38
- placeholder,
39
- value: draft,
40
- onChange: setDraft,
41
- searchPeople,
42
- listAt: "above",
43
- onKeyDown
55
+ onDragOver,
56
+ onDrop,
57
+ children: [
58
+ attachments && /* @__PURE__ */ jsx("div", {
59
+ className: "basis-full",
60
+ children: attachments
61
+ }),
62
+ actions,
63
+ /* @__PURE__ */ jsx("div", {
64
+ className: "min-w-0 flex-1",
65
+ children: /* @__PURE__ */ jsx(MentionBox, {
66
+ ref: inputRef,
67
+ rows: 1,
68
+ "aria-label": text("message"),
69
+ placeholder,
70
+ value: draft,
71
+ onChange: setDraft,
72
+ searchPeople,
73
+ listAt: "above",
74
+ onKeyDown,
75
+ onPaste
76
+ })
77
+ }),
78
+ /* @__PURE__ */ jsx(Button, {
79
+ type: "submit",
80
+ variant: "primary",
81
+ className: "w-8 px-0",
82
+ "aria-label": text("send"),
83
+ disabled: !sendable,
84
+ children: /* @__PURE__ */ jsx(SendHorizontal, { className: "size-4" })
44
85
  })
45
- }), /* @__PURE__ */ jsx(Button, {
46
- type: "submit",
47
- variant: "primary",
48
- className: "w-8 px-0",
49
- "aria-label": text("send"),
50
- disabled: !message || busy,
51
- children: /* @__PURE__ */ jsx(SendHorizontal, { className: "size-4" })
52
- })]
86
+ ]
53
87
  });
54
88
  };
55
89
  //#endregion
package/dist/package.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "0.55.4";
2
+ var version = "0.55.5";
3
3
  var protobase = { "pluginApi": 1 };
4
4
  //#endregion
5
5
  export { protobase, version };