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

Image block: fix resize listener #22277

Merged
merged 3 commits into from
May 19, 2020
Merged

Image block: fix resize listener #22277

merged 3 commits into from
May 19, 2020

Conversation

ellatrix
Copy link
Member

Description

In #22262 I forgot to account for the withGlobalEvents HoC. This PR creates a new useGlobalEvent hook to provide the same functionality for function components.

How has this been tested?

Resizing an image shouldn't log errors.

Screenshots

Types of changes

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • 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.

@ellatrix ellatrix requested a review from aduth May 11, 2020 19:06
@ellatrix ellatrix added [Type] Bug An existing feature does not function as intended [Type] Code Quality Issues or PRs that relate to code quality labels May 11, 2020
@github-actions
Copy link

github-actions bot commented May 11, 2020

Size Change: +4.69 kB (0%)

Total Size: 829 kB

Filename Size Change
build/annotations/index.js 3.62 kB -1 B
build/api-fetch/index.js 4.08 kB -2 B (0%)
build/block-directory/index.js 6.59 kB -21 B (0%)
build/block-directory/style-rtl.css 764 B +4 B (0%)
build/block-directory/style.css 764 B +3 B (0%)
build/block-editor/index.js 104 kB +1.45 kB (1%)
build/block-editor/style-rtl.css 10.8 kB +461 B (4%)
build/block-editor/style.css 10.8 kB +459 B (4%)
build/block-library/editor-rtl.css 7.25 kB +134 B (1%)
build/block-library/editor.css 7.25 kB +134 B (1%)
build/block-library/index.js 116 kB +174 B (0%)
build/block-library/style-rtl.css 7.34 kB -43 B (0%)
build/block-library/style.css 7.34 kB -42 B (0%)
build/blocks/index.js 48.1 kB +15 B (0%)
build/components/index.js 181 kB +586 B (0%)
build/components/style-rtl.css 17.1 kB +95 B (0%)
build/components/style.css 17 kB +99 B (0%)
build/compose/index.js 6.66 kB -1 B
build/core-data/index.js 11.4 kB -10 B (0%)
build/data-controls/index.js 1.29 kB +2 B (0%)
build/data/index.js 8.42 kB -22 B (0%)
build/edit-navigation/index.js 5.59 kB +1.19 kB (21%) 🚨
build/edit-post/index.js 28 kB +34 B (0%)
build/edit-site/index.js 12.1 kB -4 B (0%)
build/edit-widgets/index.js 8.37 kB +2 B (0%)
build/editor/index.js 44.3 kB -8 B (0%)
build/element/index.js 4.65 kB -1 B
build/format-library/index.js 7.63 kB -1 B
build/hooks/index.js 2.13 kB -6 B (0%)
build/keyboard-shortcuts/index.js 2.51 kB -1 B
build/list-reusable-blocks/index.js 3.13 kB +4 B (0%)
build/media-utils/index.js 5.29 kB +4 B (0%)
build/notices/index.js 1.79 kB +1 B
build/nux/index.js 3.4 kB -1 B
build/plugins/index.js 2.56 kB -1 B
build/rich-text/index.js 14.8 kB -1 B
build/server-side-render/index.js 2.68 kB +2 B (0%)
build/viewport/index.js 1.84 kB +1 B
build/wordcount/index.js 1.17 kB -1 B
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.02 kB 0 B
build/autop/index.js 2.82 kB 0 B
build/blob/index.js 620 B 0 B
build/block-library/theme-rtl.css 683 B 0 B
build/block-library/theme.css 685 B 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/date/index.js 5.47 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 568 B 0 B
build/dom/index.js 3.1 kB 0 B
build/edit-navigation/style-rtl.css 618 B 0 B
build/edit-navigation/style.css 617 B 0 B
build/edit-post/style-rtl.css 12.2 kB 0 B
build/edit-post/style.css 12.2 kB 0 B
build/edit-site/style-rtl.css 5.22 kB 0 B
build/edit-site/style.css 5.22 kB 0 B
build/edit-widgets/style-rtl.css 4.69 kB 0 B
build/edit-widgets/style.css 4.69 kB 0 B
build/editor/editor-styles-rtl.css 425 B 0 B
build/editor/editor-styles.css 428 B 0 B
build/editor/style-rtl.css 5.07 kB 0 B
build/editor/style.css 5.08 kB 0 B
build/escape-html/index.js 734 B 0 B
build/format-library/style-rtl.css 502 B 0 B
build/format-library/style.css 502 B 0 B
build/html-entities/index.js 622 B 0 B
build/i18n/index.js 3.56 kB 0 B
build/is-shallow-equal/index.js 710 B 0 B
build/keycodes/index.js 1.94 kB 0 B
build/list-reusable-blocks/style-rtl.css 226 B 0 B
build/list-reusable-blocks/style.css 226 B 0 B
build/nux/style-rtl.css 616 B 0 B
build/nux/style.css 613 B 0 B
build/primitives/index.js 1.5 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/redux-routine/index.js 2.85 kB 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.28 kB 0 B
build/url/index.js 4.02 kB 0 B
build/warning/index.js 1.14 kB 0 B

