Skip to content

feat(nav): Open secondary navigation in overlay when collapsed #92055

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

Merged
merged 6 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
61 changes: 53 additions & 8 deletions static/app/components/hovercard.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {Fragment, useCallback, useRef} from 'react';
import {createContext, Fragment, useCallback, useContext, useMemo, useRef} from 'react';
import {createPortal} from 'react-dom';
import {useTheme} from '@emotion/react';
import styled from '@emotion/styled';
import type {State} from '@popperjs/core';
import {useResizeObserver} from '@react-aria/utils';
import {AnimatePresence} from 'framer-motion';

Expand All @@ -16,6 +17,10 @@ interface HovercardProps extends Omit<UseHoverOverlayProps, 'isHoverable'> {
* Classname to apply to the hovercard
*/
children: React.ReactNode;
/**
* Whether to animate the hovercard in/out
*/
animated?: boolean;
/**
Copy link
Member Author

Choose a reason for hiding this comment

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

Added this config option to allow the animation to be disabled

* Element to display in the body
*/
Expand Down Expand Up @@ -47,11 +52,33 @@ type UseOverOverlayState = ReturnType<typeof useHoverOverlay>;
interface HovercardContentProps
extends Pick<
HovercardProps,
'bodyClassName' | 'className' | 'header' | 'body' | 'tipColor' | 'tipBorderColor'
| 'animated'
| 'bodyClassName'
| 'className'
| 'header'
| 'body'
| 'tipColor'
| 'tipBorderColor'
> {
hoverOverlayState: Omit<UseOverOverlayState, 'isOpen' | 'wrapTrigger'>;
}

interface HovercardProviderValue {
isOpen: boolean;
reset: () => void;
update: (() => Promise<Partial<State>>) | null;
}

const HovercardContext = createContext<HovercardProviderValue>({
isOpen: false,
Copy link
Member Author

Choose a reason for hiding this comment

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

Added a context here so that children can call reset to close the overlay when needed. This allows me to dismiss the overlay when a link is clicked.

reset: () => {},
update: null,
});

export function useHovercardContext() {
return useContext(HovercardContext);
}

function useUpdateOverlayPositionOnContentChange({
update,
}: Pick<UseOverOverlayState, 'update'>) {
Expand All @@ -70,6 +97,7 @@ function useUpdateOverlayPositionOnContentChange({
}

function HovercardContent({
animated,
body,
bodyClassName,
className,
Expand All @@ -84,7 +112,7 @@ function HovercardContent({
return (
<PositionWrapper zIndex={theme.zIndex.hovercard} {...overlayProps}>
<StyledHovercard
animated
animated={animated}
arrowProps={{
...arrowProps,
size: 20,
Expand Down Expand Up @@ -114,6 +142,7 @@ function Hovercard({
displayTimeout = 100,
tipBorderColor = 'translucentBorder',
tipColor = 'backgroundElevated',
animated = true,
...hoverOverlayProps
}: HovercardProps): React.ReactElement {
const {wrapTrigger, isOpen, ...hoverOverlayState} = useHoverOverlay({
Expand All @@ -124,16 +153,25 @@ function Hovercard({
...hoverOverlayProps,
});

const contextValue = useMemo<HovercardProviderValue>(
() => ({
isOpen,
reset: hoverOverlayState.reset,
update: hoverOverlayState.update,
}),
[isOpen, hoverOverlayState.reset, hoverOverlayState.update]
);
// Nothing to render if no header or body. Be consistent with wrapping the
// children with the trigger in the case that the body / header is set while
// the trigger is hovered.
if (!body && !header) {
return <Fragment>{wrapTrigger(children)}</Fragment>;
}

const hovercardContent = isOpen && (
const hovercardContent = isOpen ? (
<HovercardContent
{...{
animated,
body,
bodyClassName,
className,
Expand All @@ -143,13 +181,20 @@ function Hovercard({
hoverOverlayState,
}}
/>
);
) : null;

return (
<Fragment>
<HovercardContext.Provider value={contextValue}>
{wrapTrigger(children)}
{createPortal(<AnimatePresence>{hovercardContent}</AnimatePresence>, document.body)}
</Fragment>
{createPortal(
animated ? (
<AnimatePresence>{hovercardContent}</AnimatePresence>
) : (
hovercardContent
),
document.body
)}
</HovercardContext.Provider>
);
}

Expand Down
2 changes: 2 additions & 0 deletions static/app/views/admin/adminLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {t} from 'sentry/locale';
import type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
import useOrganization from 'sentry/utils/useOrganization';
import {prefersStackedNav} from 'sentry/views/nav/prefersStackedNav';
import {PrimaryNavGroup} from 'sentry/views/nav/types';
import {BreadcrumbProvider} from 'sentry/views/settings/components/settingsBreadcrumb/context';
import SettingsLayout from 'sentry/views/settings/components/settingsLayout';
import SettingsNavigation from 'sentry/views/settings/components/settingsNavigation';
Expand Down Expand Up @@ -40,6 +41,7 @@ export function AdminNavigation() {
],
},
]}
primaryNavGroup={PrimaryNavGroup.ADMIN}
/>
);
}
Expand Down
64 changes: 32 additions & 32 deletions static/app/views/nav/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ describe('Nav', function () {
body: {},
});

MockApiClient.addMockResponse({
url: `/organizations/org-slug/explore/saved/`,
body: [],
});

MockApiClient.addMockResponse({
url: `/organizations/org-slug/dashboards/`,
body: [],
});

ConfigStore.set('user', {
...ConfigStore.get('user'),
options: {
Expand Down Expand Up @@ -243,58 +253,48 @@ describe('Nav', function () {

await userEvent.click(screen.getByRole('button', {name: 'Collapse'}));

expect(screen.getByTestId('collapsed-secondary-sidebar')).toBeInTheDocument();
await waitFor(() => {
expect(
screen.queryByRole('navigation', {name: 'Secondary Navigation'})
).not.toBeInTheDocument();
});

await userEvent.click(screen.getByRole('button', {name: 'Expand'}));
await userEvent.hover(screen.getByRole('link', {name: 'Issues'}));

expect(
screen.queryByTestId('collapsed-secondary-sidebar')
).not.toBeInTheDocument();
await screen.findByRole('navigation', {name: 'Secondary Navigation'})
).toBeInTheDocument();

await userEvent.click(screen.getByRole('button', {name: 'Expand'}));
});

it('remembers collapsed state', async function () {
localStorage.setItem(NAV_SIDEBAR_COLLAPSED_LOCAL_STORAGE_KEY, 'true');

renderNav();

expect(
await screen.findByTestId('collapsed-secondary-sidebar')
).toBeInTheDocument();
expect(screen.getByRole('button', {name: 'Expand'})).toBeInTheDocument();
await waitFor(() => {
expect(
screen.queryByRole('navigation', {name: 'Secondary Navigation'})
).not.toBeInTheDocument();
});
});

it('expands on hover', async function () {
it('closes secondary nav overlay when navigating to a new route', async function () {
localStorage.setItem(NAV_SIDEBAR_COLLAPSED_LOCAL_STORAGE_KEY, 'true');

renderNav();

expect(
await screen.findByTestId('collapsed-secondary-sidebar')
).toBeInTheDocument();
await userEvent.hover(screen.getByRole('link', {name: 'Explore'}));

expect(screen.getByTestId('collapsed-secondary-sidebar')).toHaveAttribute(
'data-visible',
'false'
);
await screen.findByRole('navigation', {name: 'Secondary Navigation'});

// Moving pointer over the primary navigation should expand the sidebar
await userEvent.hover(
screen.getByRole('navigation', {name: 'Primary Navigation'})
);
expect(screen.getByTestId('collapsed-secondary-sidebar')).toHaveAttribute(
'data-visible',
'true'
);
await userEvent.click(screen.getByRole('link', {name: 'Traces'}));

// Moving pointer away should hide the sidebar
await userEvent.unhover(
screen.getByRole('navigation', {name: 'Primary Navigation'})
);
await waitFor(() => {
expect(screen.getByTestId('collapsed-secondary-sidebar')).toHaveAttribute(
'data-visible',
'false'
);
expect(
screen.queryByRole('navigation', {name: 'Secondary Navigation'})
).not.toBeInTheDocument();
});
});
});
Expand Down
7 changes: 6 additions & 1 deletion static/app/views/nav/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import styled from '@emotion/styled';

import {useNavContext} from 'sentry/views/nav/context';
import MobileTopbar from 'sentry/views/nav/mobileTopbar';
import {SecondaryNav} from 'sentry/views/nav/secondary/secondary';
import {SecondaryNavContent} from 'sentry/views/nav/secondary/secondaryNavContent';
import {Sidebar} from 'sentry/views/nav/sidebar';
import {
NavigationTourProvider,
useStackedNavigationTour,
} from 'sentry/views/nav/tour/tour';
import {NavLayout} from 'sentry/views/nav/types';
import {useActiveNavGroup} from 'sentry/views/nav/useActiveNavGroup';

function NavContent() {
const {layout, navParentRef} = useNavContext();
const {currentStepId, endTour} = useStackedNavigationTour();
const tourIsActive = currentStepId !== null;
const activeNavGroup = useActiveNavGroup();

// The tour only works with the sidebar layout, so if we change to the mobile
// layout in the middle of the tour, it needs to end.
Expand All @@ -32,7 +35,9 @@ function NavContent() {
isMobile={layout === NavLayout.MOBILE}
>
{layout === NavLayout.SIDEBAR ? <Sidebar /> : <MobileTopbar />}
<SecondaryNavContent />
<SecondaryNav>
<SecondaryNavContent group={activeNavGroup} />
</SecondaryNav>
</NavContainer>
);
}
Expand Down
Loading
Loading