Skip to content

Commit

Permalink
refactor to use EuiOutsideClickDetector
Browse files Browse the repository at this point in the history
  • Loading branch information
nickofthyme committed Jan 12, 2021
1 parent 4cb666a commit 587153d
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions src/plugins/vis_type_xy/public/utils/use_color_picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
* under the License.
*/

import React, { BaseSyntheticEvent, useCallback, useEffect, useMemo, useRef } from 'react';
import React, { BaseSyntheticEvent, useCallback, useMemo, useRef } from 'react';

import { LegendColorPicker, Position, XYChartSeriesIdentifier, SeriesName } from '@elastic/charts';
import { PopoverAnchorPosition, EuiWrappingPopover } from '@elastic/eui';
import { PopoverAnchorPosition, EuiWrappingPopover, EuiOutsideClickDetector } from '@elastic/eui';
import { EuiEvent } from '@elastic/eui/src/components/outside_click_detector/outside_click_detector';

import { ColorPicker } from '../../../charts/public';

Expand Down Expand Up @@ -65,35 +66,29 @@ export const useColorPicker = (

// eslint-disable-next-line react-hooks/rules-of-hooks
const handleOutsideClick = useCallback(
(e: MouseEvent) => {
(e: EuiEvent) => {
if (ref.current && !(ref.current! as any).contains(e.target)) {
onClose?.();
}
},
[onClose]
);

// eslint-disable-next-line react-hooks/rules-of-hooks
useEffect(() => {
document.addEventListener('click', handleOutsideClick);
return () => {
document.removeEventListener('click', handleOutsideClick);
};
}, [handleOutsideClick]);

return (
<EuiWrappingPopover
popoverRef={ref}
isOpen
ownFocus
display="block"
button={anchor}
anchorPosition={getAnchorPosition(legendPosition)}
closePopover={onClose}
panelPaddingSize="s"
>
<ColorPicker color={color} onChange={handlChange} label={seriesName} />
</EuiWrappingPopover>
<EuiOutsideClickDetector onOutsideClick={handleOutsideClick}>
<EuiWrappingPopover
popoverRef={ref}
isOpen
ownFocus
display="block"
button={anchor}
anchorPosition={getAnchorPosition(legendPosition)}
closePopover={onClose}
panelPaddingSize="s"
>
<ColorPicker color={color} onChange={handlChange} label={seriesName} />
</EuiWrappingPopover>
</EuiOutsideClickDetector>
);
},
[getSeriesName, legendPosition, setColor]
Expand Down

0 comments on commit 587153d

Please sign in to comment.