-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Interactivity API: add
wp-run
directive and useInit
& useWatch
…
…hooks (#57805) * Implement tests for wp-run * Implement wp-run * Rename useSignalEffect to useWatch and add useInit * Export useLayoutEffect * Expose cloneElement * Pass the scope to useWatch and useInit * Add tests for hooks inside wp-run * Document useWatch and useInit * Update changelog * Always reset scope inside `withScope` * Expose scoped versions of preact hooks * Add docs for `wp-run` * Removed `runs` from `wp-run` store * Improve `wp-init` documentation * Expose `useState` and `useRef` from preact
- Loading branch information
Showing
9 changed files
with
452 additions
and
14 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
packages/e2e-tests/plugins/interactive-blocks/directive-run/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,15 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 2, | ||
"name": "test/directive-run", | ||
"title": "E2E Interactivity tests - directive run", | ||
"category": "text", | ||
"icon": "heart", | ||
"description": "", | ||
"supports": { | ||
"interactivity": true | ||
}, | ||
"textdomain": "e2e-interactivity", | ||
"viewScript": "directive-run-view", | ||
"render": "file:./render.php" | ||
} |
54 changes: 54 additions & 0 deletions
54
packages/e2e-tests/plugins/interactive-blocks/directive-run/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,54 @@ | ||
<?php | ||
/** | ||
* HTML for testing the directive `data-wp-run`. | ||
* | ||
* @package gutenberg-test-interactive-blocks | ||
*/ | ||
|
||
gutenberg_enqueue_module( 'directive-run-view' ); | ||
?> | ||
|
||
<div | ||
data-wp-interactive='{ "namespace": "directive-run" }' | ||
data-wp-navigation-id='test-directive-run' | ||
> | ||
<div data-testid="hydrated" data-wp-text="state.isHydrated"></div> | ||
<div data-testid="mounted" data-wp-text="state.isMounted"></div> | ||
<div data-testid="renderCount" data-wp-text="state.renderCount"></div> | ||
<div data-testid="navigated">no</div> | ||
|
||
<div | ||
data-wp-run--hydrated="callbacks.updateIsHydrated" | ||
data-wp-run--renderCount="callbacks.updateRenderCount" | ||
data-wp-text="state.clickCount" | ||
></div> | ||
</div> | ||
|
||
<div data-wp-interactive='{ "namespace": "directive-run" }' > | ||
<button data-testid="toggle" data-wp-on--click="actions.toggle"> | ||
Toggle | ||
</button> | ||
|
||
<button data-testid="increment" data-wp-on--click="actions.increment"> | ||
Increment | ||
</button> | ||
|
||
<button data-testid="navigate" data-wp-on--click="actions.navigate"> | ||
Navigate | ||
</button> | ||
|
||
<!-- Hook execution results are stored in this element as attributes. --> | ||
<div | ||
data-testid="wp-run hooks results" | ||
data-wp-show-children="state.isOpen" | ||
data-init="" | ||
data-watch="" | ||
> | ||
<div | ||
data-wp-run--mounted="callbacks.updateIsMounted" | ||
data-wp-run--hooks="callbacks.useHooks" | ||
> | ||
Element with wp-run using hooks | ||
</div> | ||
</div> | ||
</div> |
108 changes: 108 additions & 0 deletions
108
packages/e2e-tests/plugins/interactive-blocks/directive-run/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,108 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
store, | ||
directive, | ||
navigate, | ||
useInit, | ||
useWatch, | ||
cloneElement, | ||
getElement, | ||
} from '@wordpress/interactivity'; | ||
|
||
// Custom directive to show hide the content elements in which it is placed. | ||
directive( | ||
'show-children', | ||
( { directives: { 'show-children': showChildren }, element, evaluate } ) => { | ||
const entry = showChildren.find( | ||
( { suffix } ) => suffix === 'default' | ||
); | ||
return evaluate( entry ) | ||
? element | ||
: cloneElement( element, { children: null } ); | ||
}, | ||
{ priority: 9 } | ||
); | ||
|
||
const html = ` | ||
<div | ||
data-wp-interactive='{ "namespace": "directive-run" }' | ||
data-wp-navigation-id='test-directive-run' | ||
> | ||
<div data-testid="hydrated" data-wp-text="state.isHydrated"></div> | ||
<div data-testid="mounted" data-wp-text="state.isMounted"></div> | ||
<div data-testid="renderCount" data-wp-text="state.renderCount"></div> | ||
<div data-testid="navigated">yes</div> | ||
<div | ||
data-wp-run--hydrated="callbacks.updateIsHydrated" | ||
data-wp-run--renderCount="callbacks.updateRenderCount" | ||
data-wp-text="state.clickCount" | ||
></div> | ||
</div> | ||
`; | ||
|
||
const { state } = store( 'directive-run', { | ||
state: { | ||
isOpen: false, | ||
isHydrated: 'no', | ||
isMounted: 'no', | ||
renderCount: 0, | ||
clickCount: 0 | ||
}, | ||
actions: { | ||
toggle() { | ||
state.isOpen = ! state.isOpen; | ||
}, | ||
increment() { | ||
state.clickCount = state.clickCount + 1; | ||
}, | ||
navigate() { | ||
navigate( window.location, { | ||
force: true, | ||
html, | ||
} ); | ||
}, | ||
}, | ||
callbacks: { | ||
updateIsHydrated() { | ||
setTimeout( () => ( state.isHydrated = 'yes' ) ); | ||
}, | ||
updateIsMounted() { | ||
setTimeout( () => ( state.isMounted = 'yes' ) ); | ||
}, | ||
updateRenderCount() { | ||
setTimeout( () => ( state.renderCount = state.renderCount + 1 ) ); | ||
}, | ||
useHooks() { | ||
// Runs only on first render. | ||
useInit( () => { | ||
const { ref } = getElement(); | ||
ref | ||
.closest( '[data-testid="wp-run hooks results"]') | ||
.setAttribute( 'data-init', 'initialized' ); | ||
return () => { | ||
ref | ||
.closest( '[data-testid="wp-run hooks results"]') | ||
.setAttribute( 'data-init', 'cleaned up' ); | ||
}; | ||
} ); | ||
|
||
// Runs whenever a signal consumed inside updates its value. Also | ||
// executes for the first render. | ||
useWatch( () => { | ||
const { ref } = getElement(); | ||
const { clickCount } = state; | ||
ref | ||
.closest( '[data-testid="wp-run hooks results"]') | ||
.setAttribute( 'data-watch', clickCount ); | ||
return () => { | ||
ref | ||
.closest( '[data-testid="wp-run hooks results"]') | ||
.setAttribute( 'data-watch', 'cleaned up' ); | ||
}; | ||
} ); | ||
} | ||
} | ||
} ); |
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
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.