@contentbit/react 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 contentbit
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 ADDED
@@ -0,0 +1,16 @@
1
+ # @contentbit/react
2
+
3
+ React renderer for [Content Blocks](https://contentbit.dev) documents, with headless accessible defaults for every generic block.
4
+
5
+ ```tsx
6
+ import { ContentBlocks } from '@contentbit/react'
7
+
8
+ <ContentBlocks
9
+ document={result.document}
10
+ renderMarkdown={(md) => <Markdown source={md} />}
11
+ />
12
+ ```
13
+
14
+ Pass `components` to override any block. A styled Tailwind pack is available through the shadcn registry: `pnpm dlx shadcn@latest add @contentbit/generic-pack`.
15
+
16
+ Docs: [contentbit.dev/docs](https://contentbit.dev/docs)
@@ -0,0 +1,3 @@
1
+ import type { BlockComponent } from './content-blocks.js';
2
+ export declare const defaultComponents: Record<string, BlockComponent>;
3
+ //# sourceMappingURL=components.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"components.d.ts","sourceRoot":"","sources":["../src/components.tsx"],"names":[],"mappings":"AAmBA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AA+JzD,eAAO,MAAM,iBAAiB,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,CAS5D,CAAA"}
@@ -0,0 +1,55 @@
1
+ 'use client';
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { splitProsCons } from '@contentbit/blocks';
4
+ import { isValidatedBlock } from '@contentbit/core';
5
+ import { useId, useState } from 'react';
6
+ function childMarkdown(node) {
7
+ return isValidatedBlock(node) ? node.data.markdown : node.body;
8
+ }
9
+ const Callout = ({ node, ctx }) => {
10
+ const data = node.data;
11
+ const type = String(node.props.type ?? 'note');
12
+ const title = node.props.title;
13
+ return (_jsxs("aside", { className: `cb-callout cb-callout-${type}`, children: [title ? _jsx("div", { className: "cb-callout-title", children: title }) : null, ctx.renderMarkdown(data.markdown)] }));
14
+ };
15
+ const Steps = ({ node }) => {
16
+ const data = node.data;
17
+ return (_jsx("ol", { className: "cb-steps", children: data.items.map((item, i) => (_jsx("li", { children: item.text }, i))) }));
18
+ };
19
+ const KeyMetrics = ({ node }) => {
20
+ const data = node.data;
21
+ return (_jsx("div", { className: "cb-key-metrics", children: data.rows.map((row, i) => (_jsxs("div", { className: "cb-key-metrics-item", children: [_jsx("span", { className: "cb-key-metrics-value", children: row.value }), _jsx("span", { className: "cb-key-metrics-label", children: row.label })] }, i))) }));
22
+ };
23
+ const QuickRef = ({ node }) => {
24
+ const data = node.data;
25
+ return (_jsx("dl", { className: "cb-quick-ref", children: data.rows.map((row, i) => (_jsxs("div", { className: "cb-quick-ref-row", children: [_jsx("dt", { children: row.key }), _jsx("dd", { children: row.value })] }, i))) }));
26
+ };
27
+ const Comparison = ({ node }) => {
28
+ const data = node.data;
29
+ return (_jsxs("table", { className: "cb-comparison", children: [_jsx("thead", { children: _jsxs("tr", { children: [_jsx("th", { scope: "col" }), _jsx("th", { scope: "col", children: String(node.props.left) }), _jsx("th", { scope: "col", children: String(node.props.right) })] }) }), _jsx("tbody", { children: data.rows.map((row, i) => (_jsxs("tr", { children: [_jsx("th", { scope: "row", children: row.label }), _jsx("td", { children: row.left }), _jsx("td", { children: row.right })] }, i))) })] }));
30
+ };
31
+ const ProsCons = ({ node }) => {
32
+ const { pros, cons } = splitProsCons(node.data);
33
+ return (_jsxs("div", { className: "cb-pros-cons", children: [_jsxs("div", { className: "cb-pros-cons-pros", children: [_jsx("div", { className: "cb-pros-cons-heading", children: "Pros" }), _jsx("ul", { children: pros.map((p, i) => (_jsx("li", { children: p }, i))) })] }), _jsxs("div", { className: "cb-pros-cons-cons", children: [_jsx("div", { className: "cb-pros-cons-heading", children: "Cons" }), _jsx("ul", { children: cons.map((c, i) => (_jsx("li", { children: c }, i))) })] })] }));
34
+ };
35
+ const Tabs = ({ node, ctx }) => {
36
+ const data = node.data;
37
+ const [active, setActive] = useState(0);
38
+ const safeActive = active < data.blocks.length ? active : 0;
39
+ const id = useId();
40
+ return (_jsxs("div", { className: "cb-tabs", children: [_jsx("div", { role: "tablist", className: "cb-tab-bar", children: data.blocks.map((tab, i) => (_jsx("button", { type: "button", role: "tab", id: `${id}-tab-${i}`, "aria-selected": i === safeActive, "aria-controls": `${id}-panel-${i}`, className: "cb-tab-button", onClick: () => setActive(i), children: String(tab.props.title) }, i))) }), data.blocks.map((tab, i) => i === safeActive ? (_jsx("div", { role: "tabpanel", id: `${id}-panel-${i}`, "aria-labelledby": `${id}-tab-${i}`, className: "cb-tab-panel", children: ctx.renderMarkdown(childMarkdown(tab)) }, i)) : null)] }));
41
+ };
42
+ const Faq = ({ node, ctx }) => {
43
+ const data = node.data;
44
+ return (_jsx("div", { className: "cb-faq", children: data.blocks.map((item, i) => (_jsxs("details", { className: "cb-faq-item", children: [_jsx("summary", { children: String(item.props.question) }), ctx.renderMarkdown(childMarkdown(item))] }, i))) }));
45
+ };
46
+ export const defaultComponents = {
47
+ callout: Callout,
48
+ steps: Steps,
49
+ 'key-metrics': KeyMetrics,
50
+ 'quick-ref': QuickRef,
51
+ comparison: Comparison,
52
+ 'pros-cons': ProsCons,
53
+ tabs: Tabs,
54
+ faq: Faq,
55
+ };
@@ -0,0 +1,25 @@
1
+ import type { ContentNode, DocumentNode, ValidatedBlockNode } from '@contentbit/core';
2
+ import type { ComponentType, ReactNode } from 'react';
3
+ export interface BlockRenderContext {
4
+ renderMarkdown(md: string): ReactNode;
5
+ renderNodes(nodes: ContentNode[]): ReactNode;
6
+ }
7
+ export interface BlockComponentProps<TData = unknown> {
8
+ node: ValidatedBlockNode<TData>;
9
+ ctx: BlockRenderContext;
10
+ }
11
+ export type BlockComponent = ComponentType<BlockComponentProps>;
12
+ export interface ContentBlocksProps {
13
+ document: DocumentNode;
14
+ /** Per-block components; merged over the headless defaults. */
15
+ components?: Record<string, BlockComponent>;
16
+ /** Host markdown renderer for prose segments. Default: paragraphs of plain text. */
17
+ renderMarkdown?: (md: string) => ReactNode;
18
+ /** Rendered for blocks that failed validation. Default: escaped body paragraphs. */
19
+ fallback?: ComponentType<{
20
+ name: string;
21
+ body: string;
22
+ }>;
23
+ }
24
+ export declare function ContentBlocks(props: ContentBlocksProps): ReactNode;
25
+ //# sourceMappingURL=content-blocks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"content-blocks.d.ts","sourceRoot":"","sources":["../src/content-blocks.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAA;AACrF,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAOrD,MAAM,WAAW,kBAAkB;IACjC,cAAc,CAAC,EAAE,EAAE,MAAM,GAAG,SAAS,CAAA;IACrC,WAAW,CAAC,KAAK,EAAE,WAAW,EAAE,GAAG,SAAS,CAAA;CAC7C;AAED,MAAM,WAAW,mBAAmB,CAAC,KAAK,GAAG,OAAO;IAClD,IAAI,EAAE,kBAAkB,CAAC,KAAK,CAAC,CAAA;IAC/B,GAAG,EAAE,kBAAkB,CAAA;CACxB;AAED,MAAM,MAAM,cAAc,GAAG,aAAa,CAAC,mBAAmB,CAAC,CAAA;AAE/D,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,YAAY,CAAA;IACtB,+DAA+D;IAC/D,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAA;IAC3C,oFAAoF;IACpF,cAAc,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,SAAS,CAAA;IAC1C,oFAAoF;IACpF,QAAQ,CAAC,EAAE,aAAa,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC,CAAA;CACzD;AAaD,wBAAgB,aAAa,CAAC,KAAK,EAAE,kBAAkB,GAAG,SAAS,CAoBlE"}
@@ -0,0 +1,34 @@
1
+ 'use client';
2
+ import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
3
+ import { isValidatedBlock } from '@contentbit/core';
4
+ import { Fragment } from 'react';
5
+ import { defaultComponents } from './components.js';
6
+ function DefaultMarkdown({ md }) {
7
+ return md
8
+ .trim()
9
+ .split(/\n{2,}/)
10
+ .map((p, i) => _jsx("p", { children: p }, i));
11
+ }
12
+ function DefaultFallback({ body }) {
13
+ return _jsx(DefaultMarkdown, { md: body });
14
+ }
15
+ export function ContentBlocks(props) {
16
+ const components = { ...defaultComponents, ...props.components };
17
+ const renderMarkdown = props.renderMarkdown ?? ((md) => _jsx(DefaultMarkdown, { md: md }));
18
+ const Fallback = props.fallback ?? DefaultFallback;
19
+ const ctx = {
20
+ renderMarkdown,
21
+ renderNodes(nodes) {
22
+ return nodes.map((node, i) => {
23
+ if (node.type === 'markdown')
24
+ return _jsx(Fragment, { children: renderMarkdown(node.value) }, i);
25
+ const Component = components[node.name];
26
+ if (Component && isValidatedBlock(node)) {
27
+ return _jsx(Component, { node: node, ctx: ctx }, i);
28
+ }
29
+ return _jsx(Fallback, { name: node.name, body: node.body }, i);
30
+ });
31
+ },
32
+ };
33
+ return _jsx(_Fragment, { children: ctx.renderNodes(props.document.children) });
34
+ }
@@ -0,0 +1,3 @@
1
+ export { ContentBlocks, type BlockComponent, type BlockComponentProps, type BlockRenderContext, type ContentBlocksProps, } from './content-blocks.js';
2
+ export { defaultComponents } from './components.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,KAAK,cAAc,EACnB,KAAK,mBAAmB,EACxB,KAAK,kBAAkB,EACvB,KAAK,kBAAkB,GACxB,MAAM,qBAAqB,CAAA;AAC5B,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { ContentBlocks, } from './content-blocks.js';
2
+ export { defaultComponents } from './components.js';
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@contentbit/react",
3
+ "version": "0.1.0",
4
+ "description": "React renderer for Content Blocks documents, with headless accessible defaults.",
5
+ "keywords": [
6
+ "content-blocks",
7
+ "headless",
8
+ "markdown",
9
+ "react",
10
+ "renderer"
11
+ ],
12
+ "homepage": "https://contentbit.dev",
13
+ "bugs": "https://github.com/agonist/contentbit/issues",
14
+ "license": "MIT",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/agonist/contentbit.git",
18
+ "directory": "packages/react"
19
+ },
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "type": "module",
24
+ "sideEffects": false,
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.ts",
28
+ "import": "./dist/index.js"
29
+ }
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "dependencies": {
35
+ "@contentbit/blocks": "0.1.0",
36
+ "@contentbit/core": "0.1.0"
37
+ },
38
+ "devDependencies": {
39
+ "@testing-library/react": "^16.3.0",
40
+ "@types/react": "^19.0.0",
41
+ "happy-dom": "^20.3.7",
42
+ "react": "^19.2.3",
43
+ "react-dom": "^19.2.3",
44
+ "typescript": "^5.9.3",
45
+ "vitest": "^4.0.17"
46
+ },
47
+ "peerDependencies": {
48
+ "react": "^19.0.0"
49
+ },
50
+ "scripts": {
51
+ "build": "tsc -p tsconfig.build.json",
52
+ "test": "vitest run",
53
+ "test:watch": "vitest"
54
+ }
55
+ }