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

query-pagination-next and previous: Changing the markup on the first and last pages of a pagination #36681

Merged
merged 16 commits into from
Dec 3, 2021

Conversation

matiasbenedetto
Copy link
Contributor

@matiasbenedetto matiasbenedetto commented Nov 19, 2021

Hi 👋

Description

This PR changes the rendering of core/query-pagination-next and core/query-pagination-previous for 2 "edge cases". These cases are when the user is navigating the first or last page of a paginated query. In these cases, we render a <span> instead of nothing as we commonly do with the pagination numbers where the number matching the pagination's current number renders a <span> instead of an <a>.

After the changes featured in this PR the expected behaviour is:

  • While navigating a first page of a pagination:

    • core/query-pagination-previous renders a <span> element instead of nothing.
    • core/query-pagination-next renders an <a> element as always.
  • While navigating a last page of a pagination:

    • core/query-pagination-previous renders an <a> element as always.
    • core/query-pagination-next renders a <span> element instead of nothing.
  • While navigating any page of pagination that is not the first or the last one:

    • core/query-pagination-previous renders an <a> element as always.
    • core/query-pagination-next renders an <a> element as always.
  • While navigating a page with a core/query-pagination block in place but no data to paginate:

    • Nothing is rendered as always.

This is just a continuation of a work started by @MaggieCabrera

Why are we introducing this change?

The intention is to fix the problems around the use of space-around justification in the flex container of the core/query-pagination block as described in this issue (#34997).

How has this been tested?

Tested with normal and inherit wp queries using Empty, TT1, and Blockbase themes.

Block code:

<!-- wp:query-pagination {"layout":{"type":"flex","justifyContent":"space-between"}} -->
	<!-- wp:query-pagination-previous /-->
	<!-- wp:query-pagination-numbers /-->
	<!-- wp:query-pagination-next /-->
<!-- /wp:query-pagination -->

Screenshots

  • First page (Empty theme):
Before After
image image
  • Last page (Empty theme):
Before After
image image
  • First page (TT1 theme):
Before After
image image
  • Last page (TT1 theme):
Before After
image image

Types of changes

Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).

@github-actions
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @matiasbenedetto! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Nov 19, 2021
@scruffian
Copy link
Contributor

This is working well for me but it would be good to have some designers eyes on it - I'm not sure about the Previous/Next Page text appearing when it doesn't do anything. cc @jasmussen @kjellr

