-
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
Query and Search blocks: support for Instant Search via query_loop_block_query_vars
filter
#67181
base: trunk
Are you sure you want to change the base?
Conversation
Size Change: +442 B (+0.02%) Total Size: 1.83 MB
ℹ️ View Unchanged
|
query_loop_block_query_vars
filterquery_loop_block_query_vars
filter
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
This is cool to see in core :) To me this highlights the need for a more customizable loading state though. By the nature of search being unpredictable we cannot really preload anything. So the amount of time it takes after each keystroke to get the results loaded more often than not is uncomfortably long. Yes we have the global navigation router loading bar. But that seems inappropriate here as it isn't a full page relead but instead is just updating this interactive region 🤔 This is totally not blocking here but I wonder if there is a way for us to make it much more obvious by either having a sort of spinner in the search bar itself or some overlayed spinner on the post list on a much shorter timer that appears when the router is fetching something. |
That makes a lot of sense. In the pagination navigation, we couldn't add a spinner because it wasn't clear where to put it, so we went with the top loading bar as a general solution. But here, it would be easier to find a place for it in the search bar. |
@luisherranz I just attempted to do this in a custom block of mine. Here are my findings:
This here is how I did it: store('example/namespace', {
context: {
isActive: false,
},
state: {
get isLoading() {
const { state: routerState } = store('core/router');
const { isActive } = getContext();
return isActive && routerState.navigation.hasStarted;
},
},
actions: {
*search(event) {
const context = getContext();
const { actions } = yield import('@wordpress/interactivity-router');
{...}
context.isActive = true; // We need to somehow know that we are the reason for the loading state
yield actions.navigate(url.toString());
context.isActive = false; // and then reset it after
},
},
}); I think what could solve this if we added a new (I created a PR for this here #67680) |
Thank you, Fabian. I've added it to the iteration of WordPress 6.8. There's also this old discussion. To avoid blocking this work, for now we can use an internal state to display the spinner, like the one you show there. |
@fabiankaegy Do you have an example of what the UI could look like for the loading state? |
@michalczaplinski I was thinking maybe starting with a simple inline spinner that gets shown absolutely positioned on the right of the search input next to the X (native clear button) 🤔 |
What?
Implementation of Instant Search using the Search and Query Loop blocks. Added as a new experiment on the Gutenberg Experiments page.
Related to #63053
output_62afd8.mp4
How does it work?
Instant Search and Query Block
Gutenberg experiment.Force Page Reload
i.e. use "enhanced pagination" in that Query block.When the Search block is inside of the Query block using "enhanced pagination", it automatically gets "updated" to an Instant Search block.
Any search input in the Instant Search block gets passed as the
?instant-search-<queryId>=<search-term>
query search param in the URL.Multiple Query + Search blocks
It's possible to have multiple Instant Search blocks in a page/post/template. In such a case, ensuring their functionality is isolated is essential. For this to work, the
queryId
is part of the param syntax:instant-search-<queryId>=<search-term>
Search button
The search block can contain a search button (in some configurations). However, when using the Instant Search functionality, the button is redundant because the list of posts updates as you type. For this reason, in the editor, when the Search block is placed inside the Query Loop block with enhanced pagination ON, the Block Controls related to the Search button are removed. The displayed name of the block (via
label
) is changed toInstant Search (Search)
.On the frontend, the Search button is also always removed for each (Instant) Search block.
output_70b3a0.mp4
Pagination
The instant search functionality respects the pagination of the Query block, which uses the
query-<queryId>-page
syntax.Limitations
Custom
query types are supported in the current experiment. TheDefault
query types are currently not supported but will be in the future version. This is prototyped already in Search and Query blocks: Add support for Default queries viapre_get_posts
filter #67289.Alternatives
Alternative approaches have been explored:
render_block_context
filter #67013 - using therender_block_context
filter.Further work
This is an initial prototype and the following features is intentionally not yet implemented:
Default
queries, including multipleDefault
queries on a single page/post/template. Those have been prototyped already in Search and Query blocks: Add support for Default queries viapre_get_posts
filter #67289.