diff --git a/.changeset/nervous-brooms-raise.md b/.changeset/nervous-brooms-raise.md new file mode 100644 index 00000000..e19ba615 --- /dev/null +++ b/.changeset/nervous-brooms-raise.md @@ -0,0 +1,5 @@ +--- +"@cambly/syntax-floating-components": major +--- + +Tooltip: Reconfigure API for easier usage + add zIndex as a prop diff --git a/packages/syntax-floating-components/src/Tooltip/Tooltip.stories.tsx b/packages/syntax-floating-components/src/Tooltip/Tooltip.stories.tsx index e3767d84..c0406d2f 100644 --- a/packages/syntax-floating-components/src/Tooltip/Tooltip.stories.tsx +++ b/packages/syntax-floating-components/src/Tooltip/Tooltip.stories.tsx @@ -1,6 +1,6 @@ import { useState, useRef, useEffect, type ReactElement } from "react"; import type { StoryObj, Meta } from "@storybook/react"; -import { Tooltip, TooltipContent, TooltipTrigger } from "./Tooltip"; +import { Tooltip } from "./Tooltip"; import Button from "../../../syntax-core/src/Button/Button"; import IconButton from "../../../syntax-core/src/IconButton/IconButton"; import InfoOutlinedIcon from "@mui/icons-material/InfoOutlined"; @@ -48,7 +48,7 @@ export default { defaultValue: { summary: "absolute" }, }, }, - children: { + content: { control: { type: "text" }, description: "The string value to show on the tooltip content", }, @@ -62,41 +62,39 @@ export const Default: StoryObj = { placement: "right", initialOpen: true, strategy: "absolute", - children: "This is a tooltip", + content: "This is a tooltip", }, - render: ({ delay, placement, initialOpen, strategy, children }) => ( + render: ({ delay, placement, initialOpen, strategy, content }) => (
- - alert("Default button pressed")} - color="tertiary" - size="lg" - /> - - {children} + alert("Default button pressed")} + color="tertiary" + size="lg" + />
), }; export const UncontrolledButtonTooltip: StoryObj = { + args: { + content: "This is a button", + }, render: () => ( - - - - - My tooltip + + , ); expect(screen.getByRole("button")).toBeInTheDocument(); @@ -32,11 +29,8 @@ describe("tooltip", () => { it("tooltip dialogue appears", async () => { render( - - - - - My tooltip + + , ); diff --git a/packages/syntax-floating-components/src/Tooltip/Tooltip.tsx b/packages/syntax-floating-components/src/Tooltip/Tooltip.tsx index 85fc1426..a07e8275 100644 --- a/packages/syntax-floating-components/src/Tooltip/Tooltip.tsx +++ b/packages/syntax-floating-components/src/Tooltip/Tooltip.tsx @@ -57,9 +57,15 @@ type TooltipOptions = { * @defaultValue "absolute" */ strategy?: Strategy; + /** + * The z-index of the tooltip + * + * @defaultValue 0 + */ + zIndex?: number; }; -export function useTooltip({ +function useTooltip({ delay = 0, initialOpen = false, open: controlledOpen, @@ -67,6 +73,7 @@ export function useTooltip({ strategy = "absolute", onOpen = undefined, onClose = undefined, + zIndex = 0, }: TooltipOptions): UseFloatingReturn & { getReferenceProps: ( userProps?: React.HTMLProps | undefined, @@ -80,6 +87,7 @@ export function useTooltip({ arrowRef: React.MutableRefObject; open: boolean; setOpen: (open: boolean) => void; + zIndex: number; } { const [uncontrolledOpen, setUncontrolledOpen] = React.useState(initialOpen); @@ -136,13 +144,14 @@ export function useTooltip({ return React.useMemo( () => ({ + zIndex, open, setOpen, ...interactions, ...data, arrowRef, }), - [open, setOpen, interactions, data], + [zIndex, open, setOpen, interactions, data], ); } @@ -160,24 +169,36 @@ const useTooltipContext = () => { return context; }; -/** - * [Tooltip](https://cambly-syntax.vercel.app/?path=/docs/floating-components-tooltip--docs) displays contextual information on hover or focus. - */ -export function Tooltip({ - children, - ...options -}: { children: React.ReactNode } & TooltipOptions): JSX.Element { - // This can accept any props as options, e.g. `placement`, - // or other positioning options. - const tooltip = useTooltip(options); +const TooltipContent = React.forwardRef< + HTMLDivElement, + React.HTMLProps +>(function TooltipContent(props, propRef) { + const { zIndex, context: floatingContext, ...context } = useTooltipContext(); + const ref = useMergeRefs([context.refs.setFloating, propRef]); + + if (!context.open) return null; + return ( - - {children} - + +
+ + {props.children} + + +
+
); -} +}); -export const TooltipTrigger = React.forwardRef< +const TooltipTrigger = React.forwardRef< HTMLElement, React.HTMLProps >(function TooltipTrigger({ children, ...props }, propRef) { @@ -207,30 +228,40 @@ export const TooltipTrigger = React.forwardRef< } }); -export const TooltipContent = React.forwardRef< - HTMLDivElement, - React.HTMLProps ->(function TooltipContent(props, propRef) { - const { context: floatingContext, ...context } = useTooltipContext(); - const ref = useMergeRefs([context.refs.setFloating, propRef]); +/** + * [Tooltip](https://cambly-syntax.vercel.app/?path=/docs/floating-components-tooltip--docs) displays contextual information on hover or focus. + * + * + * Usage: + * ```tsx + * + * + * + * ``` + */ +export function Tooltip({ + children, + content, + ...options +}: { + children: React.ReactNode; + zIndex?: number; + content: string; +} & TooltipOptions): JSX.Element { + // This can accept any props as options, e.g. `placement`, + // or other positioning options. + const tooltip = useTooltip(options); - if (!context.open) return null; + const value = React.useMemo(() => { + return { + ...tooltip, + }; + }, [tooltip]); return ( - -
- - {props.children} - - -
-
+ + {children} + {content} + ); -}); +} diff --git a/packages/syntax-floating-components/src/index.tsx b/packages/syntax-floating-components/src/index.tsx index 56c6b645..84975483 100644 --- a/packages/syntax-floating-components/src/index.tsx +++ b/packages/syntax-floating-components/src/index.tsx @@ -1,11 +1,4 @@ -import { Tooltip, TooltipContent, TooltipTrigger } from "./Tooltip/Tooltip"; +import { Tooltip } from "./Tooltip/Tooltip"; import { Popover, PopoverContent, PopoverTrigger } from "./Popover/Popover"; -export { - Popover, - PopoverContent, - PopoverTrigger, - Tooltip, - TooltipContent, - TooltipTrigger, -}; +export { Popover, PopoverContent, PopoverTrigger, Tooltip };