Skip to content
Open
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ export const popup = componentStyle({
borderRadius: vars.size.borderRadius[300],
boxShadow: vars.shadow.md,

paddingBlock: vars.size.space[150],
paddingInline: vars.size.space[200],

Comment on lines +133 to +135

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other popup elements with padding were in the popup container, but since the navigation-menu was in the content container, I moved it to the popup container to maintain consistency.

backgroundColor: vars.color.background.overlay[100],

transformOrigin: 'var(--transform-origin)',
Expand All @@ -156,9 +159,6 @@ export const content = componentStyle({
width: '100%',
height: '100%',

paddingBlock: vars.size.space[150],
paddingInline: vars.size.space[200],

whiteSpace: 'nowrap',
transition: `opacity calc(${durationVar} * 0.5) ease, transform ${durationVar} ${easingVar}`,

Expand Down Expand Up @@ -195,23 +195,6 @@ export const arrow = componentStyle({
transition: `left ${durationVar} ${easingVar}`,

selectors: {
'&[data-side="top"]': {
bottom: '-8px',
transform: 'rotate(-180deg)',
},
'&[data-side="right"]': {
left: '-12px',
transform: 'rotate(-90deg)',
},
'&[data-side="bottom"]': {
top: '-8px',
transform: 'rotate(0deg)',
},
'&[data-side="left"]': {
right: '-12px',
transform: 'rotate(90deg)',
},

Comment on lines -198 to -214

@MaxLee-dev MaxLee-dev Apr 1, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have moved the logic responsible for the arrow’s position and rotation to within use-arrow-position to align with the data-side.
The reasons are as follows:

  • Layout must be determined by taking into account the positions of the positioner, arrow and trigger. As there are a total of three components (popover, tooltip and navigation-menu), we have removed the styles to ensure consistent layout across all components.

'&[data-starting-style], &[data-ending-style]': {
opacity: 0,
},
Expand Down
149 changes: 97 additions & 52 deletions packages/core/src/components/navigation-menu/navigation-menu.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
'use client';

import type { CSSProperties, ComponentPropsWithoutRef, ReactElement } from 'react';
import { forwardRef, useEffect, useMemo, useRef, useState } from 'react';
import type { ComponentProps, ReactElement, RefObject } from 'react';
import { forwardRef, useEffect, useRef, useState } from 'react';

import { NavigationMenu as BaseNavigationMenu } from '@base-ui/react/navigation-menu';
import { ChevronDownOutlineIcon } from '@vapor-ui/icons';

import { useArrowPosition } from '~/hooks/use-arrow-position';
import { useMutationObserverRef } from '~/hooks/use-mutation-observer-ref';
import { useRenderElement } from '~/hooks/use-render-element';
import { useVaporId } from '~/hooks/use-vapor-id';
import { createContext } from '~/libs/create-context';
import { vars } from '~/styles/themes.css';
import { cn } from '~/utils/cn';
Expand All @@ -21,6 +23,24 @@ import type { VaporUIComponentProps } from '~/utils/types';
import type { LinkVariants, ListVariants } from './navigation-menu.css';
import * as styles from './navigation-menu.css';

/* -------------------------------------------------------------------------------------------------
* NavigationMenuArrowContext (internal)
* -----------------------------------------------------------------------------------------------*/

interface NavigationMenuArrowContextValue {
activeTriggerElement: Element | null;
setActiveTriggerElement: (element: Element | null) => void;
positionerRef: RefObject<HTMLElement | null>;
}

const [NavigationMenuArrowProvider, useNavigationMenuArrowContext] =
createContext<NavigationMenuArrowContextValue>({
name: 'NavigationMenuArrowContext',
strict: false,
hookName: 'useNavigationMenuArrowContext',
providerName: 'NavigationMenuRoot',
});

type NavigationMenuVariants = ListVariants & LinkVariants;
type NavigationMenuSharedProps = NavigationMenuVariants & { disabled?: boolean };
type NavigationMenuContextType = NavigationMenuSharedProps;
Expand All @@ -47,15 +67,22 @@ export const NavigationMenuRoot = forwardRef<HTMLElement, NavigationMenuRoot.Pro

const { direction } = variantProps;

const [activeTriggerElement, setActiveTriggerElement] = useState<Element | null>(null);
const positionerRef = useRef<HTMLElement>(null);
Comment on lines +70 to +71

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike tooltips and popovers, the navigation menu contains multiple triggers within the Root. As a result, there are limitations to specifying the trigger element using a ref, so the element is managed via state.


return (
<NavigationMenuProvider value={variantProps}>
<BaseNavigationMenu.Root
ref={ref}
orientation={direction}
className={className}
{...otherProps}
/>
</NavigationMenuProvider>
<NavigationMenuArrowProvider
value={{ activeTriggerElement, setActiveTriggerElement, positionerRef }}
>
<NavigationMenuProvider value={variantProps}>
<BaseNavigationMenu.Root
ref={ref}
orientation={direction}
className={className}
{...otherProps}
/>
</NavigationMenuProvider>
</NavigationMenuArrowProvider>
);
},
);
Expand Down Expand Up @@ -149,13 +176,35 @@ export const NavigationMenuTriggerPrimitive = forwardRef<
>((props, ref) => {
const { disabled: disabledProp, className, ...componentProps } = resolveStyles(props);
const { size, disabled: contextDisabled } = useNavigationMenuContext();
const { setActiveTriggerElement } = useNavigationMenuArrowContext() ?? {};

const internalRef = useRef<HTMLButtonElement>(null);
const composedRef = composeRefs(internalRef, ref);

useEffect(() => {
const node = internalRef.current;
if (!node || !setActiveTriggerElement) return;

if (node.hasAttribute('data-popup-open')) {
setActiveTriggerElement(node);
}

const observer = new MutationObserver(() => {
if (node.hasAttribute('data-popup-open')) {
setActiveTriggerElement(node);
}
});
observer.observe(node, { attributes: true, attributeFilter: ['data-popup-open'] });

return () => observer.disconnect();
}, [setActiveTriggerElement]);

const disabled = disabledProp ?? contextDisabled;
const dataAttrs = createDataAttributes({ disabled });

return (
<BaseNavigationMenu.Trigger
ref={ref}
ref={composedRef}
disabled={disabled}
className={cn(styles.link({ size }), styles.trigger, className)}
{...dataAttrs}
Expand Down Expand Up @@ -260,10 +309,12 @@ export const NavigationMenuPositionerPrimitive = forwardRef<
className,
...componentProps
} = resolveStyles(props);
const { positionerRef } = useNavigationMenuArrowContext() ?? {};
const composedRef = composeRefs(positionerRef, ref);

return (
<BaseNavigationMenu.Positioner
ref={ref}
ref={composedRef}
side={side}
align={align}
sideOffset={sideOffset}
Expand All @@ -290,7 +341,15 @@ export const NavigationMenuPopupPrimitive = forwardRef<
const [side, setSide] = useState<NavigationMenuPositionerPrimitive.Props['side']>();
const [align, setAlign] = useState<NavigationMenuPositionerPrimitive.Props['align']>();

const position = useMemo(() => getArrowPosition({ side, align }), [side, align]);
const { activeTriggerElement, positionerRef } = useNavigationMenuArrowContext() ?? {};
const arrowDimensions = { width: 16, height: 8 };

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the arrow is configured differently for each component, we calculate the position by assigning a size.

const arrowStyle = useArrowPosition({
triggerElement: activeTriggerElement ?? null,
positionerElement: positionerRef?.current ?? null,
side: side ?? 'bottom',
align: align ?? 'center',
arrowDimensions,
});
Comment thread
MaxLee-dev marked this conversation as resolved.

const popupRef = useRef<HTMLDivElement>(null);
const composedRef = composeRefs(popupRef, ref);
Expand Down Expand Up @@ -326,7 +385,13 @@ export const NavigationMenuPopupPrimitive = forwardRef<
className={cn(styles.popup, className)}
{...componentProps}
>
<BaseNavigationMenu.Arrow ref={arrowRef} style={position} className={styles.arrow}>
<BaseNavigationMenu.Arrow
ref={arrowRef}
style={{
...arrowStyle,
}}
className={styles.arrow}
>
<ArrowIcon />
</BaseNavigationMenu.Arrow>

Expand All @@ -343,34 +408,8 @@ const extractPositions = (dataset: DOMStringMap) => {
};

/* -----------------------------------------------------------------------------------------------*/

type ArrowPositionProps = Pick<NavigationMenuPositionerPrimitive.Props, 'side' | 'align'> & {
offset?: number;
};

const getArrowPosition = ({
side = 'top',
align = 'center',
offset = 12,
}: ArrowPositionProps): CSSProperties => {
const positionMap = {
'top-start': { left: offset, right: 'unset' },
'top-end': { left: 'unset', right: offset },
'bottom-start': { left: offset, right: 'unset' },
'bottom-end': { left: 'unset', right: offset },
'left-start': { top: offset, bottom: 'unset' },
'left-end': { top: 'unset', bottom: offset },
'right-start': { top: offset, bottom: 'unset' },
'right-end': { top: 'unset', bottom: offset },
};

const key = `${side}-${align}` as keyof typeof positionMap;
return positionMap[key] || {};
};

/* -----------------------------------------------------------------------------------------------*/

const ArrowIcon = (props: ComponentPropsWithoutRef<'svg'>) => {
const ArrowIcon = (props: ComponentProps<'svg'>) => {
const clipId = useVaporId();
return (
<svg
width="16"
Expand All @@ -380,19 +419,25 @@ const ArrowIcon = (props: ComponentPropsWithoutRef<'svg'>) => {
xmlns="http://www.w3.org/2000/svg"
{...props}
>
<path
d="M7.06543 1.17969C7.56267 0.620294 8.43733 0.620294 8.93457 1.17969L14.3301 7.25H1.66992L7.06543 1.17969Z"
stroke={vars.color.border.normal}
strokeWidth="1"
/>
<path
d="M8.75926 1.8858C8.36016 1.42019 7.63984 1.42019 7.24074 1.8858L2 8H14L8.75926 1.8858Z"
fill="currentColor"
/>
<g clipPath={`url(#${clipId})`}>
<path
d="M7.06543 1.17969C7.56267 0.620294 8.43733 0.620294 8.93457 1.17969L14.3301 7.25H1.66992L7.06543 1.17969Z"
stroke={vars.color.border.normal}
strokeWidth="1.5"
/>
<path
d="M8.75926 1.8858C8.36016 1.42019 7.63984 1.42019 7.24074 1.8858L2 8H14L8.75926 1.8858Z"
fill={vars.color.background.overlay[100]}
/>
</g>
<defs>
<clipPath id={clipId}>
<rect width="16" height="8" fill="white" />
</clipPath>
</defs>
</svg>
);
};

/* -------------------------------------------------------------------------------------------------
* NavigationMenu.ViewportPrimitive
* -----------------------------------------------------------------------------------------------*/
Expand Down
25 changes: 2 additions & 23 deletions packages/core/src/components/popover/popover.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,7 @@ export const arrow = componentStyle({
display: 'flex',
color: vars.color.background.overlay[100], // It's background-color, but since it's an SVG, it's specified as color.

width: vars.size.dimension[100],
height: vars.size.dimension[200],

transform: 'rotate(180deg)',

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the icons for all components are positioned relative to the bottom arrow, this transform has been removed.

width: vars.size.dimension[200],
height: vars.size.dimension[100],
zIndex: 1,

selectors: {
'&[data-side="top"]': {
bottom: '-11px',
transform: 'rotate(-90deg)',
},
'&[data-side="right"]': {
left: '-7px',
transform: 'rotate(0deg)',
},
'&[data-side="bottom"]': {
top: '-11px',
transform: 'rotate(90deg)',
},
'&[data-side="left"]': {
right: '-7px',
transform: 'rotate(180deg)',
},
},
});
43 changes: 40 additions & 3 deletions packages/core/src/components/popover/popover.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ export const Default: StoryObj<Popover.Root.Props & Popover.PositionerPrimitive.

export const Customable: StoryObj<Popover.Root.Props & Popover.PositionerPrimitive.Props> = {
argTypes: {
side: { control: 'inline-radio', options: ['top', 'right', 'bottom', 'left'] },
side: {
control: 'inline-radio',
options: ['top', 'right', 'bottom', 'left', 'inline-start', 'inline-end'],
},
sideOffset: { control: 'number' },
align: { control: 'inline-radio', options: ['start', 'center', 'end'] },
alignOffset: { control: 'number' },
Expand Down Expand Up @@ -160,6 +163,41 @@ export const TestBed: StoryObj<Popover.Root.Props & Popover.PositionerPrimitive.
</Popover.Root>
</HStack>

<HStack
$css={{
padding: '$800',
gap: '$400',
justifyContent: 'center',
alignItems: 'center',
border: '1px solid',
}}
>
<Popover.Root {...args} open>
<Popover.Trigger render={<Button>Inline Start Popover</Button>} />

<Popover.Popup
positionerElement={<Popover.PositionerPrimitive side="inline-start" />}
>
<Popover.Title>Inline Start Popover</Popover.Title>
<Popover.Description>
This popover uses a logical inline-start side.
</Popover.Description>
</Popover.Popup>
</Popover.Root>
<Popover.Root {...args} open>
<Popover.Trigger render={<Button>Inline End Popover</Button>} />

<Popover.Popup
positionerElement={<Popover.PositionerPrimitive side="inline-end" />}
>
<Popover.Title>Inline End Popover</Popover.Title>
<Popover.Description>
This popover uses a logical inline-end side.
</Popover.Description>
</Popover.Popup>
</Popover.Root>
</HStack>

<HStack
$css={{ padding: '$800', border: '1px solid', justifyContent: 'space-between' }}
>
Expand All @@ -186,7 +224,7 @@ export const TestBed: StoryObj<Popover.Root.Props & Popover.PositionerPrimitive.
>
<Popover.Title>Shift Popover</Popover.Title>
<Popover.Description>
This is a left popover content. it is shifted inside.
This is a right popover content. it is shifted inside.
</Popover.Description>
</Popover.Popup>
</Popover.Root>
Expand All @@ -203,7 +241,6 @@ export const TestBed: StoryObj<Popover.Root.Props & Popover.PositionerPrimitive.
<Popover.PositionerPrimitive
side="bottom"
align="end"
alignOffset={100}
collisionAvoidance={{ align: 'flip' }}
/>
}
Expand Down
Loading
Loading