react-native-toast-notifications
Version:
[![Version][version-badge]][package] [![MIT License][license-badge]][license]
199 lines (172 loc) • 6.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _toast = _interopRequireDefault(require("./toast"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const {
height,
width
} = _reactNative.Dimensions.get("window");
class ToastContainer extends _react.Component {
constructor(props) {
super(props);
_defineProperty(this, "show", (message, toastOptions) => {
let id = (toastOptions === null || toastOptions === void 0 ? void 0 : toastOptions.id) || Math.random().toString();
const onDestroy = () => {
(toastOptions === null || toastOptions === void 0 ? void 0 : toastOptions.onClose) && (toastOptions === null || toastOptions === void 0 ? void 0 : toastOptions.onClose());
this.setState({
toasts: this.state.toasts.filter(t => t.id !== id)
});
};
requestAnimationFrame(() => {
this.setState({
toasts: [{
id,
onDestroy,
message,
open: true,
onHide: () => this.hide(id),
...this.props,
...toastOptions
}, ...this.state.toasts.filter(t => t.open)]
});
});
return id;
});
_defineProperty(this, "update", (id, message, toastOptions) => {
this.setState({
toasts: this.state.toasts.map(toast => toast.id === id ? { ...toast,
message,
...toastOptions
} : toast)
});
});
_defineProperty(this, "hide", id => {
this.setState({
toasts: this.state.toasts.map(t => t.id === id ? { ...t,
open: false
} : t)
});
});
_defineProperty(this, "hideAll", () => {
this.setState({
toasts: this.state.toasts.map(t => ({ ...t,
open: false
}))
});
});
_defineProperty(this, "isOpen", id => {
return this.state.toasts.some(t => t.id === id && t.open);
});
this.state = {
toasts: []
};
}
renderBottomToasts() {
const {
toasts
} = this.state;
let {
offset,
offsetBottom
} = this.props;
let style = {
bottom: offsetBottom || offset,
width: width,
justifyContent: "flex-end",
flexDirection: "column"
};
return /*#__PURE__*/_react.default.createElement(_reactNative.KeyboardAvoidingView, {
behavior: _reactNative.Platform.OS === "ios" ? "position" : undefined,
style: [styles.container, style],
pointerEvents: "box-none"
}, /*#__PURE__*/_react.default.createElement(_reactNative.SafeAreaView, null, toasts.filter(t => !t.placement || t.placement === "bottom").map(toast => /*#__PURE__*/_react.default.createElement(_toast.default, _extends({
key: toast.id
}, toast)))));
}
renderTopToasts() {
const {
toasts
} = this.state;
let {
offset,
offsetTop
} = this.props;
let style = {
top: offsetTop || offset,
width: width,
justifyContent: "flex-start",
flexDirection: "column-reverse"
};
return /*#__PURE__*/_react.default.createElement(_reactNative.KeyboardAvoidingView, {
behavior: _reactNative.Platform.OS === "ios" ? "position" : undefined,
style: [styles.container, style],
pointerEvents: "box-none"
}, /*#__PURE__*/_react.default.createElement(_reactNative.SafeAreaView, null, toasts.filter(t => t.placement === "top").map(toast => /*#__PURE__*/_react.default.createElement(_toast.default, _extends({
key: toast.id
}, toast)))));
}
renderCenterToasts() {
const {
toasts
} = this.state;
let {
offset,
offsetTop
} = this.props;
let style = {
top: offsetTop || offset,
height: height,
width: width,
justifyContent: "center",
flexDirection: "column-reverse"
};
const data = toasts.filter(t => t.placement === "center");
const foundToast = data.length > 0;
if (!foundToast) return null;
return /*#__PURE__*/_react.default.createElement(_reactNative.KeyboardAvoidingView, {
behavior: _reactNative.Platform.OS === "ios" ? "position" : undefined,
style: [styles.container, style],
pointerEvents: "box-none"
}, toasts.filter(t => t.placement === "center").map(toast => /*#__PURE__*/_react.default.createElement(_toast.default, _extends({
key: toast.id
}, toast))));
}
render() {
return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, this.renderTopToasts(), this.renderBottomToasts(), this.renderCenterToasts());
}
}
_defineProperty(ToastContainer, "defaultProps", {
placement: "bottom",
offset: 10,
swipeEnabled: true
});
const styles = _reactNative.StyleSheet.create({
container: {
flex: 0,
// @ts-ignore: fixed is available on web.
position: _reactNative.Platform.OS === "web" ? "fixed" : "absolute",
maxWidth: "100%",
zIndex: 999999,
elevation: 999999,
alignSelf: 'center',
...(_reactNative.Platform.OS === "web" ? {
overflow: "hidden",
userSelect: 'none'
} : null)
},
message: {
color: "#333"
}
});
var _default = ToastContainer;
exports.default = _default;
//# sourceMappingURL=toast-container.js.map