简体   繁体   English

出于可访问性目的隐藏和 SVG 的动画 id

[英]Hide and SVG's animate ids for accessibility purposes

I'm doing a react component which is an SVG animation.我正在做一个反应组件,它是 SVG animation。 The react component's code is something like this: react 组件的代码是这样的:

const Loading = ({ className, ...props }) => {
  return (
    <svg
      aria-label="animated block"
      width="32"
      height={"32"}
      className={className}
      {...props}
    >
      <rect x="1" y="0" width="19" height="8" opacity="0.4" rx="5">
        <animate
          id="anim1"
          attributeName="width"
          from="19"
          to="8"
          begin="0s;anim2.end"
          dur="0.3s"
        />
        <animate
          id="anim2"
          attributeName="width"
          from="8"
          to="19"
          begin="anim1.end"
          dur="0.3s"
        />
      </rect>
    </svg>
  )
}

When showing a page with more than one of these, the accessibility linters show me the error "id attribute value must be unique".当显示包含多个这些的页面时,可访问性 linter 向我显示错误“id 属性值必须是唯一的”。

Is there any way to avoid this error?有没有办法避免这个错误?

I've tried using aria-hidden both in the svg element and in each animate element, but with no success.我已经尝试在svg元素和每个animate元素中使用aria-hidden ,但没有成功。

It seems like the only reason you currently need IDs on those <animate> elements is because you have two separate animations that each start when the other ends:您目前需要在这些<animate>元素上使用 ID 的唯一原因似乎是因为您有两个单独的动画,每个动画在另一个结束时开始:

 <svg width="32" height="32" viewBox="0 0 32 32"> <rect x="1" y="0" width="19" height="8" opacity="0.4" rx="5"> <animate id="anim1" attributeName="width" from="19" to="8" begin="0s;anim2.end" dur="0.3s" /> <animate id="anim2" attributeName="width" from="8" to="19" begin="anim1.end" dur="0.3s" /> </rect> </svg>

It would be better to write this as a single <animation> element, using the values attribute and repeatCount instead of to and from attributes on two animation elements that reference each other.最好将其编写为单个<animation>元素,使用values属性和repeatCount而不是两个相互引用的 animation 元素上的tofrom属性。 The exact same animation can be achieved like this:完全相同的 animation 可以这样实现:

 <svg width="32" height="32" viewBox="0 0 32 32"> <rect x="1" y="0" width="19" height="8" opacity="0.4" rx="5"> <animate attributeName="width" values="19;8;19" to="8" dur="0.6s" repeatCount="indefinite" /> </rect> </svg>

This eliminates the need to reference one element from another, and therefore removes the need for IDs entirely.这消除了从另一个元素引用一个元素的需要,因此完全不需要 ID。

Maybe you can pass a unique id in props也许你可以在 props 中传递一个唯一的 id

Example:例子:

 const Loading = ({ className, id, ...props }) => { return ( <svg aria-label="animated block" width="32" height={"32"} className={className} {...props} > <rect x="1" y="0" width="19" height="8" opacity="0.4" rx="5"> <animate id={`${id}-anim1`} attributeName="width" from="19" to="8" begin="0s;anim2.end" dur="0.3s" /> <animate id={`${id}-anim2`} attributeName="width" from="8" to="19" begin="anim1.end" dur="0.3s" /> </rect> </svg> ) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM