Skip to content

Commit

Permalink
fix visualize color picker close on blur
Browse files Browse the repository at this point in the history
  • Loading branch information
nickofthyme committed Jan 12, 2021
1 parent 752a2bd commit 4cb666a
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/plugins/vis_type_xy/public/utils/use_color_picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

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

import { LegendColorPicker, Position, XYChartSeriesIdentifier, SeriesName } from '@elastic/charts';
import { PopoverAnchorPosition, EuiWrappingPopover } from '@elastic/eui';
Expand Down Expand Up @@ -48,6 +48,8 @@ export const useColorPicker = (
): LegendColorPicker =>
useMemo(
() => ({ anchor, color, onClose, onChange, seriesIdentifier }) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const ref = useRef<HTMLDivElement | null>(null);
const seriesName = getSeriesName(seriesIdentifier as XYChartSeriesIdentifier);
const handlChange = (newColor: string | null, event: BaseSyntheticEvent) => {
if (!seriesName) {
Expand All @@ -61,8 +63,27 @@ export const useColorPicker = (
onClose();
};

// eslint-disable-next-line react-hooks/rules-of-hooks
const handleOutsideClick = useCallback(
(e: MouseEvent) => {
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"
Expand Down

0 comments on commit 4cb666a

Please sign in to comment.