@base-ui-components/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.
119 lines (117 loc) • 3.82 kB
JavaScript
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { useFloatingTree } from '@floating-ui/react';
import { useMenuRootContext } from '../root/MenuRootContext.js';
import { useBaseUiId } from '../../utils/useBaseUiId.js';
import { useComponentRenderer } from '../../utils/useComponentRenderer.js';
import { useMenuSubmenuTrigger } from './useMenuSubmenuTrigger.js';
import { useForkRef } from '../../utils/useForkRef.js';
import { triggerOpenStateMapping } from '../../utils/popupStateMapping.js';
import { useCompositeListItem } from '../../composite/list/useCompositeListItem.js';
/**
* A menu item that opens a submenu.
* Renders a `<div>` element.
*
* Documentation: [Base UI Menu](https://base-ui.com/react/components/menu)
*/
const MenuSubmenuTrigger = /*#__PURE__*/React.forwardRef(function SubmenuTriggerComponent(props, forwardedRef) {
const {
render,
className,
disabled = false,
label,
id: idProp,
...other
} = props;
const id = useBaseUiId(idProp);
const {
getTriggerProps,
parentContext,
setTriggerElement,
allowMouseUpTriggerRef,
open,
typingRef
} = useMenuRootContext();
if (parentContext === undefined) {
throw new Error('Base UI: ItemTrigger must be placed in a nested Menu.');
}
const {
activeIndex,
getItemProps
} = parentContext;
const item = useCompositeListItem();
const highlighted = activeIndex === item.index;
const mergedRef = useForkRef(forwardedRef, item.ref);
const {
events: menuEvents
} = useFloatingTree();
const {
getRootProps
} = useMenuSubmenuTrigger({
id,
highlighted,
ref: mergedRef,
disabled,
menuEvents,
setTriggerElement,
allowMouseUpTriggerRef,
typingRef
});
const state = React.useMemo(() => ({
disabled,
highlighted,
open
}), [disabled, highlighted, open]);
const {
renderElement
} = useComponentRenderer({
render: render || 'div',
className,
state,
propGetter: externalProps => getTriggerProps(getItemProps(getRootProps(externalProps))),
customStyleHookMapping: triggerOpenStateMapping,
extraProps: other
});
return renderElement();
});
process.env.NODE_ENV !== "production" ? MenuSubmenuTrigger.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* @ignore
*/
children: PropTypes.node,
/**
* CSS class applied to the element, or a function that
* returns a class based on the component’s state.
*/
className: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
/**
* Whether the component should ignore user interaction.
* @default false
*/
disabled: PropTypes.bool,
/**
* @ignore
*/
id: PropTypes.string,
/**
* Overrides the text label to use when the item is matched during keyboard text navigation.
*/
label: PropTypes.string,
/**
* @ignore
*/
onClick: PropTypes.func,
/**
* Allows you to replace the component’s HTML element
* with a different tag, or compose it with another component.
*
* Accepts a `ReactElement` or a function that returns the element to render.
*/
render: PropTypes.oneOfType([PropTypes.element, PropTypes.func])
} : void 0;
export { MenuSubmenuTrigger };