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 | 26x 26x 15x 28x 5x 17x 51x 34x 26x 18x 13x 13x 13x 13x 13x 1x 1x 1x 1x 1x 1x 1x 1x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 13x 100x 100x | /*
Copyright (c) 2018-2019 Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow
import * as React from 'react';
import {getOverrides, mergeOverrides} from '../helpers/overrides.js';
import DeleteAltIcon from '../icon/delete-alt.js';
import {
Body as StyledBody,
CloseIconSvg as StyledCloseIcon,
} from './styled-components.js';
import {KIND, TYPE} from './constants.js';
import {LocaleContext} from '../locale/index.js';
import type {
ToastPropsT,
ToastPrivateStateT,
SharedStylePropsArgT,
} from './types.js';
import type {OverridesT} from '../icon/index.js';
class Toast extends React.Component<ToastPropsT, ToastPrivateStateT> {
static defaultProps: $Shape<ToastPropsT> = {
autoHideDuration: 0,
closeable: true,
kind: KIND.info,
notificationType: TYPE.toast,
// Do we need a separate handler for
// when a notification dismisses automatically
onClose: () => {},
onBlur: () => {},
onFocus: () => {},
onMouseEnter: () => {},
onMouseLeave: () => {},
overrides: {},
};
autoHideTimeout: ?TimeoutID;
animateInTimer: ?TimeoutID;
animateOutCompleteTimer: ?TimeoutID;
state = {
isVisible: false,
isRendered: true,
};
componentDidMount() {
this.animateIn();
this.startTimeout();
}
componentWillUnmount() {
this.clearTimeout();
}
startTimeout() {
if (this.props.autoHideDuration) {
this.autoHideTimeout = setTimeout(
this.dismiss,
this.props.autoHideDuration,
);
}
}
clearTimeout() {
[
this.autoHideTimeout,
this.animateInTimer,
this.animateOutCompleteTimer,
].forEach(timerId => {
if (timerId) {
clearTimeout(timerId);
}
});
}
animateIn = () => {
// Defer to next event loop
this.animateInTimer = setTimeout(() => {
this.setState({isVisible: true});
}, 0);
};
animateOut = (callback: () => mixed = () => {}) => {
this.setState({isVisible: false});
// Remove the toast from the DOM after animation finishes
this.animateOutCompleteTimer = setTimeout(() => {
this.setState({isRendered: false});
callback();
}, 600);
};
dismiss = () => {
this.animateOut(this.props.onClose);
};
onFocus = (e: Event) => {
this.clearTimeout();
typeof this.props.onFocus === 'function' && this.props.onFocus(e);
};
onMouseEnter = (e: Event) => {
this.clearTimeout();
typeof this.props.onMouseEnter === 'function' && this.props.onMouseEnter(e);
};
onBlur = (e: Event) => {
this.startTimeout();
typeof this.props.onBlur === 'function' && this.props.onBlur(e);
};
onMouseLeave = (e: Event) => {
this.startTimeout();
typeof this.props.onMouseLeave === 'function' && this.props.onMouseLeave(e);
};
getSharedProps(): $Shape<SharedStylePropsArgT> {
const {kind, notificationType, closeable} = this.props;
const {isRendered, isVisible} = this.state;
return {
$kind: kind,
$type: notificationType,
$closeable: closeable,
$isRendered: isRendered,
$isVisible: isVisible,
};
}
render() {
const {children, closeable} = this.props;
const {isRendered} = this.state;
const {
Body: BodyOverride,
CloseIcon: CloseIconOverride,
} = this.props.overrides;
const [Body, bodyProps] = getOverrides(BodyOverride, StyledBody);
const [CloseIcon, closeIconProps] = getOverrides(
CloseIconOverride,
StyledCloseIcon,
);
const closeIconOverrides: OverridesT = mergeOverrides(
{Svg: CloseIcon},
// $FlowFixMe
{Svg: CloseIconOverride},
);
const sharedProps = this.getSharedProps();
if (!isRendered) {
return null;
}
return (
<LocaleContext.Consumer>
{locale => (
<Body
role="alert"
data-baseweb={this.props['data-baseweb'] || 'toast'}
tabIndex={-1}
{...sharedProps}
{...bodyProps}
// the properties below have to go after overrides
onBlur={this.onBlur}
onFocus={this.onFocus}
onMouseEnter={this.onMouseEnter}
onMouseLeave={this.onMouseLeave}
>
{typeof children === 'function'
? children({dismiss: this.dismiss})
: children}
{closeable ? (
<DeleteAltIcon
role="button"
tabIndex={0}
onClick={this.dismiss}
onKeyPress={event => {
if (event.key === 'Enter') {
this.dismiss();
}
}}
title={locale.toast.close}
{...sharedProps}
{...closeIconProps}
overrides={closeIconOverrides}
/>
) : null}
</Body>
)}
</LocaleContext.Consumer>
);
}
}
export default Toast;
|