Skip to content

Commit cb11fc3

Browse files
committed
Clean up toolbar demo
1 parent a5e31e3 commit cb11fc3

4 files changed

Lines changed: 97 additions & 196 deletions

File tree

examples/vanilla/toolbar-demo/example-app/index.html

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,54 +11,27 @@ <h1>Headless WordPress Toolbar Demo</h1>
1111
<p>A framework-agnostic toolbar for headless WordPress sites</p>
1212

1313
<section class="controls">
14-
<h2>User Context</h2>
14+
<h2>Demo Controls</h2>
1515
<button onclick="login()">Login</button>
1616
<button onclick="logout()">Logout</button>
17-
</section>
18-
19-
<section class="controls">
20-
<h2>Post Context</h2>
2117
<button onclick="fetchPosts()">Fetch Posts</button>
22-
<button onclick="addPost()">Add Mock Post</button>
23-
<button onclick="addPage()">Add Mock Page</button>
24-
<button onclick="clearPost()">Clear Post</button>
2518
<div id="wordpress-posts"></div>
2619
</section>
2720

28-
<section class="controls">
29-
<h2>Toolbar Management</h2>
30-
<button onclick="addNode()">Add Custom Node</button>
31-
<button onclick="clearAll()">Clear All</button>
32-
</section>
33-
3421
<section class="state">
35-
<h2>Current State:</h2>
22+
<h2>Current State</h2>
3623
<pre id="state"></pre>
3724
</section>
3825

3926
<section class="info">
40-
<h2>Try This:</h2>
27+
<h2>Try This</h2>
4128
<ol>
42-
<li>Click "Login" to fetch real WP user</li>
43-
<li>Click "Fetch Posts" to load real posts</li>
44-
<li>Click a post to add it to toolbar</li>
45-
<li>See WordPress controls appear in toolbar</li>
46-
<li>Click "Preview" to toggle preview mode</li>
47-
<li>Click "Edit post" to open in WordPress (opens new tab)</li>
29+
<li>Click "Login" to authenticate with WordPress</li>
30+
<li>Click "Fetch Posts" to load posts from WordPress</li>
31+
<li>Click a post to view toolbar controls</li>
32+
<li>Use the toolbar at the bottom to preview or edit</li>
4833
</ol>
4934
</section>
50-
51-
<section class="features">
52-
<h2>Features:</h2>
53-
<ul>
54-
<li>✅ Framework-agnostic (works with any JS framework)</li>
55-
<li>✅ No 401 errors on page load (performance optimized)</li>
56-
<li>✅ Full TypeScript support</li>
57-
<li>✅ Dark/light mode automatic</li>
58-
<li>✅ Fully themeable with CSS variables</li>
59-
<li>✅ Responsive design</li>
60-
</ul>
61-
</section>
6235
</main>
6336

6437
<!-- Toolbar will be rendered here -->

examples/vanilla/toolbar-demo/example-app/src/main.js

Lines changed: 37 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,52 @@ import './style.css';
44

55
const WP_URL = 'http://localhost:8888';
66

7-
// Create toolbar instance
7+
// ============================================================================
8+
// Initialize Toolbar
9+
// ============================================================================
10+
811
const toolbar = new Toolbar({
912
onPreviewChange: (enabled) => {
1013
console.log('Preview mode:', enabled);
1114
alert(`Preview mode: ${enabled ? 'ON' : 'OFF'}\n\nIn production, this would trigger Next.js/framework preview mode`);
1215
}
1316
});
1417

15-
// Create renderer
18+
// Render toolbar to DOM
1619
const renderer = new VanillaRenderer(toolbar, 'toolbar');
1720

18-
// Subscribe to state changes for display
21+
// ============================================================================
22+
// Register Custom Nodes
23+
// ============================================================================
24+
25+
// Simple home button
26+
toolbar.register('home', 'Home', () => {
27+
console.log('Navigate to home');
28+
window.location.href = '/';
29+
});
30+
31+
// ============================================================================
32+
// State Management
33+
// ============================================================================
34+
35+
// Subscribe to state changes for debugging display
1936
toolbar.subscribe((nodes, state) => {
2037
document.getElementById('state').textContent = JSON.stringify(state, null, 2);
2138
});
2239

