Giter VIP home page Giter VIP logo

react-native-wheel-color-picker's Introduction

React Native Wheel Color Picker

A color picker component for react native.

Features

  • Pure JS, lightweight, works on Android, iOS and Web
  • Uses hue-saturation color wheel and lightness slider
  • Selectable from color swatchs
  • Smooth and discrete color slider
  • Color change animations on wheel, slider and swatches

Demo Image

(This demo uses my react-native-advanced-ripple and react-native-elevation modules.)

Installation

npm install react-native-wheel-color-picker

Usage

import { Component } from 'react'
import { View, Text, ActivityIndicator } from 'react-native'

import ColorPicker from 'react-native-wheel-color-picker'

class App extends Component {
    render() {
        return (
            <View style={[]}>
                <ColorPicker
                    ref={r => { this.picker = r }}
                    color={this.state.currentColor}
                    swatchesOnly={this.state.swatchesOnly}
                    onColorChange={this.onColorChange}
                    onColorChangeComplete={this.onColorChangeComplete}
                    thumbSize={40}
                    sliderSize={40}
                    noSnap={true}
                    row={false}
                    swatchesLast={this.state.swatchesLast}
                    swatches={this.state.swatchesEnabled}
                    discrete={this.state.disc}
                    wheelLodingIndicator={<ActivityIndicator size={40} />}
                    sliderLodingIndicator={<ActivityIndicator size={20} />}
                    useNativeDriver={false}
                    useNativeLayout={false}
                />
                <SomeButton onPress={() => this.picker.revert()} />
            </View>
        )
    }
}

export default App

Changelog

1.3.1

  • fixed a bug related to useNativeDriver
  • no breaking changes

1.3.0

  • added changelog section to README.md
  • prop added: wheelLoadingIndicator
  • prop added: sliderLoadingIndicator
  • prop added: useNativeDriver
  • prop added: useNativeLayout
  • prop added: disabled
  • prop added: flipTouchX
  • prop added: flipTouchY
  • prop added: wheelHidden
  • fixed a bug related to discreteLength prop
  • no breaking changes

1.2.0

  • prop added: gapSize
  • prop added: discreteLength
  • prop added: swatchesHitSlop
  • prop added: palette
  • prop added: onInteractionStart
  • no breaking changes

1.1.0

  • prop added: shadeWheelThumb
  • prop added: shadeSliderThumb
  • prop added: autoResetSlider
  • no breaking changes

API

ColorPicker

Component props and default values

row: false use row or vertical layout

noSnap: false enables snapping on the center of wheel and edges of wheel and slider

thumbSize: 50 wheel color thumb size

sliderSize: 20 slider and slider color thumb size

gapSize: 16 size of gap between slider and wheel

discrete: false use swatches of shades instead of slider

discreteLength: 10 number of swatches of shades, should be > 1

sliderHidden: false if true the slider is hidden

swatches: true show color swatches

swatchesLast: true if false swatches are shown before wheel

swatchesOnly: false show swatch only and hide wheel and slider

swatchesHitSlop: undefined defines how far the touch event can start away from the swatch

color: '#ffffff' color of the color picker

palette: ['#000000','#888888','#ed1c24','#d11cd5','#1633e6','#00aeef','#00c85d','#57ff0a','#ffde17','#f26522'] palette colors of swatches

shadeWheelThumb: true if true the wheel thumb color is shaded

shadeSliderThumb: false if true the slider thumb color is shaded

autoResetSlider: false if true the slider thumb is reset to 0 value when wheel thumb is moved

onInteractionStart: () => {} callback function triggered when user begins dragging slider/wheel

onColorChange: (color) => {} callback function providing current color while user is actively dragging slider/wheel

onColorChangeComplete: (color) => {} callback function providing final color when user stops dragging slider/wheel

wheelLoadingIndicator: null wheel image loading component eg:

sliderLoadingIndicator: null slider image loading component eg:

useNativeDriver: false to use useNativeDriver for animations if possible

useNativeLayout: false to use onLayoutEvent.nativeEvent.layout instead of measureInWindow for x, y, width, height values for wheel and slider measurements which may be useful to prevent some layout problems

