Skip to content

Commit 496f26d

Browse files
Merge pull request #1929 from SUI-Components/feature/sui-svg-accessibility-improvements
Feature/sui svg accessibility improvements
2 parents aa04e3f + ac0fff6 commit 496f26d

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

packages/sui-svg/templates/icon-component.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@ const template = content => {
22
return `import {memo} from 'react'
33
import AtomIcon from '@s-ui/react-atom-icon'
44
5-
const MemoAtomIcon = memo(props =>
6-
<AtomIcon {...props}>
7-
<span dangerouslySetInnerHTML={{ __html: '${content}' }} />
8-
</AtomIcon>
9-
)
5+
const injectAccessibility = ({id, svg, title}) => {
6+
if (!title || !id) return svg.replace(/<svg([^>]*)>/, '<svg$1 aria-hidden="true">')
7+
8+
return svg.replace(
9+
/<svg([^>]*)>/,
10+
\`<svg$1 role="img" aria-labelledby="\${id}"><title id="\${id}">\${title}</title>\`
11+
)
12+
}
13+
14+
const MemoAtomIcon = memo(({id, title, ...props}) => {
15+
const safeContent = injectAccessibility({id, svg: \`${content}\`, title})
16+
17+
return (
18+
<AtomIcon {...props}>
19+
<span dangerouslySetInnerHTML={{__html: safeContent}} />
20+
</AtomIcon>
21+
)
22+
})
1023
1124
MemoAtomIcon.displayName = 'AtomIcon'
1225

0 commit comments

Comments
 (0)