reactjs - 将 `eslint-plugin-jsx-a11y` 与 `styled-components` 一起使用
问题描述
该库styled-components
使您能够创建自定义样式的组件,例如:
const Button = styled.div`
display: inline-block;
border-radius: 3px;
padding: 0.5rem 0;
margin: 0.5rem 1rem;
width: 11rem;
background: transparent;
color: white;
border: 2px solid white;
`;
function App(props) {
return (<Button onClick={() => console.log("clicked!")}>Styled</Button>);
}
以下按钮是可点击的,但由<div />
元素组成,因此eslint-plugin-jsx-a11y
通常需要tabIndex
,role
等属性。
问题是因为从styled.div
ES-Lint 创建的组件看不到它。这个问题有插件或解决方案吗?
解决方案
我为此制作了一个非常酷的插件(希望如此)。它在我的一个存储库中发现了 6,567 个错误,这些错误是使用 airbnb/jsx-a11y 规则未发现的。请给它一颗星,这样我们就可以让更多人感兴趣,使用它,并为让所有人都能访问互联网做出贡献。
这是github 存储库,它涵盖了所有 4 种风格的组件/情感语法
现有的 a11y linting 已经变得不那么有用了(就我个人而言,我需要 linting 的 99% 的组件都是样式化的。)。下面的每个场景都会显示错误
Visible, non-interactive elements with click handlers must have at least one keyboard listener.`
const Div = styled.div``;
<Div onClick={() => null}/>
const Div = styled.div.attrs({ onClick: () => null})``;
<Div />
const Div = styled.div.attrs({ onClick: () => null})``;
const StyledDiv = styled(Div)``;
<StyledDiv />
const ButtonAsDiv = styled.button``;
<ButtonAsDiv as="div" onClick={() => null} />
它并不完美(没有 linters),但它非常有用,加上一点时间和社区支持,它可能会更好。
推荐阅读
- elasticsearch - 需要帮助来关闭 Logstash
- r - 传单热图上的套索能力
- azure-ad-b2c - 使用 ADB2C 如何强制对特定应用程序页面进行凭据重新验证
- php - 未捕获的 MongoDB\Driver\Exception\InvalidArgumentException:解析 ObjectId 字符串时出错:
- python - 遍历python中的目录
- package - 卸载一个或多个包裹
- python - Python 错误:导入 dbus 时构建轮子失败
- groovy - rpc_address 未设置为 0.0.0.0 时使用 groovy 脚本的 Cassandra 连接
- sql - 填写日期查询中缺少的月份
- mule - mule 3中异常“IllegalArgumentException:FlowConstruct不能为null”的根本原因是什么?