@mantine/core
Version:
React components library focused on usability, accessibility and developer experience
1 lines • 7.75 kB
Source Map (JSON)
{"version":3,"file":"MenuItem.cjs","names":["polymorphicFactory","useProps","useMenuContext","SubMenuContext","useMantineTheme","useDirection","createEventHandler","parseThemeColor","UnstyledButton","createScopedKeydownHandler","classes"],"sources":["../../../../src/components/Menu/MenuItem/MenuItem.tsx"],"sourcesContent":["import { use, useRef } from 'react';\nimport { useMergedRef } from '@mantine/hooks';\nimport {\n BoxProps,\n CompoundStylesApiProps,\n createEventHandler,\n createScopedKeydownHandler,\n MantineColor,\n parseThemeColor,\n polymorphicFactory,\n PolymorphicFactory,\n useDirection,\n useMantineTheme,\n useProps,\n} from '../../../core';\nimport { UnstyledButton } from '../../UnstyledButton';\nimport { useMenuContext } from '../Menu.context';\nimport { SubMenuContext } from '../MenuSub/MenuSub.context';\nimport classes from '../Menu.module.css';\n\nexport type MenuItemStylesNames = 'item' | 'itemLabel' | 'itemSection' | 'itemIndicator';\n\nexport interface MenuItemProps extends BoxProps, CompoundStylesApiProps<MenuItemFactory> {\n 'data-disabled'?: boolean;\n\n /** Item label */\n children?: React.ReactNode;\n\n /** Key of `theme.colors` or any valid CSS color */\n color?: MantineColor;\n\n /** Controls whether the menu closes when this item is clicked. When undefined, inherits from Menu's `closeOnItemClick` prop. When true or false, overrides the Menu-level setting */\n closeMenuOnClick?: boolean;\n\n /** Section displayed at the start of the label */\n leftSection?: React.ReactNode;\n\n /** Section displayed at the end of the label */\n rightSection?: React.ReactNode;\n\n /** Sets disabled attribute, applies disabled styles */\n disabled?: boolean;\n}\n\nexport type MenuItemFactory = PolymorphicFactory<{\n props: MenuItemProps;\n defaultRef: HTMLButtonElement;\n defaultComponent: 'button';\n stylesNames: MenuItemStylesNames;\n compound: true;\n}>;\n\nexport const MenuItem = polymorphicFactory<MenuItemFactory>((props) => {\n const {\n classNames,\n className,\n style,\n styles,\n vars,\n color,\n closeMenuOnClick,\n leftSection,\n rightSection,\n children,\n disabled,\n 'data-disabled': dataDisabled,\n ref,\n ...others\n } = useProps('MenuItem', null, props);\n\n const ctx = useMenuContext();\n const subCtx = use(SubMenuContext);\n const theme = useMantineTheme();\n const { dir } = useDirection();\n const itemRef = useRef<HTMLButtonElement>(null);\n const _others: any = others;\n\n const handleClick = createEventHandler(_others.onClick, () => {\n if (dataDisabled) {\n return;\n }\n if (typeof closeMenuOnClick === 'boolean') {\n closeMenuOnClick && ctx.closeDropdownImmediately();\n } else {\n ctx.closeOnItemClick && ctx.closeDropdownImmediately();\n }\n });\n\n const handleMouseMove = createEventHandler<any>(_others.onMouseMove, () => {\n if (!ctx.hasSearch) {\n return;\n }\n const dropdown = itemRef.current?.closest('[data-menu-dropdown]');\n if (!dropdown) {\n return;\n }\n dropdown.querySelectorAll<HTMLElement>('[data-menu-active]').forEach((node) => {\n if (node !== itemRef.current && node.closest('[data-menu-dropdown]') === dropdown) {\n node.removeAttribute('data-menu-active');\n }\n });\n });\n\n const colors = color ? theme.variantColorResolver({ color, theme, variant: 'light' }) : undefined;\n const parsedThemeColor = color ? parseThemeColor({ color, theme }) : null;\n\n const handleKeydown = createEventHandler<any>(_others.onKeyDown, (event) => {\n if (event.key === 'ArrowLeft' && subCtx) {\n subCtx.close();\n subCtx.focusParentItem();\n }\n });\n\n return (\n <UnstyledButton\n onMouseDown={(event) => event.preventDefault()}\n {...others}\n unstyled={ctx.unstyled}\n tabIndex={ctx.menuItemTabIndex}\n {...ctx.getStyles('item', { className, style, styles, classNames })}\n ref={useMergedRef(itemRef, ref)}\n role=\"menuitem\"\n disabled={disabled}\n data-menu-item\n data-disabled={disabled || dataDisabled || undefined}\n data-mantine-stop-propagation\n onClick={handleClick}\n onMouseMove={handleMouseMove}\n onKeyDown={createScopedKeydownHandler({\n siblingSelector: '[data-menu-item]:not([data-disabled])',\n parentSelector: '[data-menu-dropdown]',\n activateOnFocus: false,\n loop: ctx.loop,\n dir,\n orientation: 'vertical',\n onKeyDown: handleKeydown,\n })}\n __vars={{\n '--menu-item-color':\n parsedThemeColor?.isThemeColor && parsedThemeColor?.shade === undefined\n ? `var(--mantine-color-${parsedThemeColor.color}-6)`\n : colors?.color,\n '--menu-item-hover': colors?.hover,\n }}\n >\n {ctx.alignItemsLabels === 'all' && (\n <div {...ctx.getStyles('itemIndicator', { styles, classNames })} data-placeholder />\n )}\n {leftSection && (\n <div {...ctx.getStyles('itemSection', { styles, classNames })} data-position=\"left\">\n {leftSection}\n </div>\n )}\n {children && (\n <div {...ctx.getStyles('itemLabel', { styles, classNames })} data-menu-item-label>\n {children}\n </div>\n )}\n {rightSection && (\n <div {...ctx.getStyles('itemSection', { styles, classNames })} data-position=\"right\">\n {rightSection}\n </div>\n )}\n </UnstyledButton>\n );\n});\n\nMenuItem.classes = classes;\nMenuItem.displayName = '@mantine/core/MenuItem';\n"],"mappings":";;;;;;;;;;;;;;;;AAoDA,MAAa,WAAWA,4BAAAA,oBAAqC,UAAU;CACrE,MAAM,EACJ,YACA,WACA,OACA,QACA,MACA,OACA,kBACA,aACA,cACA,UACA,UACA,iBAAiB,cACjB,KACA,GAAG,WACDC,kBAAAA,SAAS,YAAY,MAAM,KAAK;CAEpC,MAAM,MAAMC,qBAAAA,eAAe;CAC3B,MAAM,UAAA,GAAA,MAAA,IAAA,CAAaC,wBAAAA,cAAc;CACjC,MAAM,QAAQC,6BAAAA,gBAAgB;CAC9B,MAAM,EAAE,QAAQC,0BAAAA,aAAa;CAC7B,MAAM,WAAA,GAAA,MAAA,OAAA,CAAoC,IAAI;CAC9C,MAAM,UAAe;CAErB,MAAM,cAAcC,6BAAAA,mBAAmB,QAAQ,eAAe;EAC5D,IAAI,cACF;EAEF,IAAI,OAAO,qBAAqB,WAC9B,oBAAoB,IAAI,yBAAyB;OAEjD,IAAI,oBAAoB,IAAI,yBAAyB;CAEzD,CAAC;CAED,MAAM,kBAAkBA,6BAAAA,mBAAwB,QAAQ,mBAAmB;EACzE,IAAI,CAAC,IAAI,WACP;EAEF,MAAM,WAAW,QAAQ,SAAS,Q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