@base-ui/react
Version:
Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.
55 lines (53 loc) • 1.8 kB
JavaScript
'use client';
import * as React from 'react';
import { useMenuRootContext } from "../root/MenuRootContext.js";
import { useRenderElement } from "../../utils/useRenderElement.js";
import { useBaseUiId } from "../../utils/useBaseUiId.js";
import { useCompositeListItem } from "../../composite/list/useCompositeListItem.js";
import { useMenuPositionerContext } from "../positioner/MenuPositionerContext.js";
import { useMenuItemCommonProps } from "../item/useMenuItemCommonProps.js";
/**
* A link in the menu that can be used to navigate to a different page or section.
* Renders an `<a>` element.
*
* Documentation: [Base UI Menu](https://base-ui.com/react/components/menu)
*/
export const MenuLinkItem = /*#__PURE__*/React.forwardRef(function MenuLinkItem(componentProps, forwardedRef) {
const {
render,
className,
id: idProp,
label,
closeOnClick = false,
...elementProps
} = componentProps;
const linkRef = React.useRef(null);
const listItem = useCompositeListItem({
label
});
const menuPositionerContext = useMenuPositionerContext(true);
const nodeId = menuPositionerContext?.nodeId;
const id = useBaseUiId(idProp);
const {
store
} = useMenuRootContext();
const highlighted = store.useState('isActive', listItem.index);
const itemProps = store.useState('itemProps');
const commonProps = useMenuItemCommonProps({
closeOnClick,
highlighted,
id,
nodeId,
store,
itemRef: linkRef
});
const state = React.useMemo(() => ({
highlighted
}), [highlighted]);
return useRenderElement('a', componentProps, {
state,
props: [itemProps, elementProps, commonProps],
ref: [linkRef, forwardedRef, listItem.ref]
});
});
if (process.env.NODE_ENV !== "production") MenuLinkItem.displayName = "MenuLinkItem";