@wordpress/create-block-interactive-template 2.8.0 → 2.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,9 +1,11 @@
1
1
  <!-- Learn how to maintain this file at https://github.com/WordPress/gutenberg/tree/HEAD/packages#maintaining-changelogs. -->
2
2
 
3
- ## Unreleased
4
-
5
3
  ## 2.8.0 (2024-09-19)
6
4
 
5
+ ### Enhancements
6
+
7
+ - Added TypeScript variant of the template ([#64577](https://github.com/WordPress/gutenberg/pull/64577)).
8
+
7
9
  ## 2.7.0 (2024-09-05)
8
10
 
9
11
  ### Enhancements
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Create Block Interactive Template
2
2
 
3
- This is a template for [`@wordpress/create-block`](https://github.com/WordPress/gutenberg/tree/HEAD/packages/create-block/README.md) to create interactive blocks
3
+ This is a template for [`@wordpress/create-block`](https://github.com/WordPress/gutenberg/tree/HEAD/packages/create-block/README.md) to create interactive blocks.
4
4
 
5
5
  ## Usage
6
6
 
@@ -3,6 +3,4 @@
3
3
  > **Note**
4
4
  > Check the [Interactivity API Reference docs in the Block Editor handbook](https://developer.wordpress.org/block-editor/reference-guides/interactivity-api/) to learn more about the Interactivity API.
5
5
 
6
- {{#isBasicVariant}}
7
6
  This block has been created with the `create-block-interactive-template` and shows a basic structure of an interactive block that uses the Interactivity API.
8
- {{/isBasicVariant}}
@@ -1,4 +1,3 @@
1
- {{#isBasicVariant}}
2
1
  <?php
3
2
  /**
4
3
  * PHP file to use when rendering the block type on the server to show on the front end.
@@ -11,8 +10,19 @@
11
10
  * @see https://github.com/WordPress/gutenberg/blob/trunk/docs/reference-guides/block-api/block-metadata.md#render
12
11
  */
13
12
 
14
- // Generate unique id for aria-controls.
13
+ // Generates a unique id for aria-controls.
15
14
  $unique_id = wp_unique_id( 'p-' );
15
+
16
+ // Adds the global state.
17
+ wp_interactivity_state(
18
+ '{{namespace}}',
19
+ array(
20
+ 'isDark' => false,
21
+ 'darkText' => esc_html__( 'Switch to Light', '{{textdomain}}' ),
22
+ 'lightText' => esc_html__( 'Switch to Dark', '{{textdomain}}' ),
23
+ 'themeText' => esc_html__( 'Switch to Dark', '{{textdomain}}' ),
24
+ )
25
+ );
16
26
  ?>
17
27
 
18
28
  <div
@@ -20,9 +30,15 @@ $unique_id = wp_unique_id( 'p-' );
20
30
  data-wp-interactive="{{namespace}}"
21
31
  <?php echo wp_interactivity_data_wp_context( array( 'isOpen' => false ) ); ?>
22
32
  data-wp-watch="callbacks.logIsOpen"
33
+ data-wp-class--dark-theme="state.isDark"
23
34
  >
24
35
  <button
25
- data-wp-on--click="actions.toggle"
36
+ data-wp-on--click="actions.toggleTheme"
37
+ data-wp-text="state.themeText"
38
+ ></button>
39
+
40
+ <button
41
+ data-wp-on--click="actions.toggleOpen"
26
42
  data-wp-bind--aria-expanded="context.isOpen"
27
43
  aria-controls="<?php echo esc_attr( $unique_id ); ?>"
28
44
  >
@@ -38,4 +54,3 @@ $unique_id = wp_unique_id( 'p-' );
38
54
  ?>
39
55
  </p>
40
56
  </div>
41
- {{/isBasicVariant}}
@@ -9,4 +9,19 @@
9
9
  font-size: 1em;
10
10
  background: #ffff001a;
11
11
  padding: 1em;
12
+
13
+ &.dark-theme {
14
+ background: #333;
15
+ color: #fff;
16
+
17
+ button {
18
+ background: #555;
19
+ color: #fff;
20
+ border: 1px solid #777;
21
+ }
22
+
23
+ p {
24
+ color: #ddd;
25
+ }
26
+ }
12
27
  }
@@ -1,15 +1,23 @@
1
- {{#isBasicVariant}}
1
+ {{#isDefaultVariant}}
2
2
  /**
3
3
  * WordPress dependencies
4
4
  */
5
- import { store, getContext } from "@wordpress/interactivity";
5
+ import { store, getContext } from '@wordpress/interactivity';
6
6
 
7
- store( '{{namespace}}', {
7
+ const { state } = store( '{{namespace}}', {
8
+ state: {
9
+ get themeText() {
10
+ return state.isDark ? state.darkText : state.lightText;
11
+ }
12
+ },
8
13
  actions: {
9
- toggle: () => {
14
+ toggleOpen() {
10
15
  const context = getContext();
11
16
  context.isOpen = ! context.isOpen;
12
17
  },
18
+ toggleTheme() {
19
+ state.isDark = ! state.isDark;
20
+ }
13
21
  },
14
22
  callbacks: {
15
23
  logIsOpen: () => {
@@ -19,5 +27,4 @@ store( '{{namespace}}', {
19
27
  },
20
28
  },
21
29
  } );
22
-
23
- {{/isBasicVariant}}
30
+ {{/isDefaultVariant}}
@@ -0,0 +1,46 @@
1
+ {{#isTypescriptVariant}}
2
+ /**
3
+ * WordPress dependencies
4
+ */
5
+ import { store, getContext } from '@wordpress/interactivity';
6
+
7
+ type ServerState = {
8
+ state: {
9
+ isDark: boolean;
10
+ darkText: string;
11
+ lightText: string;
12
+ };
13
+ };
14
+
15
+ type Context = {
16
+ isOpen: boolean;
17
+ };
18
+
19
+ const storeDef = {
20
+ state: {
21
+ get themeText(): string {
22
+ return state.isDark ? state.darkText : state.lightText;
23
+ }
24
+ },
25
+ actions: {
26
+ toggleOpen() {
27
+ const context = getContext< Context >();
28
+ context.isOpen = ! context.isOpen;
29
+ },
30
+ toggleTheme() {
31
+ state.isDark = ! state.isDark;
32
+ }
33
+ },
34
+ callbacks: {
35
+ logIsOpen: () => {
36
+ const { isOpen } = getContext< Context >();
37
+ // Log the value of `isOpen` each time it changes.
38
+ console.log( `Is open: ${ isOpen }` );
39
+ },
40
+ },
41
+ };
42
+
43
+ type Store = ServerState & typeof storeDef;
44
+
45
+ const { state } = store< Store >( '{{namespace}}', storeDef );
46
+ {{/isTypescriptVariant}}
package/index.js CHANGED
@@ -7,7 +7,7 @@ module.exports = {
7
7
  defaultValues: {
8
8
  slug: 'example-interactive',
9
9
  title: 'Example Interactive',
10
- description: 'An interactive block with the Interactivity API',
10
+ description: 'An interactive block with the Interactivity API.',
11
11
  dashicon: 'media-interactive',
12
12
  npmDependencies: [ '@wordpress/interactivity' ],
13
13
  customPackageJSON: { files: [ '[^.]*' ] },
@@ -24,7 +24,14 @@ module.exports = {
24
24
  },
25
25
  },
26
26
  variants: {
27
- basic: {},
27
+ default: {},
28
+ typescript: {
29
+ slug: 'example-interactive-typescript',
30
+ title: 'Example Interactive TypeScript',
31
+ description:
32
+ 'An interactive block with the Interactivity API using TypeScript.',
33
+ viewScriptModule: 'file:./view.ts',
34
+ },
28
35
  },
29
36
  pluginTemplatesPath: join( __dirname, 'plugin-templates' ),
30
37
  blockTemplatesPath: join( __dirname, 'block-templates' ),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/create-block-interactive-template",
3
- "version": "2.8.0",
3
+ "version": "2.9.0",
4
4
  "description": "Template for @wordpress/create-block to create interactive blocks with the Interactivity API.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -26,5 +26,5 @@
26
26
  "publishConfig": {
27
27
  "access": "public"
28
28
  },
29
- "gitHead": "cecf5e14d317aa67407f77a7e5c8b6a43016bd42"
29
+ "gitHead": "2e5495c635910cb34bfaca3c6258d2e989f66214"
30
30
  }