Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion playwright/components/action-popover/locators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export const ACTION_POPOVER_BUTTON = '[data-element="action-popover-button"]';
export const ACTION_POPOVER_DATA_COMPONENT = '[data-role="menu-wrapper"]';
export const ACTION_POPOVER_SUBMENU =
'[data-role="menu-wrapper"]:has(> [data-component="scroll-wrapper"] > ul > [data-component="popover-submenu-item"])';
'[data-role="menu-wrapper"]:has(> [data-role="menu-content"] > [data-component="scroll-wrapper"] > ul > [data-component="popover-submenu-item"])';
export const ACTION_POPOVER_MENU_ITEM_ICON =
'[data-element="action-popover-menu-item-icon"]';
export const ACTION_POPOVER_MENU_ITEM_INNER_TEXT =
Expand Down
162 changes: 129 additions & 33 deletions src/__internal__/popover-menu/popover-menu.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}
`;
Expand Down Expand Up @@ -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;
Comment thread
nuria1110 marked this conversation as resolved.
Resolved
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 &&
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -290,13 +341,15 @@ const Menu = ({
referenceElement,
size,
listRef,
internalListRef,
listboxAriaLabelledBy,
listboxAriaLabel,
isButtonMenu,
children,
onKeyDown,
middleware,
scrollRef,
menuWrapperRef,
menuTreeId,
listId,
disablePortal,
Expand All @@ -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(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 contentHeight here restarts the timeout on each resize and can extend the reveal beyond that window. Could we start the timer on the first measurement and avoid resetting it for subsequent height changes?

const hasMeasuredContent = contentHeight !== undefined;

useEffect(() => {
  if (!isRevealingActive || !hasMeasuredContent) return undefined;

  const timeoutId = setTimeout(
    () => setIsRevealing(false),
    REVEAL_DURATION + 50,
  );

  return () => clearTimeout(timeoutId);
}, [isRevealingActive, hasMeasuredContent]);

if (!isRevealingActive || !hasMeasuredContent) return undefined;

const timeoutId = setTimeout(
() => setIsRevealing(false),
REVEAL_DURATION + 50,
);

return () => clearTimeout(timeoutId);
}, [isRevealingActive, hasMeasuredContent]);

return (
<Popover
placement={placement}
Expand All @@ -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>
);
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
Loading
Loading