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

Make sidebar container scroll internally #554

Merged
merged 23 commits into from
Jan 31, 2024
Merged

Conversation

adamwoodnz
Copy link
Contributor

@adamwoodnz adamwoodnz commented Jan 10, 2024

See WordPress/wporg-developer#453

This PR modifies the sidebar container so that once it becomes fixed, it also scrolls internally. When the footer collides with the bottom of the sidebar, the sidebar reduces in height rather than starting to scroll with the page.

It has been decoupled from the Table of Contents so that it can be used for the Developer Chapter List as well. JS changes are required to enable handling multiple sidebar containers on the page.

The scrolling areas have custom scrollbars so that they appear on hover. This has been inspired by the React docs (example), and should help users discover that these are scrollable more easily. With the typical Mac OS setting of hidden scrollbars, it is not immediately obvious. Note that this does not work in Firefox.

Overall this is a much simpler implementation, with no top positioning required, and no listener for main height changes.

Testing

The Sidebar Container is currently used on Developer Resources and Documentation.

Things to test

  • Fixing and scrolling on pages with long and short ToCs and Chapter Lists
  • Expand and collapse behaviour of ToC on small screens
  • Scrollbars being shown on hover
  • Show more/less Related items on Code Reference pages like http://localhost:8888/reference/functions/is_admin/

Use WordPress/wporg-developer#447 to test Developer Resources

Documentation requires no changes, so you can use that local environment as is, and switch the mu-plugins install to this branch.

@adamwoodnz adamwoodnz added the Redesign Related to the wordpress.org redesign project label Jan 10, 2024
@adamwoodnz adamwoodnz self-assigned this Jan 10, 2024
@adamwoodnz adamwoodnz force-pushed the enhance/scrolling-sidebar branch 5 times, most recently from b9309f7 to 48fee8f Compare January 12, 2024 02:01
@renintw
Copy link
Contributor

renintw commented Jan 22, 2024

I'm not sure if this has been discussed, but currently, the scrollbar appears only when the sidebar is fixed as described. What about the situation shown in the image below? At this point, the sidebar isn't fixed yet, but the chapter list is already overflowing.

image

Copy link
Contributor

@renintw renintw left a comment

Choose a reason for hiding this comment

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

Tested all the cases suggested in the description. Works as expected 👍
Left some comments, but none of them are blockers.

