Repository navigation
feat(popover-menu): add reveal animation #8305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,12 +31,12 @@ import combineRefs from "../utils/helpers/combine-refs"; | |
| import { TableBodyContext } from "../../components/table/__internal__/contexts"; | ||
| import { nextElementToFocus } from "../../hooks/__internal__/useFocusPortalContent"; | ||
| import { getWindow } from "../dom/globals"; | ||
| import useMediaQuery from "../../hooks/useMediaQuery"; | ||
| import useResizeObserver from "../../hooks/__internal__/useResizeObserver"; | ||
|
|
||
| const useIsomorphicLayoutEffect = getWindow() ? useLayoutEffect : useEffect; | ||
|
|
||
| const PopoverControlWrapper = styled.div<{ | ||
| $controlWrapperStyle?: CSSObject; | ||
| }>` | ||
| const PopoverControlWrapper = styled.div<{ $controlWrapperStyle?: CSSObject }>` | ||
| display: inline-block; | ||
| ${({ $controlWrapperStyle }) => $controlWrapperStyle} | ||
| `; | ||
|
|
@@ -79,18 +79,67 @@ const horizontalPaddingSize = { | |
| large: "var(--global-space-comp-s)", | ||
| }; | ||
|
|
||
| const MenuWrapper = styled.div<ListProps>` | ||
| box-sizing: border-box; | ||
| interface MenuWrapperProps extends Pick<ListProps, "$size" | "$isButtonMenu"> { | ||
| $contentHeight?: number; | ||
| } | ||
|
|
||
| // set full height on outer wrapper - prevents menu from flipping during reveal animation | ||
| const MenuWrapper = styled.div<MenuWrapperProps>` | ||
| width: max-content; | ||
| z-index: calc(var(--carbon-zindex-overlay) + 2); | ||
|
|
||
| ${({ $isButtonMenu }) => | ||
| !$isButtonMenu && | ||
| css` | ||
| max-width: 100%; | ||
| max-height: 100%; | ||
| `} | ||
|
|
||
| ${({ $contentHeight, $size }) => | ||
| $contentHeight !== undefined && | ||
| css` | ||
| // MenuContent is content-box, so include top and bottom padding | ||
| height: calc(${$contentHeight}px + 2 * ${verticalPaddingSize[$size]}); | ||
| `} | ||
| `; | ||
|
|
||
| interface MenuContentProps extends Pick<ListProps, "$size" | "$isButtonMenu"> { | ||
| $contentHeight?: number; | ||
| $isRevealing?: boolean; | ||
| } | ||
|
|
||
| const REVEAL_DURATION = 200; | ||
| const revealEasing = `${REVEAL_DURATION}ms cubic-bezier(0.22, 0.61, 0.36, 1)`; | ||
|
|
||
| const MenuContent = styled.div<MenuContentProps>` | ||
| box-shadow: var(--global-depth-lvl1); | ||
| border-radius: var(--global-radius-container-m); | ||
| background-color: var(--popover-bg-default); | ||
| width: max-content; | ||
| position: relative; | ||
| z-index: calc(var(--carbon-zindex-overlay) + 2); | ||
|
|
||
| padding: ${({ $size }) => css` | ||
| ${verticalPaddingSize[$size]} ${horizontalPaddingSize[$size]} | ||
| `}; | ||
| ${({ $size }) => css` | ||
| padding: ${verticalPaddingSize[$size]} ${horizontalPaddingSize[$size]}; | ||
| `} | ||
|
|
||
| ${({ $isRevealing, $contentHeight }) => | ||
| $isRevealing && | ||
| css` | ||
| box-sizing: content-box; | ||
| overflow: clip; | ||
| transition: | ||
| height ${revealEasing}, | ||
| padding-top ${revealEasing}, | ||
| padding-bottom ${revealEasing}; | ||
|
Comment on lines
+129
to
+132
|
||
|
|
||
| ${$contentHeight === undefined | ||
| ? css` | ||
| height: 0; | ||
| padding: var(--global-space-none); | ||
| ` | ||
| : css` | ||
| height: ${$contentHeight}px; | ||
| `} | ||
| `} | ||
|
|
||
| ${({ $isButtonMenu }) => | ||
| !$isButtonMenu && | ||
|
|
@@ -214,7 +263,7 @@ export const getSubmenuOffset = ({ | |
| elements: { reference: unknown }; | ||
| }) => { | ||
| const parentMenu = (elements.reference as Element).closest( | ||
| '[data-role="menu-wrapper"]', | ||
| '[data-role="menu-content"]', | ||
| ); | ||
|
|
||
| if (!parentMenu) return 0; | ||
|
|
@@ -266,12 +315,14 @@ interface MenuProps { | |
| referenceElement: HTMLElement | null; | ||
| size: PopoverMenuContextProps["size"]; | ||
| listRef: React.Ref<HTMLUListElement>; | ||
| internalListRef: React.RefObject<HTMLUListElement>; | ||
| listboxAriaLabelledBy?: string; | ||
| isButtonMenu?: boolean; | ||
| children: React.ReactNode; | ||
| onKeyDown: (ev: React.KeyboardEvent<HTMLElement>) => void; | ||
| middleware: PopoverProps["middleware"]; | ||
| scrollRef: React.RefObject<HTMLDivElement>; | ||
| menuWrapperRef: React.MutableRefObject<HTMLDivElement | null>; | ||
| menuTreeId: string; | ||
| listId: string; | ||
| disablePortal?: boolean; | ||
|
|
@@ -290,13 +341,15 @@ const Menu = ({ | |
| referenceElement, | ||
| size, | ||
| listRef, | ||
| internalListRef, | ||
| listboxAriaLabelledBy, | ||
| listboxAriaLabel, | ||
| isButtonMenu, | ||
| children, | ||
| onKeyDown, | ||
| middleware, | ||
| scrollRef, | ||
| menuWrapperRef, | ||
| menuTreeId, | ||
| listId, | ||
| disablePortal, | ||
|
|
@@ -307,6 +360,36 @@ const Menu = ({ | |
| menuWrapperDataElement, | ||
| listTabIndex, | ||
| }: MenuProps) => { | ||
| const [contentHeight, setContentHeight] = useState<number>(); | ||
| const [isRevealing, setIsRevealing] = useState(true); | ||
|
|
||
| const allowMotion = useMediaQuery( | ||
| "screen and (prefers-reduced-motion: no-preference)", | ||
| ); | ||
| const isRevealingActive = allowMotion && isRevealing; | ||
|
|
||
| const updateContentHeight = useCallback(() => { | ||
| setContentHeight( | ||
| (internalListRef.current as HTMLUListElement).offsetHeight, | ||
| ); | ||
| }, [internalListRef]); | ||
|
|
||
| useResizeObserver(internalListRef, updateContentHeight, !isRevealingActive); | ||
|
|
||
| const hasMeasuredContent = contentHeight !== undefined; | ||
|
|
||
| // starts on the first measurement only, so later resizes cannot extend the reveal | ||
| useEffect(() => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment: The reveal should run once when the menu opens, with a fixed completion time. Depending on |
||
| if (!isRevealingActive || !hasMeasuredContent) return undefined; | ||
|
|
||
| const timeoutId = setTimeout( | ||
| () => setIsRevealing(false), | ||
| REVEAL_DURATION + 50, | ||
| ); | ||
|
|
||
| return () => clearTimeout(timeoutId); | ||
| }, [isRevealingActive, hasMeasuredContent]); | ||
|
|
||
| return ( | ||
| <Popover | ||
| placement={placement} | ||
|
|
@@ -320,33 +403,43 @@ const Menu = ({ | |
| popoverStrategy={popoverStrategy} | ||
| > | ||
| <MenuWrapper | ||
| ref={menuWrapperRef} | ||
| $contentHeight={isRevealingActive ? contentHeight : undefined} | ||
| $size={size} | ||
| $isButtonMenu={isButtonMenu} | ||
| onKeyDown={onKeyDown} | ||
| data-role="menu-wrapper" | ||
| data-element={menuWrapperDataElement} | ||
| onMouseDown={(e) => e.preventDefault()} | ||
| $isButtonMenu={isButtonMenu} | ||
| > | ||
| <ScrollWrapper | ||
| ref={scrollRef} | ||
| data-component="scroll-wrapper" | ||
| data-menu-tree-id={menuTreeId} | ||
| <MenuContent | ||
| $size={size} | ||
| $isRevealing={isRevealingActive} | ||
| $contentHeight={contentHeight} | ||
| $isButtonMenu={isButtonMenu} | ||
| data-role="menu-content" | ||
| > | ||
| <List | ||
| $size={size} | ||
| ref={listRef} | ||
| role={isButtonMenu ? "list" : "listbox"} | ||
| id={listId} | ||
| aria-labelledby={listboxAriaLabelledBy} | ||
| $isButtonMenu={isButtonMenu} | ||
| aria-label={listboxAriaLabel} | ||
| $maxHeight={maxHeight} | ||
| style={listStyle} | ||
| tabIndex={listTabIndex} | ||
| <ScrollWrapper | ||
| ref={scrollRef} | ||
| data-component="scroll-wrapper" | ||
| data-menu-tree-id={menuTreeId} | ||
| > | ||
| {children} | ||
| </List> | ||
| </ScrollWrapper> | ||
| <List | ||
| $size={size} | ||
| ref={listRef} | ||
| role={isButtonMenu ? "list" : "listbox"} | ||
| id={listId} | ||
| aria-labelledby={listboxAriaLabelledBy} | ||
| $isButtonMenu={isButtonMenu} | ||
| aria-label={listboxAriaLabel} | ||
| $maxHeight={maxHeight} | ||
| style={listStyle} | ||
| tabIndex={listTabIndex} | ||
| > | ||
| {children} | ||
| </List> | ||
| </ScrollWrapper> | ||
| </MenuContent> | ||
| </MenuWrapper> | ||
| </Popover> | ||
| ); | ||
|
|
@@ -457,6 +550,7 @@ const PopoverMenuInner = <TRef extends FocusableHandle = HTMLElement>( | |
| const popoverReference = | ||
| controlElement === undefined ? controlWrapperElement : controlElement; | ||
| const scrollRef = useRef<HTMLDivElement | null>(null); | ||
| const menuWrapperRef = useRef<HTMLDivElement | null>(null); | ||
| const internalListRef = useRef<HTMLUListElement | null>(null); | ||
| const combinedListRef = useMemo( | ||
| () => combineRefs(listRef, internalListRef), | ||
|
|
@@ -750,10 +844,10 @@ const PopoverMenuInner = <TRef extends FocusableHandle = HTMLElement>( | |
| const el = isSubmenu ? scrollRef.current : wrapperRef.current; | ||
| // Portalled root menus do not bubble native events through the control wrapper. | ||
| const portalledMenuEl = | ||
| !isSubmenu && !disablePortal ? scrollRef.current : null; | ||
| !isSubmenu && !disablePortal ? menuWrapperRef.current : null; | ||
| const clickEl = clickAwayWithinReference ? popoverReference : el; | ||
| const menuEl = clickAwayWithinReference | ||
| ? scrollRef.current?.parentElement | ||
| ? menuWrapperRef.current | ||
| : portalledMenuEl; | ||
|
|
||
| clickEl?.addEventListener("click", handleClickInside, { passive: true }); | ||
|
|
@@ -874,7 +968,7 @@ const PopoverMenuInner = <TRef extends FocusableHandle = HTMLElement>( | |
| if ( | ||
| ev?.type === "click" && | ||
| ev.target instanceof Element && | ||
| scrollRef.current?.parentElement?.contains(ev.target) && | ||
| menuWrapperRef.current?.contains(ev.target) && | ||
| !ev.target.closest("button, a[href], input, select, textarea") | ||
| ) { | ||
| props.triggerElement | ||
|
|
@@ -956,12 +1050,14 @@ const PopoverMenuInner = <TRef extends FocusableHandle = HTMLElement>( | |
| referenceElement={popoverReference} | ||
| size={size} | ||
| listRef={combinedListRef} | ||
| internalListRef={internalListRef} | ||
| listboxAriaLabelledBy={listboxAriaLabelledBy} | ||
| listboxAriaLabel={listboxAriaLabel} | ||
| isButtonMenu={isButtonMenu} | ||
| onKeyDown={handleMenuKeyDown} | ||
| middleware={computedMiddleware} | ||
| scrollRef={scrollRef} | ||
| menuWrapperRef={menuWrapperRef} | ||
| menuTreeId={menuTreeId} | ||
| listId={listId.current} | ||
| disablePortal={disablePortal && !isSubmenu} | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.