-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Interactivity API: router with region-based client-side navigation (#…
…53733) * Add router with region-based client-side navigation * Add changelog * Interactivity API: merge new server-side rendered context on client-side navigation (#53853) * Add failing test * Fix the test * Add changelog * Fix lint error * Fix changelog placement * Interactivity API: Support for the `data-wp-key` directive (#53844) * Add failing test * Fix test using key * Replace key with data-wp-key * Refactor test a bit * Add changelog * Add docs * Remove unnecessary paragraph * Fix lint error * Interactivity API: Fix non stable context reference on client side navigation (#53876) * Add failing test * Fix the test * Refactor addPostWithBlock util * Add tests for router regions --------- Co-authored-by: David Arenas <david.arenas@automattic.com>
- Loading branch information
1 parent
4316be5
commit 640150a
Showing
19 changed files
with
611 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/e2e-tests/plugins/interactive-blocks/directive-key/block.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"apiVersion": 2, | ||
"name": "test/directive-key", | ||
"title": "E2E Interactivity tests - directive key", | ||
"category": "text", | ||
"icon": "heart", | ||
"description": "", | ||
"supports": { | ||
"interactivity": true | ||
}, | ||
"textdomain": "e2e-interactivity", | ||
"viewScript": "directive-key-view", | ||
"render": "file:./render.php" | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/e2e-tests/plugins/interactive-blocks/directive-key/render.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
/** | ||
* HTML for testing the directive `data-wp-key`. | ||
* | ||
* @package gutenberg-test-interactive-blocks | ||
*/ | ||
|
||
?> | ||
|
||
<div data-wp-interactive data-wp-navigation-id="some-id"> | ||
<ul> | ||
<li data-wp-key="id-2" data-testid="first-item">2</li> | ||
<li data-wp-key="id-3">3</li> | ||
</ul> | ||
<button data-testid="navigate" data-wp-on--click="actions.navigate"> | ||
Navigate | ||
</button> | ||
</div> |
23 changes: 23 additions & 0 deletions
23
packages/e2e-tests/plugins/interactive-blocks/directive-key/view.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
( ( { wp } ) => { | ||
const { store, navigate } = wp.interactivity; | ||
|
||
const html = ` | ||
<div data-wp-interactive data-wp-navigation-id="some-id"> | ||
<ul> | ||
<li data-wp-key="id-1">1</li> | ||
<li data-wp-key="id-2" data-testid="second-item">2</li> | ||
<li data-wp-key="id-3">3</li> | ||
</ul> | ||
</div>`; | ||
|
||
store( { | ||
actions: { | ||
navigate: () => { | ||
navigate( window.location, { | ||
force: true, | ||
html, | ||
} ); | ||
}, | ||
}, | ||
} ); | ||
} )( window ); |
14 changes: 14 additions & 0 deletions
14
packages/e2e-tests/plugins/interactive-blocks/router-regions/block.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"apiVersion": 2, | ||
"name": "test/router-regions", | ||
"title": "E2E Interactivity tests - router regions", | ||
"category": "text", | ||
"icon": "heart", | ||
"description": "", | ||
"supports": { | ||
"interactivity": true | ||
}, | ||
"textdomain": "e2e-interactivity", | ||
"viewScript": "router-regions-view", | ||
"render": "file:./render.php" | ||
} |
89 changes: 89 additions & 0 deletions
89
packages/e2e-tests/plugins/interactive-blocks/router-regions/render.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
/** | ||
* HTML for testing the hydration of router regions. | ||
* | ||
* @package gutenberg-test-interactive-blocks | ||
* @phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable | ||
*/ | ||
|
||
?> | ||
|
||
<section> | ||
<h2>Region 1</h2> | ||
<div data-wp-interactive data-wp-navigation-id="region-1"> | ||
<p | ||
data-testid="region-1-text" | ||
data-wp-text="state.region1.text" | ||
>not hydrated</p> | ||
<p | ||
data-testid="region-1-ssr" | ||
>content from page <?php echo $attributes['page']; ?></p> | ||
|
||
<button | ||
data-testid="state-counter" | ||
data-wp-text="state.counter.value" | ||
data-wp-on--click="actions.counter.increment" | ||
>NaN</button> | ||
|
||
<?php if ( isset( $attributes['next'] ) ) : ?> | ||
<a | ||
data-testid="next" | ||
data-wp-on--click="actions.router.navigate" | ||
href="<?php echo $attributes['next']; ?>" | ||
>Next</a> | ||
<?php else : ?> | ||
<a | ||
data-testid="back" | ||
data-wp-on--click="actions.router.back" | ||
href="#" | ||
>Back</a> | ||
<?php endif; ?> | ||
</div> | ||
</section> | ||
|
||
<div> | ||
<p | ||
data-testid="no-region-text-1" | ||
data-wp-text="state.region1.text" | ||
>not hydrated</p> | ||
</div> | ||
|
||
|
||
<section> | ||
<h2>Region 2</h2> | ||
<div data-wp-interactive data-wp-navigation-id="region-2"> | ||
<p | ||
data-testid="region-2-text" | ||
data-wp-text="state.region2.text" | ||
>not hydrated</p> | ||
<p | ||
data-testid="region-2-ssr" | ||
>content from page <?php echo $attributes['page']; ?></p> | ||
|
||
<button | ||
data-testid="context-counter" | ||
data-wp-context='{ "counter": { "initialValue": 0 } }' | ||
data-wp-init="actions.counter.init" | ||
data-wp-text="context.counter.value" | ||
data-wp-on--click="actions.counter.increment" | ||
>NaN</button> | ||
|
||
<div data-wp-ignore> | ||
<div> | ||
<p | ||
data-testid="no-region-text-2" | ||
data-wp-text="state.region2.text" | ||
>not hydrated</p> | ||
</div> | ||
|
||
<section> | ||
<h2>Nested region</h2> | ||
<div data-wp-interactive data-wp-navigation-id="nested-region"> | ||
<p | ||
data-testid="nested-region-ssr" | ||
>content from page <?php echo $attributes['page']; ?></p> | ||
</div> | ||
</section> | ||
</div> | ||
</div> | ||
</section> |
43 changes: 43 additions & 0 deletions
43
packages/e2e-tests/plugins/interactive-blocks/router-regions/view.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
( ( { wp } ) => { | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
const { store, navigate } = wp.interactivity; | ||
|
||
store( { | ||
state: { | ||
region1: { | ||
text: 'hydrated' | ||
}, | ||
region2: { | ||
text: 'hydrated' | ||
}, | ||
counter: { | ||
value: 0, | ||
}, | ||
}, | ||
actions: { | ||
router: { | ||
navigate: async ( { event: e } ) => { | ||
e.preventDefault(); | ||
await navigate( e.target.href ); | ||
}, | ||
back: () => history.back(), | ||
}, | ||
counter: { | ||
increment: ( { state, context } ) => { | ||
if ( context.counter ) { | ||
context.counter.value += 1; | ||
} else { | ||
state.counter.value += 1; | ||
} | ||
}, | ||
init: ( { context } ) => { | ||
if ( context.counter ) { | ||
context.counter.value = context.counter.initialValue; | ||
} | ||
} | ||
}, | ||
}, | ||
} ); | ||
} )( window ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.