Skip to content
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

[NavigationMenu] Add Portal #3272

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
49 changes: 49 additions & 0 deletions packages/react/navigation-menu/src/NavigationMenu.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,55 @@ export const Submenus = () => {
);
};

export const WithPortalContainer = () => {
const [portalContainer, setPortalContainer] = React.useState<HTMLDivElement | null>(null);

return (
<StoryFrame>
<NavigationMenu.Root>
<div data-portal-container="" ref={setPortalContainer} />
<NavigationMenu.List className={mainListClass()}>
<NavigationMenu.Item className={expandableItemClass()}>
<TriggerWithIndicator>Portaled Products</TriggerWithIndicator>
<NavigationMenu.Portal container={portalContainer}>
<NavigationMenu.Content className={basicContentClass()}>
<LinkGroup
bordered={false}
items={[
'Fusce pellentesque',
'Aliquam porttitor',
'Pellentesque',
'Fusce pellentesque',
'Aliquam porttitor',
'Pellentesque',
]}
/>
</NavigationMenu.Content>
</NavigationMenu.Portal>
</NavigationMenu.Item>

<NavigationMenu.Item className={expandableItemClass()}>
<TriggerWithIndicator>Not Portaled Products</TriggerWithIndicator>
<NavigationMenu.Content className={basicContentClass()}>
<LinkGroup
bordered={false}
items={[
'Fusce pellentesque',
'Aliquam porttitor',
'Pellentesque',
'Fusce pellentesque',
'Aliquam porttitor',
'Pellentesque',
]}
/>
</NavigationMenu.Content>
</NavigationMenu.Item>
</NavigationMenu.List>
</NavigationMenu.Root>
</StoryFrame>
);
};

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

const StoryFrame: React.FC<{ children: React.ReactNode }> = ({ children }) => {
Expand Down
48 changes: 48 additions & 0 deletions packages/react/navigation-menu/src/NavigationMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { usePrevious } from '@radix-ui/react-use-previous';
import { useLayoutEffect } from '@radix-ui/react-use-layout-effect';
import { useCallbackRef } from '@radix-ui/react-use-callback-ref';
import * as VisuallyHiddenPrimitive from '@radix-ui/react-visually-hidden';
import { Portal as PortalPrimitive } from '@radix-ui/react-portal';

import type { Scope } from '@radix-ui/react-context';

Expand Down Expand Up @@ -1100,6 +1101,49 @@ const FocusGroup = React.forwardRef<FocusGroupElement, FocusGroupProps>(
}
);

/* -------------------------------------------------------------------------------------------------
* NaviationMenuPortal
* -----------------------------------------------------------------------------------------------*/

const PORTAL_NAME = 'NavigationMenuPortal';

type PortalContextValue = { forceMount?: true };
const [PortalProvider] = createNavigationMenuContext<PortalContextValue>(PORTAL_NAME, {
forceMount: undefined,
});

type PortalProps = React.ComponentPropsWithoutRef<typeof PortalPrimitive>;
interface NavigationMenuPortalProps {
children?: React.ReactNode;
/**
* Specify a container element to portal the content into.
*/
container?: PortalProps['container'];
/**
* Used to force mounting when more control is needed. Useful when
* controlling animation with React animation libraries.
*/
forceMount?: true;
}

const NavigationMenuPortal: React.FC<NavigationMenuPortalProps> = (
props: ScopedProps<NavigationMenuContentProps>
) => {
const { __scopeNavigationMenu, forceMount, children } = props;
const context = useNavigationMenuContext(PORTAL_NAME, __scopeNavigationMenu);
return (
<PortalProvider scope={__scopeNavigationMenu} forceMount={forceMount}>
{React.Children.map(children, (child) => (
<Presence present={forceMount || context.isRootMenu}>
<PortalPrimitive asChild>{child}</PortalPrimitive>
</Presence>
))}
</PortalProvider>
);
};

NavigationMenuPortal.displayName = PORTAL_NAME;

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

const ARROW_KEYS = ['ArrowRight', 'ArrowLeft', 'ArrowUp', 'ArrowDown'];
Expand Down Expand Up @@ -1249,6 +1293,7 @@ const Link = NavigationMenuLink;
const Indicator = NavigationMenuIndicator;
const Content = NavigationMenuContent;
const Viewport = NavigationMenuViewport;
const Portal = NavigationMenuPortal;

export {
createNavigationMenuScope,
Expand All @@ -1262,6 +1307,7 @@ export {
NavigationMenuIndicator,
NavigationMenuContent,
NavigationMenuViewport,
NavigationMenuPortal,
//
Root,
Sub,
Expand All @@ -1272,6 +1318,7 @@ export {
Indicator,
Content,
Viewport,
Portal,
};
export type {
NavigationMenuProps,
Expand All @@ -1283,4 +1330,5 @@ export type {
NavigationMenuIndicatorProps,
NavigationMenuContentProps,
NavigationMenuViewportProps,
NavigationMenuPortalProps,
};
3 changes: 3 additions & 0 deletions packages/react/navigation-menu/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export {
NavigationMenuIndicator,
NavigationMenuContent,
NavigationMenuViewport,
NavigationMenuPortal,
//
Root,
Sub,
Expand All @@ -21,6 +22,7 @@ export {
Indicator,
Content,
Viewport,
Portal,
} from './NavigationMenu';
export type {
NavigationMenuProps,
Expand All @@ -32,4 +34,5 @@ export type {
NavigationMenuIndicatorProps,
NavigationMenuContentProps,
NavigationMenuViewportProps,
NavigationMenuPortalProps,
} from './NavigationMenu';
24 changes: 24 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Commencer par git clone le repo : https://github.com/zephyr-dassouli/radixui-primitives
On a créé une branche pour chaque composant.
Choisir la branche du composant à tester avec git checkout.

1) Se rendre dans le dossier ssr-testing du repo

2) Installation de Node.js
sudo apt install npm

3) Installation de yarn
sudo npm install -g yarn

4) Setup de yarn dans le repo
sudo yarn install

5) Build du projet
sudo yarn build

6) Lancement du serveur local de test
sudo yarn start
Par défaut, le port utilisé est le 3000. Si déjà utilisé
sudo PORT=3001 yarn start

7) Test du composant associé à la branche