From e3bd64efc73c420493c742122e6740ad3c3f8f81 Mon Sep 17 00:00:00 2001 From: Ian Sanders Date: Wed, 6 Sep 2023 14:13:42 +0000 Subject: [PATCH 1/2] Deduplicate `ActionMenu.tsx` --- src/ActionMenu.tsx | 138 ---------------------------------- src/ActionMenu/ActionMenu.tsx | 9 ++- 2 files changed, 7 insertions(+), 140 deletions(-) delete mode 100644 src/ActionMenu.tsx diff --git a/src/ActionMenu.tsx b/src/ActionMenu.tsx deleted file mode 100644 index f2494987c16..00000000000 --- a/src/ActionMenu.tsx +++ /dev/null @@ -1,138 +0,0 @@ -import React from 'react' -import {TriangleDownIcon} from '@primer/octicons-react' -import {AnchoredOverlay, AnchoredOverlayProps} from './AnchoredOverlay' -import {OverlayProps} from './Overlay' -import {useProvidedRefOrCreate, useProvidedStateOrCreate, useMenuKeyboardNavigation} from './hooks' -import {Divider} from './ActionList/Divider' -import {ActionListContainerContext} from './ActionList/ActionListContainerContext' -import {Button, ButtonProps} from './Button' -import {useId} from './hooks/useId' -import {MandateProps} from './utils/types' -import {ForwardRefComponent as PolymorphicForwardRefComponent} from './utils/polymorphic' - -export type MenuContextProps = Pick< - AnchoredOverlayProps, - 'anchorRef' | 'renderAnchor' | 'open' | 'onOpen' | 'anchorId' -> & { - onClose?: (gesture: 'anchor-click' | 'click-outside' | 'escape' | 'tab') => void -} -const MenuContext = React.createContext({renderAnchor: null, open: false}) - -export type ActionMenuProps = { - /** - * Recommended: `ActionMenu.Button` or `ActionMenu.Anchor` with `ActionMenu.Overlay` - */ - children: React.ReactElement[] | React.ReactElement - - /** - * If defined, will control the open/closed state of the overlay. Must be used in conjunction with `onOpenChange`. - */ - open?: boolean - - /** - * If defined, will control the open/closed state of the overlay. Must be used in conjunction with `open`. - */ - onOpenChange?: (s: boolean) => void -} & Pick - -const Menu: React.FC> = ({ - anchorRef: externalAnchorRef, - open, - onOpenChange, - children, -}: ActionMenuProps) => { - const [combinedOpenState, setCombinedOpenState] = useProvidedStateOrCreate(open, onOpenChange, false) - const onOpen = React.useCallback(() => setCombinedOpenState(true), [setCombinedOpenState]) - const onClose = React.useCallback(() => setCombinedOpenState(false), [setCombinedOpenState]) - - const anchorRef = useProvidedRefOrCreate(externalAnchorRef) - const anchorId = useId() - let renderAnchor: AnchoredOverlayProps['renderAnchor'] = null - - // 🚨 Hack for good API! - // we strip out Anchor from children and pass it to AnchoredOverlay to render - // with additional props for accessibility - const contents = React.Children.map(children, child => { - if (child.type === MenuButton || child.type === Anchor) { - renderAnchor = anchorProps => React.cloneElement(child, anchorProps) - return null - } - return child - }) - - return ( - - {contents} - - ) -} - -export type ActionMenuAnchorProps = {children: React.ReactElement} -const Anchor = React.forwardRef(({children, ...anchorProps}, anchorRef) => { - return React.cloneElement(children, {...anchorProps, ref: anchorRef}) -}) - -/** this component is syntactical sugar 🍭 */ -export type ActionMenuButtonProps = ButtonProps -const MenuButton = React.forwardRef(({...props}, anchorRef) => { - return ( - -