@fruits-chain/react-native-xiaoshu
Version:
React Native UI library
193 lines (153 loc) • 5.12 kB
JavaScript
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; }
/* eslint-disable react/jsx-no-constructed-context-values */
/* eslint-disable no-unmodified-loop-condition */
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
/* eslint-disable @typescript-eslint/consistent-type-definitions */
import React, { createContext, Component } from 'react';
import { DeviceEventEmitter, NativeEventEmitter, StyleSheet, View } from 'react-native';
import PortalManager from './portal-manager';
export const PortalContext = /*#__PURE__*/createContext(null); // events
const addType = 'REACT_NATIVE_XIAOSHU_ADD_PORTAL';
const removeType = 'REACT_NATIVE_XIAOSHU_REMOVE_PORTAL'; // fix react native web does not support DeviceEventEmitter
const TopViewEventEmitter = DeviceEventEmitter || new NativeEventEmitter();
class PortalGuard {
constructor() {
_defineProperty(this, "nextKey", 10000);
_defineProperty(this, "add", e => {
const key = this.nextKey++;
TopViewEventEmitter.emit(addType, e, key);
return key;
});
_defineProperty(this, "remove", key => TopViewEventEmitter.emit(removeType, key));
}
}
/**
* portal
*/
export const portal = new PortalGuard();
/**
* Portal host renders all of its children `Portal` elements.
* For example, you can wrap a screen in `Portal.Host` to render items above the screen.
* If you're using the `Provider` component, it already includes `Portal.Host`.
*
* ## Usage
* ```js
* import * as React from 'react';
* import { Text } from 'react-native';
* import { Portal } from '@ant-design/react-native';
*
* export default class MyComponent extends React.Component {
* render() {
* return (
* <Portal.Host>
* <Text>Content of the app</Text>
* </Portal.Host>
* );
* }
* }
* ```
*
* Here any `Portal` elements under `<App />` are rendered alongside `<App />` and will appear above `<App />` like a `Modal`.
*/
export default class PortalHost extends Component {
constructor() {
super(...arguments);
_defineProperty(this, "_nextKey", 0);
_defineProperty(this, "_queue", []);
_defineProperty(this, "_manager", void 0);
_defineProperty(this, "_addTypeES", void 0);
_defineProperty(this, "_removeTypeES", void 0);
_defineProperty(this, "_setManager", manager => {
this._manager = manager;
});
_defineProperty(this, "_mount", (children, _key) => {
const key = _key || this._nextKey++;
if (this._manager) {
this._manager.mount(key, children);
} else {
this._queue.push({
type: 'mount',
key,
children
});
}
return key;
});
_defineProperty(this, "_update", (key, children) => {
if (this._manager) {
this._manager.update(key, children);
} else {
const op = {
type: 'mount',
key,
children
};
const index = this._queue.findIndex(o => o.type === 'mount' || o.type === 'update' && o.key === key);
if (index > -1) {
this._queue[index] = op;
} else {
this._queue.push(op);
}
}
});
_defineProperty(this, "_unmount", key => {
if (this._manager) {
this._manager.unmount(key);
} else {
this._queue.push({
type: 'unmount',
key
});
}
});
}
componentDidMount() {
const manager = this._manager;
const queue = this._queue;
this._addTypeES = TopViewEventEmitter.addListener(addType, this._mount);
this._removeTypeES = TopViewEventEmitter.addListener(removeType, this._unmount);
while (queue.length && manager) {
const action = queue.pop();
if (!action) {
continue;
} // tslint:disable-next-line:switch-default
switch (action.type) {
case 'mount':
manager.mount(action.key, action.children);
break;
case 'update':
manager.update(action.key, action.children);
break;
case 'unmount':
manager.unmount(action.key);
break;
}
}
}
componentWillUnmount() {
this._addTypeES.remove();
this._removeTypeES.remove(); // TopViewEventEmitter.removeListener(addType, this._mount)
// TopViewEventEmitter.removeListener(removeType, this._unmount)
}
render() {
return /*#__PURE__*/React.createElement(PortalContext.Provider, {
value: {
mount: this._mount,
update: this._update,
unmount: this._unmount
}
}, /*#__PURE__*/React.createElement(View, {
style: styles.container,
collapsable: false
}, this.props.children), /*#__PURE__*/React.createElement(PortalManager, {
ref: this._setManager
}));
}
}
_defineProperty(PortalHost, "displayName", 'Portal.Host');
const styles = StyleSheet.create({
container: {
flex: 1
}
});
//# sourceMappingURL=portal-host.js.map