|
1 | 1 | ## Radial Color Picker - React
|
| 2 | + |
| 3 | +<p align="center"><img width="250" src="https://raw.githubusercontent.com/radial-color-picker/react-color-picker/HEAD/screenshots/thumbnail.png" alt="screenshot"></p> |
| 4 | + |
| 5 | +<p align="center"> |
| 6 | + <a href="https://www.npmjs.com/package/@radial-color-picker/react-color-picker"> |
| 7 | + <img src="https://img.shields.io/npm/dt/@radial-color-picker/react-color-picker.svg" alt="Downloads"> |
| 8 | + </a> |
| 9 | + <a href="https://www.npmjs.com/package/@radial-color-picker/react-color-picker"> |
| 10 | + <img src="https://img.shields.io/npm/v/@radial-color-picker/react-color-picker.svg" alt="Version"> |
| 11 | + </a> |
| 12 | + <a href="https://www.npmjs.com/package/@radial-color-picker/react-color-picker"> |
| 13 | + <img src="https://img.shields.io/npm/l/@radial-color-picker/react-color-picker.svg" alt="License"> |
| 14 | + </a> |
| 15 | +</p> |
| 16 | + |
| 17 | +## Introduction |
| 18 | + |
| 19 | +Great UX starts with two basic principles - ease of use and simplicity. Selecting a color should be as easy as moving a slider, clicking a checkbox or pressing a key just like other basic form elements behave. |
| 20 | + |
| 21 | +This is a flexible and minimalistic color picker. Developed with mobile devices and keyboard usage in mind. Key features: |
| 22 | +* Small size - 4.1 KB gzipped (JS and CSS combined) |
| 23 | +* Supports touch devices |
| 24 | +* Optimized animations |
| 25 | +* Ease of use |
| 26 | + * Double click anywhere to move the knob to a color |
| 27 | + * <kbd>Tab</kbd> to focus the picker |
| 28 | + * <kbd>↑</kbd> or <kbd>→</kbd> arrow key to increase hue. <kbd>Shift + ↑/→</kbd> to go quicker and <kbd>Ctrl + ↑/→</kbd> to go even quicker. |
| 29 | + * <kbd>↓</kbd> or <kbd>←</kbd> arrow key to decrease hue. <kbd>Shift + ↓/←</kbd> to go quicker and <kbd>Ctrl + ↓/←</kbd> to go even quicker. |
| 30 | + * <kbd>Enter</kbd> to select a color and close the picker or to open it |
| 31 | + * Mouse <kbd>ScrollUp</kbd> to increase and <kbd>ScrollDown</kbd> to decrease hue (Opt-in) |
| 32 | + |
| 33 | +## Quick Links |
| 34 | + |
| 35 | +* [Demos](#demos) |
| 36 | +* [Usage](#usage) |
| 37 | +* [Options](#options) |
| 38 | +* [FAQ](#first-asked-questions) |
| 39 | +* [Change log](#change-log) |
| 40 | +* [Contributing](#contributing) |
| 41 | +* [Credits](#credits) |
| 42 | +* [License](#license) |
| 43 | + |
| 44 | +## Demos |
| 45 | + |
| 46 | +* Basic Example - [Codepen](https://codepen.io/rkunev/pen/mjKoyK/) |
| 47 | + |
| 48 | +## Usage |
| 49 | + |
| 50 | +#### With Module Build System |
| 51 | +Color Picker on [npm](https://www.npmjs.com/package/@radial-color-picker/react-color-picker) |
| 52 | +```bash |
| 53 | +npm install @radial-color-picker/react-color-picker |
| 54 | +``` |
| 55 | + |
| 56 | +And in your app: |
| 57 | + |
| 58 | +```jsx |
| 59 | +import ColorPicker from '@radial-color-picker/react-color-picker'; |
| 60 | +import '@radial-color-picker/react-color-picker/umd/react-color-picker.min.css'; |
| 61 | + |
| 62 | +class App extends React.Component { |
| 63 | + state = { |
| 64 | + hue: 90, |
| 65 | + saturation: 100, |
| 66 | + luminosity: 50, |
| 67 | + alpha: 1, |
| 68 | + }; |
| 69 | + |
| 70 | + onChange = ({ hue, saturation, luminosity, alpha }) => { |
| 71 | + this.setState({ hue, saturation, luminosity, alpha }); |
| 72 | + }; |
| 73 | + |
| 74 | + render() { |
| 75 | + return <ColorPicker {...this.state} onChange={this.onChange} />; |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +Depending on your build tool of choice (webpack, parcel, rollup) you may have to setup the appropriate loaders or plugins. If you're using `create-react-app` you don't have to do anything else - it comes preconfigured and supports CSS/SCSS import out of the box. |
| 81 | + |
| 82 | +[Back To Top](#quick-links) |
| 83 | + |
| 84 | +## Options |
| 85 | +`ColorPicker` is a controlled component. It's current state is defined and by the props you pass to it. |
| 86 | + |
| 87 | +### Props |
| 88 | + |
| 89 | +| Options | Type | Default/Description | |
| 90 | +|---------------|---------|---------------------| |
| 91 | +| `hue` | Number | Defaults to 0 (red color) | |
| 92 | +| `saturation` | Number | Defaults to 100 | |
| 93 | +| `luminosity` | Number | Defaults to 50 | |
| 94 | +| `alpha` | Number | Defaults to 1 | |
| 95 | +| `mouseScroll` | Boolean | Use wheel (scroll) event to rotate. Defaults to false. | |
| 96 | +| `step` | Number | Amount of degrees to rotate the picker with keyboard and/or wheel. <br> Defaults to 2 degrees. | |
| 97 | +| `onSelect` | Function | Callback which is triggered when a color is selected. | |
| 98 | +| `onChange` | Function | A function to invoke when color is changed (i.e. on rotation). | |
| 99 | + |
| 100 | +[Back To Top](#quick-links) |
| 101 | + |
| 102 | +## First Asked Questions |
| 103 | + |
| 104 | +<details> |
| 105 | + <summary>How to select other shades of the solid colors?</summary> |
| 106 | + <p>We suggest to add a custom slider for saturation and luminosity or use <code><input type="range"></code>.</p> |
| 107 | +</details> |
| 108 | + |
| 109 | +<details> |
| 110 | + <summary>Why does Google Chrome throw a <code>[Violation] Added non-passive event listener to a scroll-blocking 'touchmove' event.</code> warning in the console?</summary> |
| 111 | + <p><code>touchmove</code> is used with <code>preventDefault()</code> to block scrolling on mobile while rotating the color knob. Even the <a href="https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md#removing-the-need-to-cancel-events">Web Incubator Community Group</a> acknowledges that in some cases a passive event listener can't be used.</p> |
| 112 | +</details> |
| 113 | + |
| 114 | +<details> |
| 115 | + <summary>Why is the scroll-to-rotate functionality not turned on by default?</summary> |
| 116 | + <p>It's another non-passive event that could potentially introduce jank on scroll. To rotate the color knob, but stay on the same scrolling position the <code>wheel</code> event is blocked with <code>preventDefault()</code>. Thus, if you really want this feature for your users you'll have to explicitly add <code>:mouse-scroll="true"</code>.</p> |
| 117 | +</details> |
| 118 | +<br> |
| 119 | + |
| 120 | +[Back To Top](#quick-links) |
| 121 | + |
| 122 | + |
| 123 | +## Change log |
| 124 | + |
| 125 | +Please see [Releases][link-releases] for more information on what has changed recently. |
| 126 | + |
| 127 | +[Back To Top](#quick-links) |
| 128 | + |
| 129 | +## Contributing |
| 130 | + |
| 131 | +If you're interested in the project you can help out with feature requests, bugfixes, documentation improvements or any other helpful contributions. You can use the issue list of this repo for bug reports and feature requests and as well as for questions and support. |
| 132 | + |
| 133 | +Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE_OF_CONDUCT](CODE_OF_CONDUCT.md) for details. |
| 134 | + |
| 135 | +[Back To Top](#quick-links) |
| 136 | + |
| 137 | +## Credits |
| 138 | + |
| 139 | +- [Rosen Kanev][link-author] |
| 140 | +- [All Contributors][link-contributors] |
| 141 | + |
| 142 | +This component is based on the great work that was done for the AngularJs color picker [angular-radial-color-picker][link-angular-radial-color-picker]. |
| 143 | + |
| 144 | +[Back To Top](#quick-links) |
| 145 | + |
| 146 | +## License |
| 147 | + |
| 148 | +The MIT License (MIT). Please see [License File](LICENSE) for more information. |
| 149 | + |
| 150 | +[link-angular-radial-color-picker]: https://github.com/talamaska/angular-radial-color-picker |
| 151 | +[link-author]: https://github.com/rkunev |
| 152 | +[link-contributors]: ../../contributors |
| 153 | +[link-releases]: ../../releases |
| 154 | + |
| 155 | +[Back To Top](#quick-links) |
0 commit comments