Skip to content

Commit 6cebc00

Browse files
authored
[menu] Implement content transitions with Viewport (#4060)
1 parent bb8140e commit 6cebc00

File tree

21 files changed

+1156
-39
lines changed

21 files changed

+1156
-39
lines changed

docs/reference/generated/menu-popup.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
},
4040
"data-instant": {
4141
"description": "Present if animations should be instant.",
42-
"type": "'click' | 'dismiss' | 'group'"
42+
"type": "'click' | 'dismiss' | 'group' | 'trigger-change'"
4343
},
4444
"data-side": {
4545
"description": "Indicates which side the popup is positioned relative to the trigger.",
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "MenuViewport",
3+
"description": "A viewport for displaying content transitions.\nThis component is only required if one popup can be opened by multiple triggers, its content change based on the trigger\nand switching between them is animated.\nRenders a `<div>` element.",
4+
"props": {
5+
"children": {
6+
"type": "ReactNode",
7+
"description": "The content to render inside the transition container.",
8+
"detailedType": "React.ReactNode"
9+
},
10+
"className": {
11+
"type": "string | ((state: Menu.Viewport.State) => string | undefined)",
12+
"description": "CSS class applied to the element, or a function that\nreturns a class based on the component’s state.",
13+
"detailedType": "| string\n| ((state: Menu.Viewport.State) => string | undefined)"
14+
},
15+
"style": {
16+
"type": "CSSProperties | ((state: Menu.Viewport.State) => CSSProperties | undefined)",
17+
"detailedType": "| React.CSSProperties\n| ((\n state: Menu.Viewport.State,\n ) => CSSProperties | undefined)\n| undefined"
18+
},
19+
"render": {
20+
"type": "ReactElement | ((props: HTMLProps, state: Menu.Viewport.State) => ReactElement)",
21+
"description": "Allows you to replace the component’s HTML element\nwith a different tag, or compose it with another component.\n\nAccepts a `ReactElement` or a function that returns the element to render.",
22+
"detailedType": "| ReactElement\n| ((\n props: HTMLProps,\n state: Menu.Viewport.State,\n ) => ReactElement)"
23+
}
24+
},
25+
"dataAttributes": {
26+
"data-activation-direction": {
27+
"description": "Indicates the direction from which the popup was activated.\nThis can be used to create directional animations based on how the popup was triggered.\nContains space-separated values for both horizontal and vertical axes.",
28+
"type": "`${'left' | 'right'} {'down' | 'up'}`"
29+
},
30+
"data-current": {
31+
"description": "Applied to the direct child of the viewport when no transitions are present or the new content when it's entering."
32+
},
33+
"data-instant": {
34+
"description": "Present if animations should be instant.",
35+
"type": "'click' | 'dismiss' | 'group' | 'trigger-change'"
36+
},
37+
"data-previous": {
38+
"description": "Applied to the direct child of the viewport that contains the exiting content when transitions are present."
39+
},
40+
"data-transitioning": {
41+
"description": "Indicates that the viewport is currently transitioning between old and new content."
42+
}
43+
},
44+
"cssVariables": {
45+
"--popup-height": {
46+
"description": "The height of the parent popup.\nThis variable is placed on the 'previous' container and stores the height of the popup when the previous content was rendered.\nIt can be used to freeze the dimensions of the popup when animating between different content."
47+
},
48+
"--popup-width": {
49+
"description": "The width of the parent popup.\nThis variable is placed on the 'previous' container and stores the width of the popup when the previous content was rendered.\nIt can be used to freeze the dimensions of the popup when animating between different content."
50+
}
51+
}
52+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
.Positioner {
2+
--easing: cubic-bezier(0.22, 1, 0.36, 1);
3+
--animation-duration: 0.35s;
4+
5+
width: var(--positioner-width);
6+
height: var(--positioner-height);
7+
max-width: var(--available-width);
8+
9+
transition-property: top, left, right, bottom, transform;
10+
transition-timing-function: var(--easing);
11+
transition-duration: var(--animation-duration);
12+
13+
&[data-instant] {
14+
transition: none;
15+
}
16+
}
17+
18+
.Popup {
19+
position: relative;
20+
width: var(--popup-width, auto);
21+
height: var(--popup-height, auto);
22+
23+
transition-property: width, height, opacity, transform;
24+
transition-timing-function: var(--easing);
25+
transition-duration: var(--animation-duration);
26+
27+
&[data-starting-style],
28+
&[data-ending-style] {
29+
opacity: 0;
30+
transform: scale(0.9);
31+
}
32+
33+
&[data-instant] {
34+
transition: none;
35+
}
36+
}
37+
38+
.Viewport {
39+
box-sizing: border-box;
40+
position: relative;
41+
overflow: clip;
42+
width: 100%;
43+
height: 100%;
44+
padding: 0;
45+
46+
[data-previous],
47+
[data-current] {
48+
width: var(--popup-width);
49+
transform: translateX(0);
50+
opacity: 1;
51+
transition:
52+
transform var(--animation-duration) var(--easing),
53+
opacity calc(var(--animation-duration) / 2) var(--easing);
54+
}
55+
56+
&[data-activation-direction~='right'] [data-previous][data-ending-style] {
57+
transform: translateX(-50%);
58+
opacity: 0;
59+
}
60+
61+
&[data-activation-direction~='right'] [data-current][data-starting-style] {
62+
transform: translateX(50%);
63+
opacity: 0;
64+
}
65+
66+
&[data-activation-direction~='left'] [data-previous][data-ending-style] {
67+
transform: translateX(50%);
68+
opacity: 0;
69+
}
70+
71+
&[data-activation-direction~='left'] [data-current][data-starting-style] {
72+
transform: translateX(-50%);
73+
opacity: 0;
74+
}
75+
}
76+
77+
.Arrow {
78+
transition: left calc(var(--animation-duration)) var(--easing);
79+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
'use client';
2+
import * as React from 'react';
3+
import { Menu } from '@base-ui/react/menu';
4+
import styles from '../../_index.module.css';
5+
import transitionStyles from './index.module.css';
6+
7+
type MenuContent = {
8+
heading: string;
9+
groups: string[][];
10+
};
11+
12+
const MENUS = {
13+
library: {
14+
heading: 'Library',
15+
groups: [
16+
['Add to library', 'Add to favorites'],
17+
['Create playlist', 'Create station'],
18+
],
19+
},
20+
playback: {
21+
heading: 'Playback',
22+
groups: [
23+
['Play now', 'Add to queue'],
24+
['Play next', 'Play last', 'Sleep timer'],
25+
],
26+
},
27+
share: {
28+
heading: 'Share',
29+
groups: [
30+
['Copy link', 'Copy embed code'],
31+
['Share to contacts', 'Share to social'],
32+
],
33+
},
34+
} as const satisfies Record<string, MenuContent>;
35+
36+
type MenuKey = keyof typeof MENUS;
37+
38+
const demoMenu = Menu.createHandle<MenuKey>();
39+
40+
export default function MenuDetachedTriggersFullDemo() {
41+
return (
42+
<div className={styles.Container}>
43+
<Menu.Trigger className={styles.Button} handle={demoMenu} payload="library">
44+
Library
45+
</Menu.Trigger>
46+
<Menu.Trigger className={styles.Button} handle={demoMenu} payload="playback">
47+
Playback
48+
</Menu.Trigger>
49+
<Menu.Trigger className={styles.Button} handle={demoMenu} payload="share">
50+
Share
51+
</Menu.Trigger>
52+
53+
<Menu.Root handle={demoMenu} modal={false}>
54+
{({ payload }) => (
55+
<Menu.Portal>
56+
<Menu.Positioner
57+
sideOffset={8}
58+
className={`${styles.Positioner} ${transitionStyles.Positioner}`}
59+
>
60+
<Menu.Popup className={`${styles.Popup} ${transitionStyles.Popup}`}>
61+
<Menu.Arrow className={`${styles.Arrow} ${transitionStyles.Arrow}`}>
62+
<ArrowSvg />
63+
</Menu.Arrow>
64+
65+
<Menu.Viewport className={transitionStyles.Viewport}>
66+
{payload &&
67+
MENUS[payload].groups.map((group, groupIndex) => (
68+
<React.Fragment key={groupIndex}>
69+
<Menu.Group>
70+
{groupIndex === 0 && (
71+
<Menu.GroupLabel className={styles.Label}>
72+
{MENUS[payload].heading}
73+
</Menu.GroupLabel>
74+
)}
75+
{group.map((item) => (
76+
<Menu.Item key={item} className={styles.Item}>
77+
{item}
78+
</Menu.Item>
79+
))}
80+
</Menu.Group>
81+
{groupIndex < MENUS[payload].groups.length - 1 && (
82+
<Menu.Separator className={styles.Separator} />
83+
)}
84+
</React.Fragment>
85+
))}
86+
</Menu.Viewport>
87+
</Menu.Popup>
88+
</Menu.Positioner>
89+
</Menu.Portal>
90+
)}
91+
</Menu.Root>
92+
</div>
93+
);
94+
}
95+
96+
function ArrowSvg(props: React.ComponentProps<'svg'>) {
97+
return (
98+
<svg width="20" height="10" viewBox="0 0 20 10" fill="none" {...props}>
99+
<path
100+
d="M9.66437 2.60207L4.80758 6.97318C4.07308 7.63423 3.11989 8 2.13172 8H0V10H20V8H18.5349C17.5468 8 16.5936 7.63423 15.8591 6.97318L11.0023 2.60207C10.622 2.2598 10.0447 2.25979 9.66437 2.60207Z"
101+
className={styles.ArrowFill}
102+
/>
103+
<path
104+
d="M8.99542 1.85876C9.75604 1.17425 10.9106 1.17422 11.6713 1.85878L16.5281 6.22989C17.0789 6.72568 17.7938 7.00001 18.5349 7.00001L15.89 7L11.0023 2.60207C10.622 2.2598 10.0447 2.2598 9.66436 2.60207L4.77734 7L2.13171 7.00001C2.87284 7.00001 3.58774 6.72568 4.13861 6.22989L8.99542 1.85876Z"
105+
className={styles.ArrowOuterStroke}
106+
/>
107+
<path
108+
d="M10.3333 3.34539L5.47654 7.71648C4.55842 8.54279 3.36693 9 2.13172 9H0V8H2.13172C3.11989 8 4.07308 7.63423 4.80758 6.97318L9.66437 2.60207C10.0447 2.25979 10.622 2.2598 11.0023 2.60207L15.8591 6.97318C16.5936 7.63423 17.5468 8 18.5349 8H20V9H18.5349C17.2998 9 16.1083 8.54278 15.1901 7.71648L10.3333 3.34539Z"
109+
className={styles.ArrowInnerStroke}
110+
/>
111+
</svg>
112+
);
113+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createDemoWithVariants } from 'docs/src/utils/createDemo';
2+
import CssModules from './css-modules';
3+
import Tailwind from './tailwind';
4+
5+
export const DemoMenuDetachedTriggersFull = createDemoWithVariants(import.meta.url, {
6+
CssModules,
7+
Tailwind,
8+
});

0 commit comments

Comments
 (0)