-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New hook: useDebounce for speak function #25948
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 17 additions & 37 deletions
54
packages/components/src/higher-order/with-spoken-messages/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,26 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { debounce } from 'lodash'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { Component } from '@wordpress/element'; | ||
| import { createHigherOrderComponent, useDebounce } from '@wordpress/compose'; | ||
| import { speak } from '@wordpress/a11y'; | ||
| import { createHigherOrderComponent } from '@wordpress/compose'; | ||
|
|
||
| /** | ||
| * A Higher Order Component used to be provide a unique instance ID by | ||
| * component. | ||
| * A Higher Order Component used to be provide speak and debounced speak | ||
| * functions. | ||
| * | ||
| * @see https://developer.wordpress.org/block-editor/packages/packages-a11y/#speak | ||
| * | ||
| * @param {WPComponent} WrappedComponent The wrapped component. | ||
| * @param {WPComponent} Component The component to be wrapped. | ||
| * | ||
| * @return {WPComponent} The component to be rendered. | ||
| * @return {WPComponent} The wrapped component. | ||
| */ | ||
| export default createHigherOrderComponent( ( WrappedComponent ) => { | ||
| return class extends Component { | ||
| constructor() { | ||
| super( ...arguments ); | ||
| this.debouncedSpeak = debounce( this.speak.bind( this ), 500 ); | ||
| } | ||
|
|
||
| speak( message, type = 'polite' ) { | ||
| speak( message, type ); | ||
| } | ||
|
|
||
| componentWillUnmount() { | ||
| this.debouncedSpeak.cancel(); | ||
| } | ||
|
|
||
| render() { | ||
| return ( | ||
| <WrappedComponent | ||
| { ...this.props } | ||
| speak={ this.speak } | ||
| debouncedSpeak={ this.debouncedSpeak } | ||
| /> | ||
| ); | ||
| } | ||
| }; | ||
| }, 'withSpokenMessages' ); | ||
| export default createHigherOrderComponent( | ||
| ( Component ) => ( props ) => ( | ||
| <Component | ||
| { ...props } | ||
| speak={ speak } | ||
| debouncedSpeak={ useDebounce( speak, 500 ) } | ||
| /> | ||
| ), | ||
| 'withSpokenMessages' | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { debounce } from 'lodash'; | ||
| import { useMemoOne } from 'use-memo-one'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useEffect } from '@wordpress/element'; | ||
|
|
||
| /** | ||
| * Debounces a function with Lodash's `debounce`. A new debounced function will | ||
| * be returned and any scheduled calls cancelled if any of the arguments change, | ||
| * including the function to debounce, so please wrap functions created on | ||
| * render in components in `useCallback`. | ||
| * | ||
| * @param {...any} args Arguments passed to Lodash's `debounce`. | ||
| */ | ||
| export default function useDebounce( ...args ) { | ||
| const debounced = useMemoOne( () => debounce( ...args ), args ); | ||
| useEffect( () => () => debounced.cancel(), [ debounced ] ); | ||
ellatrix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return debounced; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand why
useMemois not enough?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, it could have a debounced call randomly cancelled.
useMemoOneguarantees that will never happen. We're using it inuseSelecttoo.