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

Drop zone: avoid media query on mount #60546

Merged
merged 4 commits into from
Apr 23, 2024
Merged

Conversation

ellatrix
Copy link
Member

@ellatrix ellatrix commented Apr 7, 2024

What?

useReducedMotion triggers a media query, and it's not needed until blocks are dragging over this component.

image

Why?

How?

Testing Instructions

Testing Instructions for Keyboard

Screenshots or screencast

@ellatrix ellatrix added the [Type] Performance Related to performance efforts label Apr 7, 2024
@ellatrix ellatrix requested a review from ajitbohra as a code owner April 7, 2024 19:31
Copy link

github-actions bot commented Apr 7, 2024

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@ellatrix ellatrix requested review from mirka, jsnajdr and tyxla April 22, 2024 15:29
@ellatrix ellatrix force-pushed the try/drop-zone-avoid-media-query branch 2 times, most recently from c5e375b to c8f7e30 Compare April 23, 2024 08:44
@mirka mirka added the [Package] Components /packages/components label Apr 23, 2024
Copy link
Member

@mirka mirka left a comment

Choose a reason for hiding this comment

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

Refactor looks good and still works as expected 👍

@jsnajdr
Copy link
Member

jsnajdr commented Apr 23, 2024

A month ago we discussed useReducedMotion with @tyxla in this PR: #60168 (review)

The point is that each instance of useReducedMotion (and also of useViewportMatch) does its own media query, although they end up being duplicated many times. And if they are really visible on the performance flame chart, it's a problem.

Opposed to that, framer-motion internally does that query just once at boot, in one singleton instance, and doesn't even bother to observe changes. It assumes that the prefers-reduced-motion value is constant for the entire lifetime of the page.

@tyxla
Copy link
Member

tyxla commented Apr 23, 2024

A month ago we discussed useReducedMotion with @tyxla in this PR: #60168 (review)

The point is that each instance of useReducedMotion (and also of useViewportMatch) does its own media query, although they end up being duplicated many times. And if they are really visible on the performance flame chart, it's a problem.

Opposed to that, framer-motion internally does that query just once at boot, in one singleton instance, and doesn't even bother to observe changes. It assumes that the prefers-reduced-motion value is constant for the entire lifetime of the page.

Yes, I think this is a good idea for a follow-up. I know @DaniGuardiola mentioned he was looking at useReducedMotion() as well (if I'm not wrong) so it could be a nice little optimization to pick up.

Copy link
Member

@tyxla tyxla left a comment

Choose a reason for hiding this comment

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

Works well in my testing and code looks good 👍

As @jsnajdr suggested, it could be a good follow-up opportunity to refactor our media query hooks to run fewer times.

@@ -25,6 +25,71 @@ import {
import type { DropType, DropZoneProps } from './types';
import type { WordPressComponentProps } from '../context';

function DropIndicator( { label }: { label?: string } ) {
const disableMotion = useReducedMotion();
const backdrop = {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Given we continue to work with framer motion for this animation (which @DaniGuardiola is looking at changing in #60975), those animation variants could be declared outside of the component, there's no need to redefine them on every DropIndicator render.

@ellatrix ellatrix force-pushed the try/drop-zone-avoid-media-query branch from cd1c578 to c147d76 Compare April 23, 2024 12:31
@ellatrix ellatrix enabled auto-merge (squash) April 23, 2024 12:31
@ellatrix ellatrix merged commit 73f91a2 into trunk Apr 23, 2024
59 of 61 checks passed
@ellatrix ellatrix deleted the try/drop-zone-avoid-media-query branch April 23, 2024 13:05
@github-actions github-actions bot added this to the Gutenberg 18.3 milestone Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Components /packages/components [Type] Performance Related to performance efforts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants