Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/block-directory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"!((src|build|build-module)/components/**)"
],
"dependencies": {
"@wordpress/a11y": "file:../a11y",
"@wordpress/api-fetch": "file:../api-fetch",
"@wordpress/block-editor": "file:../block-editor",
"@wordpress/blocks": "file:../blocks",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
* WordPress dependencies
*/
import { Fragment } from '@wordpress/element';
import { compose } from '@wordpress/compose';
import { compose, useDebounce } from '@wordpress/compose';
import { withSelect } from '@wordpress/data';
import { __, _n, sprintf } from '@wordpress/i18n';
import { Spinner, withSpokenMessages } from '@wordpress/components';
import { Spinner } from '@wordpress/components';
import { speak } from '@wordpress/a11y';

/**
* Internal dependencies
Expand All @@ -19,8 +20,9 @@ function DownloadableBlocksPanel( {
hasPermission,
isLoading,
isWaiting,
debouncedSpeak,
} ) {
const debouncedSpeak = useDebounce( speak, 500 );

if ( false === hasPermission ) {
debouncedSpeak( __( 'No blocks found in your library.' ) );
return (
Expand Down Expand Up @@ -74,7 +76,6 @@ function DownloadableBlocksPanel( {
}

export default compose( [
withSpokenMessages,
withSelect( ( select, { filterValue } ) => {
const {
getDownloadableBlocks,
Expand Down
14 changes: 5 additions & 9 deletions packages/block-editor/src/components/inserter/quick-inserter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import classnames from 'classnames';
*/
import { useState, useMemo, useEffect } from '@wordpress/element';
import { __, _n, sprintf } from '@wordpress/i18n';
import {
VisuallyHidden,
Button,
withSpokenMessages,
} from '@wordpress/components';
import { VisuallyHidden, Button } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { LEFT, RIGHT, UP, DOWN, BACKSPACE, ENTER } from '@wordpress/keycodes';
import { useDebounce } from '@wordpress/compose';
import { speak } from '@wordpress/a11y';

/**
* Internal dependencies
Expand Down Expand Up @@ -107,14 +105,14 @@ function QuickInserterList( {
);
}

function QuickInserter( {
export default function QuickInserter( {
onSelect,
rootClientId,
clientId,
isAppender,
selectBlockOnInsert,
debouncedSpeak,
} ) {
const debouncedSpeak = useDebounce( speak, 500 );
const [ filterValue, setFilterValue ] = useState( '' );
const [
destinationRootClientId,
Expand Down Expand Up @@ -252,5 +250,3 @@ function QuickInserter( {
);
/* eslint-enable jsx-a11y/no-autofocus, jsx-a11y/no-static-element-interactions */
}

export default withSpokenMessages( QuickInserter );
54 changes: 17 additions & 37 deletions packages/components/src/higher-order/with-spoken-messages/index.js
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'
);
11 changes: 11 additions & 0 deletions packages/compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ _Returns_

- `boolean`: Whether or not the text has been copied. Resets after the timeout.

