|
| 1 | +'use client'; |
| 2 | +import type * as React from 'react'; |
| 3 | +import { |
| 4 | + MenuPositioner, |
| 5 | + type MenuPositionerProps, |
| 6 | + type MenuPositionerState, |
| 7 | +} from '../../menu/positioner/MenuPositioner'; |
| 8 | +import type { BaseUIComponentProps } from '../../internals/types'; |
| 9 | + |
| 10 | +export interface ContextMenuPositionerState extends MenuPositionerState {} |
| 11 | + |
| 12 | +/** |
| 13 | + * Positions the context menu popup against the pointer or a custom anchor. |
| 14 | + * Renders a `<div>` element. |
| 15 | + * |
| 16 | + * Documentation: [Base UI Context Menu](https://base-ui.com/react/components/context-menu) |
| 17 | + */ |
| 18 | +export const ContextMenuPositioner: React.ForwardRefExoticComponent< |
| 19 | + ContextMenuPositionerProps & React.RefAttributes<HTMLDivElement> |
| 20 | +> = MenuPositioner; |
| 21 | + |
| 22 | +export interface ContextMenuPositionerProps |
| 23 | + extends |
| 24 | + Omit< |
| 25 | + MenuPositionerProps, |
| 26 | + | keyof BaseUIComponentProps<'div', ContextMenuPositionerState> |
| 27 | + | 'anchor' |
| 28 | + | 'positionMethod' |
| 29 | + | 'sideOffset' |
| 30 | + | 'align' |
| 31 | + | 'alignOffset' |
| 32 | + | 'arrowPadding' |
| 33 | + >, |
| 34 | + BaseUIComponentProps<'div', ContextMenuPositionerState> { |
| 35 | + /** |
| 36 | + * An element to position the popup against. |
| 37 | + * By default, root context menus are positioned at the pointer, and submenus are positioned |
| 38 | + * against their trigger. |
| 39 | + */ |
| 40 | + anchor?: MenuPositionerProps['anchor'] | undefined; |
| 41 | + /** |
| 42 | + * Determines which CSS `position` property to use. |
| 43 | + * Context menus always use fixed positioning, so passing `'absolute'` has no effect. |
| 44 | + * @default 'fixed' |
| 45 | + */ |
| 46 | + positionMethod?: MenuPositionerProps['positionMethod'] | undefined; |
| 47 | + /** |
| 48 | + * Distance between the anchor and the popup in pixels. |
| 49 | + * Also accepts a function that returns the distance to read the dimensions of the anchor |
| 50 | + * and positioner elements, along with its side and alignment. |
| 51 | + * |
| 52 | + * The function takes a `data` object parameter with the following properties: |
| 53 | + * - `data.anchor`: the dimensions of the anchor element with properties `width` and `height`. |
| 54 | + * - `data.positioner`: the dimensions of the positioner element with properties `width` and `height`. |
| 55 | + * - `data.side`: which side of the anchor element the positioner is aligned against. |
| 56 | + * - `data.align`: how the positioner is aligned relative to the specified side. |
| 57 | + * |
| 58 | + * Defaults to `-5` for root context menus when `side` is not specified and `align` is not |
| 59 | + * `'center'`. Otherwise, it defaults to `0`. |
| 60 | + * |
| 61 | + * @example |
| 62 | + * ```jsx |
| 63 | + * <ContextMenu.Positioner |
| 64 | + * sideOffset={({ side, anchor }) => { |
| 65 | + * return side === 'top' || side === 'bottom' ? anchor.height : anchor.width; |
| 66 | + * }} |
| 67 | + * /> |
| 68 | + * ``` |
| 69 | + */ |
| 70 | + sideOffset?: MenuPositionerProps['sideOffset'] | undefined; |
| 71 | + /** |
| 72 | + * How to align the popup relative to the specified side. |
| 73 | + * @default 'start' |
| 74 | + */ |
| 75 | + align?: MenuPositionerProps['align'] | undefined; |
| 76 | + /** |
| 77 | + * Additional offset along the alignment axis in pixels. |
| 78 | + * Also accepts a function that returns the offset to read the dimensions of the anchor |
| 79 | + * and positioner elements, along with its side and alignment. |
| 80 | + * |
| 81 | + * The function takes a `data` object parameter with the following properties: |
| 82 | + * - `data.anchor`: the dimensions of the anchor element with properties `width` and `height`. |
| 83 | + * - `data.positioner`: the dimensions of the positioner element with properties `width` and `height`. |
| 84 | + * - `data.side`: which side of the anchor element the positioner is aligned against. |
| 85 | + * - `data.align`: how the positioner is aligned relative to the specified side. |
| 86 | + * |
| 87 | + * Defaults to `2` for root context menus when `side` is not specified and `align` is not |
| 88 | + * `'center'`. Otherwise, it defaults to `0`. |
| 89 | + * |
| 90 | + * @example |
| 91 | + * ```jsx |
| 92 | + * <ContextMenu.Positioner |
| 93 | + * alignOffset={({ side, anchor }) => { |
| 94 | + * return side === 'top' || side === 'bottom' ? anchor.width : anchor.height; |
| 95 | + * }} |
| 96 | + * /> |
| 97 | + * ``` |
| 98 | + */ |
| 99 | + alignOffset?: MenuPositionerProps['alignOffset'] | undefined; |
| 100 | + /** |
| 101 | + * Minimum distance to maintain between the arrow and the edges of the popup. |
| 102 | + * |
| 103 | + * Root context menus always use `0`. Submenus default to `5`. |
| 104 | + */ |
| 105 | + arrowPadding?: MenuPositionerProps['arrowPadding'] | undefined; |
| 106 | +} |
| 107 | + |
| 108 | +export namespace ContextMenuPositioner { |
| 109 | + export type Props = ContextMenuPositionerProps; |
| 110 | + export type State = ContextMenuPositionerState; |
| 111 | +} |
0 commit comments