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

fix: navbar should be sticky on mobile when hideNavbar to never #1719

Merged
Merged
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 e2e/fixtures/nav-link-item-with-hash/doc/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ pageType: custom

<div id="pageA">contentA</div>

<div id="pageB">contentB</div>
<div id="pageB">A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF A LOT OF CONTENT A LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF CONTENT A LOT OF A LOT OF CONTENT A LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENTA LOT OF CONTENT</div>
<div id="pageC">contentC</div>
1 change: 1 addition & 0 deletions e2e/fixtures/nav-link-item-with-hash/rspress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default defineConfig({
cleanUrls: true,
},
themeConfig: {
hideNavbar: 'never',
nav: [
{
text: 'PageA',
Expand Down
32 changes: 32 additions & 0 deletions e2e/tests/nav-link-item-with-hash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,36 @@ test.describe('basic test', async () => {
await page.getByRole('link', { name: 'PageC' }).click();
await expect(page.locator('.rspress-nav-screen')).not.toBeVisible();
});

test('Navbar should be visible on mobile when we scroll down with hideNavbar to never', async ({
page,
}) => {
await page.setViewportSize({ width: 375, height: 812 });

await page.goto(`http://localhost:${appPort}/`);

await page.evaluate(() => {
window.scrollBy(0, 800);
});

// Allow to check if the rspress-nav is in the viewport
// toBeVisible() doesn't work here because it check the visibility and the display property
const isInViewport = await page.evaluate(sel => {
const element = document.querySelector(sel);

if (!element) return false;

const rect = element.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <=
(window.innerHeight || document.documentElement.clientHeight) &&
rect.right <=
(window.innerWidth || document.documentElement.clientWidth)
);
}, '.rspress-nav');

expect(isInViewport).toBeTruthy();
});
});
16 changes: 11 additions & 5 deletions packages/theme-default/src/components/Nav/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const DEFAULT_NAV_POSITION = 'right';
export function Nav(props: NavProps) {
const { beforeNavTitle, afterNavTitle, beforeNav, afterNavMenu, navTitle } =
props;
const { siteData } = usePageData();
const { siteData, page } = usePageData();
const { base } = siteData;
const { pathname } = useLocation();
const [isMobile, setIsMobile] = useState(false);
Expand Down Expand Up @@ -125,17 +125,23 @@ export function Nav(props: NavProps) {
</div>
);
};

const computeNavPosition = () => {
// On doc page we have the menu bar that is already sticky
if (siteData.themeConfig.hideNavbar === 'never' && page.pageType !== 'doc')
return 'sticky';

return 'relative';
};

return (
<>
{beforeNav}
<div
className={`${styles.navContainer} rspress-nav px-6 ${
// Only hidden when it's not mobile
hiddenNav && !isMobile ? styles.hidden : ''
}`}
style={{
position: isMobile ? 'relative' : 'sticky',
}}
} ${computeNavPosition()}`}
>
<div
className={`${styles.container} flex justify-between items-center h-full`}
Expand Down
Loading