UNPKG

flowbite-react

Version:

Official React components built for Flowbite and Tailwind CSS

1 lines 6.63 kB
{"version":3,"file":"Popover.mjs","sources":["../../../../src/components/Popover/Popover.tsx"],"sourcesContent":["\"use client\";\n\nimport type { Placement } from \"@floating-ui/react\";\nimport { FloatingFocusManager, useMergeRefs } from \"@floating-ui/react\";\nimport type { ComponentProps, ComponentPropsWithRef, Dispatch, ReactNode, SetStateAction } from \"react\";\nimport { cloneElement, isValidElement, useMemo, useRef, useState } from \"react\";\nimport { mergeDeep } from \"../../helpers/merge-deep\";\nimport { useBaseFLoating, useFloatingInteractions } from \"../../hooks/use-floating\";\nimport { getTheme } from \"../../theme-store\";\nimport type { DeepPartial } from \"../../types\";\nimport type { FlowbiteFloatingArrowTheme } from \"../Floating\";\nimport { getArrowPlacement } from \"../Floating/helpers\";\n\nexport interface FlowbitePopoverTheme {\n arrow: Omit<FlowbiteFloatingArrowTheme, \"style\">;\n base: string;\n content: string;\n}\n\nexport interface PopoverProps extends Omit<ComponentProps<\"div\">, \"content\" | \"style\"> {\n arrow?: boolean;\n content: ReactNode;\n placement?: \"auto\" | Placement;\n theme?: DeepPartial<FlowbitePopoverTheme>;\n trigger?: \"hover\" | \"click\";\n initialOpen?: boolean;\n open?: boolean;\n onOpenChange?: Dispatch<SetStateAction<boolean>>;\n}\n\nexport function Popover({\n children,\n content,\n theme: customTheme = {},\n arrow = true,\n trigger = \"click\",\n initialOpen,\n open: controlledOpen,\n onOpenChange: setControlledOpen,\n placement: theirPlacement = \"bottom\",\n ...props\n}: PopoverProps) {\n const [uncontrolledOpen, setUncontrolledOpen] = useState<boolean>(Boolean(initialOpen));\n const arrowRef = useRef<HTMLDivElement>(null);\n\n const theme = mergeDeep(getTheme().popover, customTheme);\n\n const open = controlledOpen ?? uncontrolledOpen;\n const setOpen = setControlledOpen ?? setUncontrolledOpen;\n\n const floatingProps = useBaseFLoating({\n open,\n placement: theirPlacement,\n arrowRef,\n setOpen,\n });\n\n const {\n floatingStyles,\n context,\n placement,\n middlewareData: { arrow: { x: arrowX, y: arrowY } = {} },\n refs,\n } = floatingProps;\n\n const { getFloatingProps, getReferenceProps } = useFloatingInteractions({\n context,\n role: \"dialog\",\n trigger,\n });\n\n const childrenRef = (children as ComponentPropsWithRef<\"button\">).ref;\n const ref = useMergeRefs([context.refs.setReference, childrenRef]);\n\n if (!isValidElement(children)) {\n throw Error(\"Invalid target element\");\n }\n\n const target = useMemo(() => {\n return cloneElement(\n children,\n getReferenceProps({\n ref,\n \"data-testid\": \"flowbite-popover-target\",\n ...children?.props,\n }),\n );\n }, [children, ref, getReferenceProps]);\n\n return (\n <>\n {target}\n {open && (\n <FloatingFocusManager context={context} modal>\n <div\n className={theme.base}\n ref={refs.setFloating}\n data-testid=\"flowbite-popover\"\n {...props}\n style={floatingStyles}\n {...getFloatingProps()}\n >\n <div className=\"relative\">\n {arrow && (\n <div\n className={theme.arrow.base}\n data-testid=\"flowbite-popover-arrow\"\n ref={arrowRef}\n style={{\n top: arrowY ?? \" \",\n left: arrowX ?? \" \",\n right: \" \",\n bottom: \" \",\n [getArrowPlacement({ placement })]: theme.arrow.placement,\n }}\n >\n &nbsp;\n </div>\n )}\n <div className={theme.content}>{content}</div>\n </div>\n </div>\n </FloatingFocusManager>\n )}\n </>\n );\n}\n"],"names":[],"mappings":";;;;;;;;AASO,SAAS,OAAO,CAAC;AACxB,EAAE,QAAQ;AACV,EAAE,OAAO;AACT,EAAE,KAAK,EAAE,WAAW,GAAG,EAAE;AACzB,EAAE,KAAK,GAAG,IAAI;AACd,EAAE,OAAO,GAAG,OAAO;AACnB,EAAE,WAAW;AACb,EAAE,IAAI,EAAE,cAAc;AACtB,EAAE,YAAY,EAAE,iBAAiB;AACjC,EAAE,SAAS,EAAE,cAAc,GAAG,QAAQ;AACtC,EAAE,GAAG,KAAK;AACV,CAAC,EAAE;AACH,EAAE,MAAM,CAAC,gBAAgB,EAAE,mBAAmB,CAAC,GAAG,QAAQ,CAAC,OAAO,CAAC,WAAW,CA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