@@ -8,6 +8,7 @@ import React, {
88} from 'react' ;
99import { Color , ColorPicker as PaletteColorPicker } from 'react-color-palette' ;
1010import { IconButton } from '../Icon/styled' ;
11+ import { Portal } from '@devgrace/react' ;
1112
1213interface ColorPickerProps {
1314 children : React . ReactNode ;
@@ -24,6 +25,7 @@ const ColorPicker = ({
2425} : ColorPickerProps ) => {
2526 const [ isOpenColorPicker , setIsOpenColorPicker ] = useState ( false ) ;
2627 const colorRef = useRef < HTMLDivElement > ( null ) ;
28+ const [ coordinates , setCoordinates ] = useState ( { x : 0 , y : 0 } ) ;
2729
2830 const handleCloseColorPicker = useCallback (
2931 ( e : any ) => {
@@ -39,10 +41,16 @@ const ColorPicker = ({
3941 [ isOpenColorPicker , toggleIsBlockEvent ]
4042 ) ;
4143
42- const handleOpenColorPicker = useCallback ( ( ) => {
43- setIsOpenColorPicker ( ( prev ) => ! prev ) ;
44- toggleIsBlockEvent ( ) ;
45- } , [ toggleIsBlockEvent ] ) ;
44+ const handleOpenColorPicker = useCallback (
45+ ( e : React . MouseEvent < HTMLButtonElement > ) => {
46+ const { x, y } = e . currentTarget . getBoundingClientRect ( ) ;
47+
48+ setCoordinates ( { x, y } ) ;
49+ setIsOpenColorPicker ( ( prev ) => ! prev ) ;
50+ toggleIsBlockEvent ( ) ;
51+ } ,
52+ [ toggleIsBlockEvent ]
53+ ) ;
4654
4755 useEffect ( ( ) => {
4856 document . addEventListener ( 'mousedown' , handleCloseColorPicker ) ;
@@ -61,11 +69,12 @@ const ColorPicker = ({
6169 const colorPickerWrapperStyle : CSSProperties = useMemo ( ( ) => {
6270 return {
6371 position : 'absolute' ,
64- left : '50%' ,
65- bottom : '40px' ,
72+ left : ` ${ coordinates . x } px` ,
73+ top : ` ${ coordinates . y - 300 } px` ,
6674 transform : 'translateX(-50%)' ,
75+ zIndex : '9999' ,
6776 } ;
68- } , [ ] ) ;
77+ } , [ coordinates ] ) ;
6978
7079 return (
7180 < div style = { wrapperStyle } >
@@ -75,19 +84,21 @@ const ColorPicker = ({
7584 isBorder = { true } >
7685 { children }
7786 </ IconButton >
78- { isOpenColorPicker && (
79- < div ref = { colorRef } style = { colorPickerWrapperStyle } >
80- < PaletteColorPicker
81- width = { 250 }
82- height = { 150 }
83- color = { color }
84- onChange = { setColor }
85- hideHSV
86- hideRGB
87- dark
88- />
89- </ div >
90- ) }
87+ < Portal >
88+ { isOpenColorPicker && (
89+ < div ref = { colorRef } style = { colorPickerWrapperStyle } >
90+ < PaletteColorPicker
91+ width = { 250 }
92+ height = { 150 }
93+ color = { color }
94+ onChange = { setColor }
95+ hideHSV
96+ hideRGB
97+ dark
98+ />
99+ </ div >
100+ ) }
101+ </ Portal >
91102 </ div >
92103 ) ;
93104} ;
0 commit comments