Skip to content

Commit c5bc686

Browse files
authored
Merge pull request #248 from wpengine/docs/how-to-customize-the-toolbar
Docs: How To Customize The Toolbar
2 parents 7b3b24e + 2349803 commit c5bc686

File tree

4 files changed

+175
-0
lines changed

4 files changed

+175
-0
lines changed

next.config.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ const nextConfig = {
3030
destination: "/docs/how-to/sitemaps/",
3131
permanent: true,
3232
},
33+
{
34+
source: "/guide/how-to-customize-the-toolbar",
35+
destination: "/docs/how-to/customize-the-toolbar/",
36+
permanent: true,
37+
},
3338
{
3439
source: "/guide/setting-up-custom-post-types-cpts-in-faust",
3540
destination: "/docs/how-to/setup-cpt-in-faustjs/",
132 KB
Loading
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
export const metadata = {
2+
title: "Customize The Toolbar",
3+
description:
4+
"Learn how to customize the Faust.js Toolbar using the toolbarNodes filter. We'll walk you through the process of enabling the experimental toolbar feature, adding authentication, and creating a custom plugin to extend the Toolbar with new menu items and submenus.",
5+
};
6+
7+
This guide covers how to customize your site's Toolbar utilizing the `toolbarNodes` filter.
8+
9+
## 0. Prerequisites
10+
11+
Ensure that you have completed the steps in the following pages before proceeding.
12+
13+
- [Basic setup](/docs/how-to/basic-setup)
14+
15+
- [Set up authentication](/docs/how-to/authentication/)
16+
17+
Implementing authentication is necessary so that Faust.js "knows" whether the current user has the permissions necessary to view and access the toolbar.
18+
19+
> [!WARNING]
20+
> The Toolbar was an experimental feature introduced in `@faustwp/[email protected]`. As of [`@faustwp/[email protected]`](https://github.com/wpengine/faustjs/releases/tag/%40faustwp%2Fcore%403.2.0) this feature is deprecated and will no longer be maintained.
21+
22+
## 1. Add Toolbar
23+
24+
The Faust Toolbar shares mostly the the same HTML as the WordPress core Toolbar. This enables the use of the same styles that exist in WordPress core, and have been provided for you to import within `@faustwp/core`.
25+
26+
```js title="pages/_app.js"
27+
import "@faustwp/core/dist/css/toolbar.css";
28+
```
29+
30+
Add `experimentalToolbar: true` to your project's `faust.config.js`.
31+
32+
```js title="faust.config.js"
33+
import { setConfig } from "@faustwp/core";
34+
import templates from "./wp-templates";
35+
import possibleTypes from "./possibleTypes.json";
36+
37+
export default setConfig({
38+
experimentalToolbar: true, // Enable experimental toolbar
39+
templates,
40+
possibleTypes,
41+
});
42+
```
43+
44+
## 2. Login to your WP Admin
45+
46+
The Toolbar was designed to only load for the authenticated user.
47+
48+
> [!IMPORTANT]
49+
> A WordPress user will be automatically authenticated with your site when previewing a post. This approach can be used as a quick way to view the Toolbar for development purposes.
50+
51+
## 3. Create The Plugin
52+
53+
Create a new file in your Faust project: `plugins/CustomPlugin.tsx`.
54+
55+
Add the following code to `plugins/CustomPlugin.tsx`:
56+
57+
```ts title="plugins/CustomPlugin.tsx"
58+
59+
import React from 'react';
60+
import {
61+
FaustHooks,
62+
FaustPlugin,
63+
FaustToolbarNodes,
64+
FaustToolbarContext,
65+
ToolbarItem,
66+
ToolbarSubmenu,
67+
ToolbarSubmenuWrapper,
68+
} from '@faustwp/core';
69+
70+
/**
71+
* Example Custom Toolbar Plugin.
72+
*/
73+
export class CustomToolbar implements FaustPlugin {
74+
apply(hooks: FaustHooks) {
75+
/**
76+
* This example demonstrates how to filter on the core Toolbar nodes
77+
* in order to add your own custom nodes!
78+
*/
79+
hooks.addFilter(
80+
'toolbarNodes',
81+
'faust',
82+
(toolbarNodes: FaustToolbarNodes, context: FaustToolbarContext) => {
83+
const customToolbarNodes: FaustToolbarNodes = [
84+
{
85+
id: 'custom-node',
86+
location: 'primary',
87+
component: <CustomNode />,
88+
},
89+
{
90+
id: 'custom-node-with-submenu',
91+
location: 'primary',
92+
component: <CustomNodeWithSubmenu />,
93+
},
94+
];
95+
96+
return [...toolbarNodes, ...customToolbarNodes];
97+
},
98+
);
99+
}
100+
}
101+
102+
/**
103+
* A simple link.
104+
*/
105+
export function CustomNode() {
106+
return (
107+
<ToolbarItem href="https://wpengine.com" rel="nofollow">
108+
Custom Node
109+
</ToolbarItem>
110+
);
111+
}
112+
113+
/**
114+
* A simple link with a submenu that displays on hover.
115+
*/
116+
export function CustomNodeWithSubmenu() {
117+
return (
118+
<>
119+
<ToolbarItem href="https://wpengine.com" rel="nofollow">
120+
Custom Node w/ Submenu
121+
</ToolbarItem>
122+
<ToolbarSubmenuWrapper>
123+
<ToolbarSubmenu>
124+
<li>
125+
<ToolbarItem href="https://wpengine.com" rel="nofollow">
126+
Link
127+
</ToolbarItem>
128+
</li>
129+
<li>
130+
<ToolbarItem href="https://wpengine.com" rel="nofollow">
131+
Link
132+
</ToolbarItem>
133+
</li>
134+
<li>
135+
<ToolbarItem href="https://wpengine.com" rel="nofollow">
136+
Link
137+
</ToolbarItem>
138+
</li>
139+
</ToolbarSubmenu>
140+
</ToolbarSubmenuWrapper>
141+
</>
142+
);
143+
}
144+
```
145+
146+
## 4. Register The Plugin
147+
148+
Import and register your new plugin in `faust.config.js`:
149+
150+
```js title="faust.config.js"
151+
import { setConfig } from "@faustwp/core";
152+
import templates from "./wp-templates";
153+
import possibleTypes from "./possibleTypes.json";
154+
import { CustomToolbar } from "./plugins/CustomPlugin.tsx";
155+
156+
export default setConfig({
157+
templates,
158+
experimentalToolbar: true,
159+
plugins: [new CustomToolbar()], // Register plugin
160+
possibleTypes,
161+
});
162+
```
163+
164+
You should now be able to see your new Custom Nodes and toolbar at the top of the page if you visit a route as an authenticated user like so:
165+
166+
![A customized Faust.js Toolbar displaying newly added toolbar nodes, including a custom menu item and a submenu, visible at the top of the page for authenticated users.](./images/custom-toolbar-nodes.png)

src/pages/docs/nav.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
"title": "Basic Setup",
1212
"route": "/docs/how-to/basic-setup/"
1313
},
14+
{
15+
"title": "Customize the Toolbar",
16+
"route": "/docs/how-to/customize-the-toolbar/"
17+
},
1418
{
1519
"title": "Generate types with GraphQL Codegen",
1620
"route": "/docs/how-to/generate-types-with-graphql-codegen/"

0 commit comments

Comments
 (0)