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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat direct control navbar-menu #2547

Open
wants to merge 1 commit into
base: canary
Choose a base branch
from
Open
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
31 changes: 25 additions & 6 deletions packages/components/navbar/src/navbar-menu.tsx
Expand Up @@ -21,32 +21,51 @@ export interface NavbarMenuProps extends HTMLNextUIProps<"ul"> {
* The props to modify the framer motion animation. Use the `variants` API to create your own animation.
*/
motionProps?: HTMLMotionProps<"ul">;
/**
* The prop to control the open state of the menu.
*/
isOpen?: boolean;
}

const NavbarMenu = forwardRef<"ul", NavbarMenuProps>((props, ref) => {
const {className, children, portalContainer, motionProps, style, ...otherProps} = props;
const {
className,
children,
portalContainer,
motionProps,
style,
isOpen,
...otherProps
} = props;
const domRef = useDOMRef(ref);

const {slots, isMenuOpen, height, disableAnimation, classNames} = useNavbarContext();
const navBarContext = useNavbarContext();
const { slots, height, disableAnimation, classNames } = navBarContext;
let isMenuOpen: boolean | undefined;
if (isOpen == undefined) {
isMenuOpen = navBarContext.isMenuOpen;
} else {
isMenuOpen = isOpen;
}

const styles = clsx(classNames?.menu, className);

const MenuWrapper = useCallback(
({children}: {children: ReactElement}) => {
({ children }: { children: ReactElement }) => {
return (
<RemoveScroll forwardProps enabled={isMenuOpen} removeScrollBar={false}>
{children}
</RemoveScroll>
);
},
[isMenuOpen],
[isMenuOpen]
);

const contents = disableAnimation ? (
<MenuWrapper>
<ul
ref={domRef}
className={slots.menu?.({class: styles})}
className={slots.menu?.({ class: styles })}
data-open={dataAttr(isMenuOpen)}
style={{
// @ts-expect-error
Expand All @@ -66,7 +85,7 @@ const NavbarMenu = forwardRef<"ul", NavbarMenuProps>((props, ref) => {
ref={domRef}
layoutScroll
animate="enter"
className={slots.menu?.({class: styles})}
className={slots.menu?.({ class: styles })}
data-open={dataAttr(isMenuOpen)}
exit="exit"
initial="exit"
Expand Down