@supatype/ui 0.8.0 → 0.9.1

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.
Files changed (65) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/README.md +196 -0
  3. package/dist/Badge.d.ts +0 -1
  4. package/dist/Badge.js +0 -1
  5. package/dist/Button.d.ts +0 -1
  6. package/dist/Button.js +0 -1
  7. package/dist/Card.d.ts +0 -1
  8. package/dist/Card.js +0 -1
  9. package/dist/Checkbox.d.ts +7 -0
  10. package/dist/Checkbox.js +9 -0
  11. package/dist/Input.d.ts +0 -1
  12. package/dist/Input.js +5 -5
  13. package/dist/RichTextEditor.d.ts +23 -1
  14. package/dist/RichTextEditor.js +41 -32
  15. package/dist/Select.d.ts +7 -0
  16. package/dist/Select.js +9 -0
  17. package/dist/Skeleton.d.ts +0 -1
  18. package/dist/Skeleton.js +0 -1
  19. package/dist/Table.d.ts +12 -0
  20. package/dist/Table.js +16 -0
  21. package/dist/Textarea.d.ts +6 -0
  22. package/dist/Textarea.js +9 -0
  23. package/dist/ThemeProvider.d.ts +0 -1
  24. package/dist/ThemeProvider.js +0 -1
  25. package/dist/field.d.ts +45 -0
  26. package/dist/field.js +44 -0
  27. package/dist/index.d.ts +8 -3
  28. package/dist/index.js +6 -2
  29. package/dist/rich-text.d.ts +2 -0
  30. package/dist/rich-text.js +3 -0
  31. package/dist/richtext-utils.d.ts +0 -1
  32. package/dist/richtext-utils.js +0 -1
  33. package/dist/styles.css +167 -0
  34. package/package.json +41 -12
  35. package/.turbo/turbo-build.log +0 -4
  36. package/.turbo/turbo-typecheck.log +0 -4
  37. package/dist/Badge.d.ts.map +0 -1
  38. package/dist/Badge.js.map +0 -1
  39. package/dist/Button.d.ts.map +0 -1
  40. package/dist/Button.js.map +0 -1
  41. package/dist/Card.d.ts.map +0 -1
  42. package/dist/Card.js.map +0 -1
  43. package/dist/Input.d.ts.map +0 -1
  44. package/dist/Input.js.map +0 -1
  45. package/dist/RichTextEditor.d.ts.map +0 -1
  46. package/dist/RichTextEditor.js.map +0 -1
  47. package/dist/Skeleton.d.ts.map +0 -1
  48. package/dist/Skeleton.js.map +0 -1
  49. package/dist/ThemeProvider.d.ts.map +0 -1
  50. package/dist/ThemeProvider.js.map +0 -1
  51. package/dist/index.d.ts.map +0 -1
  52. package/dist/index.js.map +0 -1
  53. package/dist/richtext-utils.d.ts.map +0 -1
  54. package/dist/richtext-utils.js.map +0 -1
  55. package/src/Badge.tsx +0 -27
  56. package/src/Button.tsx +0 -36
  57. package/src/Card.tsx +0 -29
  58. package/src/Input.tsx +0 -37
  59. package/src/RichTextEditor.tsx +0 -206
  60. package/src/Skeleton.tsx +0 -11
  61. package/src/ThemeProvider.tsx +0 -72
  62. package/src/index.ts +0 -22
  63. package/src/richtext-utils.ts +0 -112
  64. package/tsconfig.json +0 -11
  65. package/tsconfig.tsbuildinfo +0 -1
