Skip to content

Commit

Permalink
feat(EventOverlay): add shouldLockFocus prop and react-focus-lock pac…
Browse files Browse the repository at this point in the history
…kage
  • Loading branch information
bfbiggs authored and pauljeter committed Jun 27, 2019
1 parent 855cd42 commit 239c0f4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"object-assign": "4.1.1",
"react-aria-modal": "^2.11.1",
"react-context-toolbox": "^2.0.2",
"react-focus-lock": "^1.19.1",
"react-uid": "^2.2.0"
},
"devDependencies": {
Expand Down
35 changes: 28 additions & 7 deletions react/src/lib/EventOverlay/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import omit from 'lodash/omit';
import FocusLock from 'react-focus-lock';

const defaultDims = {
offsetTop: 0,
Expand Down Expand Up @@ -837,10 +838,12 @@ class EventOverlay extends React.Component {
const {
children,
className,
focusLockProps,
isOpen,
maxHeight,
maxWidth,
portalNode,
shouldLockFocus,
showArrow,
style,
...props
Expand All @@ -866,7 +869,7 @@ class EventOverlay extends React.Component {
const contentNodes = (
isOpen && (
<div
className={
className={
'md-event-overlay' +
`${(showArrow && ` md-event-overlay--arrow`) || ''}` +
`${(side && ` md-event-overlay--${side}`) || ''}` +
Expand Down Expand Up @@ -895,12 +898,24 @@ class EventOverlay extends React.Component {
)
);

return portalNode ?
ReactDOM.createPortal(
contentNodes,
portalNode
)
: contentNodes;
const withFocusLock = content => (
shouldLockFocus
? <FocusLock {...focusLockProps}>{content}</FocusLock>
: content
);

const withPortal = content => (
portalNode
? ReactDOM.createPortal(
content,
portalNode
)
: content
);

return withFocusLock(
withPortal(contentNodes)
);
}
}

Expand All @@ -915,13 +930,15 @@ EventOverlay.defaultProps = {
className: '',
close: null,
direction: 'bottom-left',
focusLockProps: null,
isContained: '',
isDynamic: false,
isOpen: false,
maxHeight: null,
maxWidth: null,
portalNode: null,
scrollParentID: null,
shouldLockFocus: false,
showArrow: false,
style: null,
targetOffset: {
Expand Down Expand Up @@ -965,6 +982,8 @@ EventOverlay.propTypes = {
'right-top',
'right-bottom'
]),
/** @prop Props to be passed to focus lock component | null */
focusLockProps: PropTypes.object,
/** @prop Determines if the overlay is contained in bounding ancestor | '' */
isContained: PropTypes.oneOf([ true, false, 'horizontal', 'vertical', 'both', '']),
/** @prop When true, will flip children based on space available (does not work with isContained) | false */
Expand All @@ -979,6 +998,8 @@ EventOverlay.propTypes = {
portalNode: PropTypes.oneOfType([ PropTypes.node, PropTypes.instanceOf(Element) ]),
/** @prop Set the id of the scrollParent | null */
scrollParentID: PropTypes.string,
/** @prop Determines if focus should be locked to overlay | false */
shouldLockFocus: PropTypes.bool,
/** @prop Determines if the EventOverlay should show the open/close arrow | false */
showArrow: PropTypes.bool,
/** @prop Optional css styling | null */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ ShallowWrapper {
className=""
close={null}
direction="bottom-left"
focusLockProps={null}
isContained=""
isDynamic={false}
isOpen={false}
maxHeight={null}
maxWidth={null}
portalNode={null}
scrollParentID={null}
shouldLockFocus={false}
showArrow={false}
style={null}
targetOffset={
Expand Down

0 comments on commit 239c0f4

Please sign in to comment.