margin-top: 0;
.is-link-to-top {
display: block;
padding-top: var(--wp--preset--spacing--20);
Copy link
Contributor

Choose a reason for hiding this comment

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

We can also consider removing the extra padding when there's no TOC content.

image

image

mu-plugins/blocks/sidebar-container/postcss/style.pcss Outdated Show resolved Hide resolved
margin-bottom: 0 !important;
padding: var(--wp--preset--spacing--20) 0;

main & {
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems to be the cause of this issue for me— this isn't sass, so the nesting rules aren't the same. This seems to be ignored by postcss, and then stripped out by cssnano, so the rules below are applied to all .wp-block-wporg-sidebar-containers. I don't know why it works for you & @renintw though.

If I move this code to a new section main .wp-block-wporg-sidebar-container {… it works as expected.

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 don't know why it works for you & @renintw though.

Nice catch! All this seems to work with a dev build, but it all breaks with a prod build. Starting to think moving to one CSS style would be a really good idea as switching feature sets contributes to this sort of issue. I think that was suggested to be postcss.

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 that was suggested to be postcss.

I think that was before wp-scripts natively supported sass… and maybe if we switched to sass here, we could remove the custom build script here.

mu-plugins/blocks/sidebar-container/postcss/style.pcss Outdated Show resolved Hide resolved
mu-plugins/blocks/sidebar-container/postcss/style.pcss Outdated Show resolved Hide resolved
@adamwoodnz
Copy link
Contributor Author

I'm not sure if this has been discussed, but currently, the scrollbar appears only when the sidebar is fixed as described. What about the situation shown in the image below? At this point, the sidebar isn't fixed yet, but the chapter list is already overflowing.

My expectation is that the user would do the default and start scrolling the page, as they wouldn't expect the Chapter List to scroll until that behaviour is observed.

Block.editor.chapter.scroll.mp4

Make sense?

@renintw
Copy link
Contributor

renintw commented Jan 25, 2024

My expectation is that the user would do the default and start scrolling the page, as they wouldn't expect the Chapter List to scroll until that behaviour is observed.

I see your idea. Thanks for the explanation with the screencast👍
Was originally thought that users who are already familiar with this page might expect to be able to scroll directly through the chapter lists, similar to the React Docs. But the current setup also makes sense, as more list items can actually be viewed by scrolling as well.

@adamwoodnz adamwoodnz force-pushed the enhance/scrolling-sidebar branch 3 times, most recently from 1f0b446 to 75ef37e Compare January 30, 2024 03:21
@adamwoodnz adamwoodnz force-pushed the enhance/scrolling-sidebar branch from 75ef37e to f5c8a52 Compare January 30, 2024 03:24
This allows individual containers to become inline at different breakpoints,
eg. the Chapter List remaining fixed and scrolling when the ToC is already displayed inline.
@adamwoodnz adamwoodnz force-pushed the enhance/scrolling-sidebar branch from fbdf21b to 0b9b873 Compare January 30, 2024 03:55
@adamwoodnz
Copy link
Contributor Author

In order for the Chapter List container to be fixed and scrolling while the ToC container is inline, the fixed styles have been decoupled from a specific breakpoint and managed via a class instead. The breakpoint for each container to switch between inline and fixed/floating can now be configured via a block attribute.

In making this solution work on iPads in particular, I found that the change in the heights of the admin bar and global nav, and the sticky behaviour of the local nav bar and admin bar make for very complex calculations. A resize handler has been added to set some these changing variables on load, and update on resize to keep everything in sync. A such you should also be able to test switching orientation, not only page load.

These changes can be viewed in b24b6f2.

@adamwoodnz adamwoodnz requested review from ryelle and renintw January 30, 2024 04:09
Comment on lines +126 to +132
@media (min-width: 890px) {
/* stylelint-disable selector-id-pattern */
#wp--skip-link--target {
scroll-margin-top: var(--wp--custom--local-navigation-bar--spacing--height, 0);
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved from the sidebar container styles, as it relates directly to the change in positioning of the local nav bar

@adamwoodnz adamwoodnz force-pushed the enhance/scrolling-sidebar branch from 9de411f to 17d0add Compare January 30, 2024 04:17
Comment on lines -138 to -142
// If there is no table of contents, hide the heading.
if ( ! document.querySelector( '.wp-block-wporg-table-of-contents' ) ) {
const heading = document.querySelector( '.wp-block-wporg-sidebar-container h2' );
heading?.style.setProperty( 'display', 'none' );
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved to the render

Copy link
Contributor

@ryelle ryelle left a comment

Choose a reason for hiding this comment

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

Two pretty minor notes, but overall this is working well in my testing.

I did notice a bit of glitchy behavior when the content is shorter than the Chapters list, and the screen size is just tall enough to render chapters without scrolling, in this case ~1000-1100ish. The sidebar bounces back and forth between scroll/static.

scroll-glitch.mp4

Marking this as approved, if you'd rather fix in a follow-up PR.

esc_html__( '↑ Back to top', 'wporg' )
)
: '';
$inlineBreakpoint = $attributes['inlineBreakpoint'];
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
$inlineBreakpoint = $attributes['inlineBreakpoint'];
$inline_breakpoint = $attributes['inlineBreakpoint'];

PHP variables should be snake case. It looks like we're not running phpcs in the github actions, but you can run it locally with composer lint mu-plugins/blocks/sidebar-container mu-plugins/blocks/table-of-contents

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 in 2aed927

Comment on lines 59 to 66
// If there is no content, don't render the heading.
$content .= empty( trim( wp_strip_all_tags( $post_content ) ) )
? ''
: do_blocks(
'<!-- wp:heading {"style":{"typography":{"fontStyle":"normal","fontWeight":"400"},"spacing":{"margin":{"top":"0","bottom":"0"}}},"fontSize":"normal","fontFamily":"inter"} -->
<h2 class="wp-block-heading has-inter-font-family has-normal-font-size" style="margin-top:0;margin-bottom:0;font-style:normal;font-weight:400">' . esc_html( $title ) . '</h2>
<!-- /wp:heading -->'
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't the if ( ! $items ) check prevent the entire block (incl heading) from being output? I know this is coming from the previous JS, but I'm not sure how we'd end up in a situation where empty( trim( wp_strip_all_tags( $post_content ) ) ) is true but ! $items is false.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch, I moved the logic without checking what would happen just removing the JS. Works just as you described, reverted the render change in 07b512c

… render"

This reverts the render changes in commit 740a0aa, but keeps the JS changes
This adds space between sidebar and footer when main content is short
@adamwoodnz adamwoodnz merged commit 09d0b33 into trunk Jan 31, 2024
2 checks passed
@adamwoodnz adamwoodnz deleted the enhance/scrolling-sidebar branch January 31, 2024 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Sidebar Container [Block] Table of Contents Redesign Related to the wordpress.org redesign project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants