Skip to content

Commit

Permalink
Adds timeout before reseting scroll position on segment change
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonis Lilis committed Apr 22, 2021
1 parent 85ff940 commit d8156c1
Showing 1 changed file with 13 additions and 42 deletions.
55 changes: 13 additions & 42 deletions packages/components/src/color-palette/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
View,
Animated,
Easing,
Dimensions,
Platform,
Text,
} from 'react-native';
Expand All @@ -26,9 +25,8 @@ import ColorIndicator from '../color-indicator';
import { colorsUtils } from '../mobile/color-settings/utils';

const ANIMATION_DURATION = 200;
const SEGMENT_SWITCH_DELAY = 200;

let contentWidth = 0;
let scrollPosition = 0;
let customIndicatorWidth = 0;

function ColorPalette( {
Expand Down Expand Up @@ -100,14 +98,23 @@ function ColorPalette( {
const customText = __( 'Custom' );

useEffect( () => {
const delayedScroll = setTimeout( () => {
resetScrollPosition();
}, SEGMENT_SWITCH_DELAY );
return () => {
clearTimeout( delayedScroll );
};
}, [ currentSegment ] );

function resetScrollPosition() {
if ( scrollViewRef.current ) {
if ( isSelectedCustom() ) {
scrollViewRef.current.scrollToEnd();
scrollViewRef.current.scrollToEnd( { animated: ! isIOS } );
} else {
scrollViewRef.current.scrollTo( { x: 0, y: 0 } );
}
}
}, [ currentSegment ] );
}

function isSelectedCustom() {
const isWithinColors =
Expand Down Expand Up @@ -153,53 +160,18 @@ function ColorPalette( {
outputRange: [ 1, 0.7, 1 ],
} );

function deselectCustomGradient() {
const { width } = Dimensions.get( 'window' );
const isVisible =
contentWidth - scrollPosition - customIndicatorWidth < width;

if ( isCustomGradientColor ) {
if ( ! isIOS ) {
// Scroll position on Android doesn't adjust automatically when removing the last item from the horizontal list.
// https://github.com/facebook/react-native/issues/27504
// Workaround: Force the scroll when deselecting custom gradient color and when custom indicator is visible on layout.
if (
isCustomGradientColor &&
isVisible &&
scrollViewRef.current
) {
scrollViewRef.current.scrollTo( {
x: scrollPosition - customIndicatorWidth,
} );
}
}
}
}

function onColorPress( color ) {
deselectCustomGradient();
performAnimation( color );
setColor( color );
}

function onContentSizeChange( width ) {
contentWidth = width;
if ( isSelectedCustom() && scrollViewRef.current ) {
scrollViewRef.current.scrollToEnd( { animated: ! isIOS } );
}
}

function onCustomIndicatorLayout( { nativeEvent } ) {
const { width } = nativeEvent.layout;
if ( width !== customIndicatorWidth ) {
customIndicatorWidth = width;
}
}

function onScroll( { nativeEvent } ) {
scrollPosition = nativeEvent.contentOffset.x;
}

const verticalSeparatorStyle = usePreferredColorSchemeStyle(
styles.verticalSeparator,
styles.verticalSeparatorDark
Expand All @@ -224,8 +196,7 @@ function ColorPalette( {
keyboardShouldPersistTaps="always"
disableScrollViewPanResponder
scrollEventThrottle={ 16 }
onScroll={ onScroll }
onContentSizeChange={ onContentSizeChange }
onContentSizeChange={ resetScrollPosition }
onScrollBeginDrag={ () => shouldEnableBottomSheetScroll( false ) }
onScrollEndDrag={ () => shouldEnableBottomSheetScroll( true ) }
ref={ scrollViewRef }
Expand Down

0 comments on commit d8156c1

Please sign in to comment.