Skip to content

Commit

Permalink
VisuallyHidden: Convert component to TypeScript (#42220)
Browse files Browse the repository at this point in the history
* VisuallyHidden: Convert component to TypeScript

* Update CHANGELOG.md
  • Loading branch information
Petter Walbø Johnsgård authored Jul 9, 2022
1 parent 648e56e commit d5de22f
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 78 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- `Tip`: Convert to TypeScript ([#42262](https://github.com/WordPress/gutenberg/pull/42262)).
- `Scrollable`: Convert to TypeScript ([#42016](https://github.com/WordPress/gutenberg/pull/42016)).
- `Spacer`: Complete TypeScript migration ([#42013](https://github.com/WordPress/gutenberg/pull/42013)).
- `VisuallyHidden`: Convert to TypeScript ([#42220](https://github.com/WordPress/gutenberg/pull/42220)).
- `TreeSelect`: Refactor away from `_.repeat()` ([#42070](https://github.com/WordPress/gutenberg/pull/42070/)).
- `FocalPointPicker` updated to satisfy `react/exhaustive-deps` eslint rule ([#41520](https://github.com/WordPress/gutenberg/pull/41520)).
- `ColorPicker` updated to satisfy `react/exhaustive-deps` eslint rule ([#41294](https://github.com/WordPress/gutenberg/pull/41294)).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
/**
* External dependencies
*/
import type { ForwardedRef } from 'react';

/**
* Internal dependencies
*/
import { useContextSystem, contextConnect } from '../ui/context';
import {
useContextSystem,
contextConnect,
WordPressComponentProps,
} from '../ui/context';
import { visuallyHidden } from './styles';
import { View } from '../view';
import type { VisuallyHiddenProps } from './types';

/**
* @param {import('../ui/context').WordPressComponentProps<{ children: import('react').ReactNode }, 'div'>} props
* @param {import('react').ForwardedRef<any>} forwardedRef
*/
function VisuallyHidden( props, forwardedRef ) {
function UnconnectedVisuallyHidden(
props: WordPressComponentProps< VisuallyHiddenProps, 'div' >,
forwardedRef: ForwardedRef< any >
) {
const { style: styleProp, ...contextProps } = useContextSystem(
props,
'VisuallyHidden'
Expand All @@ -27,22 +36,21 @@ function VisuallyHidden( props, forwardedRef ) {
* `VisuallyHidden` is a component used to render text intended to be visually
* hidden, but will show for alternate devices, for example a screen reader.
*
* @example
* ```jsx
* import { VisuallyHidden } from `@wordpress/components`;
*
* function Example() {
* return (
* <VisuallyHidden>
* <label>Code is Poetry</label>
* </VisuallyHidden>
* );
* return (
* <VisuallyHidden>
* <label>Code is Poetry</label>
* </VisuallyHidden>
* );
* }
* ```
*/
const ConnectedVisuallyHidden = contextConnect(
VisuallyHidden,
export const VisuallyHidden = contextConnect(
UnconnectedVisuallyHidden,
'VisuallyHidden'
);

export default ConnectedVisuallyHidden;
export default VisuallyHidden;
42 changes: 0 additions & 42 deletions packages/components/src/visually-hidden/stories/index.js

This file was deleted.

66 changes: 66 additions & 0 deletions packages/components/src/visually-hidden/stories/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* External dependencies
*/
import type { ComponentMeta, ComponentStory } from '@storybook/react';

/**
* Internal dependencies
*/
import { VisuallyHidden } from '..';

const meta: ComponentMeta< typeof VisuallyHidden > = {
component: VisuallyHidden,
title: 'Components/VisuallyHidden',
argTypes: {
children: { control: { type: null } },
as: { control: { type: 'text' } },
},
parameters: {
controls: {
expanded: true,
},
docs: { source: { state: 'open' } },
},
};
export default meta;

export const Default: ComponentStory< typeof VisuallyHidden > = ( args ) => (
<>
<VisuallyHidden as="span" { ...args }>
This should not show.
</VisuallyHidden>
<div>
This text will{ ' ' }
<VisuallyHidden as="span" { ...args }>
but not inline{ ' ' }
</VisuallyHidden>{ ' ' }
always show.
</div>
</>
);

export const WithForwardedProps: ComponentStory< typeof VisuallyHidden > = (
args
) => (
<>
Additional props can be passed to VisuallyHidden and are forwarded to
the rendered element.{ ' ' }
<VisuallyHidden as="span" data-id="test" { ...args }>
Check out my data attribute!{ ' ' }
</VisuallyHidden>
Inspect the HTML to see!
</>
);

export const WithAdditionalClassNames: ComponentStory<
typeof VisuallyHidden
> = ( args ) => (
<>
Additional class names passed to VisuallyHidden extend the component
class name.{ ' ' }
<VisuallyHidden as="label" className="test-input" { ...args }>
Check out my class!{ ' ' }
</VisuallyHidden>
Inspect the HTML to see!
</>
);
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/** @type {import('react').CSSProperties} */
export const visuallyHidden = {
/**
* External dependencies
*/
import type { CSSProperties } from 'react';

export const visuallyHidden: CSSProperties = {
border: 0,
clip: 'rect(1px, 1px, 1px, 1px)',
WebkitClipPath: 'inset( 50% )',
Expand Down
19 changes: 0 additions & 19 deletions packages/components/src/visually-hidden/test/index.js

This file was deleted.

17 changes: 17 additions & 0 deletions packages/components/src/visually-hidden/test/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* External dependencies
*/
import { render, screen } from '@testing-library/react';

/**
* Internal dependencies
*/
import { VisuallyHidden } from '..';

describe( 'VisuallyHidden', () => {
it( 'should render correctly', () => {
const text = 'This is hidden';
render( <VisuallyHidden>{ text }</VisuallyHidden> );
expect( screen.getByText( text ) ).toMatchSnapshot();
} );
} );
11 changes: 11 additions & 0 deletions packages/components/src/visually-hidden/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* External dependencies
*/
import type { ReactNode } from 'react';

export type VisuallyHiddenProps = {
/**
* The children elements.
*/
children: ReactNode;
};

0 comments on commit d5de22f

Please sign in to comment.