-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Interactivity API: add wp-run
directive and useInit
& useWatch
hooks
#57805
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cf96e62
Implement tests for wp-run
DAreRodz 3bbaff3
Implement wp-run
DAreRodz ec101b4
Rename useSignalEffect to useWatch and add useInit
DAreRodz 7018856
Export useLayoutEffect
DAreRodz cc4b16f
Expose cloneElement
DAreRodz 9a32d11
Pass the scope to useWatch and useInit
DAreRodz 61fee76
Add tests for hooks inside wp-run
DAreRodz ef28d96
Document useWatch and useInit
DAreRodz 5823759
Update changelog
DAreRodz 966f02d
Always reset scope inside `withScope`
DAreRodz 3f34051
Expose scoped versions of preact hooks
DAreRodz 85b6948
Add docs for `wp-run`
DAreRodz 3ec3afe
Removed `runs` from `wp-run` store
DAreRodz d104a08
Improve `wp-init` documentation
DAreRodz 56566cf
Expose `useState` and `useRef` from preact
DAreRodz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's with the
setTimeout
s here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is weird. I saw an infinite loop issue, at least with
state.renderCount
(line 76), so I eventually decided to usesetTimeout
for all callbacks.PS: I don't know, but regarding this comment, maybe these
callbacks
should have beenruns
(or the other way around 🤔).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we review that infinite loop issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any luck with this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, wait. Isn't this just a "don't use
setState
inside the body function" kind of infinite loop?You can't do this:
You have to do this, which is equivalent to the
setTimeout
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't had a chance to investigate yet, but probably it is what you mention. I'll check tomorrow. 😄