This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
-
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.
fix(Dropdown): added Dropdown component
- Loading branch information
1 parent
e6ae0c6
commit cc26958
Showing
9 changed files
with
202 additions
and
4 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
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,57 @@ | ||
@use './variables' as *; | ||
@use './mixins' as *; | ||
|
||
.dropdown { | ||
position: relative; | ||
display: inline-block; | ||
font-family: $font-family; | ||
|
||
&.left .list { | ||
left: 0; | ||
} | ||
|
||
&.right .list { | ||
right: 0; | ||
} | ||
|
||
.list { | ||
@include transition(transform, opacity); | ||
|
||
position: absolute; | ||
top: calc(100% + #{$margin}); | ||
overflow: hidden; | ||
min-width: 200px; | ||
|
||
background: $primary; | ||
color: $primary-accent; | ||
border-radius: $border-radius; | ||
|
||
transform: translateY(-$margin); | ||
opacity: 0; | ||
|
||
> * { | ||
padding: $padding; | ||
} | ||
} | ||
|
||
&:not(.visible) .list { | ||
pointer-events: none; | ||
} | ||
|
||
&.visible .list { | ||
transform: translateY(0); | ||
opacity: 1; | ||
} | ||
} | ||
|
||
.dropdownItem { | ||
color: inherit; | ||
text-decoration: none; | ||
position: relative; | ||
|
||
&.clickable { | ||
@include hover(); | ||
|
||
cursor: pointer; | ||
} | ||
} |
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,36 @@ | ||
import React from 'react'; | ||
|
||
import { Dropdown, DropdownItem, DropdownProps } from './Dropdown'; | ||
import { Button } from './Button'; | ||
import { Alignment } from './constants'; | ||
|
||
export default { | ||
title: 'Components/Dropdown', | ||
component: Dropdown, | ||
}; | ||
|
||
const Template = (args: DropdownProps) => <Dropdown {...args} />; | ||
export const Simple = Template.bind({}); | ||
Simple.args = { | ||
align: Alignment.Left, | ||
children: ( | ||
<> | ||
<DropdownItem | ||
href='https://google.com' | ||
type='a' | ||
> | ||
One | ||
</DropdownItem> | ||
<DropdownItem | ||
href='https://google.com' | ||
type='a' | ||
> | ||
Two | ||
</DropdownItem> | ||
<DropdownItem> | ||
Three | ||
</DropdownItem> | ||
</> | ||
), | ||
button: <Button>Click meee!</Button>, | ||
}; |
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,73 @@ | ||
import classNames from 'classnames'; | ||
import React, { ReactNode, useEffect, useRef, useState } from 'react'; | ||
import { Alignment, Layout } from './constants'; | ||
|
||
import styles from './Dropdown.scss'; | ||
import { Grid } from './Grid'; | ||
export { DropdownItem } from './DropdownItem'; | ||
|
||
export interface DropdownProps { | ||
align: Alignment; | ||
button: ReactNode; | ||
children: ReactNode; | ||
} | ||
|
||
export function Dropdown({ | ||
align, | ||
button, | ||
children, | ||
}: DropdownProps): JSX.Element { | ||
const elementRef = useRef<HTMLDivElement>(); | ||
const listenerRef = useRef<(event: MouseEvent) => void>(); | ||
const [visible, setVisible] = useState<boolean>(); | ||
|
||
useEffect(() => { | ||
if (visible) { | ||
listenerRef.current = (event: MouseEvent): void => { | ||
const path = event.composedPath(); | ||
if (!path.includes(elementRef.current)) { | ||
setVisible(false); | ||
} | ||
}; | ||
|
||
window.addEventListener('click', listenerRef.current, { | ||
passive: true, | ||
}); | ||
} else if (listenerRef.current) { | ||
window.removeEventListener('click', listenerRef.current); | ||
listenerRef.current = null; | ||
} | ||
|
||
return () => { | ||
if (listenerRef.current) { | ||
window.removeEventListener('click', listenerRef.current); | ||
} | ||
} | ||
}, [visible]); | ||
|
||
return ( | ||
<div | ||
ref={elementRef} | ||
className={classNames( | ||
styles.dropdown, | ||
styles[align], | ||
visible && styles.visible, | ||
)} | ||
> | ||
<div | ||
onClick={() => { | ||
setVisible(!visible); | ||
}} | ||
> | ||
{button} | ||
</div> | ||
<Grid | ||
className={styles.list} | ||
gap={0} | ||
layout={Layout.Vertical} | ||
> | ||
{children} | ||
</Grid> | ||
</div> | ||
); | ||
} |
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,30 @@ | ||
import classNames from 'classnames'; | ||
import React, { ElementType, ReactNode } from 'react'; | ||
|
||
import styles from './Dropdown.scss'; | ||
|
||
export interface DropdownProps { | ||
[key: string]: any; | ||
children: ReactNode; | ||
type?: ElementType; | ||
} | ||
|
||
export function DropdownItem({ | ||
children, | ||
type: Type = 'div', | ||
...props | ||
}: DropdownProps): JSX.Element { | ||
const isClickable = Type === 'a' || props.onClick || props.href || props.to; | ||
|
||
return ( | ||
<Type | ||
className={classNames( | ||
styles.dropdownItem, | ||
isClickable && styles.clickable, | ||
)} | ||
{...props} | ||
> | ||
{children} | ||
</Type> | ||
); | ||
} |
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