89 lines
2.5 KiB
TypeScript
89 lines
2.5 KiB
TypeScript
import type { CSSProperties } from "react";
|
|
import {
|
|
FloatingFocusManager,
|
|
FloatingList,
|
|
FloatingPortal,
|
|
} from "@floating-ui/react";
|
|
import { useMenuFloatingContext } from "./use-menu-floating-context";
|
|
import { useMenuContext } from "./use-menu-context";
|
|
import { mergeClass } from "../../styles/utils/merge-class";
|
|
import { containerRecipe } from "../../styles/recipes/container-recipe";
|
|
import { useThemeContext } from "../theme/use-theme-context";
|
|
import type { CommonProps } from "../../utils/common-props";
|
|
import { brandRecipe } from "../../styles/recipes/brand-recipe";
|
|
|
|
export type MenuDropdownProps = CommonProps & {
|
|
modal?: boolean;
|
|
};
|
|
|
|
export const MenuDropdown = (props: MenuDropdownProps) => {
|
|
const {
|
|
modal = false,
|
|
className = "",
|
|
children,
|
|
style,
|
|
ref,
|
|
...rest
|
|
} = props;
|
|
|
|
// =========== Menu Context Consumer ===================================================================================================
|
|
|
|
const { size } = useMenuContext()!;
|
|
|
|
// =========== Menu Floating Context Consumer ===================================================================================================
|
|
|
|
const {
|
|
refs,
|
|
elementsRef,
|
|
context,
|
|
floatingStyles,
|
|
getFloatingProps,
|
|
isOpen,
|
|
} = useMenuFloatingContext()!;
|
|
|
|
// =========== Theme Context Consumer ===================================================================================================
|
|
|
|
const { themeBaseClass, themeClass } = useThemeContext()!;
|
|
|
|
// ============== Style =================================================================================================
|
|
|
|
const rootClass = mergeClass(
|
|
containerRecipe({
|
|
boxShadow: true,
|
|
rounded: size == "sm" ? 4 : size == "md" ? 8 : undefined,
|
|
padding: size == "sm" ? 4 : size == "md" ? 8 : undefined,
|
|
background: "base",
|
|
hasFocusOutline: "false",
|
|
}),
|
|
brandRecipe(),
|
|
themeBaseClass,
|
|
themeClass,
|
|
className,
|
|
);
|
|
|
|
const rootStyle: CSSProperties = {
|
|
...floatingStyles,
|
|
...style,
|
|
};
|
|
|
|
return (
|
|
<FloatingList elementsRef={elementsRef}>
|
|
{isOpen && (
|
|
<FloatingPortal>
|
|
<FloatingFocusManager context={context} modal={modal}>
|
|
<div
|
|
{...rest}
|
|
className={rootClass}
|
|
style={rootStyle}
|
|
ref={refs.setFloating}
|
|
{...getFloatingProps()}
|
|
>
|
|
{children}
|
|
</div>
|
|
</FloatingFocusManager>
|
|
</FloatingPortal>
|
|
)}
|
|
</FloatingList>
|
|
);
|
|
};
|