Hooks

React hooks exported by @codecanon/waraq.

All hooks are imported from @codecanon/waraq and must be used inside a <Waraq> provider.


useWaraq

Returns the full WaraqState context — the central source of truth for the editor.

import { useWaraq } from "@codecanon/waraq"

function MyComponent() {
  const waraq = useWaraq()
}

State

PropertyTypeDescription
idstringStable editor instance ID
zoomnumberCurrent zoom level (0.01–2)
frameSizeFrameSizeCanvas width and height in px
frameBackgroundColorstringCanvas background color
layersLayer[]All layers in the current design
layerTypesLayerType[]Registered layer type definitions
toolToolActive tool: "select" | "move" | "hand"
selectedLayerCountnumberNumber of currently selected layers
leftPanelVisiblebooleanLeft panel open state
rightPanelVisiblebooleanRight panel open state
keyboardShortcutsKeyboardShortcut[]Active keyboard shortcut definitions
layersHistoryHistoryState<Layer[]>Undo/redo history object

Setters

MethodDescription
setZoom(zoom)Set zoom level directly
setFrameSize(size)Resize the canvas frame
setFrameBackgroundColor(color)Change canvas background
setLayers(layers)Replace the entire layers array
setTool(tool)Switch the active tool
setLeftPanelVisible(v)Show/hide the left panel
setRightPanelVisible(v)Show/hide the right panel

Layer methods

MethodSignatureDescription
addLayer(type: string, data?: Partial<Layer>) => voidAdd a new layer
updateLayer({ id, ...partial }) => voidUpdate a single layer
updateLayers(items[]) => voidBatch update multiple layers
getLayer<T>(id: string) => Layer<T> | undefinedGet a layer by ID
select(layerId: string) => Promise<void>Programmatically select a layer
getSelectedLayerIds() => string[]IDs of currently selected layers
setSelectedTargets(els[]) => Promise<void>Select by DOM elements

Edit actions

MethodDescription
unselectAll()Clear selection
deleteSelected()Delete selected layers
duplicate()Duplicate selected layers
toggleVisibility()Show/hide selected layers
bringToFront()Move selected layers to top
sendToBack()Move selected layers to bottom
toggleLock()Lock/unlock selected layers
redraw()Force Moveable to recalculate handles

Zoom actions

MethodDescription
zoomIn()Increase zoom by one step
zoomOut()Decrease zoom by one step
zoomReset()Reset to default zoom
zoom100()Set zoom to 100%
zoomFit()Fit canvas to viewport

Panel actions

MethodDescription
toggleLeftPanel()Toggle left panel
toggleRightPanel()Toggle right panel

Generic action dispatch

waraq.waraqAction("ZOOM_FIT")
waraq.waraqAction("HISTORY_UNDO")
waraq.waraqAction("DELETE_SELECTED")

See Types & constants — ActionType for all available action strings.

Refs

RefTypeDescription
frameRefRefObject<HTMLDivElement>The canvas frame DOM element
viewerRefRefObject<InfiniteViewer>The pan/zoom viewer instance
selectoRefRefObject<Selecto>The multi-select instance
moveableRefRefObject<Moveable>The drag/resize handle instance
selectedLayerRefs(HTMLElement | SVGElement)[]DOM elements for selected layers

useWaraqAction

A focused hook for editing the currently selected layer's properties. Use this inside custom action components.

import { useWaraqAction } from "@codecanon/waraq"

function MyCustomAction() {
  const { layer, isMultiple, setLayer, getCSSVar, setCSSVar, setData, getData } =
    useWaraqAction<MyLayerData>()

  if (!layer) return null

  return (
    <input
      value={getCSSVar("--color", "#000000")}
      onChange={(e) => setCSSVar("--color", e.target.value)}
    />
  )
}

Return value

PropertyTypeDescription
layerLayer | nullThe first selected layer (or null if nothing selected)
isMultiplebooleantrue when multiple layers are selected
setLayer(partial: Partial<Layer<T>>) => voidUpdate the selected layer(s)
getCSSVar(varName, defaultValue?) => stringRead a CSS variable from the selected layer
setCSSVarSetCSSVarWrite a CSS variable to the selected layer(s)
parseCSSValue(varName, defaultValue?) => numberRead a CSS variable as a number
setData<K>(key: K, value: T[K]) => voidWrite to the layer's custom data field
getData<K>(key: K, defaultValue?) => T[K]Read from the layer's custom data field

useWaraqDocumentAction

Read and write the canvas document properties (frame size and background color).

import { useWaraqDocumentAction } from "@codecanon/waraq"

function DocumentControls() {
  const { width, height, setWidth, setHeight, backgroundColor, setBackgroundColor } =
    useWaraqDocumentAction()

  return (
    <div>
      <input type="number" value={width} onChange={(e) => setWidth(+e.target.value)} />
      <input type="number" value={height} onChange={(e) => setHeight(+e.target.value)} />
    </div>
  )
}

Return value

PropertyTypeDescription
widthnumberCanvas width in px
heightnumberCanvas height in px
setWidth(width: number) => voidUpdate canvas width
setHeight(height: number) => voidUpdate canvas height
backgroundColorstringCanvas background color
setBackgroundColorDispatch<SetStateAction<string>>Update canvas background
frameRefRefObject<HTMLDivElement>Ref to the canvas frame DOM node

On this page