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 link should change url when click on item with hash #1697

Merged
merged 2 commits into from
Dec 24, 2024
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
7 changes: 7 additions & 0 deletions e2e/fixtures/nav-link-item-with-hash/doc/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
pageType: custom
---

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

<div id="pageB">contentB</div>
16 changes: 16 additions & 0 deletions e2e/fixtures/nav-link-item-with-hash/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@rspress-fixture/rspress-nav-with-hash",
"version": "1.0.0",
"private": true,
"scripts": {
"dev": "rspress dev",
"build": "rspress build",
"preview": "rspress preview"
},
"dependencies": {
"rspress": "workspace:*"
},
"devDependencies": {
"@types/node": "^18.11.17"
}
}
23 changes: 23 additions & 0 deletions e2e/fixtures/nav-link-item-with-hash/rspress.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as path from 'node:path';
import { defineConfig } from 'rspress/config';

export default defineConfig({
root: path.join(__dirname, 'doc'),
route: {
cleanUrls: true,
},
themeConfig: {
nav: [
{
text: 'PageA',
link: '#pageA',
position: 'right',
},
{
text: 'PageB',
link: '#pageB',
position: 'right',
},
],
},
});
1 change: 1 addition & 0 deletions e2e/fixtures/nav-link-item-with-hash/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
31 changes: 31 additions & 0 deletions e2e/tests/nav-link-item-with-hash.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { expect, test } from '@playwright/test';
import path from 'node:path';
import { getPort, killProcess, runDevCommand } from '../utils/runCommands';

const fixtureDir = path.resolve(__dirname, '../fixtures');

test.describe('basic test', async () => {
let appPort;
let app;
test.beforeAll(async () => {
const appDir = path.join(fixtureDir, 'nav-link-item-with-hash');
appPort = await getPort();
app = await runDevCommand(appDir, appPort);
});

test.afterAll(async () => {
if (app) {
await killProcess(app);
}
});

test('Navigate with an hash as link', async ({ page }) => {
await page.goto(`http://localhost:${appPort}/`);

await page.locator('.rspress-nav-menu a').first().click();
expect(page.url()).toContain('index#pageA');

await page.locator('.rspress-nav-menu a').nth(1).click();
expect(page.url()).toContain('index#pageB');
});
});
1 change: 1 addition & 0 deletions packages/theme-default/src/components/Link/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export function Link(props: LinkProps) {
const el = document.getElementById(hash);
if (el) {
scrollToTarget(el, true);
navigate(withQueryUrl, { replace: false });
}
return;
}
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading