@md2ui/react 0.0.0-stage → 0.1.0-alpha.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.
- package/LICENSE +21 -0
- package/README.md +67 -2
- package/dist/lib/index.d.ts +17 -0
- package/dist/lib/index.js +74 -0
- package/package.json +49 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 alexrvarela
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,68 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @md2ui/react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`Md2uiView`: a thin React component over `@md2ui/dom`'s `<md2ui-view>`, which turns a
|
|
4
|
+
Markdown document and an optional `THEME.md` into a painted, interactive UI.
|
|
5
|
+
|
|
6
|
+
**Experimental alpha**; the API may change between any two releases. Part of
|
|
7
|
+
[md2ui-ts](https://github.com/md2ui/md2ui-ts), the client for
|
|
8
|
+
[md2ui](https://github.com/md2ui/md2ui). Home: [md2ui.org](https://md2ui.org).
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm i @md2ui/dom@alpha @md2ui/react@alpha # or: pnpm add, yarn add, bun add
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Peer: `react ^18.3 || ^19`. Install `@md2ui/dom` beside it, at the same version. With
|
|
17
|
+
Vite, use the settings in `@md2ui/dom`'s README.
|
|
18
|
+
|
|
19
|
+
## Example
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import { type FontSource, Md2uiView } from "@md2ui/react";
|
|
23
|
+
|
|
24
|
+
const FONTS: FontSource[] = [];
|
|
25
|
+
|
|
26
|
+
export function Reply({ id, markdown }: { id: string; markdown: string }) {
|
|
27
|
+
return (
|
|
28
|
+
<Md2uiView
|
|
29
|
+
markdown={markdown}
|
|
30
|
+
messageId={id}
|
|
31
|
+
fonts={FONTS}
|
|
32
|
+
style={{ height: 480 }}
|
|
33
|
+
onAction={(event) => console.log(event.detail)}
|
|
34
|
+
/>
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Keep `fonts` stable (a module constant or `useMemo`): a new array reloads the fonts.
|
|
40
|
+
More in the [md2ui-ts README](https://github.com/md2ui/md2ui-ts#react).
|
|
41
|
+
|
|
42
|
+
## API
|
|
43
|
+
|
|
44
|
+
| Export | What it is |
|
|
45
|
+
|---|---|
|
|
46
|
+
| `Md2uiView` | The component. Its `ref` is the `Md2uiViewElement`, with `release(ordinals)` |
|
|
47
|
+
| `Md2uiViewProps` | Every `Md2uiViewProperties` property, each optional, plus the props below |
|
|
48
|
+
| `FontSource`, `Md2uiAction`, `Md2uiLoadError`, `Md2uiViewElement`, `Md2uiViewProperties` | Re-exported from `@md2ui/dom` |
|
|
49
|
+
|
|
50
|
+
| Prop | Type |
|
|
51
|
+
|---|---|
|
|
52
|
+
| `markdown`, `messageId`, `themeSrc`, `themeText`, `assetBase`, `fonts`, `disabled`, `streaming`, `motion` | as the element's properties |
|
|
53
|
+
| `onAction` | `(event: CustomEvent<Md2uiAction>) => void`; `event.preventDefault()` cancels what the view would do |
|
|
54
|
+
| `onError` | `(event: CustomEvent<Md2uiLoadError>) => void` |
|
|
55
|
+
| `className` | `string` |
|
|
56
|
+
| `style` | `CSSProperties` |
|
|
57
|
+
|
|
58
|
+
The component registers `<md2ui-view>` when it mounts. To pass options, call
|
|
59
|
+
`defineMd2uiView({ worker })` from `@md2ui/dom` before the first render.
|
|
60
|
+
|
|
61
|
+
## Requirements
|
|
62
|
+
|
|
63
|
+
React 18.3 or 19; current Chrome, Edge, Firefox and Safari; an ESM bundler that ships
|
|
64
|
+
module Workers.
|
|
65
|
+
|
|
66
|
+
## Licence
|
|
67
|
+
|
|
68
|
+
MIT, in [`LICENSE`](LICENSE).
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { FontSource, Md2uiAction, Md2uiAction as Md2uiAction$1, Md2uiLoadError, Md2uiLoadError as Md2uiLoadError$1, Md2uiViewElement, Md2uiViewElement as Md2uiViewElement$1, Md2uiViewProperties, Md2uiViewProperties as Md2uiViewProperties$1 } from "@md2ui/dom";
|
|
2
|
+
import { CSSProperties, ForwardRefExoticComponent, RefAttributes } from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
/** The element's properties, each optional, and its events as callbacks. */
|
|
6
|
+
interface Md2uiViewProps extends Partial<Md2uiViewProperties$1> {
|
|
7
|
+
/** `md2ui-action`: call `event.preventDefault()` to cancel what the view would do. */
|
|
8
|
+
readonly onAction?: (event: CustomEvent<Md2uiAction$1>) => void;
|
|
9
|
+
/** `md2ui-error`: a theme, font or image that could not load. */
|
|
10
|
+
readonly onError?: (event: CustomEvent<Md2uiLoadError$1>) => void;
|
|
11
|
+
readonly className?: string;
|
|
12
|
+
readonly style?: CSSProperties;
|
|
13
|
+
}
|
|
14
|
+
/** `<md2ui-view>` as a React component; the ref is the element. */
|
|
15
|
+
declare const Md2uiView: ForwardRefExoticComponent<Md2uiViewProps & RefAttributes<Md2uiViewElement$1>>;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { type FontSource, type Md2uiAction, type Md2uiLoadError, Md2uiView, type Md2uiViewElement, type Md2uiViewProperties, Md2uiViewProps };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { defineMd2uiView } from "@md2ui/dom";
|
|
2
|
+
import { createElement, forwardRef, useCallback, useEffect, useLayoutEffect, useRef } from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
/**
|
|
6
|
+
* The order properties are set in: the theme and fonts first, then the document, then
|
|
7
|
+
* its message id, so a document and its id reach the view together.
|
|
8
|
+
*/
|
|
9
|
+
const ORDER = Object.freeze([
|
|
10
|
+
"themeSrc",
|
|
11
|
+
"themeText",
|
|
12
|
+
"assetBase",
|
|
13
|
+
"fonts",
|
|
14
|
+
"disabled",
|
|
15
|
+
"streaming",
|
|
16
|
+
"motion",
|
|
17
|
+
"markdown",
|
|
18
|
+
"messageId"
|
|
19
|
+
]);
|
|
20
|
+
function forward(ref, value) {
|
|
21
|
+
if (typeof ref === "function") ref(value);
|
|
22
|
+
else if (ref !== null) ref.current = value;
|
|
23
|
+
}
|
|
24
|
+
function Md2uiViewRender(props, ref) {
|
|
25
|
+
const { onAction, onError, className, style } = props;
|
|
26
|
+
const element = useRef(null);
|
|
27
|
+
/** What each property was last set to, so an unchanged prop is not set again. */
|
|
28
|
+
const applied = useRef(/* @__PURE__ */ new Map());
|
|
29
|
+
const callbacks = useRef({
|
|
30
|
+
onAction,
|
|
31
|
+
onError
|
|
32
|
+
});
|
|
33
|
+
callbacks.current = {
|
|
34
|
+
onAction,
|
|
35
|
+
onError
|
|
36
|
+
};
|
|
37
|
+
const attach = useCallback((node) => {
|
|
38
|
+
element.current = node;
|
|
39
|
+
forward(ref, node);
|
|
40
|
+
}, [ref]);
|
|
41
|
+
useLayoutEffect(() => {
|
|
42
|
+
const node = element.current;
|
|
43
|
+
if (node === null) return;
|
|
44
|
+
defineMd2uiView();
|
|
45
|
+
for (const name of ORDER) {
|
|
46
|
+
const value = props[name];
|
|
47
|
+
if (value === void 0 || applied.current.get(name) === value) continue;
|
|
48
|
+
applied.current.set(name, value);
|
|
49
|
+
Reflect.set(node, name, value);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
const node = element.current;
|
|
54
|
+
if (node === null) return;
|
|
55
|
+
const action = (event) => callbacks.current.onAction?.(event);
|
|
56
|
+
const error = (event) => callbacks.current.onError?.(event);
|
|
57
|
+
node.addEventListener("md2ui-action", action);
|
|
58
|
+
node.addEventListener("md2ui-error", error);
|
|
59
|
+
return () => {
|
|
60
|
+
node.removeEventListener("md2ui-action", action);
|
|
61
|
+
node.removeEventListener("md2ui-error", error);
|
|
62
|
+
};
|
|
63
|
+
}, []);
|
|
64
|
+
return createElement("md2ui-view", {
|
|
65
|
+
ref: attach,
|
|
66
|
+
className,
|
|
67
|
+
style
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/** `<md2ui-view>` as a React component; the ref is the element. */
|
|
71
|
+
const Md2uiView = forwardRef(Md2uiViewRender);
|
|
72
|
+
|
|
73
|
+
//#endregion
|
|
74
|
+
export { Md2uiView };
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@md2ui/react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0-alpha.1",
|
|
4
|
+
"description": "Thin React binding over <md2ui-view>.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"md2ui",
|
|
7
|
+
"markdown",
|
|
8
|
+
"react",
|
|
9
|
+
"generative-ui",
|
|
10
|
+
"agent"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://md2ui.org",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/md2ui/md2ui-ts/issues"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/md2ui/md2ui-ts.git",
|
|
20
|
+
"directory": "packages/react"
|
|
21
|
+
},
|
|
22
|
+
"type": "module",
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/lib/index.d.ts",
|
|
27
|
+
"default": "./dist/lib/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist/lib"
|
|
32
|
+
],
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public",
|
|
35
|
+
"tag": "alpha"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/react": "19.3.0",
|
|
39
|
+
"@types/react-dom": "19.3.0",
|
|
40
|
+
"react": "19.3.0",
|
|
41
|
+
"react-dom": "19.3.0"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@md2ui/dom": "0.1.0-alpha.1"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"react": "^18.3 || ^19"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "tsc -b && tsdown"
|
|
51
|
+
}
|
|
6
52
|
}
|