Skip to content

Commit ceac43e

Browse files
committed
chore: update docs
1 parent e52f052 commit ceac43e

File tree

20 files changed

+548
-121
lines changed

20 files changed

+548
-121
lines changed

docs/explanation/blocks-faq/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const metadata = {
77

88
If the block does not have any attributes or has only a few of them declared in the `block.json` for that block (you can view the `block.json` file for the block at https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/), you can still try to extend the block API by declaring additional attributes for that block.
99

10-
Follow the [filters reference guide](reference/plugin-filters) to create a block that uses the `additional_block_attributes` property. The attributes will then be available to query from that block.
10+
Follow the [filters reference guide](/docs/reference/use-blocks-theme/) to create a block that uses the `additional_block_attributes` property. The attributes will then be available to query from that block.
1111

1212
```php
1313
class CoreCode extends WPGraphQL\ContentBlocks\Blocks\Block

docs/explanation/deploy-your-app/index.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ Faust.js uses the `package.json` [engines field](https://docs.npmjs.com/cli/v9/c
1414

1515
## Building Your App
1616

17-
Since Faust.js is built on top of Next.js, it shares the same build process. For details on how Next.js generates optimized production builds, refer to the [Next.js Build API](<(https://nextjs.org/docs/pages/building-your-application/deploying#nextjs-build-api)>) documentation. Additionally, you can explore the Headless Platform framework guide for Next.js apps [here](https://developers.wpengine.com/docs/atlas/framework-guides/next-js/next/).
17+
Since Faust.js is built on top of Next.js, it shares the same build process. For details on how Next.js generates optimized production builds, refer to the [Next.js Build API](https://nextjs.org/docs/pages/building-your-application/deploying#nextjs-build-api) documentation. Additionally, you can explore the Headless Platform [framework guide](https://developers.wpengine.com/docs/atlas/framework-guides/next-js/next/) for Next.js apps.
1818

1919
## Deploying to WP Engine's Headless Platform
2020

21-
The Headless Platform is the most effortless way to deploy a Faust.js app. Connect your GitHub repo and Atlas will automatically build and deploy your Faust.js project on each push. Some of the benefits you get out of the box are:
21+
The Headless Platform is the most effortless way to deploy a Faust.js app. Connect your GitHub repo and Headless Platform will automatically build and deploy your Faust.js project on each push. Some of the benefits you get out of the box are:
2222

2323
- Automatic installation of required WordPress plugins (Faust, WPGraphQL, etc.)
2424
- Automatic setup of Faust.js environment variables
@@ -29,7 +29,7 @@ The Headless Platform is the most effortless way to deploy a Faust.js app. Conne
2929

3030
Deploy your Faust.js app and try for the [Headless Platform](https://wpengine.com/headless-wordpress/#form) for free!
3131

32-
For further reference, please check out the Headless Platform framework guide docs on Deploying Next.js [here](https://developers.wpengine.com/docs/atlas/framework-guides/next-js/next/).
32+
For further reference, please check out the Headless Platform [framework guide](https://developers.wpengine.com/docs/atlas/framework-guides/next-js/next/) docs on Deploying Next.js.
3333

3434
> [!NOTE]
3535
> If deploying from the WP Engine portal using your own repository (without beginning with a blueprint), you will need to set your environment variables manually. Learn how by taking a look at our [platform docs](https://developers.wpengine.com/docs/atlas/getting-started/deploy-from-existing-repo/#set-environment-variables-optional).

docs/how-to/react-components-to-blocks/images/colorpicker-controls-for-color-attributes.png renamed to docs/explanation/react-components-to-blocks/images/colorpicker-controls-for-color-attributes.png

File renamed without changes.
21.4 KB
Loading

docs/how-to/react-components-to-blocks/images/react-component-in-preview-mode.png renamed to docs/explanation/react-components-to-blocks/images/react-component-in-preview-mode.png

File renamed without changes.

docs/how-to/react-components-to-blocks/index.mdx renamed to docs/explanation/react-components-to-blocks/index.mdx

Lines changed: 80 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6,137 +6,129 @@ export const metadata = {
66

77
The `@faustwp/block-editor-utils` package provides helper functions for converting React components into blocks. This means you can use the same components in both places—your Next.js app and the WordPress Block Editor.
88

9-
In this how-to guide, we will walk through the steps required to create a new block named `my-first-block` using this workflow.
9+
## Prerequisites
1010

11-
## 0. Prerequisites
11+
In order to use this feature, you need to clone down the working boilerplate example [here](https://github.com/wpengine/faustjs/tree/canary/examples/next/block-support) and follow its instructions in the `README.md` file on what plugins you need, packages you need to install and the command that needs to be run to sync the blocks to WordPress.
1212

13-
Make sure you have followed the [Basic Setup](/docs/how-to/basic-setup/) steps to get Faust.js set up.
13+
> [!WARNING]
14+
> This feature does not work with the latest version of React. It only works with React 18.
15+
> Also, please avoid using this feature in production. It is not fully maintained.
1416
15-
## 1. Install the Dev Dependencies
17+
## Creating and Registering a Block Component
1618

17-
First, from your root folder, install the required dev dependencies:
19+
In the `pages/wp-blocks/block-b/Component.js` file, you will see the following code:
1820

19-
```bash
20-
npm install @wordpress/scripts @faustwp/block-editor-utils --save-dev
21-
```
22-
23-
Now we're ready to explore the process of using this package's helpers to convert a React component to blocks.
24-
25-
## 2. Create and Register a Block Component
26-
27-
For the purposes of this guide we are using the a simple React component. Add a folder called `my-first-block` to the `wp-blocks` folder, then add the following into a jsx file called `MyFirstBlock.jsx`:
21+
```js title="pages/wp-blocks/block-b/Component.js"
22+
import { gql } from "@apollo/client";
2823

29-
```js title="MyFirstBlock.jsx"
30-
function MyFirstBlock({ style, className, attributes, children, ...props }) {
24+
function Component({ style, attributes, children, ...props }) {
3125
const styles = {
3226
...style,
3327
};
34-
const cssClassName = "create-block-my-first-block";
28+
const cssClassName = "create-block-block-b-message";
3529
return (
36-
<div
37-
style={styles}
38-
className={cssClassName}
39-
dangerouslySetInnerHTML={{ __html: attributes.message }}
40-
/>
30+
<>
31+
<div
32+
style={styles}
33+
className={cssClassName}
34+
dangerouslySetInnerHTML={{ __html: attributes.message }}
35+
/>
36+
<div
37+
style={styles}
38+
className="rich-text"
39+
dangerouslySetInnerHTML={{ __html: attributes.richText }}
40+
/>
41+
</>
4142
);
4243
}
4344

44-
MyFirstBlock.config = {
45-
name: "MyFirstBlock",
45+
Component.fragments = {
46+
key: `CreateBlockBlockBFragment`,
47+
entry: gql`
48+
fragment CreateBlockBlockBFragment on CreateBlockBlockB {
49+
attributes {
50+
message
51+
}
52+
}
53+
`,
4654
};
4755

48-
export default MyFirstBlock;
56+
Component.config = {
57+
name: "CreateBlockBlockB",
58+
editorFields: {
59+
message: {
60+
type: "string",
61+
label: "My Message",
62+
location: "editor",
63+
},
64+
},
65+
};
66+
export default Component;
4967
```
5068

51-
This React component consists of a `div` element with three attributes that controls the content and the style of the box. Let's describe them briefly:
69+
This defines a React component that renders two `<div>` elements with inline styles and specific CSS class names, inserting `HTML` content from the `attributes` prop using `dangerouslySetInnerHTML`.
5270

53-
- **message**: is a text message that gets displayed.
54-
- **bg_color**: controls the background color.
55-
- **text_color**: controls the text color.
71+
It also specifies a GraphQL fragment to fetch the necessary data for the component, particularly the message attribute, ensuring the data structure is aligned with the expected GraphQL type. Additionally, the component includes a configuration object that defines its name and editor fields, which is used to integrate and manage the block within a WordPress block editor environment.
5672

57-
The `MyFirstBlock.config` object here with a `name` value inside is used to specify the name of the block.
73+
Within your `block-b/index.js` file, we see the following code:
5874

59-
We would like to use this React component in WordPress using the Block Editor. Traditionally, this step would require us (the developers) to register a block within WordPress, provide a `block.json` and write code for the [Edit and Save](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/) functions for the editor. Once those steps are done, then the block would be usable in the Block Editor list.
60-
61-
That is a lot of know-how and development effort for simply trying to use the React component in the editor side. What if we just provided the React component and let the framework handle all the block registration and creating the editor form fields for changing the content?
62-
63-
This is what the `@faustwp/block-editor-utils` package tries to do. It provides a `registerFaustBlock` helper function, that handles all the necessary configuration, registration and generation of Editor Form fields and [Inspector controls](https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/block-controls-toolbar-and-sidebar/) for you. A developer can provide the React component and are able to review this within the WordPress Block Editor and use it in both places.
64-
65-
Within your `wp-blocks/my-first-block` folder replace the `index.js` contents with the following code:
66-
67-
```js title="index.js"
75+
```js title="block-b/index.js"
76+
import { registerFaustBlock } from "@faustwp/block-editor-utils";
6877
import "./style.scss";
69-
70-
// Import block.json
78+
import BlockB from "./Component.js";
79+
/**
80+
* Block.json metadata
81+
*/
7182
import metadata from "./block.json";
72-
73-
// Import our React component
74-
import MyFirstBlock from "./MyFirstBlock";
75-
76-
import { registerFaustBlock } from "@faustwp/block-editor-utils";
77-
78-
// Register the React component as a Block Editor block
79-
registerFaustBlock(MyFirstBlock, { blockJson: metadata });
83+
/**
84+
* Register React block on the Block Editor
85+
*/
86+
registerFaustBlock(BlockB, {
87+
blockJson: metadata,
88+
});
8089
```
8190

82-
The `registerFaustBlock` helper takes the following arguments:
91+
This file imports the necessary styling, component logic, and metadata to register a React block with the WordPress block editor using the Faust.js framework. It leverages the `registerFaustBlock` utility from the `@faustwp/block-editor-utils` package, passing the component and its configuration metadata to properly initialize the block.
8392

84-
- **component**: the actual React component to convert into a Block Editor block. (**Required**).
85-
- **metadata**: a metadata object that contains several fields:
86-
- **blockJson**: the `block.json` object that describes the component attributes. (**Required**).
87-
- **edit**: provides a custom Edit function that describes the structure of your block in the context of the editor. (**Optional**).
88-
- **save**: provides a custom Save function that defines the way in which the different attributes should be combined into the final markup. (**Optional**).
93+
As a result, the block is seamlessly integrated into the editor, with its visual and functional properties defined by the imported SCSS and JSON metadata.
8994

90-
Now let's take a look at the `block.json`. Since we declared three configurable attributes for our component, we need to declare them as attributes here.
95+
Now let's take a look at the `block.json` file in the `block-b` folder. Since we declared three configurable attributes for our component, we need to declare them as attributes here.
9196

9297
Here is the final `block.json` with the assigned attributes object:
9398

9499
```json
95100
{
96101
"$schema": "https://schemas.wp.org/trunk/block.json",
97102
"apiVersion": 2,
98-
"name": "create-block/my-first-block",
103+
"name": "create-block/block-b",
99104
"version": "0.1.0",
100-
"title": "My First Block",
105+
"title": "Block B",
101106
"category": "widgets",
102107
"icon": "smiley",
103-
"description": "Example block scaffolded with Create Block tool.",
108+
"description": "Example static block scaffolded with Create Block tool.",
104109
"supports": {
105110
"html": false
106111
},
107112
"attributes": {
108113
"message": {
109114
"type": "string",
110-
"default": "My First Block"
115+
"default": "Hello World"
111116
},
112-
"bg_color": { "type": "string", "default": "#000000" },
113-
"text_color": { "type": "string", "default": "#ffffff" }
117+
"richText": {
118+
"type": "string",
119+
"source": "html",
120+
"selector": ".rich-text",
121+
"default": "Hello World"
122+
}
114123
},
115-
"textdomain": "my-first-block",
124+
"textdomain": "block-b",
116125
"editorScript": "file:./index.js",
117126
"editorStyle": "file:./index.css",
118127
"style": "file:./style-index.css"
119128
}
120129
```
121130

122-
### 3. Sync the block with WordPress
123-
124-
Add the following `blockset` script to your `package.json` file, then run it on the command line:
125-
126-
```json title="package.json"
127-
"scripts": {
128-
...
129-
"blockset": "faust blockset"
130-
},
131-
```
132-
133-
```sh
134-
npm run blockset
135-
```
136-
137-
The cli tool will compile the blocks within the `wp-blocks` folder and upload them to your WordPress site in a special location that Faust uses to detect and register the blocks.
138-
139-
## 4. Try out the Component in the Block Editor
131+
## Try out the Component in the Block Editor
140132

141133
Open the WordPress Block Editor and try out the new block. This is what it will look like at first glance in Edit mode:
142134

@@ -146,22 +138,22 @@ You can interact with the form fields, and then click outside the block contents
146138

147139
![React Component in Preview Mode.](./images/react-component-in-preview-mode.png)
148140

149-
## 5. Configure the Form Controls
141+
## Configure the Form Controls
150142

151-
So far we've been able to render the React component in the Block Editor, change some of the attributes, and reflect the changes in the page.
143+
So far we've been able to render the React component in the Block Editor.
152144

153145
However, a few of the attributes that control the color are using [text field](https://developer.wordpress.org/block-editor/reference-guides/components/text-control/) controls, which may prove problematic since they allow invalid values. What if we wanted to use a proper color picker component?
154146

155147
Since the `block.json` [attribute types](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/#type-validation) do not allow `color` as a value, we will have to provide a different configuration to allow that option.
156148

157149
When registering the React component using `registerFaustBlock`, it allows extra configuration to be used in case you want to declare which kinds of controls to use on each attribute.
158150

159-
In your `MyFirstBlock.jsx` file, add the following `editorFields` to your `MyFirstBlock.config` object:
151+
In your `Component.js` file, add the following `editorFields` to your `Component.config` object:
160152

161-
```js title="MyFirstBlock.jsx"
153+
```js title="block-b/Component.js"
162154
...
163-
MyFirstBlock.config = {
164-
name: "MyFirstBlock",
155+
Component.config = {
156+
name: "CreateBlockBlockB",
165157
editorFields: {// [!code ++]
166158
bg_color: {// [!code ++]
167159
location: "inspector",// [!code ++]
@@ -181,7 +173,7 @@ Once you update the component, you can refresh the page and create a new block.
181173

182174
![Using ColorPicker controls for the color attributes.](./images/colorpicker-controls-for-color-attributes.png)
183175

184-
## 6. Form Control Reference List
176+
## Form Control Reference List
185177

186178
So far we've seen examples of two controls: The `ColorPicker` handled by the `control: "color"` and the `TextControl`, which is set as default for every `type: "string"` in the `block.json` attributes list. You can experiment with adding more, however.
187179

docs/how-to/basic-setup/index.mdx

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Create a `faust.config.js` file in the root of your project with this code:
5656

5757
```js title="faust.config.js"
5858
import { setConfig } from "@faustwp/core";
59-
import templates from "./wp-templates";
59+
import templates from "./src/wp-templates";
6060
import possibleTypes from "./possibleTypes.json";
6161

6262
/**
@@ -71,24 +71,27 @@ export default setConfig({
7171

7272
### E. Create Faust API route
7373

74-
Create an API route for Faust.js to use. You can do this by creating a file in `pages/api/faust/[[...route]].js`, with the following code:
74+
Create an API route for Faust.js to use. You can do this by creating a file in `src/pages/api/faust/[[...route]].js`, with the following code:
7575

76-
```js title="pages/api/faust/[[...route]].js"
77-
import "../../../faust.config";
76+
```js title="src/pages/api/faust/[[...route]].js"
77+
import "../../faust.config"; // Adjust path as necessary
7878

7979
export { apiRouter as default } from "@faustwp/core";
8080
```
8181

82+
> [!IMPORTANT] Important
83+
> If you're not using a `src` folder in your project, you can omit "src/" from the file path above. And the same applies for other file paths mentioned throughout this doc.
84+
8285
### F. Update `_app.js` file
8386

84-
Once the API router is set up, head to `pages/_app.js` and add this code to the file:
87+
Once the API router is set up, head to `src/pages/_app.js`. Add the `import` statements, wrap your app in the `FaustProvider`, and pass `key={router.asPath}` to `Component`, as shown below.
8588

86-
```js title="pages/_app.js"
89+
```js title="src/pages/_app.js"
8790
import { useRouter } from "next/router";
8891
import { FaustProvider } from "@faustwp/core";
89-
import "../faust.config";
92+
import "../../faust.config";
9093

91-
export default function MyApp({ Component, pageProps }) {
94+
export default function App({ Component, pageProps }) {
9295
const router = useRouter();
9396

9497
return (
@@ -105,6 +108,8 @@ export default function MyApp({ Component, pageProps }) {
105108

106109
In order for Faust.js to run the GraphQL queries it needs to determine the correct template to use, it needs to have a list of all the types available in the GraphQL schema. We'll generate this list of types now.
107110

111+
In your WordPress admin sidebar, go to `GraphQL` > `Settings`. Check the `Enable Public Introspection` box if it's not checked already and save your changes. Enabling introspection is required for the `npm run generate` command below to work.
112+
108113
Add the following generate script to your Next.js project's `package.json` file, in the scripts block:
109114

110115
```json title="package.json"
@@ -124,7 +129,7 @@ Run `npm run generate` on the command line. Confirm that a possibleTypes.json ha
124129
125130
### B. Add a template
126131

127-
Create a new `wp-templates` folder in the root of your project. This is where your template files will be stored. We'll start by adding a template for rendering single blog posts.
132+
Create a new `src/wp-templates` folder (or add the `/wp-templates` folder in the root project folder if you don't use a `/src` folder). This is where your template files will be stored. We'll start by adding a template for rendering single blog posts.
128133

129134
Inside the `wp-templates` folder, create a `single.js` file that contains the following code.
130135

@@ -167,10 +172,10 @@ In the `SingleTemplate` component, we receive the props, destructure the `title`
167172
Finally, we have to make Faust.js aware that this template exists. To do that, create an `index.js` file inside the `wp-templates` folder with this code inside:
168173
169174
```js title="wp-templates/index.js"
170-
import single from "./single";
175+
import SingleTemplate from "./single";
171176

172177
const templates = {
173-
single,
178+
single: SingleTemplate,
174179
};
175180

176181
export default templates;
@@ -180,7 +185,7 @@ export default templates;
180185
181186
Create a `[...wordpressNode].js` file inside your `pages` folder. Add the following code to it.
182187
183-
```js title="pages/[...wordpressNode].js"
188+
```js title="src/pages/[...wordpressNode].js"
184189
import { getWordPressProps, WordPressTemplate } from "@faustwp/core";
185190

186191
export default function Page(props) {
@@ -201,7 +206,7 @@ export async function getStaticPaths() {
201206
202207
This catch-all route tells Next.js to use the templates to render pages.
203208
204-
Note that it is still possible to override this with hardcoded pages. For example, if you have a page in your WordPress site with a URI of `/about`, Faust.js will render that page using the relevant template in your project's `wp-templates` folder. However, if you add a `pages/about.js` file to your project, Next.js will render the `/about` path in your app using that file instead.
209+
Note that it is still possible to override this with hardcoded pages. For example, if you have a page in your WordPress site with a URI of `/about`, Faust.js will render that page using the relevant template in your project's `wp-templates` folder. However, if you add a `src/pages/about.js` file to your project, Next.js will render the `/about` path in your app using that file instead.
205210
206211
### D. Test your template
207212
Binary file not shown.

0 commit comments

Comments
 (0)