-
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-each
directive
#57859
Conversation
Size Change: +200 B (0%) Total Size: 1.7 MB
ℹ️ View Unchanged
|
609a7db
to
07a53b6
Compare
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.
Amazing work. And really good tests! I love the way you covered rerenderings.
It's amazing how well this works with such "a little effort" on our side, even on client-side navigation (thanks, Preact! 😄)
Before merging, could you please add a couple of extra tets?
- Test nested templates.
- Test derived state used as key.
// These tags are included to check that the elements are not unmounted | ||
// and mounted again. If an element remounts, its tag should be missing. |
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.
Fantastic.
wp-each
directive.wp-each
directive
Co-authored-by: Luis Herranz <luisherranz@gmail.com>
Co-authored-by: Luis Herranz <luisherranz@gmail.com>
6635608
to
00650c7
Compare
Flaky tests detected in a115a92. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7613036049
|
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.
This looks good to me. Thanks, David 🙂
By the way, unless we specifically optimize for the case of nested templates in the server directive processor, the SSRing of the internal data-wp-each
is going to include the template
and data-wp-each-child
directives as well.I added them in 1621077 and we can remove them once we finish the server wp-each
implementation if that's not the case.
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 mistakenly requested changes instead of approve.
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.
Nice work, this directive will be widely used. 🎉
Tracking issue: #56803
What
Implements the
data-wp-each
directive and related (data-wp-each-key
&data-wp-each-child
).This directive is passed to template elements and receives a path to a list of elements that are rendered using the template content. Each item is included in the context under the
item
name, so directives inside the template can access the current item.For example, the following HTML...
...generates the following output:
The prop that holds the item in the context can be changed by passing a suffix to the directive name.
By default, it uses each element as the key for the rendered nodes, but you can also specify a path to retrieve the key if necessary, e.g., when the list contains objects.
For server-side rendered lists, another directive called
data-wp-each-child
ensures hydration works as expected. It should be placed in the server-side rendered elements.Why?
The directive is meant to be part of the public 1.0 version of the Interactivity API shipped in WordPress.
How?
The
data-wp-each
implementation evaluates the list passed as value and iterates over it, rendering the content of the<template>
wrapped inside the context provider.Making the item available inside the context was tricky, not only for the elements in the template content but also for the
data-wp-each-key
directive, which is defined in the<template>
element itself along with thedata-wp-each
directive.Tasks
data-wp-each
anddata-wp-each-child
implementations from Woo main repository.data-wp-each
.