<a name="useDebounce" href="#useDebounce">#</a> **useDebounce**

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`.

_Parameters_

- _args_ `...any`: Arguments passed to Lodash's `debounce`.

<a name="useInstanceId" href="#useInstanceId">#</a> **useInstanceId**

Provides a unique instance ID.
Expand Down
3 changes: 2 additions & 1 deletion packages/compose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"clipboard": "^2.0.1",
"lodash": "^4.17.19",
"mousetrap": "^1.6.5",
"react-resize-aware": "^3.0.1"
"react-resize-aware": "^3.0.1",
"use-memo-one": "^1.1.1"
},
"publishConfig": {
"access": "public"
Expand Down
24 changes: 24 additions & 0 deletions packages/compose/src/hooks/use-debounce/index.js
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 );
Copy link
Contributor

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 useMemo is not enough?

Copy link
Member Author

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. useMemoOne guarantees that will never happen. We're using it in useSelect too.

useEffect( () => () => debounced.cancel(), [ debounced ] );
return debounced;
}
1 change: 1 addition & 0 deletions packages/compose/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export { default as useViewportMatch } from './hooks/use-viewport-match';
export { default as useResizeObserver } from './hooks/use-resize-observer';
export { default as useAsyncList } from './hooks/use-async-list';
export { default as useWarnOnChange } from './hooks/use-warn-on-change';
export { default as useDebounce } from './hooks/use-debounce';
1 change: 1 addition & 0 deletions packages/compose/src/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ export { default as withPreferredColorScheme } from './higher-order/with-preferr
export { default as usePreferredColorScheme } from './hooks/use-preferred-color-scheme';
export { default as usePreferredColorSchemeStyle } from './hooks/use-preferred-color-scheme-style';
export { default as useResizeObserver } from './hooks/use-resize-observer';
export { default as useDebounce } from './hooks/use-debounce';
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import { flow } from 'lodash';
*/
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { MenuItem, withSpokenMessages } from '@wordpress/components';
import { MenuItem } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { check } from '@wordpress/icons';
import { speak } from '@wordpress/a11y';

function FeatureToggle( {
onToggle,
Expand All @@ -19,7 +20,6 @@ function FeatureToggle( {
info,
messageActivated,
messageDeactivated,
speak,
shortcut,
} ) {
const speakMessage = () => {
Expand Down Expand Up @@ -53,5 +53,4 @@ export default compose( [
dispatch( 'core/edit-post' ).toggleFeature( ownProps.feature );
},
} ) ),
withSpokenMessages,
] )( FeatureToggle );
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ import { noop } from 'lodash';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { MenuItem, withSpokenMessages } from '@wordpress/components';
import { MenuItem } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { speak } from '@wordpress/a11y';

export function BlockInspectorButton( {
onClick = noop,
small = false,
speak,
} ) {
export function BlockInspectorButton( { onClick = noop, small = false } ) {
const { shortcut, areAdvancedSettingsOpened } = useSelect(
( select ) => ( {
shortcut: select(
Expand Down Expand Up @@ -64,4 +61,4 @@ export function BlockInspectorButton( {
);
}

export default withSpokenMessages( BlockInspectorButton );
export default BlockInspectorButton;
1 change: 1 addition & 0 deletions packages/edit-site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
],
"dependencies": {
"@babel/runtime": "^7.11.2",
"@wordpress/a11y": "file:../a11y",
"@wordpress/api-fetch": "file:../api-fetch",
"@wordpress/block-editor": "file:../block-editor",
"@wordpress/block-library": "file:../block-library",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import { flow } from 'lodash';
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { MenuItem, withSpokenMessages } from '@wordpress/components';
import { MenuItem } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { check } from '@wordpress/icons';
import { speak } from '@wordpress/a11y';

function FeatureToggle( {
export default function FeatureToggle( {
feature,
label,
info,
messageActivated,
messageDeactivated,
speak,
} ) {
const speakMessage = () => {
if ( isActive ) {
Expand Down Expand Up @@ -48,5 +48,3 @@ function FeatureToggle( {
</MenuItem>
);
}

export default withSpokenMessages( FeatureToggle );
1 change: 1 addition & 0 deletions packages/format-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@babel/runtime": "^7.11.2",
"@wordpress/block-editor": "file:../block-editor",
"@wordpress/components": "file:../components",
"@wordpress/compose": "file:../compose",
"@wordpress/data": "file:../data",
"@wordpress/dom": "file:../dom",
"@wordpress/element": "file:../element",
Expand Down
13 changes: 8 additions & 5 deletions packages/format-library/src/text-color/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { get } from 'lodash';
*/
import { useCallback, useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { withSpokenMessages } from '@wordpress/components';
import { getRectangleFromRange } from '@wordpress/dom';
import {
applyFormat,
Expand Down Expand Up @@ -119,7 +118,13 @@ const ColorPicker = ( { name, value, onChange } ) => {
return <ColorPalette value={ activeColor } onChange={ onColorChange } />;
};

const InlineColorUI = ( { name, value, onChange, onClose, addingColor } ) => {
export default function InlineColorUI( {
name,
value,
onChange,
onClose,
addingColor,
} ) {
return (
<ColorPopoverAtLink
value={ value }
Expand All @@ -130,6 +135,4 @@ const InlineColorUI = ( { name, value, onChange, onClose, addingColor } ) => {
<ColorPicker name={ name } value={ value } onChange={ onChange } />
</ColorPopoverAtLink>
);
};

export default withSpokenMessages( InlineColorUI );
}