Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react-components): Fix issue with outside click of the settings menu used by the settings button #4726

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const FilterButton = ({
<Dropdown
visible={isOpen}
hideOnSelect={false}
appendTo={document.body}
appendTo={'parent'}
placement={usedInSettings ? 'bottom-end' : 'auto-start'}
content={
<div ref={menuRef}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const OptionButton = ({
<Dropdown
visible={isOpen}
hideOnSelect={true}
appendTo={document.body}
appendTo={'parent'}
placement={usedInSettings ? 'bottom-end' : 'auto-start'}
content={
<div ref={menuRef}>
Expand Down
53 changes: 41 additions & 12 deletions react-components/src/components/Architecture/SettingsButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
* Copyright 2024 Cognite AS
*/

import { useCallback, useEffect, useMemo, useState, type ReactElement } from 'react';
import {
type MouseEvent,
useCallback,
useEffect,
useMemo,
useRef,
useState,
type ReactElement
} from 'react';
import {
Button,
Dropdown,
Expand Down Expand Up @@ -30,6 +38,7 @@ import { OptionButton } from './OptionButton';
import { BaseSliderCommand } from '../../architecture/base/commands/BaseSliderCommand';
import { BaseFilterCommand } from '../../architecture/base/commands/BaseFilterCommand';
import { FilterButton } from './FilterButton';
import { useClickOutside } from './useClickOutside';

export const SettingsButton = ({
inputCommand,
Expand Down Expand Up @@ -66,6 +75,22 @@ export const SettingsButton = ({
};
}, [command]);

const outsideAction = (): boolean => {
if (!isOpen) {
return false;
}
postAction();
return false;
};

const postAction = (): void => {
setOpen(false);
renderTarget.domElement.focus();
};

const menuRef = useRef<HTMLDivElement | null>(null);
useClickOutside(menuRef, outsideAction);

if (!isVisible || !command.hasChildren) {
return <></>;
}
Expand All @@ -83,18 +108,20 @@ export const SettingsButton = ({
<Dropdown
visible={isOpen}
hideOnSelect={false}
appendTo={document.body}
appendTo={'parent'}
placement="auto-start"
content={
<Menu
style={{
flexDirection,
padding: '4px 4px'
}}>
{children.map((child, _index): ReactElement | undefined => {
return createMenuItem(child, t);
})}
</Menu>
<div ref={menuRef}>
<Menu
style={{
flexDirection,
padding: '4px 4px'
}}>
{children.map((child, _index): ReactElement | undefined => {
return createMenuItem(child, t);
})}
</Menu>
</div>
}>
<Button
type={getButtonType(command)}
Expand All @@ -104,7 +131,9 @@ export const SettingsButton = ({
toggled={isOpen}
aria-label={label}
iconPlacement="right"
onClick={() => {
onClick={(event: MouseEvent<HTMLElement>) => {
event.stopPropagation();
event.preventDefault();
setOpen((prevState) => !prevState);
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export function useClickOutside(ref: RefObject<HTMLElement>, callback: () => boo
useEffect(() => {
const handleClickOutside = (event: MouseEvent): void => {
const current = ref.current;
if (current !== null && !current.contains(event.target as Node)) {
const target = event.target as Node;
if (current !== null && !current.contains(target)) {
if (callback()) {
event.stopPropagation();
event.preventDefault();
Expand Down
Loading