@@ -7,10 +7,10 @@ A lightweight, performant toolbar for headless WordPress. Works with any JavaScr
77## Features
88
99- 🎯 ** Framework Agnostic** - Works with React, Vue, Svelte, or vanilla JavaScript
10- - ⚡ ** Zero Dependencies** - Lightweight and fast
10+ - ⚡ ** Zero Dependencies** - Core library has no dependencies
1111- 🔒 ** Type Safe** - Full TypeScript support
12- - 🎨 ** Themeable ** - CSS custom properties for styling
13- - 🌓 ** Dark Mode ** - Automatic support
12+ - 🪝 ** React Hooks ** - First-class React support with hooks
13+ - 🎨 ** Headless UI ** - Full control over rendering and styling
1414
1515## Installation
1616
@@ -20,28 +20,55 @@ npm install @wpengine/hwp-toolbar
2020
2121## Quick Start
2222
23+ ### Vanilla JavaScript
24+
2325``` javascript
2426import { Toolbar , VanillaRenderer } from ' @wpengine/hwp-toolbar' ;
2527import ' @wpengine/hwp-toolbar/styles' ;
2628
27- // Create toolbar
2829const toolbar = new Toolbar ({
2930 onPreviewChange : (enabled ) => {
3031 console .log (' Preview mode:' , enabled);
3132 }
3233});
3334
34- // Set WordPress context
3535toolbar .setWordPressContext ({
3636 user: { id: 1 , name: ' Admin' },
3737 site: { url: ' https://example.com' , adminUrl: ' https://example.com/wp-admin' },
3838 post: { id: 123 , title: ' Hello World' , type: ' post' , status: ' draft' , slug: ' hello-world' }
3939});
4040
41- // Render
4241const renderer = new VanillaRenderer (toolbar, ' toolbar' );
4342```
4443
44+ ### React (Recommended)
45+
46+ ``` tsx
47+ import { Toolbar } from ' @wpengine/hwp-toolbar' ;
48+ import { useToolbar } from ' @wpengine/hwp-toolbar/react' ;
49+
50+ const toolbar = new Toolbar ({
51+ onPreviewChange : (enabled ) => {
52+ console .log (' Preview mode:' , enabled );
53+ }
54+ });
55+
56+ function MyToolbar() {
57+ const { state, nodes } = useToolbar (toolbar );
58+
59+ return (
60+ <div className = " toolbar" >
61+ { nodes .map (node => (
62+ <button key = { node .id } onClick = { node .onClick } >
63+ { typeof node .label === ' function' ? node .label () : node .label }
64+ </button >
65+ ))}
66+ { state .user && <span >User: { state .user .name } </span >}
67+ </div >
68+ );
69+ }
70+ ```
71+
4572## API
4673
4774### Toolbar
@@ -122,34 +149,57 @@ Available variables:
122149- ` -- hwp- toolbar- primary- hover` - Primary button hover
123150- And more...
124151
125- ## Framework Examples
152+ ## React Hooks API
153+
154+ ### ` useToolbar (toolbar)`
126155
127- ### React
156+ Returns both state and nodes in a single hook:
128157
129158` ` ` tsx
130- import { useEffect , useRef } from ' react' ;
131- import { Toolbar , VanillaRenderer } from ' @wpengine/hwp-toolbar' ;
159+ import { useToolbar } from ' @wpengine/hwp-toolbar/react' ;
160+
161+ function MyToolbar () {
162+ const { state , nodes } = useToolbar (toolbar);
163+ // Full control over rendering
164+ }
165+ ` ` `
166+
167+ ### ` useToolbarState (toolbar)`
132168
133- function WordPressToolbar ({ user, post, site }) {
134- const ref = useRef< HTMLDivElement > (null );
169+ Subscribe to toolbar state only:
135170
136- useEffect (() => {
137- if (! ref .current ) return ;
171+ ` ` ` tsx
172+ import { useToolbarState } from ' @wpengine/hwp-toolbar/react' ;
173+
174+ function UserDisplay () {
175+ const state = useToolbarState (toolbar);
176+ return < div> {state .user ? .name }< / div> ;
177+ }
178+ ` ` `
138179
139- const toolbar = new Toolbar ();
140- toolbar .setWordPressContext ({ user, post, site });
141- const renderer = new VanillaRenderer (toolbar, ref .current );
180+ ### ` useToolbarNodes (toolbar)`
142181
143- return () => {
144- renderer .destroy ();
145- toolbar .destroy ();
146- };
147- }, [user, post, site]);
182+ Subscribe to visible nodes only:
148183
149- return < div ref= {ref} / > ;
184+ ` ` ` tsx
185+ import { useToolbarNodes } from ' @wpengine/hwp-toolbar/react' ;
186+
187+ function ToolbarButtons () {
188+ const nodes = useToolbarNodes (toolbar);
189+ return (
190+ <>
191+ {nodes .map (node => (
192+ < button key= {node .id } onClick= {node .onClick }>
193+ {typeof node .label === ' function' ? node .label () : node .label }
194+ < / button>
195+ ))}
196+ < / >
197+ );
150198}
151199` ` `
152200
201+ ## Framework Examples
202+
153203### Vue
154204
155205` ` ` vue
0 commit comments