Skip to content

Headless UI ​

@writeon/ui has the behaviour and accessibility of an editor UI, without any markup or styles. Each controller:

  • keeps its state and tells you when it changes (getState(), subscribe());
  • gives prop getters (getRootProps(), getItemProps(id)…) with the attributes and event handlers to put on your elements;
  • exposes state as data-* attributes (data-state="on", data-active) to style against.

Plain JavaScript ​

applyProps(element, props) applies prop getter output to a DOM element; call it again after each change.

ts
import type { Editor } from '@writeon/core'
import {
  applyProps,
  basicToolbarItems,
  createToolbar,
} from '@writeon/ui'

// Your markup, our behaviour: roving tabindex, arrow keys,
// aria-pressed, disabled state from editor.can(), shortcut hints.
export function renderToolbar(
  root: HTMLElement,
  editor: Editor,
): () => void {
  const items = basicToolbarItems()
  const toolbar = createToolbar(editor, { items })

  // Create the buttons once.
  const buttons = toolbar.items.map((item) => {
    const button = document.createElement('button')
    button.className = 'my-button'
    button.textContent = item.label
    root.append(button)
    return { id: item.id, button }
  })

  // Apply the current attributes and handlers, on every change.
  const render = (): void => {
    applyProps(root, toolbar.getRootProps())
    for (const { id, button } of buttons) {
      applyProps(button, toolbar.getItemProps(id))
    }
  }

  render()
  return toolbar.subscribe(render)
}

React and Vue ​

The adapters wrap each controller in a hook that returns framework props (useToolbar, useMenu, useLinkPopover, useTooltip, useImageDialog, useBubbleMenu, useSuggestionMenu).

tsx
import type { Editor } from '@writeon/core'
import { useToolbar } from '@writeon/react'
import { basicToolbarItems } from '@writeon/ui'

// All the basic buttons, without underline.
const items = basicToolbarItems({ exclude: ['underline'] })

export function MyToolbar({ editor }: { editor: Editor }) {
  const toolbar = useToolbar(editor, { items })

  // The prop getters bring the ARIA and keyboard support;
  // the classes are yours (Tailwind here).
  return (
    <div {...toolbar.getRootProps()} className="flex gap-1">
      {toolbar.items.map((item) => (
        <button
          key={item.id}
          {...toolbar.getItemProps(item.id)}
          className="rounded px-2 aria-pressed:bg-indigo-100"
        >
          {item.label}
        </button>
      ))}
    </div>
  )
}

Positioning ​

Controllers never position anything. Popups that follow the text (bubble, suggestion lists) expose a reference compatible with Floating UI's virtual elements:

ts
import { autoUpdate, computePosition, flip, offset, shift } from '@floating-ui/dom'

const stop = autoUpdate(bubble.reference, element, () =>
  computePosition(bubble.reference, element, {
    strategy: 'fixed',
    placement: 'top',
    middleware: [offset(8), flip(), shift({ padding: 8 })],
  }).then(({ x, y }) => Object.assign(element.style, { left: `${x}px`, top: `${y}px` })),
)

See Your own toolbar for a complete example.