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

feat: make tooltips stay open #1383

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/ui/src/components/Floating/Floating.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import type { Placement } from "@floating-ui/core";
import { autoUpdate, useFocus } from "@floating-ui/react";
import type { ComponentProps, FC, ReactNode } from "react";
import { useEffect, useRef, useState } from "react";
import { useEffect, useMemo, useRef, useState } from "react";
import { twMerge } from "tailwind-merge";
import { useBaseFLoating, useFloatingInteractions } from "../../hooks/use-floating";
import { getArrowPlacement } from "./helpers";
Expand Down Expand Up @@ -43,6 +43,7 @@ export interface FloatingProps extends Omit<ComponentProps<"div">, "content" | "
theme: FlowbiteFloatingTheme;
trigger?: "hover" | "click";
minWidth?: number;
isOpen: boolean | null;
}

/**
Expand All @@ -59,10 +60,17 @@ export const Floating: FC<FloatingProps> = ({
theme,
trigger = "hover",
minWidth,
isOpen = null,
...props
}) => {
const arrowRef = useRef<HTMLDivElement>(null);
const [open, setOpen] = useState(false);
const [innerOpen, setOpen] = useState(false);

const open = useMemo(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to use useMemo() here since its not a heavy calculation or any iteration, we can just stick to a more simplified version:

  const [_open, setOpen] = useState(false);

  const open = isOpen ?? _open;

if (isOpen === null) return innerOpen;

return isOpen;
}, [innerOpen, isOpen]);

const floatingProperties = useBaseFLoating({
open,
Expand Down
18 changes: 18 additions & 0 deletions packages/ui/src/components/Tooltip/Tooltip.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ NoArrow.args = {
children: <Button>Tooltip with no arrow</Button>,
};

export const StillOpen = Template.bind({});
StillOpen.storyName = "Still open";
StillOpen.args = {
content: "Tooltip content",
isOpen: true,
placement: "bottom",
children: <Button>Tooltip that doesn't close</Button>,
};

export const StillClosed = Template.bind({});
StillClosed.storyName = "Still closed";
StillClosed.args = {
content: "Tooltip content",
isOpen: false,
placement: "bottom",
children: <Button>Tooltip that doesn't open</Button>,
};

export const SlowAnimation = Template.bind({});
SlowAnimation.storyName = "Slow animation";
SlowAnimation.args = {
Expand Down
3 changes: 3 additions & 0 deletions packages/ui/src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface TooltipProps extends Omit<ComponentProps<"div">, "content" | "s
style?: "dark" | "light" | "auto";
theme?: DeepPartial<FlowbiteTooltipTheme>;
trigger?: "hover" | "click";
isOpen?: boolean | null;
}

/**
Expand All @@ -30,6 +31,7 @@ export const Tooltip: FC<TooltipProps> = ({
style = "dark",
theme: customTheme = {},
trigger = "hover",
isOpen = null,
...props
}) => {
const theme = mergeDeep(getTheme().tooltip, customTheme);
Expand All @@ -44,6 +46,7 @@ export const Tooltip: FC<TooltipProps> = ({
theme={theme}
trigger={trigger}
className={className}
isOpen={isOpen}
{...props}
>
{children}
Expand Down