【发布时间】:2021-04-18 09:34:58
【问题描述】:
我不明白如何使用键盘上的 Tab 键来关注 Material UI Link。我将链接放在按钮内以完成工作。但是有什么方法可以仅使用 Link 来满足我的需求。我正在删除代码框链接以获取完整详细信息。提前致谢。
沙盒链接:https://codesandbox.io/s/material-demo-forked-4evbh?file=/demo.tsx
沙盒代码:
/* eslint-disable jsx-a11y/anchor-is-valid */
import React from "react";
import { makeStyles, createStyles, Theme } from "@material-ui/core/styles";
import Link from "@material-ui/core/Link";
import Typography from "@material-ui/core/Typography";
const useStyles = makeStyles((theme: Theme) =>
createStyles({
root: {
"& > * + *": {
marginLeft: theme.spacing(2)
}
}
})
);
export default function Links() {
const classes = useStyles();
const [state, setState] = React.useState(true);
const handleClick = () => {
setState(!state);
};
return (
<Typography className={classes.root}>
<Link onClick={handleClick}>Link</Link>
<Typography>Click on above link to display hidden content..</Typography>
{state ? "" : "focus on Link using Tab key?"}
</Typography>
);
}
【问题讨论】:
-
不想用
component="button"的原因是什么? -
@RyanCogswell 我正在使用 NVDA 屏幕阅读器。当我使用 component="button" 时,屏幕阅读器会显示并读取链接作为按钮。我尝试在 Link 中使用 aria-label ,但没有解决问题。有没有办法解决这个问题?谢谢。
-
如果屏幕阅读器没有
href,则不适合将其读取为“链接”。当屏幕阅读器告诉某人他们在链接上时,这会让他们知道某些操作可用,例如在新选项卡或窗口中打开链接。对于只有onClick处理程序而没有任何 href 的元素,这些操作是不可能的。您已决定让这个元素看起来像一个链接,但屏幕阅读器并不关心它的外观——它仍然只是一个按钮。
标签: reactjs react-hooks material-ui