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

Fix unified-sidebar scrolling #46689

Merged
merged 13 commits into from
Oct 29, 2020
Merged

Conversation

cpapazoglou
Copy link
Contributor

@cpapazoglou cpapazoglou commented Oct 22, 2020

After a lot of try & fail, I eventually:

  • used vanilla JS no without passing styles through react to the sidebar component. This way we avoid rerendering the component while scrolling.
  • had to manipulate the secondary area and not the sidebar itself as it is encapsulated over there

Changes proposed in this Pull Request

  • Adds ...props pass through to Sidebar component.
  • Adds window.addEventListener( 'scroll') in MySitesSidebarUnified component so that we reposition (scroll the sidebar) like wp-admin behaves.

Testing instructions

  • load calypso with ?flags=nav-unification
  • test that the sidebar behaves as in wp-admin, especially when the window height is shorter than the sidebar
  • test that my sites, reader, me work as expected
  • test the mobile view (sidebar full width)

Relates #46643

@matticbot
Copy link
Contributor

@cpapazoglou cpapazoglou changed the title fix/unified-sidebar-scrolling Fix unified-sidebar scrolling Oct 22, 2020
@matticbot
Copy link
Contributor

matticbot commented Oct 22, 2020

Here is how your PR affects size of JS and CSS bundles shipped to the user's browser:

App Entrypoints (~461 bytes added 📈 [gzipped])

name        parsed_size           gzip_size
entry-main      +1422 B  (+0.1%)     +461 B  (+0.1%)

Common code that is always downloaded and parsed every time the app is loaded, no matter which route is used.

Sections (~83 bytes added 📈 [gzipped])

name                   parsed_size           gzip_size
woocommerce                  +41 B  (+0.0%)       +9 B  (+0.0%)
site-blocks                  +41 B  (+0.0%)       +9 B  (+0.0%)
security                     +41 B  (+0.0%)       +9 B  (+0.0%)
purchases                    +41 B  (+0.0%)       +4 B  (+0.0%)
privacy                      +41 B  (+0.0%)       +9 B  (+0.0%)
notification-settings        +41 B  (+0.0%)       +9 B  (+0.0%)
me                           +41 B  (+0.0%)       +9 B  (+0.0%)
help                         +41 B  (+0.0%)       +9 B  (+0.0%)
happychat                    +41 B  (+0.0%)       +9 B  (+0.0%)
devdocs                      +41 B  (+0.0%)      -11 B  (-0.0%)
account-close                +41 B  (+0.0%)       +9 B  (+0.0%)
account                      +41 B  (+0.0%)       +9 B  (+0.0%)

Sections contain code specific for a given set of routes. Is downloaded and parsed only when a particular route is navigated to.

Async-loaded Components (~52 bytes added 📈 [gzipped])

name                                           parsed_size           gzip_size
async-load-calypso-reader-sidebar                    +41 B  (+0.1%)       +6 B  (+0.0%)
async-load-calypso-my-sites-sidebar-unified          +41 B  (+0.1%)       +8 B  (+0.1%)
async-load-calypso-my-sites-sidebar                  +41 B  (+0.0%)      +19 B  (+0.0%)
async-load-calypso-components-jetpack-sidebar        +41 B  (+0.2%)      +19 B  (+0.3%)

React components that are loaded lazily, when a certain part of UI is displayed for the first time.

Legend

What is parsed and gzip size?

Parsed Size: Uncompressed size of the JS and CSS files. This much code needs to be parsed and stored in memory.
Gzip Size: Compressed size of the JS and CSS files. This much data needs to be downloaded over network.

Generated by performance advisor bot at iscalypsofastyet.com.

@matticbot matticbot added the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Oct 26, 2020
@cpapazoglou cpapazoglou added the [Feature] Calypso & wp-admin Navigation All navigation in Calypso and wp-admin, and the unified transitions between the two. label Oct 26, 2020
@mreishus
Copy link
Contributor

I don't think the scroll handler is being cleaned up properly.

To test, apply diff:

