Skip to content

Commit 218c386

Browse files
committed
Refactor toolbar demo setup and update configs
Removed custom setup and dev scripts in favor of simplified npm scripts for app and WordPress management. Updated .wp-env.json for new PHP version, plugin paths, and config values. Improved README with clearer setup instructions, troubleshooting, and port configuration. Adjusted Vite config for default port fallback and cleaned up package.json scripts.
1 parent 085b439 commit 218c386

File tree

8 files changed

+84
-232
lines changed

8 files changed

+84
-232
lines changed
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
{
2-
"phpVersion": "8.3",
2+
"phpVersion": "8.4",
33
"plugins": [
44
"https://github.com/wp-graphql/wp-graphql/releases/latest/download/wp-graphql.zip",
5-
"../../../../plugins/hwp-cors-local",
6-
"../../../../plugins/hwp-frontend-links",
7-
"../../../../plugins/hwp-wp-env-helpers"
5+
"../../../plugins/hwp-cors-local",
6+
"../../../plugins/hwp-frontend-links",
7+
"../../../plugins/hwp-wp-env-helpers"
88
],
99
"config": {
1010
"WP_DEBUG": true,
11-
"WP_DEBUG_LOG": true,
11+
"SCRIPT_DEBUG": false,
1212
"GRAPHQL_DEBUG": true,
13-
"WP_HOME": "http://localhost:8644",
14-
"HEADLESS_FRONTEND_URL": "http://localhost:3644"
15-
},
16-
"port": 8644,
17-
"testsPort": 8645,
18-
"mappings": {
19-
"db": "./wp-env/db",
20-
".htaccess": "./.htaccess"
13+
"WP_DEBUG_LOG": true,
14+
"WP_DEBUG_DISPLAY": false,
15+
"SAVEQUERIES": false,
16+
"WP_HOME": "http://localhost:8888",
17+
"HEADLESS_FRONTEND_URL": "http://localhost:3000"
2118
},
2219
"lifecycleScripts": {
23-
"afterStart": "wp-env run cli -- wp rewrite structure '/%postname%/' --hard && wp-env run cli -- wp theme activate twentytwentyfour"
20+
"afterStart": "wp-env run cli -- wp theme activate twentytwentyfour && wp-env run cli -- wp rewrite structure '/%postname%/' && wp-env run cli -- wp rewrite flush"
2421
}
2522
}

examples/vanilla/toolbar-demo/README.md

Lines changed: 66 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,27 @@
1-
# Toolbar Demo - Vanilla JavaScript + Vite
1+
# Vanilla JavaScript Toolbar Demo
22

3-
> A complete example of `@wpengine/hwp-toolbar` with vanilla JavaScript and Vite
3+
In this example we show how to integrate the Headless WordPress Toolbar into a vanilla JavaScript application using Vite as the build tool and WordPress backend using WPGraphQL.
44

5-
This example demonstrates how to integrate the Headless WordPress Toolbar into a vanilla JavaScript application using Vite as the build tool.
5+
## Getting Started
66

7-
## Features
8-
9-
- ✅ Vanilla JavaScript (no framework)
10-
- ✅ Vite for fast development
11-
- ✅ TypeScript support (types available)
12-
- ✅ WordPress toolbar integration
13-
- ✅ State management example
14-
- ✅ Custom node registration
15-
- ✅ Dark/light mode support
16-
- ✅ wp-env configuration
17-
18-
## Prerequisites
7+
> [!IMPORTANT]
8+
> Docker Desktop needs to be installed to run WordPress locally.
199
20-
- Node.js >= 18
21-
- pnpm (for workspace setup)
22-
- Docker (for wp-env)
23-
24-
## Quick Start
25-
26-
From the example directory:
27-
28-
```bash
29-
# Install dependencies and start WordPress + Vite
30-
npm run example:build
31-
32-
# Or, if already set up:
33-
npm run example:start
34-
```
10+
1. Run `npm run example:setup` to install dependencies and configure the local WP server.
11+
2. Run `npm run example:start` to start the WP server and Vite development server.
3512

3613
The example will be available at:
3714
- **Frontend**: http://localhost:3000
38-
- **WordPress**: http://localhost:8000
39-
- **WordPress Admin**: http://localhost:8000/wp-admin (`admin` / `password`)
40-
- **GraphQL**: http://localhost:8000/?graphql
15+
- **WordPress**: http://localhost:8888
16+
- **WordPress Admin**: http://localhost:8888/wp-admin (`admin` / `password`)
17+
- **GraphQL**: http://localhost:8888/?graphql
4118

42-
## Project Structure
43-
44-
```
45-
toolbar-demo/
46-
├── example-app/ # Vite application
47-
│ ├── src/
48-
│ │ ├── main.js # Toolbar implementation
49-
│ │ └── style.css # Demo styles
50-
│ ├── index.html # Entry point
51-
│ ├── package.json
52-
│ └── vite.config.js
53-
├── wp-env/ # WordPress environment
54-
│ └── db/ # Database
55-
├── .wp-env.json # wp-env configuration
56-
├── package.json
57-
└── README.md
58-
```
19+
> [!NOTE]
20+
> When you kill the long running process this will not shutdown the local WP instance, only Vite. You must run `npm run example:stop` to kill the local WP server.
5921
6022
## What This Example Shows
6123

62-
### 1. Basic Integration
24+
### 1. Basic Toolbar Integration
6325

6426
```javascript
6527
import { Toolbar, VanillaRenderer } from '@wpengine/hwp-toolbar';
@@ -74,7 +36,7 @@ const toolbar = new Toolbar({
7436
const renderer = new VanillaRenderer(toolbar, 'toolbar');
7537
```
7638

77-
### 2. Real WordPress Integration
39+
### 2. WordPress Integration
7840

7941
The example fetches real data from WordPress:
8042

@@ -83,7 +45,7 @@ The example fetches real data from WordPress:
8345
const response = await fetch('http://localhost:8000/?rest_route=/wp/v2/users/1');
8446
const user = await response.json();
8547

86-
toolbar.setState({
48+
toolbar.setWordPressContext({
8749
user: {
8850
id: user.id,
8951
name: user.name,
@@ -98,8 +60,6 @@ toolbar.setState({
9860

9961
### 3. GraphQL Posts
10062

101-
Fetch posts using WPGraphQL:
102-
10363
```javascript
10464
const response = await fetch('http://localhost:8000/?graphql', {
10565
method: 'POST',
@@ -119,42 +79,68 @@ const response = await fetch('http://localhost:8000/?graphql', {
11979
`
12080
})
12181
});
122-
123-
const { data } = await response.json();
124-
// Use posts to populate toolbar
12582
```
12683

127-
### 4. Custom Nodes
84+
### 4. Custom Node Registration
12885

12986
```javascript
13087
toolbar.register('home', 'Home', () => {
13188
window.location.href = '/';
13289
});
13390
```
13491

135-
### 5. State Subscription
92+
### 5. State Management
13693

13794
```javascript
13895
toolbar.subscribe((nodes, state) => {
13996
console.log('Toolbar state updated:', state);
14097
});
14198
```
14299

100+
## Features
101+
102+
- ✅ Vanilla JavaScript (no framework)
103+
- ✅ Vite for fast development
104+
- ✅ TypeScript support (types available)
105+
- ✅ WordPress toolbar integration
106+
- ✅ State management example
107+
- ✅ Custom node registration
108+
- ✅ Dark/light mode support
109+
- ✅ Real WordPress data integration
110+
- ✅ WPGraphQL support
111+
112+
## Project Structure
113+
114+
```
115+
toolbar-demo/
116+
├── example-app/ # Vite application
117+
│ ├── src/
118+
│ │ ├── main.js # Toolbar implementation
119+
│ │ └── style.css # Demo styles
120+
│ ├── index.html # Entry point
121+
│ ├── package.json
122+
│ └── vite.config.js
123+
├── wp-env/ # WordPress environment
124+
├── .wp-env.json # wp-env configuration
125+
├── package.json
126+
└── README.md
127+
```
128+
143129
## Available Scripts
144130

145131
```bash
146-
# Start development server + WordPress
132+
# Initial setup - installs dependencies and starts WordPress
133+
npm run example:setup
134+
135+
# Start development servers (WordPress + Vite)
147136
npm run example:start
148137

149-
# Stop WordPress
138+
# Stop WordPress server
150139
npm run example:stop
151140

152-
# Rebuild everything from scratch
141+
# Reset everything and start fresh
153142
npm run example:prune
154143

155-
# Just run Vite dev server (requires WordPress running)
156-
npm run example:dev
157-
158144
# WordPress-only commands
159145
npm run wp:start
160146
npm run wp:stop
@@ -164,34 +150,29 @@ npm run wp:destroy
164150
## WordPress Setup
165151

166152
The wp-env configuration includes:
167-
168153
- WordPress with WPGraphQL plugin
169154
- Admin credentials: `admin` / `password`
170155
- GraphQL endpoint: `http://localhost:8000/?graphql`
171156
- REST API endpoint: `http://localhost:8000/?rest_route=/wp/v2/...`
172157
- Pretty permalinks enabled
173158
- CORS headers enabled for localhost:3000
174159

175-
## Using with Vite
160+
## Environment Configuration
176161

177-
Vite provides:
178-
- Hot module replacement (HMR)
179-
- Fast dev server
180-
- ES modules support
181-
- No build step needed for development
162+
The example uses standard ports (3000 for frontend, 8000 for WordPress) to match other hwptoolkit examples.
182163

183-
The toolbar package is imported via workspace protocol:
184-
```json
185-
{
186-
"dependencies": {
187-
"@wpengine/hwp-toolbar": "workspace:*"
188-
}
189-
}
164+
To customize ports, create a `.env` file in the `example-app/` directory:
165+
166+
```
167+
VITE_FRONTEND_PORT=3000
168+
VITE_WP_URL=http://localhost:8888
169+
VITE_WP_PORT=8888
170+
VITE_WP_TEST_PORT=8889
190171
```
191172

192173
## Styling
193174

194-
The example imports the base toolbar styles:
175+
The example imports the base toolbar styles and adds custom demo styling:
195176

196177
```javascript
197178
import '@wpengine/hwp-toolbar/styles';
@@ -206,6 +187,10 @@ Custom styles can override CSS variables:
206187
}
207188
```
208189

190+
## Trouble Shooting
191+
192+
To reset the WP server and re-run setup you can run `npm run example:prune` and confirm "Yes" at any prompts.
193+
209194
## Learn More
210195

211196
- [@wpengine/hwp-toolbar documentation](../../../packages/toolbar/README.md)

examples/vanilla/toolbar-demo/example-app/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"private": true,
55
"type": "module",
66
"scripts": {
7-
"predev": "node ../scripts/setup-env.js",
87
"dev": "vite",
98
"build": "vite build",
109
"preview": "vite preview"

examples/vanilla/toolbar-demo/example-app/vite.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default defineConfig(({ mode }) => {
55

66
return {
77
server: {
8-
port: parseInt(env.VITE_FRONTEND_PORT)
8+
port: parseInt(env.VITE_FRONTEND_PORT || '3000')
99
}
1010
};
1111
});

examples/vanilla/toolbar-demo/package.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
"version": "1.0.0",
44
"description": "Vanilla JavaScript example using Vite for the Headless WordPress Toolbar",
55
"scripts": {
6-
"example:build": "npm run example:dev:install && npm run wp:start && npm run example:start",
7-
"example:dev:install": "cd example-app && npm install && cd ..",
8-
"example:start": "node scripts/setup-env.js && npm install && concurrently \"npm run wp:start\" \"sleep 5 && npm run example:dev\"",
6+
"example:setup": "npm run app:install && npm run wp:start",
7+
"example:start": "concurrently \"npm run wp:start\" \"sleep 5 && npm run app:dev\"",
98
"example:stop": "npm run wp:stop",
10-
"example:prune": "wp-env destroy && npm run example:build && npm run example:start",
11-
"example:dev": "npm --prefix ./example-app run dev",
9+
"example:prune": "npm run wp:destroy && npm run example:setup && npm run example:start",
10+
"app:dev": "cd example-app && npm run dev && cd ..",
11+
"app:install": "cd example-app && npm install && cd ..",
1212
"wp:start": "wp-env start",
1313
"wp:stop": "wp-env stop",
14-
"wp:destroy": "wp-env destroy",
15-
"wp-env": "wp-env"
14+
"wp:destroy": "wp-env destroy"
1615
},
1716
"keywords": [
1817
"headless",

examples/vanilla/toolbar-demo/scripts/dev.sh

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)