Skip to content

Commit 6702681

Browse files
authored
Merge pull request #78 from jverneaut/76-add-new-api-to-explicitly-define-attributes
76 Add new api to explicitly define attributes
2 parents 6d8965b + df6ef47 commit 6702681

18 files changed

Lines changed: 291 additions & 8 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { registerBlockType } from "@wordpress/blocks";
2+
3+
import Edit from "./edit.js";
4+
import metadata from "./block.json";
5+
6+
registerBlockType(metadata.name, {
7+
edit: Edit,
8+
save: () => null,
9+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { useBlockProps, RichText } from "@wordpress/block-editor";
2+
3+
export default ({ attributes, setAttributes }) => {
4+
return (
5+
<section {...useBlockProps()}>
6+
<RichText
7+
tagName="h2"
8+
value={attributes.title}
9+
onChange={(title) => setAttributes({ title })}
10+
placeholder="Title"
11+
></RichText>
12+
</section>
13+
);
14+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "custom/input",
3+
"title": "Input",
4+
"textdomain": "input",
5+
"$schema": "https://schemas.wp.org/trunk/block.json",
6+
"apiVersion": 3,
7+
"version": "0.1.0",
8+
"category": "theme",
9+
"example": {},
10+
"attributes": {
11+
"align": { "type": "string", "default": "full" },
12+
"title": { "default": "Default title", "type": "string" }
13+
},
14+
"supports": { "html": false, "align": ["full"] },
15+
"editorScript": "file:./index.js",
16+
"render": "file:./render.php"
17+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<section <?php echo get_block_wrapper_attributes(); ?>>
2+
3+
4+
<h2><?php echo wp_kses_post($attributes['title'] ?? ''); ?></h2>
5+
</section>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<section>
2+
<!-- This has precedence over the data-attribute attribute -->
3+
<block-attribute name="title" default="Default title"></block-attribute>
4+
5+
<h2 data-attribute="title"></h2>
6+
</section>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { registerBlockType } from "@wordpress/blocks";
2+
3+
import Edit from "./edit.js";
4+
import metadata from "./block.json";
5+
6+
registerBlockType(metadata.name, {
7+
edit: Edit,
8+
save: () => null,
9+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { useBlockProps, RichText } from "@wordpress/block-editor";
2+
3+
export default ({ attributes, setAttributes }) => {
4+
return <section {...useBlockProps()}></section>;
5+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "custom/input",
3+
"title": "Input",
4+
"textdomain": "input",
5+
"$schema": "https://schemas.wp.org/trunk/block.json",
6+
"apiVersion": 3,
7+
"version": "0.1.0",
8+
"category": "theme",
9+
"example": {},
10+
"attributes": {
11+
"align": { "type": "string", "default": "full" },
12+
"stringAttrWithoutType": { "type": "string" },
13+
"stringAttrWithType": { "type": "string" },
14+
"stringAttrWithDefault": { "default": "value", "type": "string" },
15+
"stringAttrWithTypeAndDefault": { "default": "value", "type": "string" },
16+
"integerAttrWithType": { "type": "integer" },
17+
"integerAttrWithDefault": { "default": 5, "type": "integer" },
18+
"integerAttrWithTypeAndDefault": { "default": 5, "type": "integer" },
19+
"numberAttrWithType": { "type": "number" },
20+
"numberAttrWithDefault": { "default": 0.1, "type": "number" },
21+
"numberAttrWithTypeAndDefault": { "default": 0.1, "type": "number" },
22+
"booleanAttrWithType": { "type": "boolean" },
23+
"booleanAttrWithDefaultTrue": { "default": true, "type": "boolean" },
24+
"booleanAttrWithDefaultFalse": { "default": false, "type": "boolean" },
25+
"booleanAttrWithTypeAndDefaultTrue": { "default": true, "type": "boolean" },
26+
"booleanAttrWithTypeAndDefaultFalse": {
27+
"default": false,
28+
"type": "boolean"
29+
}
30+
},
31+
"supports": { "html": false, "align": ["full"] },
32+
"editorScript": "file:./index.js",
33+
"render": "file:./render.php"
34+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<section <?php echo get_block_wrapper_attributes(); ?>>
2+
3+
4+
5+
6+
7+
8+
9+
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
</section>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<section>
2+
<block-attribute name="stringAttrWithoutType"></block-attribute>
3+
<block-attribute name="stringAttrWithType" type="string"></block-attribute>
4+
<block-attribute
5+
name="stringAttrWithDefault"
6+
default="value"
7+
></block-attribute>
8+
<block-attribute
9+
name="stringAttrWithTypeAndDefault"
10+
type="string"
11+
default="value"
12+
></block-attribute>
13+
14+
<block-attribute name="integerAttrWithType" type="integer"></block-attribute>
15+
<block-attribute name="integerAttrWithDefault" default="5"></block-attribute>
16+
<block-attribute
17+
name="integerAttrWithTypeAndDefault"
18+
type="integer"
19+
default="5"
20+
></block-attribute>
21+
22+
<block-attribute name="numberAttrWithType" type="number"></block-attribute>
23+
<block-attribute name="numberAttrWithDefault" default="0.1"></block-attribute>
24+
<block-attribute
25+
name="numberAttrWithTypeAndDefault"
26+
type="number"
27+
default="0.1"
28+
></block-attribute>
29+
30+
<block-attribute name="booleanAttrWithType" type="boolean"></block-attribute>
31+
<block-attribute
32+
name="booleanAttrWithDefaultTrue"
33+
default="true"
34+
></block-attribute>
35+
<block-attribute
36+
name="booleanAttrWithDefaultFalse"
37+
default="false"
38+
></block-attribute>
39+
<block-attribute
40+
name="booleanAttrWithTypeAndDefaultTrue"
41+
type="boolean"
42+
default="true"
43+
></block-attribute>
44+
<block-attribute
45+
name="booleanAttrWithTypeAndDefaultFalse"
46+
type="boolean"
47+
default="false"
48+
></block-attribute>
49+
</section>

0 commit comments

Comments
 (0)