-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add popper dropdown * trigger toggleVisible internally * fix hook warnings * fix callback warning * fix className prop position
- Loading branch information
1 parent
aa90bb9
commit bd0057b
Showing
12 changed files
with
289 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"root": true, | ||
"parser": "babel-eslint", | ||
"plugins": ["react", "react-hooks", "jest"], | ||
"rules": { | ||
"no-unused-vars": "warn", | ||
"react/display-name": "warn", | ||
"react/jsx-key": "warn", | ||
"react/no-unescaped-entities": "warn", | ||
"react/jsx-no-duplicate-props": "warn", | ||
"react/jsx-no-target-blank": "warn", | ||
"react/no-unknown-property": "warn", | ||
"react/prop-types": "warn" | ||
}, | ||
"overrides": [ | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"parserOptions": { | ||
"ecmaVersion": 8, | ||
"sourceType": "module", | ||
"ecmaFeatures": { | ||
"jsx": true | ||
} | ||
} | ||
} | ||
], | ||
"extends": ["plugin:react/recommended"] | ||
} |
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
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 |
---|---|---|
@@ -1,71 +1,143 @@ | ||
import React, { Component } from 'react' | ||
import PropTypes from 'prop-types' | ||
import classNames from 'classnames' | ||
import wrapWithClickout from 'react-clickout' | ||
import SvgSymbol from '../SvgSymbol/SvgSymbol' | ||
import { ExternalContext } from '../External/External' | ||
|
||
class Dropdown extends Component { | ||
static contextType = ExternalContext | ||
|
||
state = { | ||
isVisible: false, | ||
} | ||
|
||
toggleDropdownVisible = () => { | ||
this.setState({isVisible: !this.state.isVisible}) | ||
} | ||
|
||
closeDropdown = () => { | ||
this.setState({isVisible: false}) | ||
} | ||
|
||
handleClickout() { | ||
if (!this.context.clickoutSuspended) { | ||
this.closeDropdown() | ||
import React, { useState, useEffect, useRef, useCallback } from "react"; | ||
import ReactDOM from "react-dom"; | ||
import PropTypes from "prop-types"; | ||
import classNames from "classnames"; | ||
import { usePopper } from "react-popper"; | ||
import UseEventListener from "../../hooks/UseEventListener"; | ||
|
||
const Portal = ({ children, querySelector = "#dropdown" }) => { | ||
return ReactDOM.createPortal(children, document.querySelector(querySelector)); | ||
}; | ||
|
||
const Dropdown = ({ | ||
dropdownButton, | ||
dropdownContent, | ||
className, | ||
rootProps, | ||
innerClassName, | ||
fixedMenu, | ||
toggleVisible, | ||
isVisible, | ||
placement, | ||
}) => { | ||
const [active, setActive] = useState(false); | ||
const [visible, setVisible] = useState(false); | ||
const referenceRef = useRef(); | ||
const popperRef = useRef(); | ||
|
||
const toggle = useCallback( | ||
(bool) => { | ||
setActive(bool); | ||
toggleVisible(); | ||
setTimeout(() => setVisible(bool), 1); | ||
}, | ||
[toggleVisible] | ||
); | ||
|
||
const { styles, attributes, forceUpdate } = usePopper( | ||
referenceRef.current, | ||
popperRef.current, | ||
{ | ||
placement: placement || "bottom-end", | ||
modifiers: [ | ||
{ | ||
name: "preventOverflow", | ||
options: { | ||
rootBoundary: "viewport", | ||
offset: [0, 10], | ||
}, | ||
}, | ||
], | ||
} | ||
); | ||
|
||
useEffect(() => { | ||
if (active && forceUpdate) { | ||
forceUpdate(); | ||
} | ||
}, [active, forceUpdate]); | ||
|
||
useEffect(() => { | ||
if (isVisible !== undefined) { | ||
if (isVisible && !active) { | ||
toggle(true); | ||
} | ||
|
||
if (!isVisible && active) { | ||
toggle(false); | ||
} | ||
} | ||
} | ||
}, [isVisible, active, toggle]); | ||
|
||
render() { | ||
const isDropdownVisible = | ||
this.props.toggleVisible ? this.props.isVisible : this.state.isVisible | ||
const handleDocumentClick = (event) => { | ||
if (referenceRef.current.contains(event.target)) { | ||
return null; | ||
} | ||
|
||
const renderFuncArgs = { | ||
isDropdownVisible, | ||
toggleDropdownVisible: this.toggleDropdownVisible, | ||
closeDropdown: this.closeDropdown | ||
if (popperRef.current.contains(event.target)) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div className={classNames('mr-dropdown', this.props.className)} {...this.props.rootProps}> | ||
{this.props.dropdownButton(renderFuncArgs)} | ||
{isDropdownVisible && ( | ||
<div className={classNames("mr-dropdown__wrapper", this.props.wrapperClassName)}> | ||
<div className="mr-dropdown__main"> | ||
<div className={classNames("mr-dropdown__inner", this.props.innerClassName, {"mr-fixed": this.props.fixedMenu})}> | ||
{!this.props.suppressControls && | ||
<SvgSymbol | ||
sym="icon-triangle" | ||
viewBox="0 0 15 10" | ||
className={classNames("mr-dropdown__arrow", this.props.arrowClassName)} | ||
aria-hidden | ||
/> | ||
} | ||
<div className="mr-dropdown__content"> | ||
{this.props.dropdownContent(renderFuncArgs)} | ||
toggle(false); | ||
}; | ||
|
||
UseEventListener("mousedown", handleDocumentClick); | ||
|
||
const renderFuncArgs = { | ||
isDropdownVisible: active, | ||
toggleDropdownVisible: () => toggle(!active), | ||
closeDropdown: () => toggle(false), | ||
}; | ||
|
||
return ( | ||
<div data-testid="mr-dropdown" {...rootProps}> | ||
<div ref={referenceRef} className={classNames("mr-dropdown", className)}> | ||
{dropdownButton(renderFuncArgs)} | ||
</div> | ||
<Portal> | ||
<div | ||
ref={popperRef} | ||
className="p-0.5 mr-z-250" | ||
style={styles.popper} | ||
{...attributes.popper} | ||
> | ||
{active && ( | ||
<div style={{ visibility: visible ? "visible" : "hidden" }}> | ||
<div className="mr-dropdown__main"> | ||
<div | ||
className={classNames("mr-dropdown__inner", innerClassName, { | ||
"mr-fixed": fixedMenu, | ||
})} | ||
> | ||
<div className="mr-dropdown__content"> | ||
{dropdownContent(renderFuncArgs)} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
) | ||
} | ||
} | ||
)} | ||
</div> | ||
</Portal> | ||
</div> | ||
); | ||
}; | ||
|
||
Dropdown.propTypes = { | ||
dropdownButton: PropTypes.func.isRequired, | ||
dropdownContent: PropTypes.func.isRequired, | ||
} | ||
className: PropTypes.string, | ||
rootProps: PropTypes.object, | ||
innerClassName: PropTypes.string, | ||
fixedMenu: PropTypes.bool, | ||
suppressControls: PropTypes.bool, | ||
arrowClassName: PropTypes.string, | ||
toggleVisible: PropTypes.func, | ||
isVisible: PropTypes.bool, | ||
placement: PropTypes.string, | ||
}; | ||
|
||
Dropdown.defaultProps = { | ||
toggleVisible: () => null, | ||
}; | ||
|
||
export default wrapWithClickout(Dropdown) | ||
export default Dropdown; |
Oops, something went wrong.