From e0015da798a8fb7db50c1ebd51eb2db2efabee9f Mon Sep 17 00:00:00 2001 From: Mitchell Austin Date: Mon, 22 Aug 2022 11:07:28 -0700 Subject: [PATCH] Simplify state handling --- .../src/focal-point-picker/index.js | 59 ++++++++----------- 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/packages/components/src/focal-point-picker/index.js b/packages/components/src/focal-point-picker/index.js index 269dc565320f4b..09128c04b980f8 100644 --- a/packages/components/src/focal-point-picker/index.js +++ b/packages/components/src/focal-point-picker/index.js @@ -45,18 +45,29 @@ export default function FocalPointPicker( { }, } ) { const [ point, setPoint ] = useState( valueProp ); - // Tracks the internal point value when it’s yet to be propagated. - const refPoint = useRef(); - const keepPoint = ( value ) => { - setPoint( value ); - refPoint.current = value; - }; - const sendPoint = ( value = refPoint.current ) => { - onChange?.( value ); - refPoint.current = undefined; - }; - // Uses the internal point if it is held or else the value from props. - const { x, y } = refPoint.current !== undefined ? point : valueProp; + + const { startDrag, endDrag, isDragging } = useDragging( { + onDragStart: ( event ) => { + dragAreaRef.current.focus(); + const value = getValueWithinDragArea( event ); + onDragStart?.( value, event ); + setPoint( value ); + }, + onDragMove: ( event ) => { + // Prevents text-selection when dragging. + event.preventDefault(); + const value = getValueWithinDragArea( event ); + onDrag?.( value, event ); + setPoint( value ); + }, + onDragEnd: ( event ) => { + onDragEnd?.( event ); + onChange?.( point ); + }, + } ); + + // Uses the internal point while dragging or else the value from props. + const { x, y } = isDragging ? point : valueProp; const dragAreaRef = useRef(); const [ bounds, setBounds ] = useState( INITIAL_BOUNDS ); @@ -81,26 +92,6 @@ export default function FocalPointPicker( { // Updates the bounds to cover cases of unspecified media or load failures. useIsomorphicLayoutEffect( () => void refUpdateBounds.current(), [] ); - const { startDrag, endDrag, isDragging } = useDragging( { - onDragStart: ( event ) => { - dragAreaRef.current.focus(); - const value = getValueWithinDragArea( event ); - onDragStart?.( value, event ); - keepPoint( value ); - }, - onDragMove: ( event ) => { - // Prevents text-selection when dragging. - event.preventDefault(); - const value = getValueWithinDragArea( event ); - onDrag?.( value, event ); - keepPoint( value ); - }, - onDragEnd: ( event ) => { - onDragEnd?.( event ); - sendPoint(); - }, - } ); - const getValueWithinDragArea = ( { clientX, clientY, shiftKey } ) => { const { top, left } = dragAreaRef.current.getBoundingClientRect(); let nextX = ( clientX - left ) / bounds.width; @@ -139,7 +130,7 @@ export default function FocalPointPicker( { code === 'ArrowUp' || code === 'ArrowLeft' ? -1 * step : step; const axis = code === 'ArrowUp' || code === 'ArrowDown' ? 'y' : 'x'; value[ axis ] = parseFloat( value[ axis ] ) + delta; - sendPoint( getFinalValue( value ) ); + onChange?.( getFinalValue( value ) ); }; const focalPointPosition = { @@ -190,7 +181,7 @@ export default function FocalPointPicker( { { - sendPoint( getFinalValue( value ) ); + onChange?.( getFinalValue( value ) ); } } />