23-
// Fetch real WordPress user
40+
// ============================================================================
41+
// Demo Actions
42+
// ============================================================================
43+
2444
window.login = async () => {
2545
try {
26-
// For demo: just set mock authenticated user
27-
// In production, you'd authenticate first via WordPress login
2846
const response = await fetch(`${WP_URL}/?rest_route=/wp/v2/users/1`);
2947

3048
if (response.ok) {
3149
const user = await response.json();
32-
toolbar.setState({
50+
51+
// Use setWordPressContext for WordPress-specific state
52+
toolbar.setWordPressContext({
3353
user: {
3454
id: user.id,
3555
name: user.name,
@@ -40,6 +60,7 @@ window.login = async () => {
4060
url: WP_URL
4161
}
4262
});
63+
4364
console.log('User logged in:', user.name);
4465
} else {
4566
console.error('WordPress not available');
@@ -52,13 +73,16 @@ window.login = async () => {
5273
};
5374

5475
window.logout = () => {
55-
toolbar.setState({
76+
// Clear WordPress context
77+
toolbar.setWordPressContext({
5678
user: null,
5779
post: null,
58-
site: null,
59-
preview: false
80+
site: null
6081
});
61-
toolbar.clear();
82+
83+
// Reset preview mode
84+
toolbar.setState({ preview: false });
85+
6286
console.log('User logged out');
6387
};
6488

@@ -109,7 +133,8 @@ window.fetchPosts = async () => {
109133
};
110134

111135
function loadPost(post) {
112-
toolbar.setState({
136+
// Use setWordPressContext for WordPress post data
137+
toolbar.setWordPressContext({
113138
post: {
114139
id: post.databaseId,
115140
title: post.title,
@@ -121,69 +146,6 @@ function loadPost(post) {
121146
console.log('Post loaded:', post.title);
122147
}
123148

124-
// Mock data functions (for when WordPress isn't running)
125-
window.addPost = () => {
126-
toolbar.setState({
127-
post: {
128-
id: 123,
129-
title: 'Hello World',
130-
type: 'post',
131-
status: 'draft',
132-
slug: 'hello-world'
133-
}
134-
});
135-
console.log('Mock post added');
136-
};
137-
138-
window.addPage = () => {
139-
toolbar.setState({
140-
post: {
141-
id: 456,
142-
title: 'About Us',
143-
type: 'page',
144-
status: 'publish',
145-
slug: 'about'
146-
}
147-
});
148-
console.log('Mock page added');
149-
};
150-
151-
window.clearPost = () => {
152-
toolbar.setState({ post: null, preview: false });
153-
console.log('Post context cleared');
154-
};
155-
156-
let customNodeCount = 0;
157-
window.addNode = () => {
158-
customNodeCount++;
159-
toolbar.register(
160-
`custom-${customNodeCount}`,
161-
`Custom ${customNodeCount}`,
162-
() => {
163-
console.log(`Custom node ${customNodeCount} clicked`);
164-
alert(`Custom Action ${customNodeCount}`);
165-
}
166-
);
167-
console.log(`Added custom node ${customNodeCount}`);
168-
};
169-
170-
window.clearAll = () => {
171-
toolbar.clear();
172-
toolbar.setState({
173-
user: null,
174-
post: null,
175-
site: null,
176-
preview: false
177-
});
178-
console.log('Toolbar cleared');
179-
};
180-
181-
// Initialize with home button
182-
toolbar.register('home', 'Home', () => {
183-
console.log('Navigate to home');
184-
window.location.href = '/';
185-
});
186-
187149
// Console greeting
188150
console.log('%cHeadless WordPress Toolbar', 'font-size: 18px; font-weight: bold; color: #0073aa;');
189151
console.log('WordPress at:', WP_URL);
Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* Minimal Utilitarian Demo Styles */
2-
31
* {
42
box-sizing: border-box;
53
margin: 0;
@@ -10,84 +8,81 @@ body {
108
font-family: system-ui, -apple-system, sans-serif;
119
line-height: 1.5;
1210
color: #333;
13-
background: #fafafa;
11+
background: #f9f9f9;
1412
padding-bottom: 60px;
1513
}
1614

1715
.container {
18-
max-width: 900px;
16+
max-width: 800px;
1917
margin: 0 auto;
20-
padding: 20px;
18+
padding: 16px;
2119
}
2220

2321
h1 {
24-
font-size: 1.5rem;
22+
font-size: 1.25rem;
2523
margin-bottom: 0.25rem;
2624
font-weight: 600;
25+
color: #222;
2726
}
2827

2928
h2 {
30-
font-size: 0.875rem;
31-
margin: 1.5rem 0 0.5rem;
29+
font-size: 0.75rem;
30+
margin: 1rem 0 0.5rem;
3231
text-transform: uppercase;
3332
letter-spacing: 0.5px;
34-
color: #666;
33+
color: #888;
3534
font-weight: 500;
3635
}
3736

3837
p {
39-
font-size: 0.9375rem;
38+
font-size: 0.875rem;
4039
color: #666;
41-
margin-bottom: 1.5rem;
40+
margin-bottom: 1rem;
4241
}
4342

4443
.controls,
4544
.state,
46-
.info,
47-
.features {
48-
margin-bottom: 1.5rem;
49-
padding: 1rem;
45+
.info {
46+
margin-bottom: 1rem;
47+
padding: 0.75rem;
5048
background: white;
51-
border: 1px solid #e0e0e0;
49+
border: 1px solid #e8e8e8;
50+
border-radius: 2px;
5251
}
5352

5453
.controls button {
55-
padding: 6px 12px;
56-
margin: 4px 4px 4px 0;
57-
border: 1px solid #ccc;
54+
padding: 4px 10px;
55+
margin: 3px 3px 3px 0;
56+
border: 1px solid #d0d0d0;
5857
background: white;
59-
color: #333;
58+
color: #555;
6059
cursor: pointer;
61-
font-size: 13px;
60+
font-size: 12px;
61+
border-radius: 2px;
6262
}
6363

6464
.controls button:hover {
65-
background: #f5f5f5;
65+
background: #fafafa;
66+
border-color: #999;
6667
}
6768

6869
.state pre {
69-
background: #f5f5f5;
70-
color: #333;
71-
padding: 0.75rem;
72-
font-size: 0.8125rem;
70+
background: #fafafa;
71+
color: #444;
72+
padding: 0.5rem;
73+
font-size: 0.75rem;
7374
font-family: monospace;
7475
overflow-x: auto;
75-
border: 1px solid #e0e0e0;
76+
border: 1px solid #eee;
77+
border-radius: 2px;
7678
}
7779

78-
.info ol,
79-
.features ul {
80+
.info ol {
8081
margin-left: 1.25rem;
81-
font-size: 0.875rem;
82-
}
83-
84-
.info li,
85-
.features li {
86-
margin: 0.375rem 0;
87-
color: #555;
82+
font-size: 0.8125rem;
8883
}
8984

90-
.features ul {
91-
list-style: none;
92-
margin-left: 0;
85+
.info li {
86+
margin: 0.25rem 0;
87+
color: #666;
9388
}

0 commit comments

Comments
 (0)