-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(focusvisible): refactor storybook (#426)
- Loading branch information
Showing
9 changed files
with
210 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Meta, Description } from '@storybook/addon-docs'; | ||
import README from '../README.md'; | ||
|
||
<Meta title="Packages/FocusVisible/README" /> | ||
|
||
<Description>{README}</Description> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useRef } from 'react'; | ||
import { Meta, ArgsTable, Canvas, Story } from '@storybook/addon-docs'; | ||
import { FocusVisibleContainer } from '@zendeskgarden/container-focusvisible'; | ||
import { FocusVisibleStory } from './stories/FocusVisibleStory'; | ||
|
||
<Meta title="Packages/FocusVisible" component={FocusVisibleContainer} /> | ||
|
||
# API | ||
|
||
<ArgsTable /> | ||
|
||
# Demo | ||
|
||
<Canvas> | ||
<Story | ||
name="FocusVisible" | ||
args={{ as: 'hook' }} | ||
argTypes={{ | ||
as: { options: ['container', 'hook'], control: 'radio', table: { category: 'Story' } }, | ||
className: { control: false }, | ||
dataAttribute: { control: false } | ||
}} | ||
> | ||
{args => { | ||
const scope = useRef(); | ||
return <FocusVisibleStory {...args} scope={scope} />; | ||
}} | ||
</Story> | ||
</Canvas> |
100 changes: 100 additions & 0 deletions
100
packages/focusvisible/demo/stories/FocusVisibleStory.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* Copyright Zendesk, Inc. | ||
* | ||
* Use of this source code is governed under the Apache License, Version 2.0 | ||
* found at http://www.apache.org/licenses/LICENSE-2.0. | ||
*/ | ||
|
||
import React, { forwardRef, RefObject } from 'react'; | ||
import { Story } from '@storybook/react'; | ||
import { createGlobalStyle } from 'styled-components'; | ||
import { PALETTE } from '@zendeskgarden/react-theming'; | ||
import { | ||
FocusVisibleContainer, | ||
IFocusVisibleContainerProps, | ||
IUseFocusVisibleProps, | ||
useFocusVisible | ||
} from '@zendeskgarden/container-focusvisible'; | ||
|
||
const GlobalStyle = createGlobalStyle` | ||
:focus { | ||
outline: none; | ||
} | ||
.garden-focus-visible, | ||
[data-garden-focus-visible='true'] { | ||
box-shadow: 0 0 0 2px ${PALETTE.green[400]}; | ||
} | ||
blockquote p::before { | ||
content: '“'; | ||
} | ||
blockquote p::after { | ||
content: '”'; | ||
} | ||
`; | ||
|
||
/* eslint-disable jsx-a11y/no-noninteractive-tabindex */ | ||
const Component = forwardRef<HTMLDivElement, any>((_, ref) => ( | ||
<> | ||
<GlobalStyle /> | ||
<div ref={ref}> | ||
<a href="#test">Anchor</a> | ||
<br /> | ||
<button className="my-5 px-2 py-1" type="button"> | ||
Button | ||
</button> | ||
<br /> | ||
<label> | ||
<span>Input</span> | ||
<input className="ml-1" /> | ||
</label> | ||
<div className="my-5" tabIndex={0}> | ||
Custom [tabindex="0"] | ||
</div> | ||
<label> | ||
<span className="align-top">Textarea</span> | ||
<textarea className="ml-1" /> | ||
</label> | ||
<br /> | ||
<blockquote className="my-5" contentEditable suppressContentEditableWarning> | ||
<p>Content editable block quote</p> | ||
</blockquote> | ||
</div> | ||
</> | ||
)); | ||
|
||
Component.displayName = 'Component'; | ||
|
||
const Container = () => ( | ||
<FocusVisibleContainer>{({ ref }) => <Component ref={ref} />}</FocusVisibleContainer> | ||
); | ||
|
||
const Hook = ({ scope, ...props }: IUseFocusVisibleProps) => { | ||
useFocusVisible({ scope, ...props }); | ||
|
||
const ref = scope as RefObject<HTMLDivElement>; | ||
|
||
return <Component {...props} ref={ref} />; | ||
}; | ||
|
||
interface IArgs extends IFocusVisibleContainerProps { | ||
as: 'hook' | 'container'; | ||
scope: RefObject<HTMLElement>; | ||
} | ||
|
||
export const FocusVisibleStory: Story<IArgs> = ({ as, ...props }) => { | ||
const FocusVisible = () => { | ||
switch (as) { | ||
case 'container': | ||
return <Container />; | ||
|
||
case 'hook': | ||
default: | ||
return <Hook {...props} />; | ||
} | ||
}; | ||
|
||
return <FocusVisible />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Meta, Canvas, Story } from '@storybook/addon-docs'; | ||
import { SelectionStory } from './stories/SelectionStory'; | ||
|
||
<Meta title="Packages/FocusVisible/[patterns]" /> | ||
|
||
# Patterns | ||
|
||
## Selection | ||
|
||
Demonstrate `useSelection` working along with `useFocusVisible`. | ||
|
||
<Canvas> | ||
<Story name="Selection">{args => <SelectionStory {...args} />}</Story> | ||
</Canvas> |
48 changes: 48 additions & 0 deletions
48
packages/focusvisible/demo/~patterns/stories/SelectionStory.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Copyright Zendesk, Inc. | ||
* | ||
* Use of this source code is governed under the Apache License, Version 2.0 | ||
* found at http://www.apache.org/licenses/LICENSE-2.0. | ||
*/ | ||
|
||
import React, { createRef, useRef } from 'react'; | ||
import { Story } from '@storybook/react'; | ||
import classNames from 'classnames'; | ||
import { useFocusVisible } from '@zendeskgarden/container-focusvisible'; | ||
import { useSelection } from '@zendeskgarden/container-selection'; | ||
|
||
export const SelectionStory: Story = () => { | ||
const items = ['Item 1', 'Item 2', 'Item 3']; | ||
const scope = useRef(null); | ||
const { getContainerProps, getItemProps, selectedItem } = useSelection({ | ||
defaultSelectedIndex: 0 | ||
}); | ||
|
||
useFocusVisible({ scope }); | ||
|
||
return ( | ||
<ul className="flex" {...getContainerProps()} ref={scope}> | ||
{items.map(item => { | ||
const focusRef = createRef(); | ||
|
||
return ( | ||
<li | ||
key={item} | ||
className={classNames( | ||
'border-0', | ||
'border-blue-600', | ||
'border-solid', | ||
'mx-3', | ||
'px-2', | ||
'pt-1', | ||
{ 'border-b-3': item === selectedItem } | ||
)} | ||
{...getItemProps({ item, focusRef })} | ||
> | ||
{item} | ||
</li> | ||
); | ||
})} | ||
</ul> | ||
); | ||
}; |
Oops, something went wrong.