11import React , { PropTypes } from 'react'
2+ import ReactDOM from 'react-dom'
23import CSSModules from 'browser/lib/CSSModules'
34import styles from './StorageItem.styl'
45import consts from 'browser/lib/consts'
@@ -18,6 +19,7 @@ class UnstyledFolderItem extends React.Component {
1819 status : 'IDLE' ,
1920 folder : {
2021 showColumnPicker : false ,
22+ colorPickerPos : { left : 0 , top : 0 } ,
2123 color : props . color ,
2224 name : props . name
2325 }
@@ -52,8 +54,22 @@ class UnstyledFolderItem extends React.Component {
5254 }
5355
5456 handleColorButtonClick ( e ) {
55- const folder = Object . assign ( { } , this . state . folder , { showColumnPicker : true } )
56- this . setState ( { folder } )
57+ const folder = Object . assign ( { } , this . state . folder , { showColumnPicker : true , colorPickerPos : { left : 0 , top : 0 } } )
58+ this . setState ( { folder } , function ( ) {
59+ // After the color picker has been painted, re-calculate its position
60+ // by comparing its dimensions to the host dimensions.
61+ const { hostBoundingBox } = this . props ;
62+ const colorPickerNode = ReactDOM . findDOMNode ( this . refs . colorPicker )
63+ const colorPickerBox = colorPickerNode . getBoundingClientRect ( )
64+ const offsetTop = hostBoundingBox . bottom - colorPickerBox . bottom
65+ const folder = Object . assign ( { } , this . state . folder , {
66+ colorPickerPos : {
67+ left : 25 ,
68+ top : offsetTop < 0 ? offsetTop - 5 : 0 // subtract 5px for aestetics
69+ }
70+ } )
71+ this . setState ( { folder } )
72+ } )
5773 }
5874
5975 handleColorChange ( color ) {
@@ -78,6 +94,9 @@ class UnstyledFolderItem extends React.Component {
7894 position : 'fixed' ,
7995 top : 0 , right : 0 , bottom : 0 , left : 0
8096 }
97+ const pickerStyle = Object . assign ( { } , {
98+ position : 'absolute'
99+ } , this . state . folder . colorPickerPos )
81100 return (
82101 < div styleName = 'folderList-item' >
83102 < div styleName = 'folderList-item-left' >
@@ -88,10 +107,13 @@ class UnstyledFolderItem extends React.Component {
88107 < div style = { popover } >
89108 < div style = { cover }
90109 onClick = { ( ) => this . handleColorPickerClose ( ) } />
91- < SketchPicker
92- color = { this . state . folder . color }
93- onChange = { ( color ) => this . handleColorChange ( color ) }
94- onChangeComplete = { ( color ) => this . handleColorChange ( color ) } />
110+ < div style = { pickerStyle } >
111+ < SketchPicker
112+ ref = "colorPicker"
113+ color = { this . state . folder . color }
114+ onChange = { ( color ) => this . handleColorChange ( color ) }
115+ onChangeComplete = { ( color ) => this . handleColorChange ( color ) } />
116+ </ div >
95117 </ div >
96118 : null }
97119 < i className = 'fa fa-square' />
@@ -154,13 +176,12 @@ class UnstyledFolderItem extends React.Component {
154176 }
155177
156178 handleEditButtonClick ( e ) {
157- let { folder } = this . props
179+ let { folder : propsFolder } = this . props
180+ let { folder : stateFolder } = this . state
181+ const folder = Object . assign ( { } , stateFolder , propsFolder )
158182 this . setState ( {
159183 status : 'EDIT' ,
160- folder : {
161- color : folder . color ,
162- name : folder . name
163- }
184+ folder
164185 } , ( ) => {
165186 this . refs . nameInput . select ( )
166187 } )
@@ -293,11 +314,12 @@ class StorageItem extends React.Component {
293314 }
294315
295316 render ( ) {
296- let { storage } = this . props
317+ let { storage, hostBoundingBox } = this . props
297318 let folderList = storage . folders . map ( ( folder ) => {
298319 return < FolderItem key = { folder . key }
299320 folder = { folder }
300321 storage = { storage }
322+ hostBoundingBox = { hostBoundingBox }
301323 />
302324 } )
303325 return (
@@ -360,6 +382,14 @@ class StorageItem extends React.Component {
360382}
361383
362384StorageItem . propTypes = {
385+ hostBoundingBox : PropTypes . shape ( {
386+ bottom : PropTypes . number ,
387+ height : PropTypes . number ,
388+ left : PropTypes . number ,
389+ right : PropTypes . number ,
390+ top : PropTypes . number ,
391+ width : PropTypes . number
392+ } ) ,
363393 storage : PropTypes . shape ( {
364394 key : PropTypes . string
365395 } ) ,
0 commit comments