-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #210 from wri/chore/upgrade-dependencies
Chore/upgrade dependencies
- Loading branch information
Showing
18 changed files
with
744 additions
and
3,031 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
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,89 @@ | ||
import React, { useState, useEffect, useRef, useCallback } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classNames from 'classnames'; | ||
|
||
import useOutsideClick from 'hooks/use-outside-click'; | ||
|
||
const DropdownContent = (props) => { | ||
const { children, className, ...dropdownContentProps } = props; | ||
return ( | ||
<div {...dropdownContentProps} className={`dropdown__content ${className}`}> | ||
{children} | ||
</div> | ||
); | ||
} | ||
|
||
const DropdownTrigger = (props) => { | ||
const { children, className, ...dropdownTriggerProps } = props; | ||
|
||
return ( | ||
<a {...dropdownTriggerProps} className={`dropdown__trigger ${className}`}> | ||
{children} | ||
</a> | ||
); | ||
} | ||
|
||
const Dropdown = (props) => { | ||
const { children, className, disabled } = props; | ||
const [active, setActive] = useState(false); | ||
const dropdownRef = useRef(null); | ||
|
||
const onToggleClick = (event) => { | ||
event.preventDefault(); | ||
if (active) { | ||
setActive(false); | ||
} else { | ||
setActive(true); | ||
} | ||
}; | ||
|
||
useOutsideClick(dropdownRef, () => setActive(false)); | ||
|
||
const classList = classNames('dropdown', { | ||
'dropdown--active': active, | ||
'dropdown--disabled': disabled | ||
}, className); | ||
|
||
// stick callback on trigger element | ||
const boundChildren = React.Children.map(children, child => { | ||
if (child.type === DropdownTrigger) { | ||
const originalOnClick = child.props.onClick; | ||
child = React.cloneElement(child, { | ||
onClick: (event) => { | ||
if (!disabled) { | ||
onToggleClick(event); | ||
if (originalOnClick) { | ||
originalOnClick.apply(child, arguments); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
return child; | ||
}); | ||
|
||
const cleanProps = { ...props }; | ||
delete cleanProps.active; | ||
|
||
return ( | ||
<div {...cleanProps} ref={dropdownRef} className={classList}> | ||
{boundChildren} | ||
</div> | ||
); | ||
} | ||
|
||
Dropdown.propTypes = { | ||
disabled: PropTypes.bool, | ||
active: PropTypes.bool, | ||
children: PropTypes.node, | ||
className: PropTypes.string, | ||
style: PropTypes.object | ||
}; | ||
|
||
Dropdown.defaultProps = { | ||
className: '' | ||
}; | ||
|
||
export { Dropdown, DropdownContent, DropdownTrigger }; | ||
|
||
export default Dropdown; |
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
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,20 @@ | ||
import { useEffect, useCallback } from 'react'; | ||
|
||
export default function useOutsideClick(element, action) { | ||
if (typeof action !== 'function') throw new Error('useOutsideClick expects action to be function'); | ||
|
||
const handleClickOutside = useCallback((event) => { | ||
if (!element.current) return; | ||
if (!element.current.contains(event.target)) action(); | ||
}, [element, action]); | ||
|
||
useEffect(() => { | ||
document.addEventListener('mousedown', handleClickOutside); | ||
document.addEventListener('touchStart', handleClickOutside); | ||
|
||
return () => { | ||
document.removeEventListener('mousedown', handleClickOutside); | ||
document.removeEventListener('touchstart', handleClickOutside); | ||
}; | ||
}, [handleClickOutside]); | ||
} |
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
Oops, something went wrong.