@@ -26,7 +27,13 @@ function render_block_core_query_pagination_next( $attributes, $content, $block
if ( $pagination_arrow ) {
$label .= $pagination_arrow;
}
$content = '';

$wrapper_attributes = get_block_wrapper_attributes();
Copy link
Contributor

Choose a reason for hiding this comment

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

This is already defined on line 23, do we need to call it again?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

$content = '';

$wrapper_attributes = get_block_wrapper_attributes();
$content = sprintf(
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we escape the output here incase the user tries to insert something untrustworthy in the attribute? (https://developer.wordpress.org/themes/theme-security/data-sanitization-escaping/)

Copy link
Contributor

Choose a reason for hiding this comment

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

Given that its not escaped elsewhere in this file it's probably safe...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I think so

} elseif ( 1 !== $page ) {
$content = sprintf(
} else {
$content = 1 !== $page ? sprintf(
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 this goes against the brace style in the PHP guidelines: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed :)

@jasmussen
Copy link
Contributor

I'm not sure about the Previous/Next Page text appearing when it doesn't do anything

I would agree, the text makes it sound actionable when it isn't. I do recall Kjell having some trouble with aligning the three harmoniously in a container, and I imagine there might be a layout challenge if the 3rd one is removed entirely.

@MaggieCabrera
Copy link
Contributor

I'm not sure about the Previous/Next Page text appearing when it doesn't do anything

I would agree, the text makes it sound actionable when it isn't. I do recall Kjell having some trouble with aligning the three harmoniously in a container, and I imagine there might be a layout challenge if the 3rd one is removed entirely.

I think it would make sense to have it with lowered opacity. Right now it's already a challenge to keep the 3 items in the same place with any given layout if one of them disappears completely on first/last page too. And with the class added here, theme developers have the chance to style those cases in their own way too.

@MaggieCabrera MaggieCabrera added the [Block] Query Pagination Affects the Query Pagination Block - used for pagination within the Query Loop Block label Nov 22, 2021
@kjellr
Copy link
Contributor

kjellr commented Nov 22, 2021

Yeah, having the text present but unclickable seems incorrect here, even if it's a lower opacity. The standard behavior around the internet is basically what we have already today: the text only shows up if it's possible to paginate in that direction.

The issue we're trying to solve for here is just that we can't automatically center the page numbers if there are two items in this container. That's just not the way that space-around works. If these were standard blocks and I always wanted the page numbers centered, I might try using a columns block. Have we tried something like that here?

@MaggieCabrera
Copy link
Contributor

The issue we're trying to solve for here is just that we can't automatically center the page numbers if there are two items in this container. That's just not the way that space-around works. If these were standard blocks and I always wanted the page numbers centered, I might try using a columns block. Have we tried something like that here?

The problem is that inside the pagination block only the query previous/next/numbers blocks are allowed, so you can't wrap them inside column blocks.

@kjellr
Copy link
Contributor

kjellr commented Nov 22, 2021

Right, but that's theoretically something we could change if it is necessary.

@scruffian
Copy link
Contributor

What if we use CSS grid?

@kjellr
Copy link
Contributor

kjellr commented Nov 22, 2021

Yeah, this seems like an appropriate use case for grid. Not sure the feasibility of adding that in here though. @jasmussen are you up to date on the explorations around Grid in the editor?

@jasmussen
Copy link
Contributor

jasmussen commented Nov 22, 2021

Grid in terms of theme.json integration with corresponding UI is a bit backburnered and not something to consider. However it looks like the "Query Pagination" block (wp-block-query-pagination) comes with its own style.scss file which we could add some grid rules to. If those grid rules are sufficiently generic and utilitarian, that seems fine?

@matiasbenedetto
Copy link
Contributor Author

I think using display:grid is not the best option here. Query pagination has flex controls that work fine and are useful to customize the block rendering.
image

What do you think about adding visibility:hidden and aria-hidden="true" to the non clickeable previous and next elements. I think that solution covers the three issue discussed here:

  • Solves the positioning issue
  • Invisible to the eye
  • Invisible for the screen readers

What do you think?

@matiasbenedetto
Copy link
Contributor Author

matiasbenedetto commented Nov 23, 2021

I implemented the change suggested in my last comment with this commit:

Results:

  • First page:

image

  • Last page:

image

@scruffian
Copy link
Contributor

I like this solution. It would be good to get some input from someone from an accessibility perspective, although I'm not sure who to ask...

@MaggieCabrera
Copy link
Contributor

What happens with this solution when you align the items to the left or to the right instead of using space between?

@matiasbenedetto
Copy link
Contributor Author

matiasbenedetto commented Nov 23, 2021

This last case mentioned by @MaggieCabrera (the use of justify:flex-start), can be problematic on the first page. Because the element, even though is not visible (visibility:hidden), is still there. The same happens if we use justify:flex-end and navigate the last page.

image

@scruffian
Copy link
Contributor

I think using display:grid is not the best option here. Query pagination has flex controls that work fine and are useful to customize the block rendering.

We could conisder mapping the flex controls to CSS grid styles without making the user aware that's what's happening...

@matiasbenedetto
Copy link
Contributor Author

matiasbenedetto commented Nov 24, 2021

I think that the latest 2 commits fix the remaining problem around left and right Flex justifications, so I think it covers all the issues discussed so far:

  • Solves the positioning issue with space-around
  • Invisible to the eye
  • Invisible for the screen readers
  • Works with left and right justified elements

Justify left on first page:
image

Justify left on middle page:
image

Justify left on last page:
image

Space around on first page:
image

Space around on middle page:
image

Space around on last page:
image

@matiasbenedetto
Copy link
Contributor Author

matiasbenedetto commented Nov 29, 2021

The latest commits are fixing some failing use cases, as the last one spotted by @scruffian.
The block should render as the examples shared in my previous comment.

@scruffian
Copy link
Contributor

I no longer see the empty span on the first and last pages...

@scruffian
Copy link
Contributor

I no longer see the empty span on the first and last pages...

This was an environment issue!

Copy link
Contributor

@scruffian scruffian left a comment

Choose a reason for hiding this comment

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

This is looking good to me. There's some duplication between the blocks that would be good to clear up in a followup PR.

$id = uniqid();
$style = gutenberg_get_layout_style( ".wp-container-$id", $used_layout, $has_block_gap_support );
$container_class = 'wp-container-' . $id . ' ';
$justify_class = $used_layout['justifyContent'] ? 'wp-justify-' . $used_layout['justifyContent'] . ' ' : '';
Copy link
Contributor

Choose a reason for hiding this comment

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

Why we added this to the layout abstraction? Was something specific to pagination blocks?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, we needed to identify with a class the justification of the block, without this change that was not possible.

Copy link
Contributor

Choose a reason for hiding this comment

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

There shouldn't be any block logic leaked into the layout abstraction. I'll check a bit the PR better to try to understand. That was the case recently with Navigation block as well, but we should find another way to handle specific block needs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I see, let me submit a PR with this change you propose.

Copy link
Contributor Author

@matiasbenedetto matiasbenedetto Dec 3, 2021

Choose a reason for hiding this comment

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

@ntsekouras a PR moving the functionality from Layout to Query Pagination code:
#37113

@@ -32,7 +32,17 @@ $pagination-margin: 0.5em;
}
}

&.aligncenter {
justify-content: center;
Copy link
Contributor

Choose a reason for hiding this comment

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

This shouldn't be removed. It was added here: #34739 as a fix for align center.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That behavior is achieved using the centered justification:
image

Copy link
Contributor

Choose a reason for hiding this comment

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

I know, but we also support the center alignment and is needed for backwards compatibility.

Copy link
Contributor Author

@matiasbenedetto matiasbenedetto Dec 3, 2021

Choose a reason for hiding this comment

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

@ntsekouras could you provide more context about backward compatibility related to this, please?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure. Even if the align:center achieves the same think with centered justification, we still support this alignment. So it cannot be removed because it has created a regression. You can check this on trunk by selecting align:center and checking the front-end.

Even if we removed the align:center option though and with a new block migration in place, we would still need to keep the css rule because the migrations run and update the blocks markup in the editor and then we have to also save to get the new markup in the post content. So for content with blocks that had align:center but the migration was never applied, the content should look as before in the front end without and broken styles.

Makes sense? Please tell me to clarify anything from the above if you want.

);
} elseif ( ! $max_page || $max_page > $page ) {
$custom_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) );
$max_num_pages = $custom_query->max_num_pages ? $custom_query->max_num_pages : 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need this max_num_pages logic? Previously it would enter this if when it was a custom query and it wasn't on the first page. So wouldn't a check about $page === 1 be enough? Am I missing something?

Copy link
Contributor Author

@matiasbenedetto matiasbenedetto Dec 3, 2021

Choose a reason for hiding this comment

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

This is necessary because if the query has no results $custom_query->max_num_pages equals to zero. So the logic to know if there are multiple pages 1 < $max_num_pages doesn't work without this.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm sorry but I still don't get it. In Query Pagination Previous we only care about whether we are on the first page or not, no? If we are on the first page, we don't have a link to show, but if we are not we always have a link. This is different from Query Pagination Next where we have to recreate the query run from Query Loop to know if we have a link for the next posts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, we don't care only about if we are on the first page or not. We need to know if there are pages to paginate. If there are not pages to paginate should not render anything.

matiasbenedetto added a commit to matiasbenedetto/gutenberg that referenced this pull request Dec 3, 2021
…requirements.

This characteristic was introduced here: WordPress#36681 and currently it is only necesary for the Query Pagination block so we can move the functionality to the block's code to avoid adding extra logic to the Layout block support.
@ntsekouras
Copy link
Contributor

Aside my comments that I'm trying to understand the changes here, I'd really want to thank you @matiasbenedetto for your contribution and effort here! Really appreciate it that you have also opened a follow up PR 💯

@ntsekouras
Copy link
Contributor

ntsekouras commented Dec 4, 2021