disabled: false disable all interactions

flipTouchX: false flip touch positioning on X axis, might be useful in UI with RTL support

flipTouchY: false flip touch positioning on Y axis, might be useful in UI with RTL support

wheelHidden: false if true the wheel is hidden, does not work with sliderHidden = true

Instance methods

revert() reverts the color to the one provided in the color prop

License

The MIT License (MIT)

Copyright (c) 2020-2024 Md. Naeemur Rahman (https://naeemur.github.io)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

react-native-wheel-color-picker's People

Contributors

brkastner avatar linusu avatar naeemur avatar rskogeby avatar sungsong88 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-native-wheel-color-picker's Issues

Dragging in web on mobile

I am trying to use the library with expo web which is working great so far.

But there is one issue when used in mobile, which is that the dragging of the window is interfering with the dragging of the color-dragger.

This is not working on iOS nor Android. Is there any way to fix this easily?

Issue reproduced on mobile:
https://snack.expo.io/@hit-em/excited-salsa

RPReplay_Final1616260328.mp4

Changing color state manually

My setup is basically something like this:

`

    const someEvent = () => {
          setCurrentColor('#cccccc');
    }
    
    const [currentColor, setCurrentColor} = useState('#ffffff');
    
   <ColorPicker
          color={currentColor}
          onColorChange={setCurrentColor}
    />

`

When someEvent function is triggered, currentColor is not affecting <ColorPicker/> component.

Would appreciate any help with this

picker view outside of the circle

screenshot-iPhone 14 Pro Max-16 0

       <ColorPicker
          style={styles.colorPickerStyle}
            color={ThemeObj.bgColor}
            onColorChangeComplete={color => {
              //console.log('onColorChangeComplete===>', color);
              selectedPickerColor = color;
            }}
            thumbSize={30}
            sliderSize={30}
            noSnap={false}
            swatches={false}
            row={false}
            gapSize={1000}
          />

how we can solve this issue?

Palette colors doesnt update

I am using react-native-wheel-color-picker to select a color, and then generate a color scheme from that color. I thought it would be cool if I used the palette prop to show the generated colors but I found that the colors of the palette prop doesnt update after the first render.
(Full code here)

import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
import ColorPicker from 'react-native-wheel-color-picker';
import {genColorScheme} from './helpers/colorGenerator'

export default function App() {
  const [ colors, setColors ] = React.useState(genColorScheme());
  // unmounting and remounting color picker shows correct palette
  const [showPicker , setShowPicker] = React.useState(true);
  const rerenderColorPicker = ()=>{
    setShowPicker(false);
    setTimeout(()=>{
      setShowPicker(true)
    },0)
  }
  return (
    <View style={styles.container}>
      <Button
        title="Force Picker Rerender"
        onPress={rerenderColorPicker}
      />
      <View style={styles.pickerContainer}>
        <Text>Actual Palette:</Text>
        <View style={styles.row}>
          {colors.map(color=>
              <View style={[styles.circle,{backgroundColor:color}]}/>
          )}
        </View>
        {showPicker && <ColorPicker
            style={styles.picker}
            color={colors[0]}
            onColorChange={color=>{
              setColors(genColorScheme(color))
            }}
            thumbSize={24}
            palette={colors}
          />
        }
      </View>
    </View>
  );
}

Further inspection into the source code revealed that palette is generated by this.renderSwatches. renderSwatches is called in the only called in the constructor. And because the constructor only runs once, the swatches doesnt update when palette changes

Support for the experimental syntax 'jsx' isn't currently enabled

I am using this module on react-native-web:

Failed to compile.

./node_modules/react-native-wheel-color-picker/ColorPicker.js
SyntaxError: /Users/kasra/Documents/realnote_app/react/node_modules/react-native-wheel-color-picker/ColorPicker.js: Support for the experimental syntax 'jsx' isn't currently enabled (499:4):

  497 |         renderSwatches () {
  498 |                 this.swatches = PALLETE.map((c,i) => (
> 499 |                         <View style={[ss.swatch,{backgroundColor:c}]} key={'S'+i}>
      |                         ^
  500 |                                 <TouchableWithoutFeedback onPress={x=>this.onSwatchPress(c,i)}>
  501 |                                         <Animated.View style={[ss.swatchTouch,{backgroundColor:c,transform:[{scale:this.swatchAnim[i].interpolate({inputRange:[0,0.5,1],outputRange:[0.666,1,0.666]})}]}]} />
  502 |                                 </TouchableWithoutFeedback>

Add @babel/preset-react (https://git.io/JfeDR) to the 'presets' section of your Babel config to enable transformation.
If you want to leave it as-is, add @babel/plugin-syntax-jsx (https://git.io/vb4yA) to the 'plugins' section to enable parsing.
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-native-wheel-color-picker": "^1.1.0",

Typescript errors and patch-package fixes

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch [email protected] for the project I'm working on.

Currently, all props on the ColorPicker component needs to be specified even though in reality it doesn't need to as you have defined static default props in the component itself. onColorChange and onColorChangeComplete also had wrong types. This patch fixes both of the issues that I mentioned, feel free to incorporate them if you agree with the changes.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-wheel-color-picker/types.d.ts b/node_modules/react-native-wheel-color-picker/types.d.ts
index 365aab5..6fec768 100644
--- a/node_modules/react-native-wheel-color-picker/types.d.ts
+++ b/node_modules/react-native-wheel-color-picker/types.d.ts
@@ -1,20 +1,20 @@
 import * as React from 'react';
 
 export interface ColorPickerProps extends React.Props<ColorPicker> {
-  row: boolean,
-  noSnap: boolean,
-  thumbSize: number,
-  sliderSize: number,
-  discrete: boolean,
-  swatches: boolean,
-  swatchesLast: boolean,
-  swatchesOnly: boolean,
-  color: string,
-  shadeWheelThumb: boolean,
-  shadeSliderThumb: boolean,
-  autoResetSlider: boolean,
-  onColorChange(): void,
-  onColorChangeComplete(): void,
+  row?: boolean,
+  noSnap?: boolean,
+  thumbSize?: number,
+  sliderSize?: number,
+  discrete?: boolean,
+  swatches?: boolean,
+  swatchesLast?: boolean,
+  swatchesOnly?: boolean,
+  color?: string,
+  shadeWheelThumb?: boolean,
+  shadeSliderThumb?: boolean,
+  autoResetSlider?: boolean,
+  onColorChange?(color: string): void,
+  onColorChangeComplete?(color: string): void,
 }
 
 declare class ColorPicker extends React.Component<ColorPickerProps, any> {

This issue body was partially generated by patch-package.

When user changes color onColorChange fires multiple times

When user changes color on the picker onColorChange fires multiple times, probably the "state" being applied again.

I think when the state sets color={currentColor}, there shouldn't be an event fired.

    const someEvent = () => {
          setCurrentColor('#cccccc');
    }
    
    const [currentColor, setCurrentColor} = useState('#ffffff');
    
   <ColorPicker
          color={currentColor}
          onColorChange={setCurrentColor}
    />

Possibly related #5

Warning related to rendering

image

This is caused by the render() method in ColorPicker.js.

image

This is line seems useless and this is the one that actually generates the warning. Any solutions?

Thumb bug on iOS devices

Not sure why but on my physical device the thumb for the color wheel & slider refuse to move. Unable to recreate the bug in the simulator on on device but basically the thumb is stuck on the upper left corner of the wheel and for the slider it's just stuck on the left.

Can I disable the input?

I'd like to be able to call

<ColorPicker ... disabled={true} />

which would then show the color picker with its current input but without the ability to select a new color.

Is it possible with the current API?

Add ActivityIndicator while image is loading

Would it be possible for you to add an ActivityIndicator while the image is loading. Maybe a configurable property for that?
I'm using this for a project with a very constrained bandwidth. The user therefore just sees an empty field at the beginning which is a bit annoying.

Animated: `useNativeDriver` was not specified

Hello! I'm getting a warning when adding this to my react native project:

Animated: useNativeDriverwas not specified. This is a required option and must be explicitly set totrueorfalse``

Here is the code I use. I added useNativeDriver at the end to try to fix the issue, but true/false still gives the same error.

<ColorPicker
      color={'#ffffff'}
      // onColorChange={(color) => setColor(color)}
      // onColorChangeComplete={(color) => setColor(color)}
      thumbSize={40}
      sliderSize={20}
      noSnap={true}
      row={false}
      swatchesLast={false}
      swatches={false}
      discrete={false}
      useNativeDriver={true}
/>

Would appreciate any help with this! Thank you!

Add support for alpha/transparency

Hello,

As far as I can see there is no support for alpha/transparency?
The following snippet will crash and there is no way to control transparency of color?

<ColorPicker
    shadeWheelThumb
    sliderSize={16}
    thumbSize={20}
    swatches={false}
    color={value} // if values is: "transparent" or "rgba(0, 0, 0, 0)" or "#00000000" ColorPicker crashes.

onColorChangeComplete called thrice when dragging stops

Hello there,
when using onColorChangeComplete the call back function is executed thrice.
<ColorPicker color={this.state.rgbObject.rgbHex} style={{paddingBottom: 40, marginTop: 20}} onColorChangeComplete={this.onChangeColor} thumbSize={30} sliderSize={30} gapSize={10} swatchesLast={false} row={false} />

where
onChangeColor = (color) => { console.log('onChangeColor Called', color) }

Changing component props and default values not working

I can't seem to update the discreteLength and palettes props to a specific or desired value.

Version of react-native-wheel-color-picker
^1.1.0

Version of react-native
0.65.1

Platforms you faced the error (IOS or Android or both?)
IOS and Android

Expected behavior
Replacing discreteLength: 10 to 6 would display only 6 shades from swatch along with replacing color palettes to desired colors.

Actual behavior
Does nothing basically.

Tested environment (Emulator? Real Device?)
Emulator

Steps to reproduce the behavior
Replace the props with desired values.
<ColorPicker
color={color}
onColorChangeComplete={(color) => setColor(color)}
swatchesLast={false}
sliderSize={30}
thumbSize={30}
discrete={true}
discreteLength={6}
palette={PALETTE}
/>

const PALETTE = ['#ed1c24', '#d11cd5', '#1633e6', '#00c85d', '#ffde17'];

Typescript declaration file

Could not find a declaration file for module 'react-native-wheel-color-picker'.

May it be possible that a type declaration is added to this package?

Support RTL

video_2022-04-13_13-36-01.mp4

please fix this problem (in the video) in the RTL application.
thanks.

revert() api throwing "Uncaught Error: null is not an object (evaluating 'rgb.r')"

Calling "revert()" API sometimes throws Uncaught Error: null is not an object (evaluating 'rgb.r') from hex2Hsv method.

Version of react-native-wheel-color-picker
^1.2.0

Version of react-native
0.66.3

Platforms you faced the error (IOS or Android or both?)
iOS

Expected behavior
revert() should not throw

Actual behavior
It is random occurance. Sometimes it throws above error, other times it works as expected.

Tested environment (Emulator? Real Device?)
Emulator

Steps to reproduce the behavior

const colorPickerRef = useRef();
 const [color, setColor] = React.useState(props.color || "#FFF");

return (<View style={styles.wrapper}>
<ColorPicker
          ref={(ref) => (colorPickerRef.current = ref)}
          color={props.color || "#000000"}
          swatchesOnly={false}
          onColorChangeComplete={setColor}
          thumbSize={40}
          sliderHidden
          sliderSize={10}
          noSnap={true}
          row={false}
          swatchesLast={true}
          swatches={false}
          discrete={false}
        />

<Button text="Cancel" onPress={() => colorPickerRef.current?.revert()} />
</View>)

color-picker-error

Return wrong hex color code

onColorChangeComplete and onColorChange method return wrong color code when color selected and set in TextInput . Even if select same color it's value change .

Change Swatch Size

I've been reading the docs but couldn't find any solutions so increase/decrease the swatches size, but I found in the source code that this is made with just a simple style.

By now i'm just a begginer in react native and cant make a fork to code a solution for that, could you please make a parameter for that?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.