package/CHANGELOG.md ADDED
@@ -0,0 +1,52 @@
1
+ # Changelog
2
+
3
+ ## Unreleased
4
+
5
+ ### Breaking Changes
6
+
7
+ * **rich-text:** `RichTextEditor` and `RichTextEditorProps` moved to `@supatype/ui/rich-text`.
8
+ Importing them from `@supatype/ui` fails; change the import to
9
+ `import { RichTextEditor } from "@supatype/ui/rich-text"`. The main entry no longer loads Lexical,
10
+ so an app that never renders the editor no longer needs the Lexical packages, which are now
11
+ optional peer dependencies. The rich text helpers (`emptyRichTextDocument`, `richTextIsEmpty`,
12
+ `stringToRichTextDocument`, `normalizeRichTextDefault`) and the `SerializedEditorState` types stay
13
+ in `@supatype/ui`.
14
+
15
+ ### Features
16
+
17
+ * **ui:** `Select`, `Textarea` and `Checkbox` form controls. Each wraps the native element, takes
18
+ `label` and `error` the way `Input` does, forwards its ref, and accepts the element's own props.
19
+ The label points at the control through an id from `React.useId()` unless you pass `id`, and an
20
+ `error` sets `aria-invalid` and adds the error text to `aria-describedby`.
21
+ * **ui:** `Table`, `THead`, `TBody`, `Tr`, `Th` and `Td`, styled parts that render the native table
22
+ elements, so screen readers get the table, row, column header and cell roles. They hold no data
23
+ logic: map your rows into them.
24
+
25
+ ### Bug Fixes
26
+
27
+ * **styles:** `@supatype/ui/styles.css` exists. The package exported it, but no build produced it,
28
+ so the import failed and the editor rendered unstyled unless an app copied the rules in by hand.
29
+ Import it once, before your own CSS, and theme the editor with the `--su-rt-*` custom properties
30
+ listed in the README.
31
+ * **rich-text:** text that is both underlined and struck through shows both lines. It showed only
32
+ the strikethrough.
33
+ * **rich-text:** headings and lists in the editor are styled. Headings had no rule for the classes
34
+ the editor puts on them, and lists lost their markers under Tailwind's reset.
35
+ * **rich-text:** `editable` takes effect when it changes, not only on mount. Changing it swapped
36
+ the toolbar but left the content as editable, or as locked, as it started.
37
+ * **rich-text:** quotes are styled. A pasted blockquote rendered with no rule of the package's,
38
+ and now sits behind a left border in the muted colour, from the `--su-rt-*` variables.
39
+ * **package:** the tarball ships `dist`, the README, the changelog and the license only, with no
40
+ source or declaration maps, and drops six Radix UI dependencies no component used.
41
+ * **input:** an `Input` with no `id` gets a unique one from `React.useId()`. It used the label as
42
+ a slug (`"Email address"` became `email-address`), so two inputs with the same label shared an
43
+ id and the second label focused the first input. If your code or tests find an input by that
44
+ slug, pass `id` explicitly. With an `error`, the input now sets `aria-invalid` and points
45
+ `aria-describedby` at the error text (id `<input id>-error`), keeping any `aria-describedby` you
46
+ pass.
47
+
48
+ ### Documentation
49
+
50
+ * **readme:** a README covering install, the Tailwind setup the components need (v4 `@source`, v3
51
+ `content`, class-based dark mode for `ThemeProvider`), the stylesheet, the exports and the
52
+ rich-text entry.
package/README.md ADDED
@@ -0,0 +1,196 @@
1
+ # `@supatype/ui`
2
+
3
+ React components for Supatype apps: `Button`, the form controls `Input`, `Select`, `Textarea` and
4
+ `Checkbox`, `Card`, `Badge`, `Table`, `Skeleton`, a `ThemeProvider`, and a Lexical rich text editor
5
+ that reads and writes the `RichText` JSON a Supatype schema stores.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install @supatype/ui
11
+ ```
12
+
13
+ React 18 or 19 is a peer dependency. The rich text editor also needs Lexical; install it only if
14
+ you use the editor (see [Rich text editor](#rich-text-editor)).
15
+
16
+ ## Styling: Tailwind is required
17
+
18
+ Every component except the rich text editor is styled with Tailwind utility classes from
19
+ the default palette (`bg-purple-600`, `text-neutral-700`, `dark:bg-neutral-900` and so on). The
20
+ package ships those class names in its JavaScript, not as compiled CSS, so your app's Tailwind
21
+ build has to see them. Without that step the components render unstyled.
22
+
23
+ ### Tailwind v4
24
+
25
+ Tailwind v4 does not scan `node_modules`, so register the package with `@source` in the CSS file
26
+ that imports Tailwind:
27
+
28
+ ```css
29
+ /* app/globals.css */
30
+ @import "tailwindcss";
31
+ @source "../node_modules/@supatype/ui/dist";
32
+ ```
33
+
34
+ The `@source` path is resolved relative to the CSS file it appears in, not to the project root.
35
+ From `app/globals.css` the `node_modules` folder is one level up, hence `../`; from
36
+ `src/styles/index.css` it would be `../../node_modules/@supatype/ui/dist`. In a monorepo, point it
37
+ at whichever `node_modules` actually holds the package.
38
+
39
+ ### Tailwind v3
40
+
41
+ Add the package to `content` in `tailwind.config.js`:
42
+
43
+ ```js
44
+ export default {
45
+ content: [
46
+ "./src/**/*.{ts,tsx}",
47
+ "./node_modules/@supatype/ui/dist/**/*.js",
48
+ ],
49
+ }
50
+ ```
51
+
52
+ ### Dark mode
53
+
54
+ `ThemeProvider` switches themes by toggling a `dark` class on `<html>`. Tailwind's `dark:` variant
55
+ has to follow that class rather than the operating system setting:
56
+
57
+ - v4: add `@custom-variant dark (&:where(.dark, .dark *));` after `@import "tailwindcss";`
58
+ - v3: set `darkMode: "class"` in `tailwind.config.js`
59
+
60
+ ## Stylesheet
61
+
62
+ Import the package stylesheet once, before your own CSS, so your rules can override it:
63
+
64
+ ```ts
65
+ import "@supatype/ui/styles.css"
66
+ ```
67
+
68
+ or, from CSS:
69
+
70
+ ```css
71
+ @import "@supatype/ui/styles.css";
72
+ ```
73
+
74
+ It holds the rich text editor's styles. The editor is themed with CSS custom properties, which you
75
+ can set on `:root` or on any ancestor of the editor:
76
+
77
+ | Property | Default | Used for |
78
+ |---|---|---|
79
+ | `--su-rt-border` | `#e5e7eb` | toolbar, surface and button borders |
80
+ | `--su-rt-border-width` | `1px` | toolbar and surface border width |
81
+ | `--su-rt-radius` | `6px` | corner radius (buttons use two thirds of it) |
82
+ | `--su-rt-bg` | `#ffffff` | editing surface |
83
+ | `--su-rt-toolbar-bg` | `#f9fafb` | toolbar strip |
84
+ | `--su-rt-button-bg` | `--su-rt-bg` | toolbar buttons |
85
+ | `--su-rt-fg` | inherited | toolbar button text |
86
+ | `--su-rt-muted` | `#6b7280` | placeholder text |
87
+ | `--su-rt-accent` | `#2563eb` | links and toolbar button hover |
88
+ | `--su-rt-min-height` | `8rem` | editing surface height |
89
+ | `--su-rt-pad-y`, `--su-rt-pad-x` | `0.75rem`, `1rem` | editing surface padding |
90
+
91
+ ## Exports
92
+
93
+ From `@supatype/ui`:
94
+
95
+ | Export | What it is |
96
+ |---|---|
97
+ | `Button`, `ButtonProps` | button with `variant` (`primary`, `secondary`, `ghost`, `danger`) and `size` (`sm`, `md`, `lg`) |
98
+ | `Input`, `InputProps` | text input with optional `label` and `error` |
99
+ | `Select`, `SelectProps` | native `<select>` with optional `label` and `error`; pass `<option>` children |
100
+ | `Textarea`, `TextareaProps` | multi-line text input with optional `label` and `error` |
101
+ | `Checkbox`, `CheckboxProps` | native checkbox with its `label` beside it and optional `error` |
102
+ | `Card`, `CardProps` | bordered container with `padding` (`sm`, `md`, `lg`) |
103
+ | `Badge`, `BadgeProps` | pill label with `variant` (`default`, `success`, `warning`, `danger`, `purple`) |
104
+ | `Table`, `THead`, `TBody`, `Tr`, `Th`, `Td` | styled native table parts; props `TableProps`, `TableSectionProps`, `TrProps`, `ThProps`, `TdProps` |
105
+ | `Skeleton` | pulsing loading placeholder |
106
+ | `ThemeProvider`, `useTheme` | light, dark or system theme, saved in `localStorage` |
107
+ | `emptyRichTextDocument` | an empty rich text document (one empty paragraph) |
108
+ | `stringToRichTextDocument` | plain text to a rich text document |
109
+ | `normalizeRichTextDefault` | turns a stored default (document, JSON string or plain string) into a document, or `null` |
110
+ | `richTextIsEmpty` | true when a document has no text |
111
+ | `SerializedEditorState`, `SerializedLexicalNode` | the rich text JSON types |
112
+
113
+ None of these import Lexical.
114
+
115
+ ## Form controls
116
+
117
+ `Input`, `Select`, `Textarea` and `Checkbox` wrap the native element, forward their ref to it and
118
+ accept all of its props, so they work with `value`/`onChange`, `defaultValue`, `name` and form
119
+ libraries that register through a ref. Each takes an optional `label` and `error`:
120
+
121
+ - With no `id`, the control gets a unique one from `React.useId()`, and the label points at it.
122
+ Pass `id` to choose your own.
123
+ - With an `error`, the control sets `aria-invalid` and adds the error text's id
124
+ (`<control id>-error`) to `aria-describedby`, keeping any `aria-describedby` you pass.
125
+
126
+ ```tsx
127
+ import { Checkbox, Input, Select, Textarea } from "@supatype/ui"
128
+
129
+ <Input label="Email" type="email" name="email" error={errors.email} />
130
+
131
+ <Select label="Role" name="role" defaultValue="member">
132
+ <option value="member">Member</option>
133
+ <option value="admin">Admin</option>
134
+ </Select>
135
+
136
+ <Textarea label="Bio" name="bio" rows={4} />
137
+
138
+ <Checkbox label="Send me product updates" name="updates" defaultChecked />
139
+ ```
140
+
141
+ `Select` is the browser's own `<select>`, so it keeps native keyboard handling, mobile pickers and
142
+ form submission. `Checkbox` puts its label to the right of the box; clicking either toggles it.
143
+
144
+ ## Table
145
+
146
+ `Table`, `THead`, `TBody`, `Tr`, `Th` and `Td` render `<table>`, `<thead>`, `<tbody>`, `<tr>`,
147
+ `<th>` and `<td>` with the package's borders and type scale, and accept each element's own props.
148
+ They fetch, sort and page nothing: map your rows into them.
149
+
150
+ ```tsx
151
+ import { Table, TBody, Td, Th, THead, Tr } from "@supatype/ui"
152
+
153
+ <Table aria-label="Users">
154
+ <THead>
155
+ <Tr>
156
+ <Th scope="col">Name</Th>
157
+ <Th scope="col">Email</Th>
158
+ </Tr>
159
+ </THead>
160
+ <TBody>
161
+ {users.map((user) => (
162
+ <Tr key={user.id}>
163
+ <Td>{user.name}</Td>
164
+ <Td>{user.email}</Td>
165
+ </Tr>
166
+ ))}
167
+ </TBody>
168
+ </Table>
169
+ ```
170
+
171
+ ## Rich text editor
172
+
173
+ `RichTextEditor` has its own entry point so that apps which never use it do not need Lexical:
174
+
175
+ ```bash
176
+ npm install lexical @lexical/react @lexical/rich-text @lexical/list @lexical/link @lexical/selection
177
+ ```
178
+
179
+ ```tsx
180
+ import { useState } from "react"
181
+ import "@supatype/ui/styles.css"
182
+ import { RichTextEditor } from "@supatype/ui/rich-text"
183
+ import { emptyRichTextDocument, type SerializedEditorState } from "@supatype/ui"
184
+
185
+ function BodyField(): React.ReactElement {
186
+ const [body, setBody] = useState<SerializedEditorState>(emptyRichTextDocument())
187
+ return <RichTextEditor value={body} onChange={setBody} placeholder="Write your post" />
188
+ }
189
+ ```
190
+
191
+ `value` is read once, on mount. To load a different document into a mounted editor, change
192
+ `documentKey`. Pass `editable={false}` to render a document read only, with no toolbar; `editable`
193
+ can change at any time.
194
+
195
+ The Lexical packages are optional peer dependencies: your package manager will not install or warn
196
+ about them, and importing `@supatype/ui/rich-text` without them fails at build time.
package/dist/Badge.d.ts CHANGED
@@ -3,4 +3,3 @@ export interface BadgeProps extends React.HTMLAttributes<HTMLSpanElement> {
3
3
  variant?: "default" | "success" | "warning" | "danger" | "purple";
4
4
  }