compressed-size-action

@gziolo gziolo added the [Type] Feature New feature to highlight in changelogs. label May 12, 2020
* @param {Array} args `addEventListener` arguments.
* @param {Array} dependencies Hook dependencies.
*/
export default function useGlobalEvent( ref, args, dependencies ) {
Copy link
Contributor

@youknowriad youknowriad May 12, 2020

Choose a reason for hiding this comment

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

One of the reasons for the existence of withGlobalEvent is the performance impact of using a single event listener for multiple components using the same args. Do we want to support that here?

Copy link
Member

@aduth aduth May 12, 2020

Choose a reason for hiding this comment

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

One of the reasons for the existence of withGlobalEvent is the performance impact of using a single event listener for multiple components using the same args. Do we want to support that here?

I forget where exactly I'd remarked about it, but over time I started to question whether there really should expect to be any performance benefit of using one event handler for all global events, since in either case something would need to iterate and invoke each of the handlers. It seems browser native code could likely be more efficient about this.

Copy link
Member

Choose a reason for hiding this comment

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

I forget where exactly I'd remarked about it

See #18816 (comment)

Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

To be honest, I find the usage to be a bit awkward, between needing to pass a ref of the node and arguments of addEventListener as an array. ref feels odd because if this is intended to be capturing global events, why should I need to be tracking and passing some reference to my component's local element? (I understand the need, just expressing a sentiment which we could expect from developers using this)

I might wonder instead if...

  • We just assume window global, don't do anything dealing with ownerDocument
  • Or, we just avoid a common hook, and leave it to the individual component to implement. Compared to withGlobalEvent, there's an appeal here in that (a) it's a bit more straight-forward with hooks and (b) we're not leveraging the "one event handler" behavior of withGlobalEvent anyways.
  • Or, change the behavior of the hook to be more of a generic event-handling hook, where we give it the element we want to bind to (give it ref.current.ownerDocument, or window).

For arguments, could it be something like...

useGlobalEvent( 'resize', callback, deps );

?

It misses the useCapture argument, which we could either be fine with not supporting, or because it's of a different type than deps, maybe we can overload to support it as a third argument.

useGlobalEvent( 'resize', callback[, useCapture], deps );

@aduth
Copy link
Member

aduth commented May 13, 2020

If we introduce a hook like this, should we refactor withGlobalEvents to use it in place of its own implementation? (General question, not necessarily as part of this pull request)

@ellatrix
Copy link
Member Author

I think it makes sense to let components do it for itself right now and we can decide later if we want a hook for it. To be honest, it might just be better like that because the hooks also doesn't provide an option to set it to document. I think in order to keep components isolated and working in any context, window should never be used directly.

@ellatrix ellatrix changed the title New hook: useGlobalEvent Image block: fix resize listener May 13, 2020
@ellatrix
Copy link
Member Author

Seeing the code without useGlobalEvent it seems like a really stupid idea looking back.

@ellatrix ellatrix removed the [Type] Feature New feature to highlight in changelogs. label May 13, 2020

defaultView.addEventListener( 'resize', calculateSize );
image.addEventListener( 'load', calculateSize );
Copy link
Member

Choose a reason for hiding this comment

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

I guess there's no meaningful difference between addEventListener( 'load', fn ) and onload = fn ? (Aside from the ability to have multiple event handlers in the former, which doesn't apply here anyways)

Copy link
Member Author

Choose a reason for hiding this comment

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

Exactly. I like the consistency and it's clearer how the listener is removed.

@aduth
Copy link
Member

aduth commented May 13, 2020

Separately worth considering:

  • Maybe we should soft-deprecate withGlobalEvents? While the same sorts of issues can happen with hooks (forgetting to unbind), the interface of hooks makes it a bit more obvious and colocated to associate binding and unbinding behaviors.
  • Edit: withGlobalEvents could probably benefit from a refactoring. Even if it would be discouraged in our own code, it will still always need to be shipped. Glancing at the implementation, a hooks refactor might be able to save some bytes.
  • Need Testing: Use SCRIPT_DEBUG in end-to-end test environment #14845 😄

@ellatrix
Copy link
Member Author

Thanks! Yeah I agree we should warn when using the HoC once we've removed all uses from our own code.

@ellatrix ellatrix merged commit 4bb758d into master May 19, 2020
@ellatrix ellatrix deleted the try/use-global-event branch May 19, 2020 11:12
@github-actions github-actions bot added this to the Gutenberg 8.2 milestone May 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Bug An existing feature does not function as intended [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants