Skip to content

Commit d82a9ef

Browse files
authored
chore(focusvisible): refactor storybook (#426)
1 parent b220324 commit d82a9ef

File tree

9 files changed

+210
-176
lines changed

9 files changed

+210
-176
lines changed

packages/buttongroup/demo/stories/ButtonGroupStory.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@ const Component = ({ getGroupProps, getButtonProps, selectedItem, buttons }: ICo
2525
{Object.keys(buttons).map((key, index) => (
2626
<button
2727
key={key}
28-
className={classNames({
29-
'bg-blue-300': key === selectedItem,
30-
border: true,
31-
'px-2': true,
32-
'py-1': true,
33-
'rounded-none': true
28+
className={classNames('border', 'px-2', 'py-1', 'rounded-none', {
29+
'bg-blue-300': key === selectedItem
3430
})}
3531
type="button"
3632
{...getButtonProps({ item: key, focusRef: buttons[key] })}

packages/focusvisible/README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
[npm version badge]: https://flat.badgen.net/npm/v/@zendeskgarden/container-focusvisible
44
[npm version link]: https://www.npmjs.com/package/@zendeskgarden/container-focusvisible
55

6-
This package includes containers relating to the `:focus-visible`
7-
polyfill in the [Garden Design System](https://zendeskgarden.github.io/).
6+
This package includes containers relating to [the `:focus-visible`
7+
polyfill](https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-visible) in
8+
the [Garden Design System](https://zendeskgarden.github.io/).
89

910
## Installation
1011

@@ -14,11 +15,12 @@ npm install @zendeskgarden/container-focusvisible
1415

1516
## Usage
1617

17-
For live examples check out our [storybook](https://zendeskgarden.github.io/react-containers).
18+
Check out [storybook](https://zendeskgarden.github.io/react-containers) for live
19+
examples.
1820

1921
### useFocusVisible
2022

21-
```jsx static
23+
```jsx
2224
import { useRef } from 'react';
2325
import styled from 'styled-components';
2426
import { useFocusVisible } from '@zendeskgarden/container-focusvisible';
@@ -55,7 +57,7 @@ const Example = () => {
5557

5658
### FocusVisibleContainer
5759

58-
```jsx static
60+
```jsx
5961
import { FocusVisibleContainer } from '@zendeskgarden/container-focusvisible';
6062

6163
const Example = () => {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Meta, Description } from '@storybook/addon-docs';
2+
import README from '../README.md';
3+
4+
<Meta title="Packages/FocusVisible/README" />
5+
6+
<Description>{README}</Description>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { useRef } from 'react';
2+
import { Meta, ArgsTable, Canvas, Story } from '@storybook/addon-docs';
3+
import { FocusVisibleContainer } from '@zendeskgarden/container-focusvisible';
4+
import { FocusVisibleStory } from './stories/FocusVisibleStory';
5+
6+
<Meta title="Packages/FocusVisible" component={FocusVisibleContainer} />
7+
8+
# API
9+
10+
<ArgsTable />
11+
12+
# Demo
13+
14+
<Canvas>
15+
<Story
16+
name="FocusVisible"
17+
args={{ as: 'hook' }}
18+
argTypes={{
19+
as: { options: ['container', 'hook'], control: 'radio', table: { category: 'Story' } },
20+
className: { control: false },
21+
dataAttribute: { control: false }
22+
}}
23+
>
24+
{args => {
25+
const scope = useRef();
26+
return <FocusVisibleStory {...args} scope={scope} />;
27+
}}
28+
</Story>
29+
</Canvas>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Copyright Zendesk, Inc.
3+
*
4+
* Use of this source code is governed under the Apache License, Version 2.0
5+
* found at http://www.apache.org/licenses/LICENSE-2.0.
6+
*/
7+
8+
import React, { forwardRef, RefObject } from 'react';
9+
import { Story } from '@storybook/react';
10+
import { createGlobalStyle } from 'styled-components';
11+
import { PALETTE } from '@zendeskgarden/react-theming';
12+
import {
13+
FocusVisibleContainer,
14+
IFocusVisibleContainerProps,
15+
IUseFocusVisibleProps,
16+
useFocusVisible
17+
} from '@zendeskgarden/container-focusvisible';
18+
19+
const GlobalStyle = createGlobalStyle`
20+
:focus {
21+
outline: none;
22+
}
23+
24+
.garden-focus-visible,
25+
[data-garden-focus-visible='true'] {
26+
box-shadow: 0 0 0 2px ${PALETTE.green[400]};
27+
}
28+
29+
blockquote p::before {
30+
content: '“';
31+
}
32+
33+
blockquote p::after {
34+
content: '”';
35+
}
36+
`;
37+
38+
/* eslint-disable jsx-a11y/no-noninteractive-tabindex */
39+
const Component = forwardRef<HTMLDivElement, any>((_, ref) => (
40+
<>
41+
<GlobalStyle />
42+
<div ref={ref}>
43+
<a href="#test">Anchor</a>
44+
<br />
45+
<button className="my-5 px-2 py-1" type="button">
46+
Button
47+
</button>
48+
<br />
49+
<label>
50+
<span>Input</span>
51+
<input className="ml-1" />
52+
</label>
53+
<div className="my-5" tabIndex={0}>
54+
Custom [tabindex=&quot;0&quot;]
55+
</div>
56+
<label>
57+
<span className="align-top">Textarea</span>
58+
<textarea className="ml-1" />
59+
</label>
60+
<br />
61+
<blockquote className="my-5" contentEditable suppressContentEditableWarning>
62+
<p>Content editable block quote</p>
63+
</blockquote>
64+
</div>
65+
</>
66+
));
67+
68+
Component.displayName = 'Component';
69+
70+
const Container = () => (
71+
<FocusVisibleContainer>{({ ref }) => <Component ref={ref} />}</FocusVisibleContainer>
72+
);
73+
74+
const Hook = ({ scope, ...props }: IUseFocusVisibleProps) => {
75+
useFocusVisible({ scope, ...props });
76+
77+
const ref = scope as RefObject<HTMLDivElement>;
78+
79+
return <Component {...props} ref={ref} />;
80+
};
81+
82+
interface IArgs extends IFocusVisibleContainerProps {
83+
as: 'hook' | 'container';
84+
scope: RefObject<HTMLElement>;
85+
}
86+
87+
export const FocusVisibleStory: Story<IArgs> = ({ as, ...props }) => {
88+
const FocusVisible = () => {
89+
switch (as) {
90+
case 'container':
91+
return <Container />;
92+
93+
case 'hook':
94+
default:
95+
return <Hook {...props} />;
96+
}
97+
};
98+
99+
return <FocusVisible />;
100+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Meta, Canvas, Story } from '@storybook/addon-docs';
2+
import { SelectionStory } from './stories/SelectionStory';
3+
4+
<Meta title="Packages/FocusVisible/[patterns]" />
5+
6+
# Patterns
7+
8+
## Selection
9+
10+
Demonstrate `useSelection` working along with `useFocusVisible`.
11+
12+
<Canvas>
13+
<Story name="Selection">{args => <SelectionStory {...args} />}</Story>
14+
</Canvas>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright Zendesk, Inc.
3+
*
4+
* Use of this source code is governed under the Apache License, Version 2.0
5+
* found at http://www.apache.org/licenses/LICENSE-2.0.
6+
*/
7+
8+
import React, { createRef, useRef } from 'react';
9+
import { Story } from '@storybook/react';
10+
import classNames from 'classnames';
11+
import { useFocusVisible } from '@zendeskgarden/container-focusvisible';
12+
import { useSelection } from '@zendeskgarden/container-selection';
13+
14+
export const SelectionStory: Story = () => {
15+
const items = ['Item 1', 'Item 2', 'Item 3'];
16+
const scope = useRef(null);
17+
const { getContainerProps, getItemProps, selectedItem } = useSelection({
18+
defaultSelectedIndex: 0
19+
});
20+
21+
useFocusVisible({ scope });
22+
23+
return (
24+
<ul className="flex" {...getContainerProps()} ref={scope}>
25+
{items.map(item => {
26+
const focusRef = createRef();
27+
28+
return (
29+
<li
30+
key={item}
31+
className={classNames(
32+
'border-0',
33+
'border-blue-600',
34+
'border-solid',
35+
'mx-3',
36+
'px-2',
37+
'pt-1',
38+
{ 'border-b-3': item === selectedItem }
39+
)}
40+
{...getItemProps({ item, focusRef })}
41+
>
42+
{item}
43+
</li>
44+
);
45+
})}
46+
</ul>
47+
);
48+
};

0 commit comments

Comments
 (0)