GithubHelp home page GithubHelp logo

instea / react-native-color-picker Goto Github PK

View Code? Open in Web Editor NEW
271.0 7.0 67.0 732 KB

Color picker component for IOS/Android

License: Apache License 2.0

TypeScript 100.00%
color-picker react-native android ios

react-native-color-picker's Introduction

react-native-color-picker

React Native implementation of color picker for both Android and iOS.

android preview iphone preview

  • works both in controlled and uncontrolled way
  • old color can be displayed for visual comparison
  • holo and triangle color pickers

Getting started

Install the color picker

npm install react-native-color-picker --save

And use it in your application

import { ColorPicker } from 'react-native-color-picker'

const Picker = () => (
  <ColorPicker
    onColorSelected={color => alert(`Color selected: ${color}`)}
    style={{flex: 1}}
  />
)

Color picker will use space you provide. Therefore it is necessary to provide styles that will determine picker's size.

For HoloPicker (ColorPicker) you might need to install @react-native-community/slider and pass it (or any other Slider compatible component) as sliderComponent prop if you don't want to use deprecated RN Slider.

API

Color picker type

We provide two types of color picker - holo (default) and triangle color picker. Both has the same API so that they are interchangable. Just import it and use it the same way:

import { ColorPicker, TriangleColorPicker } from 'react-native-color-picker'
ColorPicker TriangleColorPicker

Props

Color pickers accepts properties below. Each property which define color is represented as a color string.

Both color pickers are PureComponents thus if you want to update it you should not mutate its properties deeply.

Property Type Note
color String|HSV Color string or HSV object (see below). Defines selected color in controlled component.
defaultColor String Defines initial selected color in uncontrolled component.
oldColor String Old color to be used for visual comparision. If it is not defined, whole circle is representing selected color.
style Style Styles passed to color picker container
onColorSelected Function Callback with color (HEX string) as argument called when user confirms color selection.
onColorChange Function Callback called each time when color is changed. Used in controlled component. Argument is color in HSV representation (see below)
onOldColorSelected Function Callback with color (HEX string) as argument called when user selects old color.
hideSliders Boolean Option to hide bottom sliders (holo picker only)
hideControls Boolean Option to hide bottom buttons (triangle picker only)

When using color picker as a controlled component you should always use HSV color representation to avoid conversion from/to HEX or RGB. HSV color representation is an object literal with properties:

{
  h: number, // <0, 360>
  s: number, // <0, 1>
  v: number, // <0, 1>
}

Helper functions

To utilize HSV -> HEX/RGB conversion we provide helper functions:

import { toHsv, fromHsv } from 'react-native-color-picker'

toHsv('blue') // { h: 24, s: 1, v: 1 }

fromHsv({ h: 200, s: 0.4, v:0.4 }) // #3d5866

Examples

See our examples on Expo

Limitations

  • Does not work well within ScrollView due to touch event interference.
  • RN has deprecated Slider component. You need to provide Slider component as prop to overcome this.
  • There is known bug affecting RN 0.61. See more info about the workaround.

Thanks

Our implementation was inspired by Android Holo ColorPicker

react-native-color-picker's People

Contributors

afreeland avatar artus9033 avatar ashoat avatar dastoori avatar jordenchang55 avatar madox2 avatar pr0da avatar rbravo avatar sodik82 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-native-color-picker's Issues

Add Black Color Option

I would like to request the addition of black as a color option in the React Native Color Picker library. Currently, the library does not include black as a selectable color, which is a common and important color choice in various applications.

-16777216}] is not usable

Hi,

Sometimes I hit this issue

Invariant Violation: [1289,"RCTView",{"top":"<>","left":"<>","backgroundColor":-16777216}] is not usable

Any clue?

TypeError (TS2740) sliderComponent

I just want to use the default slider from '@react-native-community/slider'

Bildschirmfoto 2021-02-22 um 17 56 39

adding this to the 'sliderComponent' prop results in an type error

