Skip to content

Commit

Permalink
Adds custom hook useResponsiveBlockControl
Browse files Browse the repository at this point in the history
Attempt to relieve some of the overhead associated with having ResponsiveBlockControl be a fully controlled component. By consuming this hook, a developer can wire up a default handler for toggling responsive mode without having to worry about creating their own useState-based hooks.
  • Loading branch information
getdave committed Nov 1, 2019
1 parent e6aad72 commit 48529f7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export { default as MediaUploadCheck } from './media-upload/check';
export { default as PanelColorSettings } from './panel-color-settings';
export { default as PlainText } from './plain-text';
export { default as __experimentalResponsiveBlockControl } from './responsive-block-control';
export { default as __experimentalUseResponsiveBlockControl } from './responsive-block-control/use-responsive-block-control';
export {
default as RichText,
RichTextShortcut,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { uniqueId } from 'lodash';
/**
* WordPress dependencies
*/
import { Fragment, useState } from '@wordpress/element';
import { Fragment } from '@wordpress/element';

import {
SelectControl,
Expand All @@ -18,6 +18,7 @@ import {
* Internal dependencies
*/
import ResponsiveBlockControl from '../index';
import useResponsiveBlockControl from '../use-responsive-block-control';

let container = null;
beforeEach( () => {
Expand Down Expand Up @@ -254,16 +255,14 @@ describe( 'Default and Responsive modes', () => {

it( 'should switch between default and responsive modes when interacting with toggle control', () => {
const ResponsiveBlockControlConsumer = () => {
const [ isResponsive, setIsResponsive ] = useState( false );
const [ isResponsive, onIsResponsiveChange ] = useResponsiveBlockControl();

return (
<ResponsiveBlockControl
title="Padding"
property="padding"
isResponsive={ isResponsive }
onIsResponsiveChange={ () => {
setIsResponsive( ! isResponsive );
} }
onIsResponsiveChange={ onIsResponsiveChange }
renderDefaultControl={ renderTestDefaultControlComponent }
/>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';

const useResponsiveBlockControl = ( initialState = false ) => {
const [ isResponsive, setIsResponsive ] = useState( initialState );

const onIsResponsiveChange = () => {
setIsResponsive( ! isResponsive );
};

return [
isResponsive,
onIsResponsiveChange,
];
};

export default useResponsiveBlockControl;
14 changes: 4 additions & 10 deletions packages/block-library/src/group/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@ import {
PanelColorSettings,
withColors,
__experimentalResponsiveBlockControl as ResponsiveBlockControl,
__experimentalUseResponsiveBlockControl as useResponsiveBlockControl,
} from '@wordpress/block-editor';

import {

useState,

} from '@wordpress/element';

import {
PanelBody,
SelectControl,
Expand All @@ -34,7 +29,8 @@ function GroupEdit( {
backgroundColor,
hasInnerBlocks,
} ) {
const [ isResponsive, setIsResponsive ] = useState( false );
const [ isResponsive, onIsResponsiveChange ] = useResponsiveBlockControl();

const styles = {
backgroundColor: backgroundColor.color,
};
Expand Down Expand Up @@ -78,9 +74,7 @@ function GroupEdit( {
title="Padding"
property="padding"
isResponsive={ isResponsive }
onIsResponsiveChange={ () => {
setIsResponsive( ! isResponsive );
} }
onIsResponsiveChange={ onIsResponsiveChange }
renderDefaultControl={ ( labelComponent ) => (
<SelectControl
label={ labelComponent }
Expand Down

0 comments on commit 48529f7

Please sign in to comment.