5
5
  export declare const Badge: React.FC<BadgeProps>;
6
- //# sourceMappingURL=Badge.d.ts.map
package/dist/Badge.js CHANGED
@@ -9,4 +9,3 @@ export const Badge = ({ variant = "default", className, children, ...props }) =>
9
9
  "bg-purple-100 text-purple-700 dark:bg-purple-900 dark:text-purple-300": variant === "purple",
10
10
  }, className), ...props, children: children }));
11
11
  };
12
- //# sourceMappingURL=Badge.js.map
package/dist/Button.d.ts CHANGED
@@ -4,4 +4,3 @@ export interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElemen
4
4
  size?: "sm" | "md" | "lg";
5
5
  }
6
6
  export declare const Button: React.ForwardRefExoticComponent<ButtonProps & React.RefAttributes<HTMLButtonElement>>;
7
- //# sourceMappingURL=Button.d.ts.map
package/dist/Button.js CHANGED
@@ -14,4 +14,3 @@ export const Button = React.forwardRef(({ variant = "primary", size = "md", clas
14
14
  }, className), ...props, children: children }));
15
15
  });
16
16
  Button.displayName = "Button";
17
- //# sourceMappingURL=Button.js.map
package/dist/Card.d.ts CHANGED
@@ -3,4 +3,3 @@ export interface CardProps extends React.HTMLAttributes<HTMLDivElement> {
3
3
  padding?: "sm" | "md" | "lg";
4
4
  }
