aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/molecules
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-05-18 14:27:11 +0200
committerArmand Philippot <git@armandphilippot.com>2022-05-18 14:27:11 +0200
commitb214baab3e17d92f784b4f782863deafc5558ee4 (patch)
treecdc20c7e77ba6926285917eead8bb088bdc843f8 /src/components/molecules
parent54883bb5c36cf21462a421605a709fdd6f04b150 (diff)
chore: close toolbar modals on click/focus outside
Diffstat (limited to 'src/components/molecules')
-rw-r--r--src/components/molecules/forms/select-with-tooltip.tsx10
-rw-r--r--src/components/molecules/modals/tooltip.tsx16
2 files changed, 16 insertions, 10 deletions
diff --git a/src/components/molecules/forms/select-with-tooltip.tsx b/src/components/molecules/forms/select-with-tooltip.tsx
index cf7b041..f576a15 100644
--- a/src/components/molecules/forms/select-with-tooltip.tsx
+++ b/src/components/molecules/forms/select-with-tooltip.tsx
@@ -1,4 +1,5 @@
-import { FC, useState } from 'react';
+import useClickOutside from '@utils/hooks/use-click-outside';
+import { FC, useRef, useState } from 'react';
import HelpButton from '../buttons/help-button';
import Tooltip, { type TooltipProps } from '../modals/tooltip';
import LabelledSelect, { type LabelledSelectProps } from './labelled-select';
@@ -28,11 +29,17 @@ const SelectWithTooltip: FC<SelectWithTooltipProps> = ({
...props
}) => {
const [isTooltipOpened, setIsTooltipOpened] = useState<boolean>(false);
+ const tooltipRef = useRef<HTMLDivElement>(null);
const buttonModifier = isTooltipOpened ? styles['btn--activated'] : '';
const tooltipModifier = isTooltipOpened
? styles['tooltip--visible']
: styles['tooltip--hidden'];
+ useClickOutside(
+ tooltipRef,
+ () => isTooltipOpened && setIsTooltipOpened(false)
+ );
+
return (
<div className={styles.wrapper}>
<LabelledSelect
@@ -50,6 +57,7 @@ const SelectWithTooltip: FC<SelectWithTooltipProps> = ({
content={content}
icon="?"
className={`${styles.tooltip} ${tooltipModifier} ${tooltipClassName}`}
+ ref={tooltipRef}
/>
</div>
);
diff --git a/src/components/molecules/modals/tooltip.tsx b/src/components/molecules/modals/tooltip.tsx
index 80721f3..efb3009 100644
--- a/src/components/molecules/modals/tooltip.tsx
+++ b/src/components/molecules/modals/tooltip.tsx
@@ -1,5 +1,5 @@
import List, { type ListItem } from '@components/atoms/lists/list';
-import { FC, ReactNode } from 'react';
+import { forwardRef, ForwardRefRenderFunction, ReactNode } from 'react';
import styles from './tooltip.module.scss';
export type TooltipProps = {
@@ -26,12 +26,10 @@ export type TooltipProps = {
*
* Render a tooltip modal.
*/
-const Tooltip: FC<TooltipProps> = ({
- className = '',
- content,
- icon,
- title,
-}) => {
+const Tooltip: ForwardRefRenderFunction<HTMLDivElement, TooltipProps> = (
+ { className = '', content, icon, title },
+ ref
+) => {
/**
* Format an array of strings to an array of object with id and value.
*
@@ -45,7 +43,7 @@ const Tooltip: FC<TooltipProps> = ({
};
return (
- <div className={`${styles.wrapper} ${className}`}>
+ <div className={`${styles.wrapper} ${className}`} ref={ref}>
<div className={styles.title}>
<span className={styles.icon}>{icon}</span>
{title}
@@ -59,4 +57,4 @@ const Tooltip: FC<TooltipProps> = ({
);
};
-export default Tooltip;
+export default forwardRef(Tooltip);