Skip to content

Commit 7bd740b

Browse files
committed
fix(ui:anchor): use getElementById
1 parent 2b98c82 commit 7bd740b

9 files changed

Lines changed: 23 additions & 38 deletions

File tree

.stylelintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
/coverage
44
/.yarn
55
/packages/site/dist
6-
/packages/site/src/app/routes
6+
/packages/site/src/app/routes

packages/site/src/app/components/route/RouteArticle.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export function AppRouteArticle(props: AppRouteArticleProps) {
2525
const { i18n } = useTranslation();
2626
const mediaMatch = useMediaMatch();
2727

28-
const [_links, setLinks] = useImmer<{ title: string; href: string }[]>([]);
29-
const links = isUndefined(props.links) ? _links : [...props.links, { title: 'API', href: '#API' }];
28+
const [_links, setLinks] = useImmer<{ title?: string; href: string }[]>([]);
29+
const links = isUndefined(props.links) ? _links : [...props.links, { href: 'API' }];
3030

3131
const [menuOpen, setMenuOpen] = useState(false);
3232

@@ -58,9 +58,9 @@ m -673.67664,1221.6502 -231.2455,-231.24803 55.6165,
5858

5959
useLayoutEffect(() => {
6060
if (isUndefined(props.links)) {
61-
const arr: { title: string; href: string }[] = [];
61+
const arr: { href: string }[] = [];
6262
document.querySelectorAll('.app-route-article h2').forEach((el) => {
63-
arr.push({ title: el.id, href: `#${el.id}` });
63+
arr.push({ href: el.id });
6464
});
6565
setLinks(arr);
6666
}

packages/site/src/app/routes/docs/Internationalization/Internationalization.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default function App() {
5757
}
5858
```
5959

60-
## add language
60+
## Add language
6161

6262
Support for adding languages:
6363

packages/ui/components/anchor/Anchor.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { usePrefixConfig, useComponentConfig, useLayout } from '../../hooks';
1111
import { registerComponentMate } from '../../utils';
1212

1313
export interface DAnchorItem {
14-
title: React.ReactNode;
1514
href: string;
15+
title?: React.ReactNode;
1616
target?: string;
1717
}
1818

@@ -76,7 +76,7 @@ function Anchor<T extends DAnchorItem>(props: DAnchorProps<T>, ref: React.Forwar
7676
let nearestEl: [string, number] | undefined;
7777
const reduceLinks = (arr: DNestedChildren<T>[]) => {
7878
arr.forEach(({ href, children }) => {
79-
const el = document.querySelector(href);
79+
const el = document.getElementById(href);
8080
if (el) {
8181
const top = el.getBoundingClientRect().top;
8282
// Add 1 because `getBoundingClientRect` return decimal
@@ -165,10 +165,10 @@ function Anchor<T extends DAnchorItem>(props: DAnchorProps<T>, ref: React.Forwar
165165
const pageTop = pageEl.getBoundingClientRect().top;
166166

167167
const scrollTop = pageEl.scrollTop;
168-
window.location.hash = href;
168+
window.location.hash = `#${href}`;
169169
pageEl.scrollTop = scrollTop;
170170

171-
const el = document.querySelector(href);
171+
const el = document.getElementById(href);
172172
if (el) {
173173
const top = el.getBoundingClientRect().top;
174174
const scrollTop = top - pageTop + pageEl.scrollTop - dDistance;
@@ -192,7 +192,7 @@ function Anchor<T extends DAnchorItem>(props: DAnchorProps<T>, ref: React.Forwar
192192
'is-active': linkHref === activeHref,
193193
})}
194194
style={{ paddingLeft: 16 + level * 16 }}
195-
href={linkHref}
195+
href={`#${linkHref}`}
196196
target={linkTarget}
197197
onClick={(e) => {
198198
e.preventDefault();
@@ -201,7 +201,7 @@ function Anchor<T extends DAnchorItem>(props: DAnchorProps<T>, ref: React.Forwar
201201
handleLinkClick(linkHref);
202202
}}
203203
>
204-
{linkTitle}
204+
{linkTitle ?? linkHref}
205205
</a>
206206
</li>
207207
{children && getNodes(children, level + 1)}

packages/ui/components/anchor/demos/1.Basic.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,10 @@ export default function Demo() {
2121
<DAnchor
2222
dList={[
2323
{
24-
title: 'Examples',
25-
href: '#Examples',
26-
children: [
27-
{ title: 'AnchorBasicDemo', href: '#AnchorBasicDemo' },
28-
{ title: 'AnchorIndicatorDemo', href: '#AnchorIndicatorDemo' },
29-
{ title: 'AnchorScrollDemo', href: '#AnchorScrollDemo' },
30-
],
24+
href: 'Examples',
25+
children: [{ href: 'AnchorBasicDemo' }, { href: 'AnchorIndicatorDemo' }, { href: 'AnchorScrollDemo' }],
3126
},
32-
{ title: 'API', href: '#API' },
27+
{ href: 'API' },
3328
]}
3429
/>
3530
</DAffix>

packages/ui/components/anchor/demos/2.Indicator.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,10 @@ export default function Demo() {
2424
<DAnchor
2525
dList={[
2626
{
27-
title: 'Examples',
28-
href: '#Examples',
29-
children: [
30-
{ title: 'AnchorBasicDemo', href: '#AnchorBasicDemo' },
31-
{ title: 'AnchorIndicatorDemo', href: '#AnchorIndicatorDemo' },
32-
{ title: 'AnchorScrollDemo', href: '#AnchorScrollDemo' },
33-
],
27+
href: 'Examples',
28+
children: [{ href: 'AnchorBasicDemo' }, { href: 'AnchorIndicatorDemo' }, { href: 'AnchorScrollDemo' }],
3429
},
35-
{ title: 'API', href: '#API' },
30+
{ href: 'API' },
3631
]}
3732
dIndicator={DAnchor.LINE_INDICATOR}
3833
/>

packages/ui/components/anchor/demos/3.Scroll.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,10 @@ export default function Demo() {
2020
<DAnchor
2121
dList={[
2222
{
23-
title: 'Examples',
24-
href: '#Examples',
25-
children: [
26-
{ title: 'AnchorBasicDemo', href: '#AnchorBasicDemo' },
27-
{ title: 'AnchorIndicatorDemo', href: '#AnchorIndicatorDemo' },
28-
{ title: 'AnchorScrollDemo', href: '#AnchorScrollDemo' },
29-
],
23+
href: 'Examples',
24+
children: [{ href: 'AnchorBasicDemo' }, { href: 'AnchorIndicatorDemo' }, { href: 'AnchorScrollDemo' }],
3025
},
31-
{ title: 'API', href: '#API' },
26+
{ href: 'API' },
3227
]}
3328
dDistance={window.innerHeight / 2}
3429
dScrollBehavior="smooth"

tools/executors/site/build-base/impl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ class GenerateSite {
246246
demoStr = demoStr.replace(/__tsxSource__/g, new TextEncoder().encode(demo.get(lang).tsx.match(/(?<=```tsx\n)[\s\S]*?(?=```)/g)[0]).join());
247247
demoStr = demoStr.replace(/__scssSource__/g, demo.get(lang).scss ? new TextEncoder().encode(demo.get(lang).scss.match(/(?<=```scss\n)[\s\S]*?(?=```)/g)[0]).join() : '');
248248
demosStr += demoStr;
249-
linksStr += String.raw `{ title: '${demo.get(lang).title}', href: '#${demo.get(lang).id}' }, `;
249+
linksStr += String.raw `{ title: '${demo.get(lang).title}', href: '${demo.get(lang).id}' }, `;
250250
}
251251
});
252252
let routeArticleProps = String.raw `

tools/executors/site/build-base/impl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ class GenerateSite {
261261

262262
demosStr += demoStr;
263263

264-
linksStr += String.raw`{ title: '${demo.get(lang)!.title!}', href: '#${demo.get(lang)!.id!}' }, `;
264+
linksStr += String.raw`{ title: '${demo.get(lang)!.title!}', href: '${demo.get(lang)!.id!}' }, `;
265265
}
266266
});
267267

0 commit comments

Comments
 (0)