Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jsx): add @tiptap/jsx for more convenient rendering of Tiptap content #5558

Open
wants to merge 5 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions demos/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ const getPackageDependencies = () => {
}
})

// Handle the JSX runtime alias
paths.unshift({ find: '@tiptap/core/jsx-runtime', replacement: resolve('../packages/core/src/jsx-runtime.ts') })
paths.unshift({ find: '@tiptap/core/jsx-dev-runtime', replacement: resolve('../packages/core/src/jsx-runtime.ts') })

return paths
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/jsx-runtime/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../dist/jsx-runtime.cjs'
17 changes: 17 additions & 0 deletions packages/core/jsx-runtime/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export type * from '../dist/packages/core/src/jsx-runtime.d.ts'

declare namespace React {
namespace JSX {
interface IntrinsicElements {
[elemName: string]: any
}
}
}

declare global {
namespace JSX {
interface IntrinsicElements {
[elemName: string]: any
}
}
}
1 change: 1 addition & 0 deletions packages/core/jsx-runtime/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../dist/jsx-runtime.js'
15 changes: 14 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
"types": "./dist/packages/core/src/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"./jsx-runtime": {
"types": "./jsx-runtime/index.d.ts",
"browser": "./jsx-runtime/index.js",
"import": "./jsx-runtime/index.js",
"require": "./jsx-runtime/index.cjs"
},
"./jsx-dev-runtime": {
"types": "./jsx-runtime/index.d.ts",
"browser": "./jsx-runtime/index.js",
"import": "./jsx-runtime/index.js",
"require": "./jsx-runtime/index.cjs"
}
},
"main": "dist/index.cjs",
Expand All @@ -29,7 +41,8 @@
"types": "dist/packages/core/src/index.d.ts",
"files": [
"src",
"dist"
"dist",
"jsx-runtime"
],
"devDependencies": {
"@tiptap/pm": "^2.6.6"
Expand Down
10 changes: 9 additions & 1 deletion packages/core/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ import { baseConfig } from '@tiptap-shared/rollup-config'

import pkg from './package.json' assert { type: 'json' }

export default baseConfig({ input: 'src/index.ts', pkg })
export default [baseConfig({ input: 'src/index.ts', pkg }), baseConfig({
input: 'src/jsx-runtime.ts',
pkg: {
name: '@tiptap/core/jsx-runtime',
main: 'dist/jsx-runtime.cjs',
module: 'dist/jsx-runtime.js',
umd: 'dist/jsx-runtime.umd.js',
},
})]
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * as extensions from './extensions/index.js'
export * from './helpers/index.js'
export * from './InputRule.js'
export * from './inputRules/index.js'
export * from './jsx-runtime.js'
export * from './Mark.js'
export * from './Node.js'
export * from './NodePos.js'
Expand Down
52 changes: 52 additions & 0 deletions packages/core/src/jsx-runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
type Attributes = Record<string, any>;

export type DOMOutputSpecElement = 0 | Attributes | DOMOutputSpecArray;
/**
* Better describes the output of a `renderHTML` function in prosemirror
* @see https://prosemirror.net/docs/ref/#model.DOMOutputSpec
*/
export type DOMOutputSpecArray =
| [string]
| [string, Attributes]
| [string, 0]
| [string, Attributes, 0]
| [string, Attributes, DOMOutputSpecArray | 0]
| [string, DOMOutputSpecArray];

export type JSXRenderer = (
tag: 'slot' | string | ((props?: Attributes) => DOMOutputSpecArray | DOMOutputSpecElement),
props?: Attributes,
...children: JSXRenderer[]
) => DOMOutputSpecArray | DOMOutputSpecElement;

export function Fragment(props: { children: JSXRenderer[] }) {
return props.children
}

export const h: JSXRenderer = (tag, attributes) => {
// Treat the slot tag as the Prosemirror hole to render content into
if (tag === 'slot') {
return 0
}

// If the tag is a function, call it with the props
if (tag instanceof Function) {
return tag(attributes)
}

const { children, ...rest } = attributes ?? {}

// Otherwise, return the tag, attributes, and children
return [tag, rest, children]
}

// See
// https://esbuild.github.io/api/#jsx-import-source
// https://www.typescriptlang.org/tsconfig/#jsxImportSource

export {
h as createElement,
h as jsx,
h as jsxDEV,
h as jsxs,
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"target": "es2019",
"module": "esnext",
"strict": true,
"jsx": "react",
"jsx": "react-jsx",
"importHelpers": true,
"moduleResolution": "node",
"resolveJsonModule": true,
Expand Down