Skip to content

Commit

Permalink
feat: Add VisuallyHidden component
Browse files Browse the repository at this point in the history
  • Loading branch information
diegohaz committed Apr 23, 2019
1 parent c8f7ca5 commit 7b1d826
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/reakit-playground/src/__deps/reakit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export default {
"reakit/system": require("reakit/system"),
"reakit/system/SystemProvider": require("reakit/system/SystemProvider"),
"reakit/system/SystemContext": require("reakit/system/SystemContext"),
"reakit/VisuallyHidden": require("reakit/VisuallyHidden"),
"reakit/VisuallyHidden/VisuallyHidden": require("reakit/VisuallyHidden/VisuallyHidden"),
"reakit/Tooltip": require("reakit/Tooltip"),
"reakit/Tooltip/TooltipState": require("reakit/Tooltip/TooltipState"),
"reakit/Tooltip/TooltipReference": require("reakit/Tooltip/TooltipReference"),
Expand Down
1 change: 1 addition & 0 deletions packages/reakit/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/ts
/utils
/system
/VisuallyHidden
/Tooltip
/Toolbar
/Tabbable
Expand Down
53 changes: 53 additions & 0 deletions packages/reakit/src/VisuallyHidden/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
path: /docs/visually-hidden
---

# VisuallyHidden

`VisuallyHidden` is a common techinique used in web accessibility to hide content from the visual client, but keep it readable for screen readers.

## Installation

```sh
npm install reakit
```

Learn more in [Get started](/docs/get-started).

## Usage

<!-- eslint-disable import/no-unresolved -->

```jsx
import { Button } from "reakit/Button";
import { VisuallyHidden } from "reakit/VisuallyHidden";
import UniversalAccess from "./UniversalAccess";

function Example() {
return (
<Button>
<VisuallyHidden>Universal Access</VisuallyHidden> <UniversalAccess />
</Button>
);
}
```

## Accessibility

- `VisuallyHidden` has all the styles necessary to hide it from visual clients, but keep it for screen readers.

Learn more in [Accessibility](/docs/accessibility).

## Composition

- `Separator` uses [Box](/docs/box).

Learn more in [Composition](/docs/composition#props-hooks).

## Props

<!-- Automatically generated -->

### `VisuallyHidden`

No props to show
47 changes: 47 additions & 0 deletions packages/reakit/src/VisuallyHidden/VisuallyHidden.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { unstable_createComponent } from "../utils/createComponent";
import { mergeProps } from "../utils/mergeProps";
import { unstable_useProps } from "../system/useProps";
import { BoxOptions, BoxProps, useBox } from "../Box/Box";
import { Keys } from "../__utils/types";
import { unstable_useOptions } from "../system";

export type VisuallyHiddenOptions = BoxOptions;

export type VisuallyHiddenProps = BoxProps;

export function useVisuallyHidden(
options: VisuallyHiddenOptions = {},
htmlProps: VisuallyHiddenProps = {}
) {
options = unstable_useOptions("VisuallyHidden", options, htmlProps);

htmlProps = mergeProps(
{
style: {
border: 0,
clip: "rect(0 0 0 0)",
height: "1px",
margin: "-1px",
overflow: "hidden",
padding: 0,
position: "absolute",
whiteSpace: "nowrap",
width: "1px"
}
} as typeof htmlProps,
htmlProps
);

htmlProps = unstable_useProps("VisuallyHidden", options, htmlProps);
htmlProps = useBox(options, htmlProps);
return htmlProps;
}

const keys: Keys<VisuallyHiddenOptions> = [...useBox.__keys];

useVisuallyHidden.__keys = keys;

export const VisuallyHidden = unstable_createComponent({
as: "div",
useHook: useVisuallyHidden
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as React from "react";
import { render } from "react-testing-library";
import { VisuallyHidden } from "../VisuallyHidden";

test("render", () => {
const { baseElement } = render(<VisuallyHidden />);
expect(baseElement).toMatchInlineSnapshot();
});
1 change: 1 addition & 0 deletions packages/reakit/src/VisuallyHidden/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./VisuallyHidden";
7 changes: 5 additions & 2 deletions packages/website/src/templates/Docs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
usePlaygroundState
} from "reakit-playground";
import createUseContext from "constate";
import * as fa from "react-icons/fa";
import { FaUniversalAccess } from "react-icons/fa";
import CoreLayout from "../components/CoreLayout";
import FiraCodeBold from "../fonts/FiraCode-Bold.woff";
import FiraCodeLight from "../fonts/FiraCode-Light.woff";
Expand Down Expand Up @@ -122,7 +122,10 @@ const { Compiler: renderAst } = new RehypeReact({
<div>
<PlaygroundPreview
noSystem={noSystem}
modules={{ constate: createUseContext, "react-icons/fa": fa }}
modules={{
constate: createUseContext,
"./UniversalAccess": FaUniversalAccess
}}
{...state}
/>
<PlaygroundEditor mode={mode} maxHeight={maxHeight} {...state} />
Expand Down

0 comments on commit 7b1d826

Please sign in to comment.