site: fix a11y issue that <button> is nested inside <a> in homepage (#49848)

* site: fix a11y issue that <button> is nested inside <a>

* Update .dumi/theme/common/LinkButton.tsx

Co-authored-by: lijianan <574980606@qq.com>
Signed-off-by: afc163 <afc163@gmail.com>

---------

Signed-off-by: afc163 <afc163@gmail.com>
Co-authored-by: lijianan <574980606@qq.com>
This commit is contained in:
afc163 2024-07-15 09:22:02 +08:00 committed by GitHub
parent fe3378f644
commit 7329c8a293
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 50 additions and 20 deletions

View File

@ -1,10 +1,9 @@
import React, { Suspense } from 'react'; import React, { Suspense } from 'react';
import { Button, ConfigProvider, Flex, Typography } from 'antd'; import { ConfigProvider, Flex, Typography } from 'antd';
import { createStyles } from 'antd-style'; import { createStyles } from 'antd-style';
import { useLocation } from 'dumi'; import { useLocation } from 'dumi';
import useLocale from '../../../../hooks/useLocale'; import useLocale from '../../../../hooks/useLocale';
import Link from '../../../../theme/common/Link'; import LinkButton from '../../../../theme/common/LinkButton';
import SiteContext from '../../../../theme/slots/SiteContext'; import SiteContext from '../../../../theme/slots/SiteContext';
import * as utils from '../../../../theme/utils'; import * as utils from '../../../../theme/utils';
import GroupMaskLayer from '../GroupMaskLayer'; import GroupMaskLayer from '../GroupMaskLayer';
@ -144,14 +143,19 @@ const PreviewBanner: React.FC<React.PropsWithChildren> = (props) => {
<p>{locale.slogan}</p> <p>{locale.slogan}</p>
</Typography> </Typography>
<Flex gap="middle" className={styles.btnWrap}> <Flex gap="middle" className={styles.btnWrap}>
<Link to={utils.getLocalizedPathname('/components/overview/', isZhCN, search)}> <LinkButton
<Button size="large" type="primary"> size="large"
{locale.start} type="primary"
</Button> to={utils.getLocalizedPathname('/components/overview/', isZhCN, search)}
</Link> >
<Link to={utils.getLocalizedPathname('/docs/spec/introduce/', isZhCN, search)}> {locale.start}
<Button size="large">{locale.designLanguage}</Button> </LinkButton>
</Link> <LinkButton
size="large"
to={utils.getLocalizedPathname('/docs/spec/introduce/', isZhCN, search)}
>
{locale.designLanguage}
</LinkButton>
</Flex> </Flex>
<div className={styles.child}>{children}</div> <div className={styles.child}>{children}</div>
</div> </div>

View File

@ -10,7 +10,6 @@ import { TinyColor } from '@ctrl/tinycolor';
import type { MenuProps, ThemeConfig } from 'antd'; import type { MenuProps, ThemeConfig } from 'antd';
import { import {
Breadcrumb, Breadcrumb,
Button,
Card, Card,
ConfigProvider, ConfigProvider,
Flex, Flex,
@ -29,7 +28,7 @@ import { useLocation } from 'dumi';
import useDark from '../../../../hooks/useDark'; import useDark from '../../../../hooks/useDark';
import useLocale from '../../../../hooks/useLocale'; import useLocale from '../../../../hooks/useLocale';
import Link from '../../../../theme/common/Link'; import LinkButton from '../../../../theme/common/LinkButton';
import SiteContext from '../../../../theme/slots/SiteContext'; import SiteContext from '../../../../theme/slots/SiteContext';
import { getLocalizedPathname } from '../../../../theme/utils'; import { getLocalizedPathname } from '../../../../theme/utils';
import Group from '../Group'; import Group from '../Group';
@ -518,14 +517,15 @@ const Theme: React.FC = () => {
title={locale.myTheme} title={locale.myTheme}
extra={ extra={
<Flex gap="small"> <Flex gap="small">
<Link to={getLocalizedPathname('/theme-editor', isZhCN, search)}> <LinkButton to={getLocalizedPathname('/theme-editor', isZhCN, search)}>
<Button type="default">{locale.toDef}</Button> {locale.toDef}
</Link> </LinkButton>
<Link <LinkButton
type="primary"
to={getLocalizedPathname('/docs/react/customize-theme', isZhCN, search)} to={getLocalizedPathname('/docs/react/customize-theme', isZhCN, search)}
> >
<Button type="primary">{locale.toUse}</Button> {locale.toUse}
</Link> </LinkButton>
</Flex> </Flex>
} }
> >

View File

@ -8,12 +8,13 @@ export interface LinkProps {
style?: React.CSSProperties; style?: React.CSSProperties;
className?: string; className?: string;
onClick?: MouseEventHandler; onClick?: MouseEventHandler;
component?: React.ComponentType<any>;
} }
nprogress.configure({ showSpinner: false }); nprogress.configure({ showSpinner: false });
const Link = forwardRef<HTMLAnchorElement, React.PropsWithChildren<LinkProps>>((props, ref) => { const Link = forwardRef<HTMLAnchorElement, React.PropsWithChildren<LinkProps>>((props, ref) => {
const { to, children, ...rest } = props; const { to, children, component, ...rest } = props;
const [isPending, startTransition] = useTransition(); const [isPending, startTransition] = useTransition();
const navigate = useNavigate(); const navigate = useNavigate();
const { pathname } = useLocation(); const { pathname } = useLocation();
@ -48,6 +49,19 @@ const Link = forwardRef<HTMLAnchorElement, React.PropsWithChildren<LinkProps>>((
} }
}, [isPending]); }, [isPending]);
if (component) {
return React.createElement(
component,
{
...rest,
ref,
onClick: handleClick,
href,
},
children,
);
}
return ( return (
<DumiLink ref={ref} onClick={handleClick} {...rest} to={href} prefetch> <DumiLink ref={ref} onClick={handleClick} {...rest} to={href} prefetch>
{children} {children}

View File

@ -0,0 +1,12 @@
import React from 'react';
import { Button } from 'antd';
import type { ButtonProps } from 'antd';
import Link from './Link';
import type { LinkProps } from './Link';
type LinkButtonProps = LinkProps &
Readonly<React.PropsWithChildren<Pick<ButtonProps, 'type' | 'size'>>>;
const LinkButton: React.FC<LinkButtonProps> = (props) => <Link component={Button} {...props} />;
export default LinkButton;