5
5
  export declare const Card: React.ForwardRefExoticComponent<CardProps & React.RefAttributes<HTMLDivElement>>;
6
- //# sourceMappingURL=Card.d.ts.map
package/dist/Card.js CHANGED
@@ -9,4 +9,3 @@ export const Card = React.forwardRef(({ padding = "md", className, children, ...
9
9
  }, className), ...props, children: children }));
10
10
  });
11
11
  Card.displayName = "Card";
12
- //# sourceMappingURL=Card.js.map
@@ -0,0 +1,7 @@
1
+ import React from "react";
2
+ /** A native checkbox, so `checked`, `defaultChecked` and `onChange` work as they do on `<input>`. */
3
+ export interface CheckboxProps extends Omit<React.InputHTMLAttributes<HTMLInputElement>, "type"> {
4
+ label?: string;
5
+ error?: string;
6
+ }
7
+ export declare const Checkbox: React.ForwardRefExoticComponent<CheckboxProps & React.RefAttributes<HTMLInputElement>>;
@@ -0,0 +1,9 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import React from "react";
3
+ import { clsx } from "clsx";
4
+ import { FieldError, FieldLabel, useField } from "./field.js";
5
+ export const Checkbox = React.forwardRef(({ label, error, className, ...props }, ref) => {
6
+ const field = useField(props, { label, error });
7
+ return (_jsxs("div", { className: "flex flex-col gap-1.5", children: [_jsxs("div", { className: "flex items-center gap-2", children: [_jsx("input", { ref: ref, type: "checkbox", ...props, ...field.control, className: clsx("h-4 w-4 shrink-0 rounded accent-purple-600 disabled:opacity-50", "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-purple-500 focus-visible:ring-offset-2", error && "outline outline-1 outline-offset-1 outline-red-500", className) }), _jsx(FieldLabel, { field: field })] }), _jsx(FieldError, { field: field })] }));
8
+ });
9
+ Checkbox.displayName = "Checkbox";
package/dist/Input.d.ts CHANGED
@@ -4,4 +4,3 @@ export interface InputProps extends React.InputHTMLAttributes<HTMLInputElement>
4
4
  error?: string;
5
5
  }
6
6
  export declare const Input: React.ForwardRefExoticComponent<InputProps & React.RefAttributes<HTMLInputElement>>;
7
- //# sourceMappingURL=Input.d.ts.map
package/dist/Input.js CHANGED
@@ -1,9 +1,9 @@
1
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import React from "react";
3
3
  import { clsx } from "clsx";
