UNPKG

d2-ui

Version:
81 lines (71 loc) 1.75 kB
``` 'use strict' import React from 'react' import ReactCSS from 'reactcss' import { SketchPicker } from 'react-color' class SketchExample extends ReactCSS.Component { state = { displayColorPicker: false, color: { r: '241', g: '112', b: '19', a: '1', }, }; classes() { return { 'default': { color: { width: '36px', height: '14px', borderRadius: '2px', background: `rgba(${ this.state.color.r }, ${ this.state.color.g }, ${ this.state.color.b }, ${ this.state.color.a })`, }, swatch: { padding: '5px', background: '#fff', borderRadius: '1px', boxShadow: '0 0 0 1px rgba(0,0,0,.1)', display: 'inline-block', cursor: 'pointer', }, popover: { position: 'absolute', zIndex: '2', }, cover: { position: 'fixed', top: '0', right: '0', bottom: '0', left: '0', }, }, } } handleClick = () => { this.setState({ displayColorPicker: !this.state.displayColorPicker }) }; handleClose = () => { this.setState({ displayColorPicker: false }) }; handleChange = (color) => { this.setState({ color: color.rgb }) }; render() { return ( <div> <div is="swatch" onClick={ this.handleClick }> <div is="color" /> </div> { this.state.displayColorPicker ? <div is="popover"> <div is="cover" onClick={ this.handleClose }/> <SketchPicker color={ this.state.color } onChange={ this.handleChange } /> </div> : null } </div> ) } } export default SketchExample ```