@rasamjs/react
v0.1.1
Published
React bindings for the rasam SVG editor.
Readme
@rasamjs/react
Use useActiveTool to observe and activate optional
registered tools alongside the built-in tools. useTool retains its built-in types.
React bindings for @rasamjs/editor: a component that mounts the
editor, and hooks that read its selection, history, layers, properties, commands and tool.
import { RasamEditor, useHistory, useProperty, useSelection } from '@rasamjs/react';
function Inspector() {
const nodes = useSelection();
const [fill, setFill] = useProperty('fill');
const { canUndo, undoLabel } = useHistory();
return (
<aside>
{nodes.length} selected, {canUndo ? `undo ${undoLabel}` : 'nothing to undo'}
<input
type="color"
disabled={!nodes.length}
value={typeof fill === 'string' ? fill : '#000000'}
onChange={(event) => setFill(event.target.value, { mergeKey: 'fill' })}
/>
</aside>
);
}
export function App() {
return (
<RasamEditor options={{ autosave: { key: 'my-app' } }} style={{ height: 600 }}>
<Inspector />
</RasamEditor>
);
}Mounting
<RasamEditor> renders a host div and creates the Editor in an effect, so rendering
never touches the DOM. Give the host a size with className or style; the editor
document fills it. Children render after the host and receive the editor through context.
options are read once, when the editor is created. Change a section later with
editor.configure(), or give the component a new key to create a fresh editor.
onReady(editor) runs after creation, for example to register commands. A function it
returns runs before the editor is destroyed; failures in either are reported through the
editor's error event instead of thrown.
Unmounting destroys the editor, which saves pending autosave changes first. In StrictMode, React mounts, unmounts and mounts again: the first editor is destroyed and the second one stays, so the host always holds exactly one editor document.
Hooks
Every hook reads the nearest <RasamEditor>, or takes an editor as its last argument
(null for none). Without an editor, before mounting and on the server, they return empty
values: no selection, no history, no layers, the Select tool and undefined properties.
| Hook | Returns |
| -------------------------- | ----------------------------------------------------------------- |
| useEditor() | The Editor, or null |
| useSelection() | The selected nodes |
| useHistory() | canUndo, canRedo, counts and labels |
| useLayers() | The layers rows, topmost first |
| useProperty(key) | [value, write]; the value is mixed when the selection differs |
| useProperties() | The shared Properties model |
| useCommands() | Every command with its live enabled state and run() |
| useCommand(id) | One command's state, or undefined |
| useTool() | [tool, setTool] |
| useEditorEvent(type, fn) | Subscribes fn to an editor event for the component's lifetime |
Hooks re-render only when their value changes: a pan or zoom doesn't re-render a selection
panel. Values keep their identity until they change, so they work as effect dependencies.
Hooks follow editor.load() and component reconfiguration, including a replaced layers
model.
Command state follows selection, history, tool, gesture and scene changes, and
updates immediately on registry registration, override, removal and rebinding.
Call editor.commands.invalidate() when application-owned enabled state changes.
onReady remains useful for registering commands when an editor is created.
RasamEditorContext is exported for apps that create the Editor themselves and want the
hooks: wrap the tree in <RasamEditorContext.Provider value={editor}>.
Requirements
React 18.3 or 19, and @rasamjs/editor with its seven rasam peers. The package has no
runtime dependencies. See API.md for the full reference and
apps/react-example for a complete Vite app.
