11import React , { Component } from 'react' ;
2+ import { createPortal } from 'react-dom' ;
23import { environment } from '#store/environment' ;
34import { observer } from 'mobx-react' ;
45import { SortableContainer , SortableElement , SortableHandle } from 'react-sortable-hoc' ;
56import { hexToMDHex } from '#formats/palette' ;
67import ColorPicker from 'rc-color-picker' ;
8+ import { SketchPicker } from 'react-color' ;
79
810const Handle = SortableHandle ( ( { lineIndex} ) => < div className = "index" >
911 { lineIndex }
1012</ div > ) ;
1113
12- const SortableItem = SortableElement ( observer ( ( { line, lineIndex} ) => (
13- < div className = "line" >
14- < Handle lineIndex = { lineIndex } />
15- { line . map ( ( color , colorIndex ) => {
16- return < div
17- key = { colorIndex }
18- className = "color"
19- style = { {
20- backgroundColor : color ,
21- textAlign : 'center' ,
22- } }
23- >
24- < ColorPicker
25- animation = "slide-up"
26- color = { color }
27- enableAlpha = { false }
28- mode = "RGB"
29- onChange = { ( { color} ) => {
30- environment . palettes [ lineIndex ] [ colorIndex ] = hexToMDHex ( color ) ;
31- } }
32- >
33- < div className = "picker" />
34- </ ColorPicker >
35- </ div > ;
36- } ) }
37- </ div >
38- ) ) ) ;
14+ const container = document . body . appendChild ( document . createElement ( 'div' ) ) ;
15+ container . className = 'palette-color-picker' ;
3916
40- const SortableList = SortableContainer ( observer ( ( { items , vert } ) => {
17+ const SortableItem = SortableElement ( observer ( ( { line , lineIndex , onClick } ) => {
4118 return (
42- < div className = { `palettes ${ vert ?'vert' :'' } ` } >
43- { items . map ( ( line , index ) => (
44- < SortableItem
45- key = { `item-${ index } ` }
46- index = { index }
47- line = { line }
48- lineIndex = { index }
49- />
50- ) ) }
19+ < div className = "line" >
20+ < Handle lineIndex = { lineIndex } />
21+ { line . map ( ( color , colorIndex ) => {
22+ return < div
23+ key = { colorIndex }
24+ className = "color"
25+ style = { {
26+ backgroundColor : color ,
27+ textAlign : 'center' ,
28+ } }
29+ onClick = { ( e ) => onClick ( lineIndex , colorIndex , e ) }
30+ >
31+ < ColorPicker
32+ animation = "slide-up"
33+ color = { color }
34+ enableAlpha = { false }
35+ mode = "RGB"
36+ onChange = { ( { color} ) => {
37+ environment . palettes [ lineIndex ] [ colorIndex ] = hexToMDHex ( color ) ;
38+ } }
39+ >
40+ < div className = "picker" />
41+ </ ColorPicker >
42+ </ div > ;
43+ } ) }
5144 </ div >
5245 ) ;
53- } ) , { withRef : true } ) ;
46+ } ) ) ;
47+
48+ const SortableList = SortableContainer (
49+ observer ( class extends Component {
50+ render ( ) {
51+ const { items, vert } = this . props ;
52+ return (
53+ < div className = { `palettes ${ vert ?'vert' :'' } ` } >
54+ { items . map ( ( line , index ) => (
55+ < SortableItem
56+ key = { `item-${ index } ` }
57+ index = { index }
58+ line = { line }
59+ lineIndex = { index }
60+ onClick = { this . props . onClick }
61+ />
62+ ) ) }
63+ </ div >
64+ ) ;
65+ }
66+ } ) ,
67+ { withRef : true }
68+ ) ;
5469
5570@observer
5671export class Palettes extends Component {
5772
58- state = { vert : false } ;
73+ state = { vert : false , picker : false } ;
5974 mounted = false ;
6075
6176 onRef = ( node ) => {
@@ -68,7 +83,7 @@ export class Palettes extends Component {
6883 }
6984 } ;
7085
71- componentWillMount ( ) {
86+ componentDidMount ( ) {
7287 this . mounted = true ;
7388 this . props . node . setEventListener ( 'resize' , ( e ) => {
7489 const { width, height } = e . rect ;
@@ -88,8 +103,18 @@ export class Palettes extends Component {
88103 environment . swapPalette ( oldIndex , newIndex ) ;
89104 } ;
90105
106+ onClickColor = ( lineIndex , colorIndex , e ) => {
107+ const { x, y } = e . target . getBoundingClientRect ( ) ;
108+ const { picker } = this . state ;
109+ if ( ! picker ) {
110+ this . setState ( { picker : { lineIndex, colorIndex } } ) ;
111+ }
112+ } ;
113+
114+ // closePicker = () => this.setState({ picker: false });
115+
91116 render ( ) {
92- const { vert } = this . state ;
117+ const { vert, picker } = this . state ;
93118 const { palettes } = environment ;
94119 return (
95120 < div ref = { this . onRef } className = "paletteWrap" >
@@ -102,7 +127,15 @@ export class Palettes extends Component {
102127 useDragHandle = { true }
103128 vert = { vert }
104129 onSortEnd = { this . onSortEnd }
130+ onClick = { this . onClickColor }
105131 />
132+ { createPortal ( (
133+ picker && (
134+ < div >
135+ < SketchPicker />
136+ </ div >
137+ )
138+ ) , container ) }
106139 </ div >
107140 ) ;
108141 }
0 commit comments