Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/loud-spoons-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

Migrated `VisuallyHidden` to CSS Modules
2 changes: 1 addition & 1 deletion packages/react/src/DataTable/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ function Range({pageStart, pageEnd, totalCount}: RangeProps) {
<Message value={`Showing ${start} through ${end} of ${totalCount}`} />
<p className="TablePaginationRange">
{start}
<VisuallyHidden as="span">&nbsp;through&nbsp;</VisuallyHidden>
<VisuallyHidden>&nbsp;through&nbsp;</VisuallyHidden>
<span aria-hidden="true">‒</span>
{end} of {totalCount}
</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'
import type {Meta} from '@storybook/react'
import {VisuallyHidden} from './VisuallyHidden'

export default {
title: 'Components/VisuallyHidden/Dev',
component: VisuallyHidden,
} as Meta<typeof VisuallyHidden>

export const Default = () => (
<div>
<span>Visible Text</span>
<VisuallyHidden>Visually Hidden Text</VisuallyHidden>
</div>
)
10 changes: 10 additions & 0 deletions packages/react/src/VisuallyHidden/VisuallyHidden.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.VisuallyHidden {
&:not(:focus):not(:active):not(:focus-within) {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
white-space: nowrap;
clip-path: inset(50%);
}
}
48 changes: 36 additions & 12 deletions packages/react/src/VisuallyHidden/VisuallyHidden.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import styled from 'styled-components'
import type {SxProp} from '../sx'
import sx from '../sx'
import {toggleStyledComponent} from '../internal/utils/toggleStyledComponent'
import {clsx} from 'clsx'
import {useFeatureFlag} from '../FeatureFlags'
import React, {type HTMLAttributes} from 'react'
import classes from './VisuallyHidden.module.css'

const CSS_MODULES_FEATURE_FLAG = 'primer_react_css_modules_team'

/**
* Provides a component that implements the "visually hidden" technique. This is
Expand All @@ -12,17 +19,34 @@ import sx from '../sx'
*
* @see https://www.scottohara.me/blog/2023/03/21/visually-hidden-hack.html
*/
export const VisuallyHidden = styled.span<SxProp>`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wanted to share in case it was helpful (and feel free to ignore me if this is obvious 😂), when migrating styled components that are exported two helpful things I've found to double-check check are the as prop and ref support.

When we update these kinds of components these don't come along and they may be used downstream. It's hard to tell if these are breaking changes or not so I think we've been evaluating it by seeing if they are used this way in dotcom when updating.

Thankfully the integration tests tend to do a good job of double-checking this, also feel free to use Primer Query (e.g. this link, if you need to login then you'll have to click the link again since query params aren't persisted 😅)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the reminder. I'll definitely add this to my routine on components moving forward!

&:not(:focus):not(:active):not(:focus-within) {
clip-path: inset(50%);
height: 1px;
overflow: hidden;
position: absolute;
white-space: nowrap;
width: 1px;
}
const StyledVisuallyHidden = toggleStyledComponent(
CSS_MODULES_FEATURE_FLAG,
'span',
styled.span<SxProp>`
&:not(:focus):not(:active):not(:focus-within) {
clip-path: inset(50%);
height: 1px;
overflow: hidden;
position: absolute;
white-space: nowrap;
width: 1px;
}

${sx}
`,
)

${sx}
`
export const VisuallyHidden = ({className, children, ...rest}: VisuallyHiddenProps) => {
const enabled = useFeatureFlag(CSS_MODULES_FEATURE_FLAG)
return (
<StyledVisuallyHidden className={clsx(className, enabled && classes.VisuallyHidden)} {...rest}>
{children}
</StyledVisuallyHidden>
)
}

export type VisuallyHiddenProps = React.ComponentPropsWithoutRef<typeof VisuallyHidden>
export type VisuallyHiddenProps = React.PropsWithChildren<
HTMLAttributes<HTMLSpanElement> & {
className?: string
} & SxProp
>
Loading