Skip to content
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

New block: Query filter #441

Merged
merged 21 commits into from Sep 12, 2023
Merged

New block: Query filter #441

merged 21 commits into from Sep 12, 2023

Conversation

ryelle
Copy link
Contributor

@ryelle ryelle commented Sep 5, 2023

See #419 — this adds a new block called "Query Filter", which renders a dropdown form using values passed in via filter. When "Apply" is clicked, the form is submitted, and the resulting URL is expected to handle the applied filters. In many cases, wp_query can handle combined filters without any customization (ex, taxonomies). For non-core filters, hooking into pre_get_posts (+ maybe query_vars?) in the child theme should be enough.

This block is using the Interactivity API, which uses data-wp- attributes to attach events and conditional attributes. This API is still experimental, but I took a lot of inspiration from the navigation block.

By itself, this block doesn't render anything. It expects a key attribute, which is used to create a specific filter for the individual sites to populate a settings object. For example, the following would be used to create an "example" filter.

<!-- wp:group {"layout":{"type":"flex","flexWrap":"nowrap","justifyContent":"right"}} -->
<div class="wp-block-group">
    <!-- wp:wporg/query-filter {"key":"example"} /-->
</div>
<!-- /wp:group -->

This creates the WP filter hook wporg_query_filter_options_example, which expects an object used to render the options. There's more documentation inline in render.php.

To create a simple dropdown with 3 options:

add_filter(
	'wporg_query_filter_options_example',
	function() {
		return array(
			'label' => 'Example <span>1</span>',
			'key' => 'q',
			'action' => home_url(),
			'options' => array(
				'opt-1' => 'Option 1',
				'opt-2' => 'Option 2',
				'opt-3' => 'Option 3',
			),
			'selected' => array( 'opt-2' ),
		);
	}
);

The output would look like this:

Screenshot 2023-09-05 at 6 51 56 PM

When "Apply" is clicked, the page loads /?q[]=opt-2. It's up to each site to load the correct archive.

❓ I think this approach will work well for the child theme setup, but I definitely want feedback on it before committing us all to this for all the directories 🙂

🖌️ I've also used block-specific custom properties so that other themes can easily change colors with theme.json changes (here's an example in showcase, though i removed it once everything was set up since the defaults work). This is in case the theme directory needs a dark scheme.

Practical example: Showcase

I've got this working with real filters in WordPress/wporg-showcase-2022#147 — here are some screen recordings of everything in place:

A basic filtering, starting on the homepage, shows all 3 filters & options. When a filter is applied, the results load using the archive template.

basic-demo.mp4

More filtering interactions — clearing filters using "Clear" and by clicking the option; applying multiple filters, clicking out before applying.

more-demo.mp4

Search and filters can work together — once on a search results page, filtering will filter the search results. (the opposite is not true, if you search on a filtered page it will remove the filters, we talked through that and it was expected behavior)

search-demo.mp4

When clicking on a tag archive page, the filter for that tag is correctly selected.

tag-demo.mp4

And on mobile, the dropdown switches to a modal

Screen Shot 2023-09-05 at 18 37 24

/>
<input
type="submit"
value="<?php esc_html_e( 'Apply', 'wporg' ); ?>"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was agreed that this would be updated with the filter count:

Context Link
Figma Link

Image
Screenshot 2023-09-06 at 10 22 42 AM

Copy link
Contributor Author

@ryelle ryelle Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been only referencing the "filters 6.1" page in the Filter component file, thinking that was the final version 🙃

The link doesn't work for me (opens the file but wrong page)… but I found it by searching through the other pages, so thanks for pointing it out.

@WordPress/meta-design — Can you confirm how this should behave? It sounds like the top number (on black, red highlight) would be the number of filters currently applied on the page, while the Apply button (on blue, yellow highlight) would be the number of filters currently selected— so it would stay "Popular tags 1" even if you select two more, but the button would be "Apply (3)"?

Screenshot 2023-09-06 at 10 50 06 AM

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds like the top number (on black, red highlight) would be the number of filters currently applied on the page, while the Apply button (on blue, yellow highlight) would be the number of filters currently selected— so it would stay "Popular tags 1" even if you select two more, but the button would be "Apply (3)"?

Correct, but honestly I think we can remove the number on the Apply button for now, I'm not sure it adds clarity. If Francisco when he comes back from AFK has to correct me, I will buy him an apology milkshake at that time. How does this sound?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was an accessibility request, but maybe it would be easier to understand the UX once this is merged and live on the staging site. I'll leave this as-is (no number on button) for this initial pass, and ask for feedback once there's a live place to try it out.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ryelle is correct. It was an accessibility request shared here.

@StevenDufresne
Copy link
Contributor

I tested it locally, and while I don't yet have the expertise to review the interactivity implementation, it does work well and updates the query string appropriately. 👍


&.wporg-query-filter__modal-action-clear {
background-color: transparent;
color: #a13f2a; /* pomegrade-1 fails contrast against white. */
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WordPress/meta-design I meant to flag this in the description— the mockup uses "Pomegrade" on white but that fails to meet contrast requirements. There's a note in the design library about darker versions of colors, but I didn't see any, so I've made this one up. If you'd rather something different, it just needs to have a 4.5+ value against light-grey-2, since I'm using that as the hover color.

Screenshot 2023-09-06 at 11 02 36 AM

Hover:
Screenshot 2023-09-06 at 11 02 50 AM

@ryelle ryelle added the Redesign Related to the wordpress.org redesign project label Sep 7, 2023
This was added to support the "click outside to close" behavior, but that can glitch when the click target is not a focusable element — clicking the space between buttons in the actions bar would close the dropdown. So the hasHover check was added to prevent that, but that brought in new glitches, where the focus could be reset to the first focusable element if hover was changed.

This PR removes all that in favor of using the backdrop element as a click target.
This also adds a new label "title" which is used on the small screen modal too.
@ryelle ryelle merged commit a3f8c80 into trunk Sep 12, 2023
2 checks passed
@ryelle ryelle deleted the add/block-query-filters branch September 12, 2023 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] New Block Redesign Related to the wordpress.org redesign project
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

5 participants