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

Refactor FocalPointPicker to function component #39168

Merged
merged 10 commits into from
Aug 25, 2022
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Internal

- Refactor `FocalPointPicker` to function component ([#39168](https://github.com/WordPress/gutenberg/pull/39168)).

## 20.0.0 (2022-08-24)

### Breaking Changes
Expand Down
9 changes: 3 additions & 6 deletions packages/components/src/focal-point-picker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ const Example = () => {
} );

const url = '/path/to/image';
const dimensions = {
width: 400,
height: 100,
};

/* Example function to render the CSS styles based on Focal Point Picker value */
const style = {
Expand All @@ -33,9 +29,10 @@ const Example = () => {
<>
<FocalPointPicker
url={ url }
dimensions={ dimensions }
value={ focalPoint }
onChange={ ( focalPoint ) => setFocalPoint( { focalPoint } ) }
onDragStart={ setFocalPoint }
onDrag={ setFocalPoint }
onChange={ setFocalPoint }
/>
<div style={ style } />
</>
Expand Down
8 changes: 4 additions & 4 deletions packages/components/src/focal-point-picker/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ const noop = () => {};

export default function FocalPointPickerControls( {
onChange = noop,
percentages = {
point = {
x: 0.5,
y: 0.5,
},
} ) {
const valueX = fractionToPercentage( percentages.x );
const valueY = fractionToPercentage( percentages.y );
const valueX = fractionToPercentage( point.x );
const valueY = fractionToPercentage( point.y );

const handleChange = ( value, axis ) => {
const num = parseInt( value, 10 );

if ( ! isNaN( num ) ) {
onChange( { ...percentages, [ axis ]: num / 100 } );
onChange( { ...point, [ axis ]: num / 100 } );
Comment on lines -21 to +33
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is cosmetic but point just seems like a better name than percentages. It's an internal component so there's no concern of breakage.

}
};

Expand Down
10 changes: 2 additions & 8 deletions packages/components/src/focal-point-picker/focal-point.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,12 @@ import {
*/
import classnames from 'classnames';

export default function FocalPoint( {
coordinates = { left: '50%', top: '50%' },
...props
} ) {
export default function FocalPoint( { left = '50%', top = '50%', ...props } ) {
const classes = classnames(
'components-focal-point-picker__icon_container'
);

const style = {
left: coordinates.left,
top: coordinates.top,
};
const style = { left, top };
Comment on lines -16 to +21
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cosmetic. The separate left and right props are a touch simpler. Less abstract seems a fine thing for an internal component.


return (
<FocalPointWrapper { ...props } className={ classes } style={ style }>
Expand Down
46 changes: 5 additions & 41 deletions packages/components/src/focal-point-picker/grid.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';

/**
* Internal dependencies
*/
Expand All @@ -11,25 +6,16 @@ import {
GridLineX,
GridLineY,
} from './styles/focal-point-picker-style';
import { useUpdateEffect } from '../utils/hooks';

export default function FocalPointPickerGrid( {
bounds = {},
value,
...props
} ) {
const animationProps = useRevealAnimation( value );
const style = {
width: bounds.width,
height: bounds.height,
};

export default function FocalPointPickerGrid( { bounds, ...props } ) {
return (
<GridView
{ ...props }
{ ...animationProps }
className="components-focal-point-picker__grid"
style={ style }
style={ {
width: bounds.width,
height: bounds.height,
} }
>
<GridLineX style={ { top: '33%' } } />
<GridLineX style={ { top: '66%' } } />
Expand All @@ -38,25 +24,3 @@ export default function FocalPointPickerGrid( {
</GridView>
);
}

/**
* Custom hook that renders the "flash" animation whenever the value changes.
*
* @param {string} value Value of (box) side.
*/
function useRevealAnimation( value ) {
const [ isActive, setIsActive ] = useState( false );

useUpdateEffect( () => {
setIsActive( true );
const timeout = window.setTimeout( () => {
setIsActive( false );
}, 600 );

return () => window.clearTimeout( timeout );
}, [ value ] );

return {
isActive,
};
}
Loading