|
| 1 | +# @wpengine/hwp-toolbar |
| 2 | + |
| 3 | +> Framework-agnostic toolbar for headless WordPress applications |
| 4 | +
|
| 5 | +A lightweight, performant toolbar for headless WordPress. Works with any JavaScript framework or vanilla JS. |
| 6 | + |
| 7 | +## Table of Contents |
| 8 | + |
| 9 | +- [Features](#features) |
| 10 | +- [Installation](#installation) |
| 11 | +- [Quick Start](#quick-start) |
| 12 | + - [Example Projects](#example-projects) |
| 13 | + - [Vanilla JavaScript](#vanilla-javascript) |
| 14 | + - [React (Recommended)](#react-recommended) |
| 15 | +- [Core API](#core-api) |
| 16 | + - [Toolbar Class](#toolbar-class) |
| 17 | + - [Renderers](#renderers) |
| 18 | +- [Styling](#styling) |
| 19 | +- [React Hooks API](#react-hooks-api) |
| 20 | + - [`useToolbar(toolbar)`](#usetoolbartoolbar) |
| 21 | + - [`useToolbarState(toolbar)`](#usetoolbarstatetoolbar) |
| 22 | + - [`useToolbarNodes(toolbar)`](#usetoolbarnodestoolbar) |
| 23 | +- [Framework Examples](#framework-examples) |
| 24 | + - [Vue](#vue) |
| 25 | + - [Vanilla JavaScript](#vanilla-javascript-1) |
| 26 | +- [TypeScript](#typescript) |
| 27 | +- [Development](#development) |
| 28 | +- [License](#license) |
| 29 | + |
| 30 | + |
| 31 | +## Features |
| 32 | + |
| 33 | +- **Framework Agnostic** - Works with React, Vue, Svelte, or vanilla JavaScript |
| 34 | +- **Zero Dependencies** - Core library has no dependencies |
| 35 | +- **React Hooks** - First-class React support with hooks |
| 36 | +- **Headless UI** - Full control over rendering and styling |
| 37 | + |
| 38 | +## Installation |
| 39 | + |
| 40 | +```bash |
| 41 | +npm install @wpengine/hwp-toolbar |
| 42 | +``` |
| 43 | + |
| 44 | +## Quick Start |
| 45 | + |
| 46 | +### Example Projects |
| 47 | + |
| 48 | +Check out the complete example projects in the `examples/` directory: |
| 49 | + |
| 50 | +- **Next.js**: `examples/next/` - Full Next.js application with Apollo GraphQL integration |
| 51 | +- **Vanilla JavaScript**: `examples/vanilla/` - Pure JavaScript implementation with demo HTML |
| 52 | + |
| 53 | +Each example includes setup instructions and demonstrates different integration patterns. |
| 54 | + |
| 55 | +### Vanilla JavaScript |
| 56 | + |
| 57 | +```javascript |
| 58 | +import { Toolbar, VanillaRenderer } from "@wpengine/hwp-toolbar"; |
| 59 | +import "@wpengine/hwp-toolbar/styles"; |
| 60 | + |
| 61 | +const toolbar = new Toolbar({ |
| 62 | + onPreviewChange: (enabled) => { |
| 63 | + console.log("Preview mode:", enabled); |
| 64 | + }, |
| 65 | +}); |
| 66 | + |
| 67 | +toolbar.setWordPressContext({ |
| 68 | + user: { id: 1, name: "Admin" }, |
| 69 | + site: { url: "https://example.com", adminUrl: "https://example.com/wp-admin" }, |
| 70 | + post: { id: 123, title: "Hello World", type: "post", status: "draft", slug: "hello-world" }, |
| 71 | +}); |
| 72 | + |
| 73 | +const renderer = new VanillaRenderer(toolbar, "toolbar"); |
| 74 | +``` |
| 75 | + |
| 76 | +### React (Recommended) |
| 77 | + |
| 78 | +```tsx |
| 79 | +import { Toolbar } from "@wpengine/hwp-toolbar"; |
| 80 | +import { useToolbar } from "@wpengine/hwp-toolbar/react"; |
| 81 | + |
| 82 | +const toolbar = new Toolbar({ |
| 83 | + onPreviewChange: (enabled) => { |
| 84 | + console.log("Preview mode:", enabled); |
| 85 | + }, |
| 86 | +}); |
| 87 | + |
| 88 | +function MyToolbar() { |
| 89 | + const { state, nodes } = useToolbar(toolbar); |
| 90 | + |
| 91 | + return ( |
| 92 | + <div className='toolbar'> |
| 93 | + {nodes.map((node) => ( |
| 94 | + <button key={node.id} onClick={node.onClick}> |
| 95 | + {typeof node.label === "function" ? node.label() : node.label} |
| 96 | + </button> |
| 97 | + ))} |
| 98 | + {state.user && <span>User: {state.user.name}</span>} |
| 99 | + </div> |
| 100 | + ); |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +## API |
| 105 | + |
| 106 | +### Toolbar |
| 107 | + |
| 108 | +**Constructor** |
| 109 | + |
| 110 | +```javascript |
| 111 | +new Toolbar(config?) |
| 112 | +``` |
| 113 | +
|
| 114 | +**Config:** |
| 115 | +
|
| 116 | +- `onPreviewChange?: (enabled: boolean) => void` - Preview toggle callback |
| 117 | +
|
| 118 | +**Methods:** |
| 119 | +
|
| 120 | +```javascript |
| 121 | +// Register a node |
| 122 | +toolbar.register(id, label, onClick?) |
| 123 | +toolbar.register('help', 'Help', () => window.open('/help')) |
| 124 | + |
| 125 | +// Update state |
| 126 | +toolbar.setState({ user, post, site, preview }) |
| 127 | + |
| 128 | +// Set WordPress context (helper) |
| 129 | +toolbar.setWordPressContext({ user, post, site }) |
| 130 | + |
| 131 | +// Subscribe to changes |
| 132 | +const unsubscribe = toolbar.subscribe((nodes, state) => { |
| 133 | + console.log('State changed:', state); |
| 134 | +}) |
| 135 | + |
| 136 | +// Cleanup |
| 137 | +toolbar.destroy() |
| 138 | +``` |
| 139 | +
|
| 140 | +### VanillaRenderer |
| 141 | +
|
| 142 | +```javascript |
| 143 | +const renderer = new VanillaRenderer(toolbar, "element-id"); |
| 144 | +// or |
| 145 | +const renderer = new VanillaRenderer(toolbar, document.getElementById("toolbar")); |
| 146 | + |
| 147 | +// Cleanup |
| 148 | +renderer.destroy(); |
| 149 | +``` |
| 150 | +
|
| 151 | +## Default Nodes |
| 152 | +
|
| 153 | +The toolbar includes three built-in nodes: |
| 154 | +
|
| 155 | +- **Edit Post** - Opens WordPress editor (visible when post + user) |
| 156 | +- **WP Admin** - Dashboard link (visible when user exists) |
| 157 | +- **Preview** - Toggle preview mode (visible when post or user) |
| 158 | +
|
| 159 | +## Styling |
| 160 | +
|
| 161 | +Import the base styles: |
| 162 | +
|
| 163 | +```javascript |
| 164 | +import "@wpengine/hwp-toolbar/styles"; |
| 165 | +``` |
| 166 | +
|
| 167 | +### Customization |
| 168 | +
|
| 169 | +Override CSS custom properties: |
| 170 | +
|
| 171 | +```css |
| 172 | +:root { |
| 173 | + --hwp-toolbar-bg: #1a1a1a; |
| 174 | + --hwp-toolbar-primary: #00a0d2; |
| 175 | +} |
| 176 | +``` |
| 177 | +
|
| 178 | +## React Hooks API |
| 179 | +
|
| 180 | +### `useToolbar(toolbar)` |
| 181 | +
|
| 182 | +Returns both state and nodes in a single hook: |
| 183 | +
|
| 184 | +```tsx |
| 185 | +import { useToolbar } from "@wpengine/hwp-toolbar/react"; |
| 186 | + |
| 187 | +function MyToolbar() { |
| 188 | + const { state, nodes } = useToolbar(toolbar); |
| 189 | + // Full control over rendering |
| 190 | +} |
| 191 | +``` |
| 192 | +
|
| 193 | +### `useToolbarState(toolbar)` |
| 194 | +
|
| 195 | +Subscribe to toolbar state only: |
| 196 | +
|
| 197 | +```tsx |
| 198 | +import { useToolbarState } from "@wpengine/hwp-toolbar/react"; |
| 199 | + |
| 200 | +function UserDisplay() { |
| 201 | + const state = useToolbarState(toolbar); |
| 202 | + return <div>{state.user?.name}</div>; |
| 203 | +} |
| 204 | +``` |
| 205 | +
|
| 206 | +### `useToolbarNodes(toolbar)` |
| 207 | +
|
| 208 | +Subscribe to visible nodes only: |
| 209 | +
|
| 210 | +```tsx |
| 211 | +import { useToolbarNodes } from "@wpengine/hwp-toolbar/react"; |
| 212 | + |
| 213 | +function ToolbarButtons() { |
| 214 | + const nodes = useToolbarNodes(toolbar); |
| 215 | + return ( |
| 216 | + <> |
| 217 | + {nodes.map((node) => ( |
| 218 | + <button key={node.id} onClick={node.onClick}> |
| 219 | + {typeof node.label === "function" ? node.label() : node.label} |
| 220 | + </button> |
| 221 | + ))} |
| 222 | + </> |
| 223 | + ); |
| 224 | +} |
| 225 | +``` |
| 226 | +
|
| 227 | +## Framework Examples |
| 228 | +
|
| 229 | +### Vue |
| 230 | +
|
| 231 | +```vue |
| 232 | +<template> |
| 233 | + <div ref="toolbarRef" /> |
| 234 | +</template> |
| 235 | + |
| 236 | +<script setup> |
| 237 | +import { onMounted, onUnmounted, ref } from "vue"; |
| 238 | +import { Toolbar, VanillaRenderer } from "@wpengine/hwp-toolbar"; |
| 239 | + |
| 240 | +const toolbarRef = ref(null); |
| 241 | +let toolbar, renderer; |
| 242 | + |
| 243 | +onMounted(() => { |
| 244 | + toolbar = new Toolbar(); |
| 245 | + renderer = new VanillaRenderer(toolbar, toolbarRef.value); |
| 246 | +}); |
| 247 | + |
| 248 | +onUnmounted(() => { |
| 249 | + renderer?.destroy(); |
| 250 | + toolbar?.destroy(); |
| 251 | +}); |
| 252 | +</script> |
| 253 | +``` |
| 254 | +
|
| 255 | +### Vanilla JavaScript |
| 256 | +
|
| 257 | +See `demo.html` for a complete example. |
| 258 | +
|
| 259 | +## Development |
| 260 | +
|
| 261 | +```bash |
| 262 | +# Build |
| 263 | +npm run build |
| 264 | + |
| 265 | +# Watch mode |
| 266 | +npm run dev |
| 267 | + |
| 268 | +# Clean |
| 269 | +npm run clean |
| 270 | + |
| 271 | +# View demo |
| 272 | +open demo.html |
| 273 | +``` |
| 274 | +
|
| 275 | +## License |
| 276 | +
|
| 277 | +BSD-2-Clause |
0 commit comments