Bildschirmfoto 2021-02-22 um 17 48 45

Slider is an intersection type of React.Component

Bildschirmfoto 2021-02-22 um 17 59 52

Fixed the issue for future release of React Native (removal of Slider extracted from react-native)

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.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-color-picker/src/HoloColorPicker.tsx b/node_modules/react-native-color-picker/src/HoloColorPicker.tsx
index 51a7bcc..6707587 100644
--- a/node_modules/react-native-color-picker/src/HoloColorPicker.tsx
+++ b/node_modules/react-native-color-picker/src/HoloColorPicker.tsx
@@ -4,13 +4,12 @@ import {
   Image,
   InteractionManager,
   PanResponderInstance,
-  Slider,
   StyleSheet,
   TouchableOpacity,
   View,
 } from "react-native"
 import tinycolor from "tinycolor2"
-
+import Slider from '@react-native-community/slider';
 import { HsvColor, IPickerProps, Point2D } from "./typeHelpers"
 import { createPanResponder } from "./utils"
 

This issue body was partially generated by patch-package.

Change Slider Colors

How would I, if possible, change the color of the sliders? I have imported the Slider component from react-native-community/slider and passed it in as a prop but don't know how to pass props like slider color to the slider this way.

<ColorPicker
       sliderComponent={Slider}
       onColorSelected={color => alert(`Color selected: ${color}`)}
       onColorChange={color => setNewPaletteColor(fromHsv(color))}
       style={{ flex: 1, height: 400, width: 300 }}
       defaultColor={Constants['collectionData']['fields']['palette']['mapValue']['fields'][colorToChange]['stringValue']}
/>

Feature Request: Show selected color string

It would be nice to have an option to show a textbox with the selected color string. Using the same textbox it would be good to allow editing the value and updating the color wheel based on that value.

I have tried to implement this myself by adding react-native-color-picker to a wrapper component but it seems binding the color property of the color picker to color={ this.state.mycolor} breaks the picker on IOS and stops the outer circle being able to be moved around to select a different color.

Customize colorPreview button/s in TriangleColorPicker

Hi there!

Could you please add ability (if it is possible) to customize these (colorPreview new and old) buttons.
As I see this โ€” it can be a style prop for example.
And maybe it is necessary to add some sorta placeholder to the button, because right now it is not clear what this colored square thing do.

I attached example of what I'd want to see
image

Thanks in advance!

Can't install properly

I tried to install using following command but while installing getting error and can not use the color picker.
command:- "npm i react-native-color-picker"
I'm using expo.
![Uploading Screenshot 2020-08-24 at 9.51.26 PM.pngโ€ฆ]
Screenshot 2020-08-24 at 9 52 46 PM

Setting "color" property breaks outer circle selection

Both the ColorPicker and TriangleColorPicker work perfectly with RN 0.63.2 when the "color" property is NOT used.

When this property is set, the outer circle no longer registers a tap on either component. The TriangleColorPicker does update the color when tapping within the triangle, but the marker does not move to the new position.

Any help would be greatly appreciated, cheers.

Triangle picker not working (triangle not rotating)

I tried this package on iOS and it didn't work properly,

Screenshot

you can see that:

  • the triangle didn't rotate
  • the dash has a weird rotation
  • the circle is out of bounds (bcz triangle didn't rotate)
  • and there's a warning about componentWillMount deprecation

Not Giving Colors Except Black Color

I am using react-native-color-picker and every time I click the it is giving me black color irrespective of where ever I click. I am not finding such issues anywhere. Following is my code

onColorChange(color) {
    console.log(color)
}

<ColorPicker
    oldColor='purple'
    color={this.state.color}
    onColorChange={this.onColorChange}
    onColorSelected={color => alert(`Color selected: ${color}`)}
    onOldColorSelected={color => alert(`Old color selected: ${color}`)}
    style={{height: 200, width: 200}}
    hideSliders={true}
/>

And the responses I received on clicking in different areas

{h: 53.023091807183505, s: 0, v: 0}

{h: 124.51081849529443, s: 0, v: 0}

{h: 205.9255659745072, s: 0, v: 0}

Any help is highly appreciable.

xcode 12 -Ios14 -colorpicker triangle single color inside triangle

xcode 12
expo init AwesomeProject

    "react-native-color-picker": "^0.6.0",
constructor(props) {
    super(props)
    this.state = { color: toHsv('red') }
    this.onColorChange = this.onColorChange.bind(this)
  }

  onColorChange(color) {
    console.log("color", color)
    this.setState({ color })
  }
 render(){
  return (
       <View style={styles.container}>
        <Text>Open up App.js to start working on your app!</Text>
        <StatusBar style="auto" />
        <View style={{flex: 1, padding: 45, backgroundColor: "#212021"}}>
          <Text style={{color: "white"}}>
            React Native Color Picker - Controlled
          </Text>
          <TriangleColorPicker
            oldColor="purple"
            color={this.state.color}
            onColorChange={this.onColorChange}
            onColorSelected={color => alert(`Color selected: ${color}`)}
            onOldColorSelected={color => alert(`Old color selected: ${color}`)}
            style={{flex: 1}}
          />
        </View>
      </View>

Screen Shot 2020-09-23 at 4 38 32 pm

Can't remove sliders and choose color with maximum opacity

I guess this is not an error but I would like to use the color with 1 of opacity but also hide the sliders. Currently, there is a default value for the color opacity, which is less than 1, and when the sliders are hidden there is no way of getting it back to 1.

TriangleColorPicker rotating with flickering on IOS

Hi there, I have some issue with TriangleColorPicker.
On IOS device, color trianlge rotating with flickering and color indicator (small black circle) don't move.
But it started working after changing screen orientation.
I use the last version of this lib - 0.52
RN - 0.59.9
React - 16.8.3

ios bug

How to label the sliders

I would like to label each of the sliders so that users can tell what each one controls.

How would I do this so that I can change the text that appears above each individual slider? Here is my code so far.

<ColorPicker
      sliderComponent={renderSliderComponent}
      onColorSelected={color => alert(`Color selected: ${color}`)}
      onColorChange={color => setNewPaletteColor(fromHsv(color))}
      style={{ flex: 1, height: 400, width: 300 }}
      defaultColor={selectedColor}
/>
const renderSliderComponent = () => {
    return (
      <View>
        <Text>Testing</Text>
        <Slider 
          minimumTrackTintColor={theme.colors.medium}
        />
      </View>
    )
  }

Color picker shrinks automatically on android every time keyboard comes up.

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2020-03-28 แ„‹แ…ฉแ„’แ…ฎ 1 38 01
แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2020-03-28 แ„‹แ…ฉแ„’แ…ฎ 1 37 31

First of all, I did not use and 'KeyboardAvoingView'. So on iOS, Color picker circle remains same even if the keyboard comes up. But on android, color circle shrinks to avoid the keyboard.
But I want the color circle to remain same at the origin place even if the keyboard comes up.

Any solutions for this?

Thank you very much!

sliderComponent

I have error when i try to add sliderComponent

My code :

<ColorPicker onColorSelected={color => alert(Color selected: ${color})} style={{ flex: 1 }} color={this.state.color} onColorChange={this.onColorChange} sliderComponent={<Slider style={{ width: 200, height: 40 }} minimumValue={0} maximumValue={10} minimumTrackTintColor="#FFFFFF" maximumTrackTintColor="#000000" />} />

I get this error :

element type is invalid expected a string (for built-in components) or a class/function but got objec.

According code of https://github.com/instea/react-native-color-picker/blob/master/src/HoloColorPicker.js ,
sliderComponent is of type 'PropTypes.elementType' where PropType is a npm library
https://www.npmjs.com/package/prop-types

So on prop-types library documentation is indicated that

// A React element type (ie. MyComponent). optionalElementType: PropTypes.elementType,
So i am doing wrong , please help

Handle touchRelease

Hey there,
I'm using the TriangleColorPicker and think, that it is somehow tricky as an unexperienced user to know, that I have to press the colored bar to select a color.

So I'm trying to detect when the user releases the finger from within the triangle and use that event as onColorSelected. I was able to insert that functionality (it seems, you alreade started that in utils.js) . But unfortunately I unable to distinguish between clicking on the circle and picking the color from inside the triangle.

Is there any line where you determine that? I mean at some point you need to know whether to rotate the triangle or not, right? My fork is https://github.com/paul-git/react-native-color-picker

Thanks and Greetings,
Paul

Outer circle missing with RN62

It shows a warning:
ReactNativeFiberHostComponent: Calling getNode() on the ref of an Animated component is no longer necessary. You can now directly use the ref instead.

And the outer circle is missing:
Screenshot_20200404-233757

IPicker type declarations problem

It seems like there are a few properties (onColorChange, onColorSelected, defaultColor, color) declared as required in IPicker, although even the example usage snippet shows they are optional and so does runtime. This triggers code assist to treat omitting them as an error.

Slider import from react native resolved

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.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-color-picker/dist/HoloColorPicker.d.ts b/node_modules/react-native-color-picker/dist/HoloColorPicker.d.ts
index e4a4a10..3df2476 100644
--- a/node_modules/react-native-color-picker/dist/HoloColorPicker.d.ts
+++ b/node_modules/react-native-color-picker/dist/HoloColorPicker.d.ts
@@ -1,5 +1,5 @@
 import React from "react";
-import { Slider } from "react-native";
+import Slider from '@react-native-community/slider';
 import { HsvColor, IPickerProps, Point2D } from "./typeHelpers";
 declare type SliderProps = {
     onValueChange?: (value: number) => void;
diff --git a/node_modules/react-native-color-picker/dist/HoloColorPicker.js b/node_modules/react-native-color-picker/dist/HoloColorPicker.js
index a177142..95280a3 100644
--- a/node_modules/react-native-color-picker/dist/HoloColorPicker.js
+++ b/node_modules/react-native-color-picker/dist/HoloColorPicker.js
@@ -12,7 +12,8 @@ var __extends = (this && this.__extends) || (function () {
     };
 })();
 import React from "react";
-import { I18nManager, Image, InteractionManager, Slider, StyleSheet, TouchableOpacity, View, } from "react-native";
+import { I18nManager, Image, InteractionManager, StyleSheet, TouchableOpacity, View, } from "react-native";
+import Slider from '@react-native-community/slider';
 import tinycolor from "tinycolor2";
 import { createPanResponder } from "./utils";
 var HoloColorPicker = /** @class */ (function (_super) {
diff --git a/node_modules/react-native-color-picker/src/HoloColorPicker.tsx b/node_modules/react-native-color-picker/src/HoloColorPicker.tsx
index 51a7bcc..6707587 100644
--- a/node_modules/react-native-color-picker/src/HoloColorPicker.tsx
+++ b/node_modules/react-native-color-picker/src/HoloColorPicker.tsx
@@ -4,13 +4,12 @@ import {
   Image,
   InteractionManager,
   PanResponderInstance,
-  Slider,
   StyleSheet,
   TouchableOpacity,
   View,
 } from "react-native"
 import tinycolor from "tinycolor2"
-
+import Slider from '@react-native-community/slider';
 import { HsvColor, IPickerProps, Point2D } from "./typeHelpers"
 import { createPanResponder } from "./utils"
 

This issue body was partially generated by patch-package.

Center color circle loose circle shape when defining 'oldColor' params

Hello here,

First of all thanks for your work on this one !

I have an issue: when I set a value for oldColor params the center circle (with old/current color) switch from a perfect circle to something strange between square and circle I would say.

Any idea of the origin of that ? Thanks a lot for infos

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.