-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add DropdownMenu component (#826)
- Loading branch information
Showing
16 changed files
with
389 additions
and
76 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
16 changes: 16 additions & 0 deletions
16
packages/components/src/components/controls/ButtonSecondary.stories.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,16 @@ | ||
import { Story, Meta } from '@storybook/react'; | ||
import ButtonSecondary from './ButtonSecondary'; | ||
|
||
export default { | ||
title: 'Controls/Buttons/ButtonSecondary', | ||
component: ButtonSecondary, | ||
} as Meta; | ||
|
||
const Template: Story<React.ComponentProps<typeof ButtonSecondary>> = ( | ||
args | ||
) => <ButtonSecondary {...args} />; | ||
|
||
export const Example = Template.bind({}); | ||
Example.args = { | ||
children: 'Get started', | ||
}; |
26 changes: 26 additions & 0 deletions
26
packages/components/src/components/controls/ButtonSecondary.test.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,26 @@ | ||
import { fireEvent, render } from '@testing-library/react'; | ||
import ButtonSecondary from './ButtonSecondary'; | ||
|
||
test('normal button', () => { | ||
const handleClick = jest.fn(); | ||
const { getByText } = render( | ||
<ButtonSecondary onClick={handleClick}>test</ButtonSecondary> | ||
); | ||
expect(getByText('test').tagName).toBe('BUTTON'); | ||
fireEvent.click(getByText('test')); | ||
expect(handleClick).toHaveBeenCalled(); | ||
}); | ||
|
||
test('button as an anchor tag', () => { | ||
const handleClick = jest.fn(); | ||
const { getByText } = render( | ||
<ButtonSecondary href="/test" onClick={handleClick}> | ||
test | ||
</ButtonSecondary> | ||
); | ||
expect(getByText('test').tagName).toBe('A'); | ||
expect(getByText('test')).toHaveAttribute('href', '/test'); | ||
fireEvent.click(getByText('test')); | ||
expect(handleClick).toHaveBeenCalled(); | ||
expect(handleClick.mock.calls[0][0].defaultPrevented).toBeTruthy(); | ||
}); |
40 changes: 40 additions & 0 deletions
40
packages/components/src/components/controls/ButtonSecondary.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,40 @@ | ||
import styled from 'styled-components'; | ||
import ButtonBase from './ButtonBase'; | ||
import { v } from '../../theme'; | ||
|
||
const Button = styled(ButtonBase)` | ||
color: ${v.accentMain}; | ||
background-color: ${v.backgroundRaised}; | ||
box-shadow: ${v.shadowRaised}; | ||
transition: box-shadow 0.2s, color 0.2s, background 0.2s; | ||
&:hover { | ||
color: ${v.accentHover}; | ||
background-color: ${v.backgroundOverlay}; | ||
} | ||
&:active { | ||
background-color: ${v.backgroundOverlay}; | ||
box-shadow: ${v.shadowPressed}; | ||
} | ||
`; | ||
|
||
interface ButtonSecondaryProps { | ||
href?: string; | ||
disabled?: boolean; | ||
onClick?: (e: React.MouseEvent) => void; | ||
} | ||
|
||
function ButtonSecondary({ | ||
children, | ||
href, | ||
disabled, | ||
onClick, | ||
}: React.PropsWithChildren<ButtonSecondaryProps>) { | ||
return ( | ||
<Button href={href} onClick={onClick} disabled={disabled}> | ||
{children} | ||
</Button> | ||
); | ||
} | ||
|
||
export default ButtonSecondary; |
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
33 changes: 33 additions & 0 deletions
33
packages/components/src/components/controls/DropdownMenu.stories.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,33 @@ | ||
import { Story, Meta } from '@storybook/react'; | ||
import DropdownMenu from './DropdownMenu'; | ||
|
||
export default { | ||
title: 'Controls/DropdownMenu', | ||
component: DropdownMenu, | ||
} as Meta; | ||
|
||
const Template: Story<React.ComponentProps<typeof DropdownMenu>> = (args) => ( | ||
<div> | ||
<DropdownMenu {...args} /> | ||
<button>test</button> | ||
</div> | ||
); | ||
|
||
export const Example = Template.bind({}); | ||
Example.args = { | ||
value: 'a', | ||
options: [ | ||
{ value: 'a', label: 'Test 1' }, | ||
{ value: 'b', label: 'Test 2' }, | ||
], | ||
}; | ||
|
||
export const MenuRightAlign = Template.bind({}); | ||
MenuRightAlign.args = { | ||
align: 'right', | ||
value: 'a', | ||
options: [ | ||
{ value: 'a', label: 'Test 1' }, | ||
{ value: 'b', label: 'Test 2' }, | ||
], | ||
}; |
22 changes: 22 additions & 0 deletions
22
packages/components/src/components/controls/DropdownMenu.test.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,22 @@ | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import DropdownMenu from './DropdownMenu'; | ||
|
||
test('render', () => { | ||
const handleChange = jest.fn(); | ||
const { getByText } = render( | ||
<DropdownMenu | ||
value="a" | ||
options={[ | ||
{ label: 'Test 1', value: 'a' }, | ||
{ label: 'Test 2', value: 'b' }, | ||
]} | ||
onChange={handleChange} | ||
/> | ||
); | ||
|
||
fireEvent.click(getByText('Test 1')); | ||
expect(getByText('Test 2')).toBeInTheDocument(); | ||
|
||
fireEvent.click(getByText('Test 2')); | ||
expect(handleChange).toHaveBeenCalledWith('b'); | ||
}); |
86 changes: 86 additions & 0 deletions
86
packages/components/src/components/controls/DropdownMenu.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,86 @@ | ||
import { useState, useRef, useEffect } from 'react'; | ||
import { useClickAway } from 'react-use'; | ||
import styled, { css } from 'styled-components'; | ||
import { CaretDown } from 'phosphor-react'; | ||
import ButtonBase from './ButtonBase'; | ||
import ButtonSecondary from './ButtonSecondary'; | ||
import { v } from '../../theme'; | ||
|
||
type MenuAlign = 'left' | 'right'; | ||
|
||
const Container = styled.div<{ align?: MenuAlign }>` | ||
position: relative; | ||
${({ align }) => align === 'right' && 'text-align: right;'} | ||
`; | ||
|
||
const ButtonLabel = styled.span` | ||
margin-right: ${v.spacerXS}; | ||
`; | ||
|
||
const Menu = styled.div<{ align?: MenuAlign }>` | ||
position: absolute; | ||
top: 0; | ||
border-radius: ${v.radiusCard}; | ||
background-color: ${v.backgroundOverlay}; | ||
box-shadow: ${v.shadowOverlay}; | ||
padding: ${v.spacerXS}; | ||
${({ align }) => (align === 'right' ? 'right: 0;' : 'left: 0;')} | ||
`; | ||
|
||
const MenuItem = styled(ButtonBase)` | ||
display: block; | ||
border-radius: ${v.radiusMedia}; | ||
padding: ${v.spacerS} ${v.spacerM}; | ||
&:hover { | ||
background-color: ${v.backgroundHover}; | ||
} | ||
`; | ||
|
||
interface DropdownMenuProps { | ||
value: string; | ||
options: { value: string; label: string }[]; | ||
align?: MenuAlign; | ||
onChange?: (value: string) => void; | ||
} | ||
|
||
function DropdownMenu({ align, value, options, onChange }: DropdownMenuProps) { | ||
const [showOverlay, setShowOverlay] = useState(false); | ||
|
||
const menuRef = useRef<HTMLDivElement>(null); | ||
useClickAway(menuRef, () => setShowOverlay(false), [ | ||
'mousedown', | ||
'touchstart', | ||
'focusin', | ||
]); | ||
|
||
return ( | ||
<Container align={align}> | ||
<ButtonSecondary onClick={() => setShowOverlay(true)}> | ||
<ButtonLabel> | ||
{options.find((o) => o.value === value)?.label} | ||
</ButtonLabel> | ||
<CaretDown weight="bold" /> | ||
</ButtonSecondary> | ||
{showOverlay && ( | ||
<Menu ref={menuRef} align={align}> | ||
{options.map((option, i) => ( | ||
<MenuItem | ||
key={`menu-option-${i}`} | ||
autoFocus={i === 0} | ||
onClick={() => { | ||
onChange?.(option.value); | ||
setShowOverlay(false); | ||
}} | ||
> | ||
{option.label} | ||
</MenuItem> | ||
))} | ||
</Menu> | ||
)} | ||
</Container> | ||
); | ||
} | ||
|
||
export default DropdownMenu; |
Oops, something went wrong.