prosekit
blockpreact-example-keymapsource: ok · refreshed
preact-example-keymap
Custom keyboard shortcuts and key bindings.
npx shadcn add "https://prosekit.dev/r/preact-example-keymap.json"free
Where it goes
kanban
Tags auto-generated by rules on 9/24/2026 — empty means no evidence, not lack of support.
Dependencies
Bundle weight · lightprosekit@^0.22.2
Registry dependencies
https://unpkg.com/prosekit-registry/dist/r/preact-ui-button.json
Files5 · source cached 10/7/2026, 5:44:12 AM
registry/src/preact/examples/keymap/editor.tsx
import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'
import { useCallback, useMemo, useState } from 'preact/hooks'
import { createEditor } from 'prosekit/core'
import { ProseKit } from 'prosekit/preact'
import { defineExtension } from './extension.ts'
import Toolbar from './toolbar.tsx'
export default function Editor() {
const editor = useMemo(() => {
const extension = defineExtension()
return createEditor({ extension })
}, [])
const [submissions, setSubmissions] = useState<string[]>([])
const pushSubmission = useCallback(
(hotkey: string) => {
const docString = JSON.stringify(editor.getDocJSON())
const submission = `${new Date().toISOString()}\t${hotkey}\n${docString}`
setSubmissions((prev) => [...prev, submission])
},
[editor],
)
return (
<ProseKit editor={editor}>
<div className="box-border h-full w-full min-h-36 overflow-y-hidden overflow-x-hidden rounded-md border border-solid border-gray-200 dark:border-gray-700 shadow-sm flex flex-col bg-[canvas] text-black dark:text-white">
<Toolbar onSubmit={pushSubmission} />
<div className="relative w-full flex-1 box-border overflow-y-auto">
<div ref={editor.mount} className="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500"></div>
</div>
</div>
<fieldset className="mt-4 box-border flex max-w-full w-full overflow-x-auto border p-4 rounded-md shadow-sm min-w-0">
<legend>Submit Records</legend>
<ol>
{submissions.map((submission, index) => (
<li key={index}>
<pre>{submission}</pre>
</li>
))}
</ol>
{submissions.length === 0 && <div>No submissions yet</div>}
</fieldset>
</ProseKit>
)
}
registry/src/preact/examples/keymap/extension.ts
import { defineBasicExtension } from 'prosekit/basic'
import { union } from 'prosekit/core'
export function defineExtension() {
return union(defineBasicExtension())
}
export type EditorExtension = ReturnType<typeof defineExtension>
registry/src/preact/examples/keymap/index.ts
export { default as ExampleEditor } from './editor.tsx'
registry/src/preact/examples/keymap/toolbar.tsx
import { useState } from 'preact/hooks'
import { Button } from '../../ui/button/index.ts'
import { useSubmitKeymap } from './use-submit-keymap.ts'
export default function Toolbar(props: {
onSubmit: (hotkey: string) => void
}) {
const [hotkey, setHotkey] = useState<'Shift-Enter' | 'Enter'>('Shift-Enter')
useSubmitKeymap(hotkey, props.onSubmit)
return (
<div className="z-2 box-border border-gray-200 dark:border-gray-800 border-solid border-l-0 border-r-0 border-t-0 border-b flex flex-wrap gap-1 p-2 items-center">
<Button
pressed={hotkey === 'Shift-Enter'}
onClick={() => setHotkey('Shift-Enter')}
>
<span className="mr-1">Submit with</span>
<kbd>Shift + Enter</kbd>
</Button>
<Button pressed={hotkey === 'Enter'} onClick={() => setHotkey('Enter')}>
<span className="mr-1">Submit with</span>
<kbd>Enter</kbd>
</Button>
</div>
)
}
registry/src/preact/examples/keymap/use-submit-keymap.ts
import { useMemo } from 'preact/hooks'
import type { Keymap } from 'prosekit/core'
import { useKeymap } from 'prosekit/preact'
export function useSubmitKeymap(
hotkey: 'Shift-Enter' | 'Enter',
onSubmit: (hotkey: string) => void,
) {
const keymap: Keymap = useMemo(() => {
return {
[hotkey]: () => {
onSubmit(hotkey)
return true
},
}
}, [hotkey, onSubmit])
useKeymap(keymap)
}