4
- export const Input = React.forwardRef(({ label, error, className, id, ...props }, ref) => {
5
- const inputId = id ?? label?.toLowerCase().replace(/\s+/g, "-");
6
- return (_jsxs("div", { className: "flex flex-col gap-1.5", children: [label && (_jsx("label", { htmlFor: inputId, className: "text-sm font-medium text-neutral-700 dark:text-neutral-300", children: label })), _jsx("input", { ref: ref, id: inputId, className: clsx("h-10 rounded-lg border px-3 text-sm transition-colors", "border-neutral-300 bg-white text-neutral-900 placeholder:text-neutral-500", "dark:border-neutral-700 dark:bg-neutral-900 dark:text-neutral-100 dark:placeholder:text-neutral-500", "focus:border-purple-500 focus:outline-none focus:ring-1 focus:ring-purple-500", error && "border-red-500 focus:border-red-500 focus:ring-red-500", className), ...props }), error && _jsx("p", { className: "text-sm text-red-500", children: error })] }));
4
+ import { FIELD_ERROR, FIELD_FRAME, FieldLayout, useField } from "./field.js";
5
+ export const Input = React.forwardRef(({ label, error, className, ...props }, ref) => {
6
+ const field = useField(props, { label, error });
7
+ return (_jsx(FieldLayout, { field: field, children: _jsx("input", { ref: ref, ...props, ...field.control, className: clsx("h-10 px-3", FIELD_FRAME, error && FIELD_ERROR, className) }) }));
7
8
  });
8
9
  Input.displayName = "Input";
9
- //# sourceMappingURL=Input.js.map
@@ -1,5 +1,28 @@
1
1
  import React from "react";
2
2
  import type { SerializedEditorState } from "@supatype/types/lexical";
3
+ /** Class names Lexical puts on content nodes. Exported for the package test, not from an entry. */
4
+ export declare const EDITOR_THEME: {
5
+ heading: {
6
+ h1: string;
7
+ h2: string;
8
+ h3: string;
9
+ };
10
+ text: {
11
+ bold: string;
12
+ italic: string;
13
+ underline: string;
14
+ strikethrough: string;
15
+ underlineStrikethrough: string;
16
+ };
17
+ list: {
18
+ ul: string;
19
+ ol: string;
20
+ listitem: string;
21
+ };
22
+ link: string;
23
+ paragraph: string;
24
+ quote: string;
25
+ };
3
26
  export interface RichTextEditorProps {
4
27
  /** Initial document (Lexical JSON). Updates only on mount; use `documentKey` to reload. */
5
28
  value?: SerializedEditorState | null | undefined;
@@ -18,4 +41,3 @@ export interface RichTextEditorProps {
18
41
  * Peer dependencies: `lexical`, `@lexical/react`, `@lexical/rich-text`, `@lexical/list`, `@lexical/link`, `@lexical/selection`.
19
42
  */
20
43
  export declare function RichTextEditor({ value, onChange, placeholder, className, editable, documentKey, contentEditableId, }: RichTextEditorProps): React.ReactElement;
21
- //# sourceMappingURL=RichTextEditor.d.ts.map
@@ -10,49 +10,59 @@ import { ListPlugin } from "@lexical/react/LexicalListPlugin";
10
10
  import { LinkPlugin } from "@lexical/react/LexicalLinkPlugin";
11
11
  import { LexicalErrorBoundary } from "@lexical/react/LexicalErrorBoundary";
12
12
  import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
13
- import { $getSelection, $isRangeSelection, FORMAT_TEXT_COMMAND, } from "lexical";
13
+ import { $createParagraphNode, $getRoot, $getSelection, $isRangeSelection, FORMAT_TEXT_COMMAND, } from "lexical";
14
14
  import { $createHeadingNode, HeadingNode, QuoteNode } from "@lexical/rich-text";
15
15
  import { ListNode, ListItemNode, INSERT_ORDERED_LIST_COMMAND, INSERT_UNORDERED_LIST_COMMAND } from "@lexical/list";
16
16
  import { AutoLinkNode, LinkNode } from "@lexical/link";
17
17
  import { $setBlocksType } from "@lexical/selection";
18
- import { $createParagraphNode, $getRoot } from "lexical";
19
18
  import clsx from "clsx";
20
19
  const EDITOR_NODES = [HeadingNode, QuoteNode, ListNode, ListItemNode, LinkNode, AutoLinkNode];
21
- const EDITOR_THEME = {
20
+ /** Class names Lexical puts on content nodes. Exported for the package test, not from an entry. */
21
+ export const EDITOR_THEME = {
22
22
  heading: { h1: "su-rt-h1", h2: "su-rt-h2", h3: "su-rt-h3" },
23
- text: { bold: "su-rt-bold", italic: "su-rt-italic", underline: "su-rt-underline", strikethrough: "su-rt-strikethrough" },
23
+ text: {
24
+ bold: "su-rt-bold",
25
+ italic: "su-rt-italic",
26
+ underline: "su-rt-underline",
27
+ strikethrough: "su-rt-strikethrough",
28
+ underlineStrikethrough: "su-rt-underline-strikethrough",
29
+ },
24
30
  list: { ul: "su-rt-ul", ol: "su-rt-ol", listitem: "su-rt-li" },
25
31
  link: "su-rt-link",
26
32
  paragraph: "su-rt-p",
33
+ quote: "su-rt-quote",
27
34
  };
35
+ /**
36
+ * A toolbar button that acts on mouse down, with the default prevented so the editor keeps its
37
+ * selection: a click would move focus to the button first and leave nothing selected to format.
38
+ */
39
+ function ToolbarButton({ title, onPress, children, }) {
40
+ return (_jsx("button", { type: "button", title: title, onMouseDown: (e) => { e.preventDefault(); onPress(); }, children: children }));
41
+ }
42
+ /** Turn the selected blocks into the block `create` makes. */
43
+ function setBlocks(editor, create) {
44
+ editor.update(() => {
45
+ const selection = $getSelection();
46
+ if ($isRangeSelection(selection))
47
+ $setBlocksType(selection, create);
48
+ });
49
+ }
28
50
  function ToolbarPlugin() {
29
51
  const [editor] = useLexicalComposerContext();
30
- const format = useCallback((fmt) => {
31
- editor.dispatchCommand(FORMAT_TEXT_COMMAND, fmt);
32
- }, [editor]);
33
- const setHeading = useCallback((tag) => {
34
- editor.update(() => {
35
- const selection = $getSelection();
36
- if ($isRangeSelection(selection)) {
37
- $setBlocksType(selection, () => $createHeadingNode(tag));
38
- }
39
- });
40
- }, [editor]);
41
- const setParagraph = useCallback(() => {
42
- editor.update(() => {
43
- const selection = $getSelection();
44
- if ($isRangeSelection(selection)) {
45
- $setBlocksType(selection, () => $createParagraphNode());
46
- }
47
- });
48
- }, [editor]);
49
- return (_jsxs("div", { className: "su-richtext-toolbar", role: "toolbar", "aria-label": "Formatting", children: [_jsx("button", { type: "button", title: "Bold", onMouseDown: (e) => { e.preventDefault(); format("bold"); }, children: "B" }), _jsx("button", { type: "button", title: "Italic", onMouseDown: (e) => { e.preventDefault(); format("italic"); }, children: "I" }), _jsx("button", { type: "button", title: "Underline", onMouseDown: (e) => { e.preventDefault(); format("underline"); }, children: "U" }), _jsx("span", { className: "su-richtext-toolbar-divider", "aria-hidden": true }), _jsx("button", { type: "button", title: "Heading 1", onMouseDown: (e) => { e.preventDefault(); setHeading("h1"); }, children: "H1" }), _jsx("button", { type: "button", title: "Heading 2", onMouseDown: (e) => { e.preventDefault(); setHeading("h2"); }, children: "H2" }), _jsx("button", { type: "button", title: "Paragraph", onMouseDown: (e) => { e.preventDefault(); setParagraph(); }, children: "\u00B6" }), _jsx("span", { className: "su-richtext-toolbar-divider", "aria-hidden": true }), _jsx("button", { type: "button", title: "Bullet list", onMouseDown: (e) => {
50
- e.preventDefault();
51
- editor.dispatchCommand(INSERT_UNORDERED_LIST_COMMAND, undefined);
52
- }, children: "\u2022" }), _jsx("button", { type: "button", title: "Numbered list", onMouseDown: (e) => {
53
- e.preventDefault();
54
- editor.dispatchCommand(INSERT_ORDERED_LIST_COMMAND, undefined);
55
- }, children: "1." })] }));
52
+ const format = useCallback((fmt) => editor.dispatchCommand(FORMAT_TEXT_COMMAND, fmt), [editor]);
53
+ const heading = useCallback((tag) => setBlocks(editor, () => $createHeadingNode(tag)), [editor]);
54
+ return (_jsxs("div", { className: "su-richtext-toolbar", role: "toolbar", "aria-label": "Formatting", children: [_jsx(ToolbarButton, { title: "Bold", onPress: () => format("bold"), children: "B" }), _jsx(ToolbarButton, { title: "Italic", onPress: () => format("italic"), children: "I" }), _jsx(ToolbarButton, { title: "Underline", onPress: () => format("underline"), children: "U" }), _jsx("span", { className: "su-richtext-toolbar-divider", "aria-hidden": true }), _jsx(ToolbarButton, { title: "Heading 1", onPress: () => heading("h1"), children: "H1" }), _jsx(ToolbarButton, { title: "Heading 2", onPress: () => heading("h2"), children: "H2" }), _jsx(ToolbarButton, { title: "Paragraph", onPress: () => setBlocks(editor, () => $createParagraphNode()), children: "\u00B6" }), _jsx("span", { className: "su-richtext-toolbar-divider", "aria-hidden": true }), _jsx(ToolbarButton, { title: "Bullet list", onPress: () => editor.dispatchCommand(INSERT_UNORDERED_LIST_COMMAND, undefined), children: "\u2022" }), _jsx(ToolbarButton, { title: "Numbered list", onPress: () => editor.dispatchCommand(INSERT_ORDERED_LIST_COMMAND, undefined), children: "1." })] }));
55
+ }
56
+ /**
57
+ * Keeps the editor's editable state on the `editable` prop. Lexical reads `initialConfig` on mount
58
+ * only, so a change to the prop after it swapped the toolbar but left the content as it was.
59
+ */
60
+ function EditablePlugin({ editable }) {
61
+ const [editor] = useLexicalComposerContext();
62
+ useEffect(() => {
63
+ editor.setEditable(editable);
64
+ }, [editor, editable]);
65
+ return null;
56
66
  }
57
67
  function InitialStatePlugin({ value }) {
58
68
  const [editor] = useLexicalComposerContext();
@@ -95,6 +105,5 @@ export function RichTextEditor({ value, onChange, placeholder = "Write something
95
105
  const handleChange = useCallback((editorState, _editor) => {
96
106
  onChange(editorState.toJSON());
97
107
  }, [onChange]);
98
- return (_jsx("div", { className: clsx("su-richtext", className, !editable && "su-richtext-readonly"), children: _jsxs(LexicalComposer, { initialConfig: initialConfig, children: [editable ? _jsx(ToolbarPlugin, {}) : null, _jsx(InitialStatePlugin, { value: value }), _jsx("div", { className: "su-richtext-surface", children: _jsx(RichTextPlugin, { contentEditable: _jsx(ContentEditable, { id: contentEditableId, className: "su-richtext-editable", "aria-label": placeholder }), placeholder: _jsx("div", { className: "su-richtext-placeholder", children: placeholder }), ErrorBoundary: LexicalErrorBoundary }) }), _jsx(HistoryPlugin, {}), _jsx(ListPlugin, {}), _jsx(LinkPlugin, {}), _jsx(OnChangePlugin, { onChange: handleChange, ignoreSelectionChange: true })] }, documentKey) }));
108
+ return (_jsx("div", { className: clsx("su-richtext", className, !editable && "su-richtext-readonly"), children: _jsxs(LexicalComposer, { initialConfig: initialConfig, children: [editable ? _jsx(ToolbarPlugin, {}) : null, _jsx(EditablePlugin, { editable: editable }), _jsx(InitialStatePlugin, { value: value }), _jsx("div", { className: "su-richtext-surface", children: _jsx(RichTextPlugin, { contentEditable: _jsx(ContentEditable, { id: contentEditableId, className: "su-richtext-editable", "aria-label": placeholder }), placeholder: _jsx("div", { className: "su-richtext-placeholder", children: placeholder }), ErrorBoundary: LexicalErrorBoundary }) }), _jsx(HistoryPlugin, {}), _jsx(ListPlugin, {}), _jsx(LinkPlugin, {}), _jsx(OnChangePlugin, { onChange: handleChange, ignoreSelectionChange: true })] }, documentKey) }));
99
109
  }
100
- //# sourceMappingURL=RichTextEditor.js.map
@@ -0,0 +1,7 @@
1
+ import React from "react";
2
+ /** A native `<select>`: pass `<option>` (and `<optgroup>`) elements as children. */
3
+ export interface SelectProps extends React.SelectHTMLAttributes<HTMLSelectElement> {
4
+ label?: string;
5
+ error?: string;
6
+ }
7
+ export declare const Select: React.ForwardRefExoticComponent<SelectProps & React.RefAttributes<HTMLSelectElement>>;
package/dist/Select.js ADDED
@@ -0,0 +1,9 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import React from "react";
3
+ import { clsx } from "clsx";
4
+ import { FIELD_ERROR, FIELD_FRAME, FieldLayout, useField } from "./field.js";
5
+ export const Select = React.forwardRef(({ label, error, className, children, ...props }, ref) => {
6
+ const field = useField(props, { label, error });
7
+ return (_jsx(FieldLayout, { field: field, children: _jsx("select", { ref: ref, ...props, ...field.control, className: clsx("h-10 px-3", FIELD_FRAME, error && FIELD_ERROR, className), children: children }) }));
8
+ });
9
+ Select.displayName = "Select";
@@ -1,3 +1,2 @@
1
1
  import React from "react";
2
2
  export declare const Skeleton: React.FC<React.HTMLAttributes<HTMLDivElement>>;
3
- //# sourceMappingURL=Skeleton.d.ts.map
package/dist/Skeleton.js CHANGED
@@ -3,4 +3,3 @@ import { clsx } from "clsx";
3
3
  export const Skeleton = ({ className, ...props }) => {
4
4
  return (_jsx("div", { className: clsx("animate-pulse rounded-md bg-neutral-200 dark:bg-neutral-800", className), ...props }));
5
5
  };
6
- //# sourceMappingURL=Skeleton.js.map
@@ -0,0 +1,12 @@
1
+ import React from "react";
2
+ export type TableProps = React.TableHTMLAttributes<HTMLTableElement>;
3
+ export type TableSectionProps = React.HTMLAttributes<HTMLTableSectionElement>;
4
+ export type TrProps = React.HTMLAttributes<HTMLTableRowElement>;
5
+ export type ThProps = React.ThHTMLAttributes<HTMLTableCellElement>;
6
+ export type TdProps = React.TdHTMLAttributes<HTMLTableCellElement>;
7
+ export declare const Table: React.ForwardRefExoticComponent<TableProps & React.RefAttributes<HTMLTableElement>>;
8
+ export declare const THead: React.ForwardRefExoticComponent<TableSectionProps & React.RefAttributes<HTMLTableSectionElement>>;
9
+ export declare const TBody: React.ForwardRefExoticComponent<TableSectionProps & React.RefAttributes<HTMLTableSectionElement>>;
10
+ export declare const Tr: React.ForwardRefExoticComponent<TrProps & React.RefAttributes<HTMLTableRowElement>>;
11
+ export declare const Th: React.ForwardRefExoticComponent<ThProps & React.RefAttributes<HTMLTableCellElement>>;
12
+ export declare const Td: React.ForwardRefExoticComponent<TdProps & React.RefAttributes<HTMLTableCellElement>>;
package/dist/Table.js ADDED
@@ -0,0 +1,16 @@
1
+ // Table parts that render the native table elements, so each keeps its implicit ARIA role
2
+ // (table, rowgroup, row, columnheader, cell). No data fetching, sorting or paging: compose them.
3
+ import React from "react";
4
+ import { clsx } from "clsx";
5
+ /** One table part: the native `tag` with the base classes, the caller's className and its ref. */
6
+ function tablePart(tag, base, displayName) {
7
+ const Part = React.forwardRef(({ className, ...props }, ref) => React.createElement(tag, { ...props, ref, className: clsx(base, className) }));
8
+ Part.displayName = displayName;
9
+ return Part;
10
+ }
11
+ export const Table = tablePart("table", "w-full text-sm", "Table");
12
+ export const THead = tablePart("thead", "border-b border-neutral-200 bg-neutral-50 dark:border-neutral-800 dark:bg-neutral-800/40", "THead");
13
+ export const TBody = tablePart("tbody", "", "TBody");
14
+ export const Tr = tablePart("tr", "border-b border-neutral-200 last:border-0 dark:border-neutral-800", "Tr");
15
+ export const Th = tablePart("th", "whitespace-nowrap px-3 py-2 text-left text-xs font-medium uppercase tracking-wider text-neutral-500 dark:text-neutral-400", "Th");
16
+ export const Td = tablePart("td", "px-3 py-2 text-neutral-900 dark:text-neutral-100", "Td");
@@ -0,0 +1,6 @@
1
+ import React from "react";
2
+ export interface TextareaProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {
3
+ label?: string;
4
+ error?: string;
5
+ }
6
+ export declare const Textarea: React.ForwardRefExoticComponent<TextareaProps & React.RefAttributes<HTMLTextAreaElement>>;
@@ -0,0 +1,9 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import React from "react";
3
+ import { clsx } from "clsx";
4
+ import { FIELD_ERROR, FIELD_FRAME, FieldLayout, useField } from "./field.js";
5
+ export const Textarea = React.forwardRef(({ label, error, className, ...props }, ref) => {
6
+ const field = useField(props, { label, error });
7
+ return (_jsx(FieldLayout, { field: field, children: _jsx("textarea", { ref: ref, ...props, ...field.control, className: clsx("min-h-20 px-3 py-2", FIELD_FRAME, error && FIELD_ERROR, className) }) }));
8
+ });
9
+ Textarea.displayName = "Textarea";
@@ -12,4 +12,3 @@ export declare const ThemeProvider: React.FC<{
12
12
  children: React.ReactNode;
13
13
  }>;
14
14
  export {};
15
- //# sourceMappingURL=ThemeProvider.d.ts.map
@@ -48,4 +48,3 @@ export const ThemeProvider = ({ defaultTheme = "system", children, }) => {
48
48
  };
49
49
  return (_jsx(ThemeContext.Provider, { value: { theme, resolvedTheme, setTheme }, children: children }));
50
50
  };
51
- //# sourceMappingURL=ThemeProvider.js.map
@@ -0,0 +1,45 @@
1
+ import React from "react";
2
+ type AriaInvalid = React.AriaAttributes["aria-invalid"];
3
+ /** The caller's props that the field wiring reads and then overrides on the control. */
4
+ export interface FieldAttrs {
5
+ id?: string | undefined;
6
+ "aria-describedby"?: string | undefined;
7
+ "aria-invalid"?: AriaInvalid | undefined;
8
+ }
9
+ /** The words around a control: its label and its error text. */
10
+ export interface FieldText {
11
+ label: string | undefined;
12
+ error: string | undefined;
13
+ }
14
+ /** A control's ids, and the attributes to spread on the native control after the caller's props. */
15
+ export interface Field extends FieldText {
16
+ errorId: string | undefined;
17
+ control: {
18
+ id: string;
19
+ "aria-describedby": string | undefined;
20
+ "aria-invalid": AriaInvalid | undefined;
21
+ };
22
+ }
23
+ /** The border, fill, text and focus ring every boxed control (Input, Select, Textarea) wears. */
24
+ export declare const FIELD_FRAME: string[];
25
+ /** Added to {@link FIELD_FRAME} while the control has an error. */
26
+ export declare const FIELD_ERROR = "border-red-500 focus:border-red-500 focus:ring-red-500";
27
+ /**
28
+ * Ids and ARIA for one control. useId, not a slug of the label: two controls labelled the same
29
+ * would share an id. A caller's `id` wins, and its aria-describedby is kept beside the error's.
30
+ */
31
+ export declare function useField(attrs: FieldAttrs, text: FieldText): Field;
32
+ /** The field's label, pointing at its control, or nothing when it has none. */
33
+ export declare function FieldLabel({ field }: {
34
+ field: Field;
35
+ }): React.ReactElement | null;
36
+ /** The field's error text, which the control names in aria-describedby, or nothing. */
37
+ export declare function FieldError({ field }: {
38
+ field: Field;
39
+ }): React.ReactElement | null;
40
+ /** A label over its control over the error text: the layout of Input, Select and Textarea. */
41
+ export declare function FieldLayout({ field, children }: {
42
+ field: Field;
43
+ children: React.ReactNode;
44
+ }): React.ReactElement;
45
+ export {};