diff --git a/client/my-sites/sidebar-unified/index.jsx b/client/my-sites/sidebar-unified/index.jsx
index 6763389a39..6296943c20 100644
--- a/client/my-sites/sidebar-unified/index.jsx
+++ b/client/my-sites/sidebar-unified/index.jsx
@@ -39,8 +39,10 @@ export const MySitesSidebarUnified = ( { path } ) => {
        const sidebarIsCollapsed = useSelector( getSidebarIsCollapsed );
 
        useEffect( () => {
+               console.log( 'Registered event listener' );
                window.addEventListener( 'scroll', () => handleScroll() );
                return () => {
+                       console.log( 'Cleared event listener' );
                        window.removeEventListener( 'scroll', () => handleScroll() );
                };
        } );
diff --git a/client/my-sites/sidebar-unified/utils.jsx b/client/my-sites/sidebar-unified/utils.jsx
index 22c82acb7b..bdf43d71f8 100644
--- a/client/my-sites/sidebar-unified/utils.jsx
+++ b/client/my-sites/sidebar-unified/utils.jsx
@@ -6,6 +6,7 @@ let pinnedSidebarBottom = false;
 let ticking = false; // Used for Scroll event throttling.
 
 export const handleScroll = () => {
+       console.log( 'handlescroll' );
        const windowHeight = window?.innerHeight;
        const secondaryElHeight = secondaryEl?.scrollHeight;
        const masterbarHeight = document.getElementById( 'header' ).getBoundingClientRect().height;

I think because A and B are different anonymous functions, it can't match them up:

	useEffect( () => {
		window.addEventListener( 'scroll', () => handleScroll() ); // A
		return () => {
			window.removeEventListener( 'scroll', () => handleScroll() ); // B
		};
	} );

I'm about to head out, so I haven't tested it, but I think const x = useCallback(() => handleScroll()); then using x in both addEventListener and removeEventListener should fix.

@frontdevde
Copy link
Contributor

From a purely functional perspective this seems to be working fine for me locally until I interact with the My Sites switcher. When scrolling while the site list is showing, it starts detaching the sidebar. Sent you a screen-recording via DM.

@cpapazoglou
Copy link
Contributor Author

I don't think the scroll handler is being cleaned up properly.

To test, apply diff:

diff --git a/client/my-sites/sidebar-unified/index.jsx b/client/my-sites/sidebar-unified/index.jsx
index 6763389a39..6296943c20 100644
--- a/client/my-sites/sidebar-unified/index.jsx
+++ b/client/my-sites/sidebar-unified/index.jsx
@@ -39,8 +39,10 @@ export const MySitesSidebarUnified = ( { path } ) => {
        const sidebarIsCollapsed = useSelector( getSidebarIsCollapsed );
 
        useEffect( () => {
+               console.log( 'Registered event listener' );
                window.addEventListener( 'scroll', () => handleScroll() );
                return () => {
+                       console.log( 'Cleared event listener' );
                        window.removeEventListener( 'scroll', () => handleScroll() );
                };
        } );
diff --git a/client/my-sites/sidebar-unified/utils.jsx b/client/my-sites/sidebar-unified/utils.jsx
index 22c82acb7b..bdf43d71f8 100644
--- a/client/my-sites/sidebar-unified/utils.jsx
+++ b/client/my-sites/sidebar-unified/utils.jsx
@@ -6,6 +6,7 @@ let pinnedSidebarBottom = false;
 let ticking = false; // Used for Scroll event throttling.
 
 export const handleScroll = () => {
+       console.log( 'handlescroll' );
        const windowHeight = window?.innerHeight;
        const secondaryElHeight = secondaryEl?.scrollHeight;
        const masterbarHeight = document.getElementById( 'header' ).getBoundingClientRect().height;

I think because A and B are different anonymous functions, it can't match them up:

	useEffect( () => {
		window.addEventListener( 'scroll', () => handleScroll() ); // A
		return () => {
			window.removeEventListener( 'scroll', () => handleScroll() ); // B
		};
	} );

I'm about to head out, so I haven't tested it, but I think const x = useCallback(() => handleScroll()); then using x in both addEventListener and removeEventListener should fix.

Great finding @mreishus , thanks! Since the callback has no dependencies, declaring
const scrollCallback = () => handleScroll(); outside of the component and using scrollCallback in the event listener is enough bccd0ff

@cpapazoglou
Copy link
Contributor Author

From a purely functional perspective this seems to be working fine for me locally until I interact with the My Sites switcher. When scrolling while the site list is showing, it starts detaching the sidebar. Sent you a screen-recording via DM.

Thanks @frontdevde for uncovering this! Fixed here 8a74b4c

@cpapazoglou cpapazoglou force-pushed the fix/unified-sidebar-scrolling branch from 8a74b4c to 493b60c Compare October 28, 2020 19:14
@cpapazoglou
Copy link
Contributor Author

Since the changes are regarding the secondary and not the sidebar, I moved the code to wp-calypso/client/layout/index.jsx and also rebased.

This change also helps reader and me behave the same way.

@@ -78,6 +80,15 @@ class Layout extends Component {
.classList.add( `is-${ this.props.colorSchemePreference }` );
}
}
if ( config.isEnabled( 'nav-unification' ) ) {
window.addEventListener( 'scroll', scrollCallback );
Copy link
Member

Choose a reason for hiding this comment

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

Could this be simplified to the following?

Suggested change
window.addEventListener( 'scroll', scrollCallback );
window.addEventListener( 'scroll', handleScroll );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was done so that we can effectively clear the event listener as discussed here #46689 (comment). I have already tried using the exported function handleScroll directly in vain. I may be doing something wrong though..

@cpapazoglou cpapazoglou requested a review from obenland October 29, 2020 09:12
@frontdevde
Copy link
Contributor

I'm loving the direction we're going in here. This already feels way better than current Calypso. Scrolling the sidebar by scrolling the page is so much more intuitive than having to scroll inside the sidebar. 👏

Have we tested this on an iPad (in landscape and switching between orientations)?

One thing I noticed is that it's still possible to recreate a detached state when using the dev tools. We might need to factor in resize in this as well.

nav-dev-tools

@frontdevde
Copy link
Contributor

Suggestion: Let's remove the Fixes #46643 keyword from the description (or create a separate issue for the second part of the issue).

This PR fixes the first part of the issue, which is great! We should definitely limit the scope of this PR to that issue. That said, we'll still need to address the my site switcher overflow being visible during the transition when a site is selected. This will probably involve dynamically hiding the overflow for the duration of the transition.

@cpapazoglou
Copy link
Contributor Author

I have added the resize event which hopefully will fix any problems on orientation change. Lets merge it so I can test it on a real device easier.

client/layout/utils.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@frontdevde frontdevde left a comment

Choose a reason for hiding this comment

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

After some more testing I can confirm that the resize is now handled well and the previously reported issue is gone.

Let's remove the rouge console.log and then I'd be fine with merging it so we can a) get it in in time for demo and b) you can properly test this on the iPad. Let's keep iterating! 👍

@cpapazoglou cpapazoglou merged commit b8233e1 into master Oct 29, 2020
@cpapazoglou cpapazoglou deleted the fix/unified-sidebar-scrolling branch October 29, 2020 13:16
@matticbot matticbot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Oct 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Calypso & wp-admin Navigation All navigation in Calypso and wp-admin, and the unified transitions between the two.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants