Skip to content

Commit

Permalink
fix: navbar link should change url when click on item with hash (#1697)
Browse files Browse the repository at this point in the history
  • Loading branch information
coratgerl authored Dec 24, 2024
1 parent 91e8c25 commit bf9f99a
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 0 deletions.
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.

0 comments on commit bf9f99a

Please sign in to comment.