@supatype/ui 0.7.0 → 0.9.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/CHANGELOG.md +42 -0
- package/README.md +136 -0
- package/dist/Badge.d.ts +0 -1
- package/dist/Badge.js +0 -1
- package/dist/Button.d.ts +0 -1
- package/dist/Button.js +0 -1
- package/dist/Card.d.ts +0 -1
- package/dist/Card.js +0 -1
- package/dist/Input.d.ts +0 -1
- package/dist/Input.js +11 -4
- package/dist/RichTextEditor.d.ts +23 -1
- package/dist/RichTextEditor.js +41 -32
- package/dist/Skeleton.d.ts +0 -1
- package/dist/Skeleton.js +0 -1
- package/dist/ThemeProvider.d.ts +0 -1
- package/dist/ThemeProvider.js +0 -1
- package/dist/index.d.ts +0 -3
- package/dist/index.js +2 -2
- package/dist/rich-text.d.ts +2 -0
- package/dist/rich-text.js +3 -0
- package/dist/richtext-utils.d.ts +0 -1
- package/dist/richtext-utils.js +0 -1
- package/dist/styles.css +167 -0
- package/package.json +41 -12
- package/.turbo/turbo-build.log +0 -4
- package/.turbo/turbo-typecheck.log +0 -4
- package/dist/Badge.d.ts.map +0 -1
- package/dist/Badge.js.map +0 -1
- package/dist/Button.d.ts.map +0 -1
- package/dist/Button.js.map +0 -1
- package/dist/Card.d.ts.map +0 -1
- package/dist/Card.js.map +0 -1
- package/dist/Input.d.ts.map +0 -1
- package/dist/Input.js.map +0 -1
- package/dist/RichTextEditor.d.ts.map +0 -1
- package/dist/RichTextEditor.js.map +0 -1
- package/dist/Skeleton.d.ts.map +0 -1
- package/dist/Skeleton.js.map +0 -1
- package/dist/ThemeProvider.d.ts.map +0 -1
- package/dist/ThemeProvider.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/richtext-utils.d.ts.map +0 -1
- package/dist/richtext-utils.js.map +0 -1
- package/src/Badge.tsx +0 -27
- package/src/Button.tsx +0 -36
- package/src/Card.tsx +0 -29
- package/src/Input.tsx +0 -37
- package/src/RichTextEditor.tsx +0 -206
- package/src/Skeleton.tsx +0 -11
- package/src/ThemeProvider.tsx +0 -72
- package/src/index.ts +0 -22
- package/src/richtext-utils.ts +0 -112
- package/tsconfig.json +0 -11
- package/tsconfig.tsbuildinfo +0 -1
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
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
|
+
### Bug Fixes
|
|
16
|
+
|
|
17
|
+
* **styles:** `@supatype/ui/styles.css` exists. The package exported it, but no build produced it,
|
|
18
|
+
so the import failed and the editor rendered unstyled unless an app copied the rules in by hand.
|
|
19
|
+
Import it once, before your own CSS, and theme the editor with the `--su-rt-*` custom properties
|
|
20
|
+
listed in the README.
|
|
21
|
+
* **rich-text:** text that is both underlined and struck through shows both lines. It showed only
|
|
22
|
+
the strikethrough.
|
|
23
|
+
* **rich-text:** headings and lists in the editor are styled. Headings had no rule for the classes
|
|
24
|
+
the editor puts on them, and lists lost their markers under Tailwind's reset.
|
|
25
|
+
* **rich-text:** `editable` takes effect when it changes, not only on mount. Changing it swapped
|
|
26
|
+
the toolbar but left the content as editable, or as locked, as it started.
|
|
27
|
+
* **rich-text:** quotes are styled. A pasted blockquote rendered with no rule of the package's,
|
|
28
|
+
and now sits behind a left border in the muted colour, from the `--su-rt-*` variables.
|
|
29
|
+
* **package:** the tarball ships `dist`, the README, the changelog and the license only, with no
|
|
30
|
+
source or declaration maps, and drops six Radix UI dependencies no component used.
|
|
31
|
+
* **input:** an `Input` with no `id` gets a unique one from `React.useId()`. It used the label as
|
|
32
|
+
a slug (`"Email address"` became `email-address`), so two inputs with the same label shared an
|
|
33
|
+
id and the second label focused the first input. If your code or tests find an input by that
|
|
34
|
+
slug, pass `id` explicitly. With an `error`, the input now sets `aria-invalid` and points
|
|
35
|
+
`aria-describedby` at the error text (id `<input id>-error`), keeping any `aria-describedby` you
|
|
36
|
+
pass.
|
|
37
|
+
|
|
38
|
+
### Documentation
|
|
39
|
+
|
|
40
|
+
* **readme:** a README covering install, the Tailwind setup the components need (v4 `@source`, v3
|
|
41
|
+
`content`, class-based dark mode for `ThemeProvider`), the stylesheet, the exports and the
|
|
42
|
+
rich-text entry.
|
package/README.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# `@supatype/ui`
|
|
2
|
+
|
|
3
|
+
React components for Supatype apps: `Button`, `Input`, `Card`, `Badge`, `Skeleton`, a
|
|
4
|
+
`ThemeProvider`, and a Lexical rich text editor that reads and writes the `RichText` JSON a
|
|
5
|
+
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
|
+
`Button`, `Input`, `Card`, `Badge` and `Skeleton` are 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
|
+
| `Card`, `CardProps` | bordered container with `padding` (`sm`, `md`, `lg`) |
|
|
100
|
+
| `Badge`, `BadgeProps` | pill label with `variant` (`default`, `success`, `warning`, `danger`, `purple`) |
|
|
101
|
+
| `Skeleton` | pulsing loading placeholder |
|
|
102
|
+
| `ThemeProvider`, `useTheme` | light, dark or system theme, saved in `localStorage` |
|
|
103
|
+
| `emptyRichTextDocument` | an empty rich text document (one empty paragraph) |
|
|
104
|
+
| `stringToRichTextDocument` | plain text to a rich text document |
|
|
105
|
+
| `normalizeRichTextDefault` | turns a stored default (document, JSON string or plain string) into a document, or `null` |
|
|
106
|
+
| `richTextIsEmpty` | true when a document has no text |
|
|
107
|
+
| `SerializedEditorState`, `SerializedLexicalNode` | the rich text JSON types |
|
|
108
|
+
|
|
109
|
+
None of these import Lexical.
|
|
110
|
+
|
|
111
|
+
## Rich text editor
|
|
112
|
+
|
|
113
|
+
`RichTextEditor` has its own entry point so that apps which never use it do not need Lexical:
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
npm install lexical @lexical/react @lexical/rich-text @lexical/list @lexical/link @lexical/selection
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
```tsx
|
|
120
|
+
import { useState } from "react"
|
|
121
|
+
import "@supatype/ui/styles.css"
|
|
122
|
+
import { RichTextEditor } from "@supatype/ui/rich-text"
|
|
123
|
+
import { emptyRichTextDocument, type SerializedEditorState } from "@supatype/ui"
|
|
124
|
+
|
|
125
|
+
function BodyField(): React.ReactElement {
|
|
126
|
+
const [body, setBody] = useState<SerializedEditorState>(emptyRichTextDocument())
|
|
127
|
+
return <RichTextEditor value={body} onChange={setBody} placeholder="Write your post" />
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
`value` is read once, on mount. To load a different document into a mounted editor, change
|
|
132
|
+
`documentKey`. Pass `editable={false}` to render a document read only, with no toolbar; `editable`
|
|
133
|
+
can change at any time.
|
|
134
|
+
|
|
135
|
+
The Lexical packages are optional peer dependencies: your package manager will not install or warn
|
|
136
|
+
about them, and importing `@supatype/ui/rich-text` without them fails at build time.
|
package/dist/Badge.d.ts
CHANGED
package/dist/Badge.js
CHANGED
package/dist/Button.d.ts
CHANGED
package/dist/Button.js
CHANGED
package/dist/Card.d.ts
CHANGED
package/dist/Card.js
CHANGED
package/dist/Input.d.ts
CHANGED
package/dist/Input.js
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import React from "react";
|
|
3
3
|
import { clsx } from "clsx";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
/** The caller's aria-describedby, with the error text's id added when there is an error. */
|
|
5
|
+
function describedBy(callerValue, errorId) {
|
|
6
|
+
const ids = [callerValue, errorId].filter((value) => value !== undefined && value !== "");
|
|
7
|
+
return ids.length > 0 ? ids.join(" ") : undefined;
|
|
8
|
+
}
|
|
9
|
+
export const Input = React.forwardRef(({ label, error, className, id, "aria-describedby": ariaDescribedBy, "aria-invalid": ariaInvalid, ...props }, ref) => {
|
|
10
|
+
// useId, not a slug of the label: two inputs labelled the same would share an id.
|
|
11
|
+
const generatedId = React.useId();
|
|
12
|
+
const inputId = id ?? generatedId;
|
|
13
|
+
const errorId = error ? `${inputId}-error` : undefined;
|
|
14
|
+
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, "aria-describedby": describedBy(ariaDescribedBy, errorId), "aria-invalid": ariaInvalid ?? (error ? true : undefined), 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", { id: errorId, className: "text-sm text-red-500", children: error })] }));
|
|
7
15
|
});
|
|
8
16
|
Input.displayName = "Input";
|
|
9
|
-
//# sourceMappingURL=Input.js.map
|
package/dist/RichTextEditor.d.ts
CHANGED
|
@@ -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
|
package/dist/RichTextEditor.js
CHANGED
|
@@ -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
|
-
|
|
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: {
|
|
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
|
-
|
|
32
|
-
},
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
package/dist/Skeleton.d.ts
CHANGED
package/dist/Skeleton.js
CHANGED
package/dist/ThemeProvider.d.ts
CHANGED
package/dist/ThemeProvider.js
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -8,8 +8,5 @@ export { Badge } from "./Badge.js";
|
|
|
8
8
|
export type { BadgeProps } from "./Badge.js";
|
|
9
9
|
export { Skeleton } from "./Skeleton.js";
|
|
10
10
|
export { ThemeProvider, useTheme } from "./ThemeProvider.js";
|
|
11
|
-
export { RichTextEditor } from "./RichTextEditor.js";
|
|
12
|
-
export type { RichTextEditorProps } from "./RichTextEditor.js";
|
|
13
11
|
export { emptyRichTextDocument, normalizeRichTextDefault, richTextIsEmpty, stringToRichTextDocument } from "./richtext-utils.js";
|
|
14
12
|
export type { SerializedEditorState, SerializedLexicalNode } from "@supatype/types/lexical";
|
|
15
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -5,6 +5,6 @@ export { Card } from "./Card.js";
|
|
|
5
5
|
export { Badge } from "./Badge.js";
|
|
6
6
|
export { Skeleton } from "./Skeleton.js";
|
|
7
7
|
export { ThemeProvider, useTheme } from "./ThemeProvider.js";
|
|
8
|
-
|
|
8
|
+
// RichTextEditor lives at "@supatype/ui/rich-text" so this entry never imports lexical.
|
|
9
|
+
// The helpers below only touch the serialized JSON shape, so they stay here.
|
|
9
10
|
export { emptyRichTextDocument, normalizeRichTextDefault, richTextIsEmpty, stringToRichTextDocument } from "./richtext-utils.js";
|
|
10
|
-
//# sourceMappingURL=index.js.map
|
package/dist/richtext-utils.d.ts
CHANGED
|
@@ -10,4 +10,3 @@ export declare function stringToRichTextDocument(text: string): SerializedEditor
|
|
|
10
10
|
export declare function normalizeRichTextDefault(value: SerializedEditorState | string | null | undefined): SerializedEditorState | null;
|
|
11
11
|
/** True when the editor has no visible text (whitespace-only counts as empty). */
|
|
12
12
|
export declare function richTextIsEmpty(state: SerializedEditorState | null | undefined): boolean;
|
|
13
|
-
//# sourceMappingURL=richtext-utils.d.ts.map
|
package/dist/richtext-utils.js
CHANGED
package/dist/styles.css
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @supatype/ui stylesheet: import once, before your own CSS.
|
|
3
|
+
*
|
|
4
|
+
* import "@supatype/ui/styles.css"
|
|
5
|
+
*
|
|
6
|
+
* The RichTextEditor (from "@supatype/ui/rich-text") renders plain su-richtext / su-rt-* classes,
|
|
7
|
+
* styled here. Theme it by setting the --su-rt-* custom properties on :root or any ancestor.
|
|
8
|
+
* The variable defaults sit in :where(:root), which has zero specificity, so setting a variable
|
|
9
|
+
* anywhere wins. Overriding the rules themselves still needs this file imported first.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
:where(:root) {
|
|
13
|
+
--su-rt-muted: #6b7280;
|
|
14
|
+
--su-rt-accent: #2563eb;
|
|
15
|
+
--su-rt-border: #e5e7eb;
|
|
16
|
+
--su-rt-border-width: 1px;
|
|
17
|
+
--su-rt-radius: 6px;
|
|
18
|
+
--su-rt-bg: #ffffff;
|
|
19
|
+
--su-rt-toolbar-bg: #f9fafb;
|
|
20
|
+
--su-rt-min-height: 8rem;
|
|
21
|
+
--su-rt-pad-y: 0.75rem;
|
|
22
|
+
--su-rt-pad-x: 1rem;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
.su-richtext {
|
|
26
|
+
font-size: 1rem;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.su-richtext-toolbar {
|
|
30
|
+
display: flex;
|
|
31
|
+
flex-wrap: wrap;
|
|
32
|
+
align-items: center;
|
|
33
|
+
gap: 0.25rem;
|
|
34
|
+
padding: 0.4rem 0.5rem;
|
|
35
|
+
border: var(--su-rt-border-width) solid var(--su-rt-border);
|
|
36
|
+
border-bottom: none;
|
|
37
|
+
border-radius: var(--su-rt-radius) var(--su-rt-radius) 0 0;
|
|
38
|
+
background: var(--su-rt-toolbar-bg);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
.su-richtext-toolbar button {
|
|
42
|
+
min-width: 1.85rem;
|
|
43
|
+
padding: 0.25rem 0.5rem;
|
|
44
|
+
font: inherit;
|
|
45
|
+
font-size: 0.85rem;
|
|
46
|
+
border: 1px solid var(--su-rt-border);
|
|
47
|
+
border-radius: calc(var(--su-rt-radius) * 2 / 3);
|
|
48
|
+
background: var(--su-rt-button-bg, var(--su-rt-bg));
|
|
49
|
+
color: var(--su-rt-fg, inherit);
|
|
50
|
+
cursor: pointer;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
.su-richtext-toolbar button:hover {
|
|
54
|
+
border-color: var(--su-rt-accent);
|
|
55
|
+
color: var(--su-rt-accent);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.su-richtext-toolbar-divider {
|
|
59
|
+
width: 1px;
|
|
60
|
+
height: 1.1rem;
|
|
61
|
+
margin: 0 0.2rem;
|
|
62
|
+
background: var(--su-rt-border);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
.su-richtext-surface {
|
|
66
|
+
position: relative;
|
|
67
|
+
border: var(--su-rt-border-width) solid var(--su-rt-border);
|
|
68
|
+
border-radius: 0 0 var(--su-rt-radius) var(--su-rt-radius);
|
|
69
|
+
background: var(--su-rt-bg);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
.su-richtext-readonly .su-richtext-surface {
|
|
73
|
+
border-radius: var(--su-rt-radius);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
.su-richtext-editable {
|
|
77
|
+
min-height: var(--su-rt-min-height);
|
|
78
|
+
padding: var(--su-rt-pad-y) var(--su-rt-pad-x);
|
|
79
|
+
outline: none;
|
|
80
|
+
line-height: 1.6;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
.su-richtext-placeholder {
|
|
84
|
+
position: absolute;
|
|
85
|
+
top: var(--su-rt-pad-y);
|
|
86
|
+
left: var(--su-rt-pad-x);
|
|
87
|
+
color: var(--su-rt-muted);
|
|
88
|
+
pointer-events: none;
|
|
89
|
+
user-select: none;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/* Content nodes, named by the editor theme in RichTextEditor.tsx */
|
|
93
|
+
|
|
94
|
+
.su-rt-h1,
|
|
95
|
+
.su-rt-h2,
|
|
96
|
+
.su-rt-h3 {
|
|
97
|
+
font-weight: 800;
|
|
98
|
+
line-height: 1.25;
|
|
99
|
+
margin: 0 0 0.4rem;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
.su-rt-h1 {
|
|
103
|
+
font-size: 1.6em;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
.su-rt-h2 {
|
|
107
|
+
font-size: 1.3em;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
.su-rt-h3 {
|
|
111
|
+
font-size: 1.1em;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
.su-rt-p {
|
|
115
|
+
margin: 0 0 0.5rem;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
.su-rt-ul,
|
|
119
|
+
.su-rt-ol {
|
|
120
|
+
margin: 0 0 0.5rem 1.4rem;
|
|
121
|
+
padding: 0;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
.su-rt-ul {
|
|
125
|
+
list-style: disc;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
.su-rt-ol {
|
|
129
|
+
list-style: decimal;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
.su-rt-li {
|
|
133
|
+
margin: 0.15rem 0;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
.su-rt-bold {
|
|
137
|
+
font-weight: 700;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
.su-rt-italic {
|
|
141
|
+
font-style: italic;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
.su-rt-underline {
|
|
145
|
+
text-decoration: underline;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
.su-rt-strikethrough {
|
|
149
|
+
text-decoration: line-through;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/* Lexical uses this instead of the two classes above when text has both formats. */
|
|
153
|
+
.su-rt-underline-strikethrough {
|
|
154
|
+
text-decoration: underline line-through;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
.su-rt-quote {
|
|
158
|
+
margin: 0 0 0.5rem;
|
|
159
|
+
padding-left: var(--su-rt-pad-x);
|
|
160
|
+
border-left: calc(var(--su-rt-border-width) * 3) solid var(--su-rt-border);
|
|
161
|
+
color: var(--su-rt-muted);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
.su-rt-link {
|
|
165
|
+
color: var(--su-rt-accent);
|
|
166
|
+
text-decoration: underline;
|
|
167
|
+
}
|