Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | 176x 176x 176x 176x 176x 176x 19x 176x 19x 13x 13x 6x 6x 19x 7x 7x 19x 2x 1x 19x 19x 19x 19x | import _ from 'lodash';
import React from 'react';
import PropTypes from 'react-peek/prop-types';
import Portal from '../Portal/Portal';
import { CSSTransition } from 'react-transition-group';
import { lucidClassNames, uniqueName } from '../../util/style-helpers';
import { omitProps, StandardProps } from '../../util/component-types';
const cx = lucidClassNames.bind('&-Overlay');
const { string, bool, func, node } = PropTypes;
export interface IOverlayProps
extends StandardProps,
React.DetailedHTMLProps<
React.HTMLAttributes<HTMLDivElement>,
HTMLDivElement
> {
/** Controls visibility. */
isShown: boolean;
/** Enables animated transitions during expansion and collapse. */
isAnimated: boolean;
/** Determines if it shows with a gray background. If `false`, the
background will be rendered but will be invisible, except for the
contents, and it won't capture any of the user click events. */
isModal: boolean;
/** Set your own id for the \`Portal\` is that is opened up to contain the
contents. In practice you should never need to set this manually. */
portalId?: string;
/** Fired when the user hits escape. */
onEscape: ({
event,
props,
}: {
event: KeyboardEvent;
props: IOverlayProps;
}) => void;
/** Fired when the user clicks on the background, this may or may not be
visible depending on \`isModal\`. */
onBackgroundClick: ({
event,
props,
}: {
event: React.MouseEvent;
props: IOverlayProps;
}) => void;
}
interface IOverlayState {
portalId: string;
}
export const defaultProps = {
isShown: false,
isModal: true,
onEscape: _.noop,
onBackgroundClick: _.noop,
isAnimated: true,
};
class Overlay extends React.Component<IOverlayProps, IOverlayState, {}> {
static displayName = 'Overlay';
static peek = {
description: `
Overlay is used to block user interaction with the rest of the app
until they have completed something.
`,
categories: ['utility'],
madeFrom: ['Portal'],
};
static propTypes = {
className: string`
Appended to the component-specific class names set on the root element.
`,
children: node`
Generally you should only have a single child element so the centering
works correctly.
`,
isShown: bool`
Controls visibility.
`,
isAnimated: bool,
isModal: bool`
Determines if it shows with a gray background. If \`false\`, the
background will be rendered but will be invisible, except for the
contents, and it won't capture any of the user click events.
`,
portalId: string`
Set your own id for the \`Portal\` is that is opened up to contain the
contents. In practice you should never need to set this manually.
`,
onEscape: func`
Fired when the user hits escape. Signature: \`({ event, props }) => {}\`
`,
onBackgroundClick: func`
Fired when the user clicks on the background, this may or may not be
visible depending on \`isModal\`. Signature: \`({ event, props }) => {}\`
`,
};
private rootHTMLDivElement = React.createRef<HTMLDivElement>();
static defaultProps = defaultProps;
state = {
// This must be in state because getDefaultProps only runs once per
// component import which causes collisions
portalId: this.props.portalId || uniqueName('Overlay-Portal-'),
};
componentDidMount(): void {
Eif (window && window.document) {
window.document.addEventListener('keydown', this.handleDocumentKeyDown);
}
}
componentWillUnmount(): void {
Eif (window && window.document) {
window.document.removeEventListener(
'keydown',
this.handleDocumentKeyDown
);
}
}
handleDocumentKeyDown = (event: KeyboardEvent): void => {
// If the user hits the "escape" key, then fire an `onEscape`
// TODO: use key helpers
Eif (event.keyCode === 27) {
this.props.onEscape({ event, props: this.props });
}
};
handleBackgroundClick = (event: React.MouseEvent): void => {
// Use the reference we previously stored from the `ref` to check what
// element was clicked on.
if (
this.rootHTMLDivElement.current &&
event.target === this.rootHTMLDivElement.current
) {
this.props.onBackgroundClick({ event, props: this.props });
}
};
render(): React.ReactNode {
const {
className,
isShown,
isModal,
isAnimated,
children,
...passThroughs
} = this.props;
const { portalId } = this.state;
const overlayElement = (
<div
{...omitProps(passThroughs, undefined, Object.keys(Overlay.propTypes))}
className={cx(className, '&', {
'&-is-not-modal': !isModal,
'&-is-animated': isAnimated,
})}
onClick={this.handleBackgroundClick}
ref={this.rootHTMLDivElement}
>
{children}
</div>
);
return (
<Portal portalId={portalId}>
{isAnimated ? (
<CSSTransition
in={isShown}
classNames={cx('&')}
timeout={300}
unmountOnExit
>
{overlayElement}
</CSSTransition>
) : isShown ? (
overlayElement
) : null}
</Portal>
);
}
}
export default Overlay;
|