Skip to content

Commit d3987e2

Browse files
committed
feat: tooltip stay open/closed
1 parent 2afa3c8 commit d3987e2

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

packages/ui/src/components/Floating/Floating.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import type { Placement } from "@floating-ui/core";
44
import { autoUpdate, useFocus } from "@floating-ui/react";
55
import type { ComponentProps, FC, ReactNode } from "react";
6-
import { useEffect, useRef, useState } from "react";
6+
import { useEffect, useMemo, useRef, useState } from "react";
77
import { twMerge } from "tailwind-merge";
88
import { useBaseFLoating, useFloatingInteractions } from "../../hooks/use-floating";
99
import { getArrowPlacement } from "./helpers";
@@ -43,6 +43,7 @@ export interface FloatingProps extends Omit<ComponentProps<"div">, "content" | "
4343
theme: FlowbiteFloatingTheme;
4444
trigger?: "hover" | "click";
4545
minWidth?: number;
46+
isOpen: boolean | null;
4647
}
4748

4849
/**
@@ -59,10 +60,17 @@ export const Floating: FC<FloatingProps> = ({
5960
theme,
6061
trigger = "hover",
6162
minWidth,
63+
isOpen = null,
6264
...props
6365
}) => {
6466
const arrowRef = useRef<HTMLDivElement>(null);
65-
const [open, setOpen] = useState(false);
67+
const [innerOpen, setOpen] = useState(false);
68+
69+
const open = useMemo(() => {
70+
if (isOpen === null) return innerOpen;
71+
72+
return isOpen;
73+
}, [innerOpen, isOpen]);
6674

6775
const floatingProperties = useBaseFLoating({
6876
open,

packages/ui/src/components/Tooltip/Tooltip.stories.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ NoArrow.args = {
3636
children: <Button>Tooltip with no arrow</Button>,
3737
};
3838

39+
export const StillOpen = Template.bind({});
40+
StillOpen.storyName = "Still open";
41+
StillOpen.args = {
42+
content: "Tooltip content",
43+
isOpen: true,
44+
placement: "bottom",
45+
children: <Button>Tooltip that doesn't close</Button>,
46+
};
47+
48+
export const StillClosed = Template.bind({});
49+
StillClosed.storyName = "Still closed";
50+
StillClosed.args = {
51+
content: "Tooltip content",
52+
isOpen: false,
53+
placement: "bottom",
54+
children: <Button>Tooltip that doesn't open</Button>,
55+
};
56+
3957
export const SlowAnimation = Template.bind({});
4058
SlowAnimation.storyName = "Slow animation";
4159
SlowAnimation.args = {

packages/ui/src/components/Tooltip/Tooltip.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface TooltipProps extends Omit<ComponentProps<"div">, "content" | "s
1515
style?: "dark" | "light" | "auto";
1616
theme?: DeepPartial<FlowbiteTooltipTheme>;
1717
trigger?: "hover" | "click";
18+
isOpen?: boolean | null;
1819
}
1920

2021
/**
@@ -30,6 +31,7 @@ export const Tooltip: FC<TooltipProps> = ({
3031
style = "dark",
3132
theme: customTheme = {},
3233
trigger = "hover",
34+
isOpen = null,
3335
...props
3436
}) => {
3537
const theme = mergeDeep(getTheme().tooltip, customTheme);
@@ -44,6 +46,7 @@ export const Tooltip: FC<TooltipProps> = ({
4446
theme={theme}
4547
trigger={trigger}
4648
className={className}
49+
isOpen={isOpen}
4750
{...props}
4851
>
4952
{children}

0 commit comments

Comments
 (0)