DEV Community

Jacob Paris
Jacob Paris

Posted on

4 1

Hide text for accessibility with React + Styled Components

I built this little component to hide text that isn't important when you have the whole screen visible for context, but for anyone using a screen reader that hidden content can be invaluable.

import styled from 'styled-components';

const Hide = styled.span.attrs({
    "aria-hidden": true
})`
    display: none;
`;
Enter fullscreen mode Exit fullscreen mode

So if you have a Shop Now button, you can make that a lot more accessible.

Shop <Hide>our {category.name} collection</Hide> now
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
dance2die profile image
Sung M. Kim

Would content wrapped inside <Hide> won't be visible to users but screen readers will be able to read it? Or is it the other way around?

(Kind of confused as aria-hidden is also set to true)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay