Skip to content

With RNGH ScrollView

Dragging a slider inside a scroll view usually fights the scroll. The fix is to import ScrollView from react-native-gesture-handler rather than react-native — its gestures negotiate with the picker's instead of racing them.

Code

WithRnghScrollView.tsx
import React, { useState } from 'react';
import { Text, View } from 'react-native';
import { GestureHandlerRootView, ScrollView } from 'react-native-gesture-handler';
import type { ColorFormatsObject } from 'reanimated-color-picker';
import ColorPicker, { HSLSaturationSlider, HueSlider, LuminanceSlider, OpacitySlider } from 'reanimated-color-picker';
import { useContainerBackgroundColor } from './components/BaseContainer';
import { colorPickerStyle } from './components/colorPickerStyle';
/*
* Using react-native-gesture-handler ScrollView to prevent scrolling while using the color picker
*/
export default function Example() {
const [resultColor, setResultColor] = useState('#f00');
const currentColor = useContainerBackgroundColor('#f00');
// runs on the ui thread on color change
const onColorChange = (color: ColorFormatsObject) => {
'worklet';
currentColor.value = color.hex;
};
// runs on the js thread on color pick
const onColorPick = (color: ColorFormatsObject) => {
setResultColor(color.hex);
};
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<ScrollView contentContainerStyle={{ height: '150%', justifyContent: 'center' }}>
<Text style={colorPickerStyle.title}>Color Picker in a RNGH ScrollView</Text>
<View style={colorPickerStyle.pickerContainer}>
<ColorPicker
value={resultColor}
sliderThickness={30}
thumbSize={30}
onChange={onColorChange}
onCompleteJS={onColorPick}
style={colorPickerStyle.picker}
boundedThumb
>
<View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
<View style={{ alignItems: 'center' }}>
<Text style={colorPickerStyle.sliderTitle}>H</Text>
<View style={colorPickerStyle.sliderBorderContainer}>
<HueSlider style={colorPickerStyle.sliderVerticalStyle} vertical reverse />
</View>
</View>
<View style={{ alignItems: 'center' }}>
<Text style={colorPickerStyle.sliderTitle}>S</Text>
<View style={colorPickerStyle.sliderBorderContainer}>
<HSLSaturationSlider style={colorPickerStyle.sliderVerticalStyle} vertical reverse />
</View>
</View>
<View style={{ alignItems: 'center' }}>
<Text style={colorPickerStyle.sliderTitle}>L</Text>
<View style={colorPickerStyle.sliderBorderContainer}>
<LuminanceSlider style={colorPickerStyle.sliderVerticalStyle} vertical reverse />
</View>
</View>
<View style={{ alignItems: 'center' }}>
<Text style={colorPickerStyle.sliderTitle}>A</Text>
<View style={colorPickerStyle.sliderBorderContainer}>
<OpacitySlider style={colorPickerStyle.sliderVerticalStyle} vertical reverse />
</View>
</View>
</View>
</ColorPicker>
</View>
</ScrollView>
</GestureHandlerRootView>
);
}

The colorPickerStyle object is shared by every example. Divider is a 1px line, and useContainerBackgroundColor only tints the modal of the example app.

components/colorPickerStyle.ts
import { StyleSheet } from 'react-native';
const border = {
borderWidth: StyleSheet.hairlineWidth,
borderColor: 'rgba(0, 0, 0, 0.15)',
};
export const colorPickerStyle = StyleSheet.create({
title: {
textAlign: 'center',
fontFamily: 'Quicksand',
fontWeight: 'bold',
marginVertical: 20,
color: 'gray',
},
picker: {
gap: 20,
},
pickerContainer: {
alignSelf: 'center',
width: 300,
backgroundColor: '#eae5f1',
padding: 20,
borderRadius: 20,
...border,
},
panelStyle: {
borderRadius: 16,
},
sliderStyle: {
borderRadius: 20,
},
sliderVerticalStyle: {
borderRadius: 20,
height: 300,
},
sliderBorderContainer: {
borderRadius: 20 + StyleSheet.hairlineWidth,
...border,
},
panelBorderContainer: {
borderRadius: 16 + StyleSheet.hairlineWidth,
...border,
},
gridPanelBorderContainer: {
borderRadius: 4 + StyleSheet.hairlineWidth,
...border,
},
wheelPanelBorderContainer: {
borderRadius: 2000,
...border,
},
sliderTitle: {
color: '#000',
fontWeight: 'bold',
marginBottom: 5,
paddingHorizontal: 4,
fontFamily: 'Quicksand',
},
previewStyle: {
height: 40,
borderRadius: 14,
},
previewTxt: {
color: '#707070',
fontFamily: 'Quicksand',
},
inputStyle: {
color: '#707070',
paddingVertical: 2,
borderColor: '#707070',
fontSize: 12,
marginLeft: 5,
},
swatchesContainer: {
alignItems: 'center',
flexWrap: 'nowrap',
gap: 10,
},
swatchStyle: {
borderRadius: 20,
height: 30,
width: 30,
margin: 0,
marginBottom: 0,
marginHorizontal: 0,
marginVertical: 0,
},
});