1- import React , { Component } from 'react' ;
1+ import React , { Component , useRef , useEffect , useState } from 'react' ;
22import { environment } from '#store/environment' ;
33import { observer } from 'mobx-react' ;
44import {
@@ -14,43 +14,69 @@ const Handle = SortableHandle(({ lineIndex }) => (
1414 < div className = "index" > { lineIndex } </ div >
1515) ) ;
1616
17- // anim, colour, placement
17+ const Color = ( { color, onChange, rect } ) => {
18+ const [ points , setPoints ] = useState ( [ 'br' , 'tr' ] ) ;
19+ const [ offset , setOffset ] = useState ( [ 2 , 2 ] )
20+ const node = useRef ( ) ;
21+ useEffect ( ( ) => {
22+ if ( node . current ) {
23+ const { x, y, height } = node . current . getBoundingClientRect ( ) ;
24+ const overflowX = x + 250 > window . innerWidth ;
25+ const overflowY = ( y + height ) + 250 > window . innerHeight ;
26+ const xType = 'lr' [ + overflowX ] ;
27+ setPoints ( [ 'tb' [ + overflowY ] + xType , 'bt' [ + overflowY ] + xType ] )
28+ setOffset ( [ overflowX ? - 2 : 2 , overflowY ? - 3 : 3 ] )
29+ }
30+ } , [ node . current , rect ] ) ;
31+ return (
32+ < Trigger
33+ action = { [ 'click' ] }
34+ prefixCls = "color-picker"
35+ popup = { (
36+ < Picker
37+ color = { color }
38+ onChange = { onChange }
39+ />
40+ ) }
41+ popupAlign = { {
42+ points,
43+ offset,
44+ targetOffset : [ 0 , 0 ] ,
45+ // overflow: { adjustX: true, adjustY: true },
46+ } }
47+ destroyPopupOnHide
48+ >
49+ < div
50+ ref = { node }
51+ className = "color"
52+ style = { {
53+ backgroundColor : color ,
54+ textAlign : 'center' ,
55+ } }
56+ />
57+ </ Trigger >
58+ ) ;
59+ } ;
60+
61+ // placement
1862
1963const SortableItem = SortableElement (
20- observer ( ( { line, lineIndex } ) => {
64+ observer ( ( { line, lineIndex, rect } ) => {
2165 return (
2266 < div className = "line" >
2367 < Handle lineIndex = { lineIndex } />
2468 { line . map ( ( color , colorIndex ) => {
2569 return (
26- < Trigger
70+ < Color
2771 key = { colorIndex }
28- action = { [ 'click' ] }
29- prefixCls = "color-picker"
30- popup = { (
31- < Picker
32- color = { color }
33- onChange = { ( { hex } ) => {
34- environment . palettes [ lineIndex ] [
35- colorIndex
36- ] = hexToMDHex ( hex ) ;
37- } }
38- />
39- ) }
40- popupAlign = { {
41- points : [ 'tl' , 'bl' ] ,
42- offset : [ 0 , 3 ]
72+ color = { color }
73+ rect = { rect }
74+ onChange = { ( { hex } ) => {
75+ environment . palettes [ lineIndex ] [
76+ colorIndex
77+ ] = hexToMDHex ( hex ) ;
4378 } }
44- destroyPopupOnHide
45- >
46- < div
47- className = "color"
48- style = { {
49- backgroundColor : color ,
50- textAlign : 'center' ,
51- } }
52- />
53- </ Trigger >
79+ />
5480 ) ;
5581 } ) }
5682 </ div >
@@ -62,15 +88,16 @@ const SortableList = SortableContainer(
6288 observer (
6389 class extends Component {
6490 render ( ) {
65- const { items, vert } = this . props ;
91+ const { items, rect } = this . props ;
6692 return (
67- < div className = { `palettes ${ vert ? 'vert' : '' } ` } >
93+ < div className = { `palettes ${ rect . vert ? 'vert' : '' } ` } >
6894 { items . map ( ( line , index ) => (
6995 < SortableItem
7096 key = { `item-${ index } ` }
7197 index = { index }
7298 line = { line }
7399 lineIndex = { index }
100+ rect = { rect }
74101 />
75102 ) ) }
76103 </ div >
@@ -83,25 +110,25 @@ const SortableList = SortableContainer(
83110
84111@observer
85112export class Palettes extends Component {
86- state = { vert : false } ;
113+ state = { width : 0 , height : 0 , x : 0 , y : 0 , } ;
87114 mounted = false ;
88115
89116 onRef = ( node ) => {
90117 if ( node ) {
91118 requestAnimationFrame ( ( ) => {
92- const { width, height } = node . getBoundingClientRect ( ) ;
93- this . mounted && this . setState ( { vert : width < height } ) ;
119+ const { width, height, x, y } = node . getBoundingClientRect ( ) ;
120+ const vert = width < height ;
121+ this . mounted && this . setState ( { vert, width, height, x, y } ) ;
94122 } ) ;
95123 }
96124 } ;
97125
98126 componentDidMount ( ) {
99127 this . mounted = true ;
100128 this . props . node . setEventListener ( 'resize' , ( e ) => {
101- const { width, height } = e . rect ;
102-
129+ const vert = e . rect . width < e . rect . height ;
103130 requestAnimationFrame ( ( ) => {
104- this . mounted && this . setState ( { vert : width < height } ) ;
131+ this . mounted && this . setState ( { vert, ... e . rect } ) ;
105132 } ) ;
106133 } ) ;
107134 }
@@ -126,7 +153,7 @@ export class Palettes extends Component {
126153 items = { palettes }
127154 lockToContainerEdges = { true }
128155 useDragHandle = { true }
129- vert = { vert }
156+ rect = { this . state }
130157 onSortEnd = { this . onSortEnd }
131158 />
132159
0 commit comments