Skip to content

Commit

Permalink
Docs(Overlay): Add default and playground story
Browse files Browse the repository at this point in the history
  • Loading branch information
francinelucca committed Nov 6, 2024
1 parent 209c9b0 commit eab0004
Show file tree
Hide file tree
Showing 3 changed files with 256 additions and 4 deletions.
59 changes: 55 additions & 4 deletions packages/react/.storybook/preview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,69 @@ const preview = {
],
],
],
'Experimental',
[
'Experimental',
[
[
// Match on any story that leads with "Components"
'Components',
// This is the ordering of stories under "Components", by default
// we'll sort alphabetically
[
[
'*',
// Within a set of stories, set the order to the following
['*', 'Playground', /Playground$/, 'Features', 'Examples'],
],
],
]
]
],
'Behaviors',
'Hooks',
'Deprecated',
'Private',
[
'Deprecated',
[
[
// Match on any story that leads with "Components"
'Components',
// This is the ordering of stories under "Components", by default
// we'll sort alphabetically
[
[
'*',
// Within a set of stories, set the order to the following
['*', 'Playground', /Playground$/, 'Features', 'Examples'],
],
],
]
]
],
[
'Private',
[
[
// Match on any story that leads with "Components"
'Components',
// This is the ordering of stories under "Components", by default
// we'll sort alphabetically
[
[
'*',
// Within a set of stories, set the order to the following
['*', 'Playground', /Playground$/, 'Features', 'Examples'],
],
],
]
]
],
'*',
]

/**
* Get the position of an item within a given order. This will return the
* index if the item is defined in the given array, or the wildcard index
* if it is not explicitlyd efined
* if it is not explicitly defined
*/
function getPosition(order, item) {
const position = order.findIndex(value => {
Expand Down
198 changes: 198 additions & 0 deletions packages/react/src/Overlay/Overlay.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import React, {useState, useRef, type ComponentProps} from 'react'
import type {Args, Meta} from '@storybook/react'
import {XIcon} from '@primer/octicons-react'
import {Button, Text, useFocusTrap, Box, IconButton} from '..'
import Overlay from '../Overlay'

export default {
title: 'Private/Components/Overlay',
component: Overlay,
} as Meta<ComponentProps<typeof Overlay>>

export const Default = () => {
const [isOpen, setIsOpen] = useState(false)
const buttonRef = useRef<HTMLButtonElement>(null)
const confirmButtonRef = useRef<HTMLButtonElement>(null)
const anchorRef = useRef<HTMLDivElement>(null)
const closeOverlay = () => setIsOpen(false)
const containerRef = useRef<HTMLDivElement>(null)
useFocusTrap({
containerRef,
disabled: !isOpen,
})
return (
<Box ref={anchorRef}>
<Button
ref={buttonRef}
onClick={() => {
setIsOpen(!isOpen)
}}
>
Open overlay
</Button>
{isOpen ? (
<Overlay
initialFocusRef={confirmButtonRef}
returnFocusRef={buttonRef}
ignoreClickRefs={[buttonRef]}
onEscape={closeOverlay}
onClickOutside={closeOverlay}
width="large"
anchorSide="inside-right"
role="dialog"
aria-modal="true"
ref={containerRef}
>
<Box
sx={{
height: '100vh',
maxWidth: 'calc(-1rem + 100vw)',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
}}
>
<IconButton
aria-label="Close"
onClick={closeOverlay}
icon={XIcon}
variant="invisible"
sx={{
position: 'absolute',
left: '5px',
top: '5px',
}}
/>
<Text>Look! an overlay</Text>
</Box>
</Overlay>
) : null}
</Box>
)
}
export const Playground = (args: Args) => {
const [isOpen, setIsOpen] = useState(false)
const buttonRef = useRef<HTMLButtonElement>(null)
const confirmButtonRef = useRef<HTMLButtonElement>(null)
const anchorRef = useRef<HTMLDivElement>(null)
const closeOverlay = () => setIsOpen(false)
const containerRef = useRef<HTMLDivElement>(null)
useFocusTrap({
containerRef,
disabled: !isOpen,
})
return (
<Box ref={anchorRef}>
<Button
ref={buttonRef}
onClick={() => {
setIsOpen(!isOpen)
}}
>
Open overlay
</Button>
{isOpen ? (
<Overlay
initialFocusRef={confirmButtonRef}
returnFocusRef={buttonRef}
ignoreClickRefs={[buttonRef]}
onEscape={closeOverlay}
onClickOutside={closeOverlay}
width={args.width}
height={args.height}
aria-modal={args.role === 'dialog'}
ref={containerRef}
{...args}
>
<Box
sx={{
width: ['350px', '500px'],
}}
>
<Box
sx={{
height: '100vh',
maxWidth: 'calc(-1rem + 100vw)',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
}}
>
<IconButton
aria-label="Close"
onClick={closeOverlay}
icon={XIcon}
variant="invisible"
sx={{
position: 'absolute',
left: '5px',
top: '5px',
}}
/>
<Text>Look! an overlay</Text>
</Box>
</Box>
</Overlay>
) : null}
</Box>
)
}
Playground.args = {
width: 'auto',
height: 'auto',
side: 'outside-bottom',
preventOverflow: 'false',
role: 'dialog',
visibility: 'visible',
}
Playground.argTypes = {
width: {
type: {
name: 'enum',
value: ['small', 'medium', 'large', 'xlarge', 'xxlarge', 'auto'],
},
},
height: {
type: {
name: 'enum',
value: ['xsmall', 'small', 'medium', 'large', 'xlarge', 'auto', 'initial'],
},
},
side: {
type: {
name: 'enum',
value: [
'inside-top',
'inside-bottom',
'inside-left',
'inside-right',
'inside-center',
'outside-top',
'outside-bottom',
'outside-left',
'outside-right',
],
},
},
open: {
control: false,
},
portalContainerName: {
control: false,
},
style: {
control: false,
},
preventOverflow: {
type: 'boolean',
},
role: {
type: 'string',
},
visibility: {
type: {
name: 'enum',
value: ['visible', 'hidden'],
},
},
}
3 changes: 3 additions & 0 deletions packages/react/src/Overlay/Overlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import type {ForwardRefComponent as PolymorphicForwardRefComponent} from '../uti
import {useFeatureFlag} from '../FeatureFlags'

type StyledOverlayProps = {
/**
* Sets the width of the `Overlay`, pick from our set list of widths, or pass `auto` to automatically set the width based on the content of the `Overlay`. `small` corresponds to `256px`, `medium` corresponds to `320px`, `large` corresponds to `480px`, `xlarge` corresponds to `640px`, `xxlarge` corresponds to `960px`.
*/
width?: keyof typeof widthMap
height?: keyof typeof heightMap
maxHeight?: keyof Omit<typeof heightMap, 'auto' | 'initial'>
Expand Down

0 comments on commit eab0004

Please sign in to comment.