I've created a follow up PR to discuss/check how to simplify things (now is WIP).

@ntsekouras
Copy link
Contributor

ntsekouras commented Dec 6, 2021

👋 I noticed this PR when this notice was shown:

Screen Shot 2021-12-06 at 9 15 37 AM

but it made me look into the changes here. I tried to understand the added complexity, especially in the case of inherit the global query case, where the added logic seemed to me like trying to duplicate the get_next_posts_link and get_previous_posts_link. That led to a draft PR which I just closed.

Personally the concept of adding an extra element (hidden wrapper) for design purposes seems a bit odd to me and I'm not really sure if this should be added in the first place. Would another kind of layout be a better fit for these blocks (even if it doesn't exist yet like grid)? If yes, could this be something that a theme could accomplish with css overrides until it's properly implemented in core/GB?

Additionally it seems the vertical layout still renders the placeholders and that can't be desired I think.

Screen.Recording.2021-12-06.at.8.40.45.PM.mov

To sum up, I think we should consider reverting this PR and see for alternative approaches that should properly fix the design issues we want. I'm worried that if we follow this path, we just hack our way around the root issue and add complexity in block logic and styles to accommodate cases like the flex-start where we now display:none, the vertical layout etc..

The root issue for me is that using flex layout for Query Pagination might not just be the right layout for this block.

--cc
@youknowriad @mcsf @gziolo @ockham

@matiasbenedetto
Copy link
Contributor Author

matiasbenedetto commented Dec 6, 2021

Personally, I don't see how using CSS grid could make this better for users and theme devs.

I haven't tested the vertical flex orientation in this component before. The fix for the vertical orientation requires a really tiny CSS change proposed in this PR.

@kjellr
Copy link
Contributor

kjellr commented Dec 16, 2021

To sum up, I think we should consider reverting this PR and see for alternative approaches that should properly fix the design issues we want. I'm worried that if we follow this path, we just hack our way around the root issue and add complexity in block logic and styles to accommodate cases like the flex-start where we now display:none, the vertical layout etc..

The root issue for me is that using flex layout for Query Pagination might not just be the right layout for this block.

I haven't been keeping up to date on developments since this comment, but for what it's worth, I think reverting this would be fine for now. Two thoughts:

  • The solution here is a bit complicated, and I think we can probably come up with something more ideal with a bit more time. We're leaning on the flex layouts more and more as we continue to build themes, so we may stumble on something better soon.
  • The design issue this is solving is kind of awkward, but not a deal-breaker for me. The end state we reached is also a little awkward, since the homepage will still have empty space on one side.
  • Plus, in the immediate future, I think we can work around the design/layout issue by just removing the numbered pagination from the default display of the block. I don't think it's a must-have block by default, and folks can add it in manually if they require it.

@mcsf
Copy link
Contributor

mcsf commented Dec 17, 2021

  • The solution here is a bit complicated, and I think we can probably come up with something more ideal with a bit more time. We're leaning on the flex layouts more and more as we continue to build themes, so we may stumble on something better soon.
  • The design issue this is solving is kind of awkward, but not a deal-breaker for me. The end state we reached is also a little awkward, since the homepage will still have empty space on one side.
  • Plus, in the immediate future, I think we can work around the design/layout issue by just removing the numbered pagination from the default display of the block. I don't think it's a must-have block by default, and folks can add it in manually if they require it.

Thanks for the design and product perspective! They match my assessment of the situation and give me confidence to also recommend reverting. Especially since we're dealing with a non-must-have issue that can be handled downstream. /cc @matiasbenedetto @jeffikus

@scruffian
Copy link
Contributor

It looks like the revert button isn't going to work on this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Query Pagination Affects the Query Pagination Block - used for pagination within the Query Loop Block First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants