@@ -2,83 +2,93 @@ import React, { Component } from 'react';
22import { createPortal } from 'react-dom' ;
33import { environment } from '#store/environment' ;
44import { observer } from 'mobx-react' ;
5- import { SortableContainer , SortableElement , SortableHandle } from 'react-sortable-hoc' ;
5+ import {
6+ SortableContainer ,
7+ SortableElement ,
8+ SortableHandle ,
9+ } from 'react-sortable-hoc' ;
610import { hexToMDHex } from '#formats/palette' ;
711import ColorPicker from 'rc-color-picker' ;
812import { SketchPicker } from 'react-color' ;
913
10- const Handle = SortableHandle ( ( { lineIndex} ) => < div className = "index" >
11- { lineIndex }
12- </ div > ) ;
14+ const Handle = SortableHandle ( ( { lineIndex } ) => (
15+ < div className = "index" > { lineIndex } </ div >
16+ ) ) ;
1317
1418const container = document . body . appendChild ( document . createElement ( 'div' ) ) ;
1519container . className = 'palette-color-picker' ;
1620
17- const SortableItem = SortableElement ( observer ( ( { line, lineIndex, onClick} ) => {
18- return (
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- } ) }
44- </ div >
45- ) ;
46- } ) ) ;
21+ const SortableItem = SortableElement (
22+ observer ( ( { line, lineIndex, onClick } ) => {
23+ return (
24+ < div className = "line" >
25+ < Handle lineIndex = { lineIndex } />
26+ { line . map ( ( color , colorIndex ) => {
27+ return (
28+ < div
29+ key = { colorIndex }
30+ className = "color"
31+ style = { {
32+ backgroundColor : color ,
33+ textAlign : 'center' ,
34+ } }
35+ onClick = { ( e ) => onClick ( lineIndex , colorIndex , e ) }
36+ >
37+ < ColorPicker
38+ animation = "slide-up"
39+ color = { color }
40+ enableAlpha = { false }
41+ mode = "RGB"
42+ onChange = { ( { color } ) => {
43+ environment . palettes [ lineIndex ] [
44+ colorIndex
45+ ] = hexToMDHex ( color ) ;
46+ } }
47+ >
48+ < div className = "picker" />
49+ </ ColorPicker >
50+ </ div >
51+ ) ;
52+ } ) }
53+ </ div >
54+ ) ;
55+ } ) ,
56+ ) ;
4757
4858const 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 }
59+ observer (
60+ class extends Component {
61+ render ( ) {
62+ const { items, vert } = this . props ;
63+ return (
64+ < div className = { `palettes ${ vert ? 'vert' : '' } ` } >
65+ { items . map ( ( line , index ) => (
66+ < SortableItem
67+ key = { `item-${ index } ` }
68+ index = { index }
69+ line = { line }
70+ lineIndex = { index }
71+ onClick = { this . props . onClick }
72+ />
73+ ) ) }
74+ </ div >
75+ ) ;
76+ }
77+ } ,
78+ ) ,
79+ { withRef : true } ,
6880) ;
6981
7082@observer
7183export class Palettes extends Component {
72-
7384 state = { vert : false , picker : false } ;
7485 mounted = false ;
7586
7687 onRef = ( node ) => {
7788 if ( node ) {
7889 requestAnimationFrame ( ( ) => {
7990 const { width, height } = node . getBoundingClientRect ( ) ;
80- this . mounted &&
81- this . setState ( { vert : width < height } ) ;
91+ this . mounted && this . setState ( { vert : width < height } ) ;
8292 } ) ;
8393 }
8494 } ;
@@ -89,8 +99,7 @@ export class Palettes extends Component {
8999 const { width, height } = e . rect ;
90100
91101 requestAnimationFrame ( ( ) => {
92- this . mounted &&
93- this . setState ( { vert : width < height } ) ;
102+ this . mounted && this . setState ( { vert : width < height } ) ;
94103 } ) ;
95104 } ) ;
96105 }
@@ -104,14 +113,20 @@ export class Palettes extends Component {
104113 } ;
105114
106115 onClickColor = ( lineIndex , colorIndex , e ) => {
107- const { x , y } = e . target . getBoundingClientRect ( ) ;
116+ const rect = e . target . getBoundingClientRect ( ) ;
108117 const { picker } = this . state ;
109- if ( ! picker ) {
110- this . setState ( { picker : { lineIndex, colorIndex } } ) ;
118+ if (
119+ picker &&
120+ lineIndex === picker . lineIndex &&
121+ colorIndex === picker . colorIndex
122+ ) {
123+ this . closePicker ( ) ;
124+ } else {
125+ this . setState ( { picker : { lineIndex, colorIndex, rect } } ) ;
111126 }
112127 } ;
113128
114- // closePicker = () => this.setState({ picker: false });
129+ closePicker = ( ) => this . setState ( { picker : false } ) ;
115130
116131 render ( ) {
117132 const { vert, picker } = this . state ;
@@ -121,23 +136,29 @@ export class Palettes extends Component {
121136 < SortableList
122137 axis = { vert ? 'x' : 'y' }
123138 lockAxis = { vert ? 'x' : 'y' }
124- helperClass = { `sortable-float-palette${ vert ? '-vert' : '' } ` }
139+ helperClass = { `sortable-float-palette${ vert ? '-vert' : '' } ` }
125140 items = { palettes }
126141 lockToContainerEdges = { true }
127142 useDragHandle = { true }
128143 vert = { vert }
129144 onSortEnd = { this . onSortEnd }
130145 onClick = { this . onClickColor }
131146 />
132- { createPortal ( (
147+ { createPortal (
133148 picker && (
134- < div >
149+ < div
150+ style = { {
151+ left : picker . rect . x + 'px' ,
152+ top : picker . rect . y + picker . rect . height + 'px' ,
153+ position : 'absolute' ,
154+ } }
155+ >
135156 < SketchPicker />
136157 </ div >
137- )
138- ) , container ) }
139- </ div >
158+ ) ,
159+ container ,
160+ ) }
161+ </ div >
140162 ) ;
141163 }
142-
143164}
0 commit comments