選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

heading-has-content.md 1.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # heading-has-content
  2. Enforce that heading elements (`h1`, `h2`, etc.) have content and that the content is accessible to screen readers. Accessible means that it is not hidden using the `aria-hidden` prop. Refer to the references to learn about why this is important.
  3. #### References
  4. 1. [Deque University](https://dequeuniversity.com/rules/axe/1.1/empty-heading)
  5. ## Rule details
  6. This rule takes one optional object argument of type object:
  7. ```json
  8. {
  9. "rules": {
  10. "jsx-a11y/heading-has-content": [ 2, {
  11. "components": [ "MyHeading" ],
  12. }],
  13. }
  14. }
  15. ```
  16. For the `components` option, these strings determine which JSX elements (**always including** `<h1>` thru `<h6>`) should be checked for having content. This is a good use case when you have a wrapper component that simply renders an `h1` element (like in React):
  17. ```js
  18. // Header.js
  19. const Header = props => {
  20. return (
  21. <h1 {...props}>{ props.children }</h1>
  22. );
  23. }
  24. ...
  25. // CreateAccount.js (for example)
  26. ...
  27. return (
  28. <Header>Create Account</Header>
  29. );
  30. ```
  31. #### Bad
  32. ```jsx
  33. function Foo(props) {
  34. return <label {...props} />
  35. }
  36. ```
  37. ### Succeed
  38. ```jsx
  39. <h1>Heading Content!</h1>
  40. <h1><TextWrapper /><h1>
  41. <h1 dangerouslySetInnerHTML={{ __html: 'foo' }} />
  42. ```
  43. ### Fail
  44. ```jsx
  45. <h1 />
  46. <h1><TextWrapper aria-hidden />
  47. ```