| 43 | * Here any `Portal` elements under `<App />` are rendered alongside `<App />` and will appear above `<App />` like a `Modal`. |
| 44 | */ |
| 45 | export default class PortalHost extends React.Component<Props> { |
| 46 | static displayName = 'Portal.Host'; |
| 47 | |
| 48 | componentDidMount() { |
| 49 | const manager = this.manager; |
| 50 | const queue = this.queue; |
| 51 | |
| 52 | while (queue.length && manager) { |
| 53 | const action = queue.pop(); |
| 54 | if (action) { |
| 55 | // eslint-disable-next-line default-case |
| 56 | switch (action.type) { |
| 57 | case 'mount': |
| 58 | manager.mount(action.key, action.children); |
| 59 | break; |
| 60 | case 'update': |
| 61 | manager.update(action.key, action.children); |
| 62 | break; |
| 63 | case 'unmount': |
| 64 | manager.unmount(action.key); |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | private setManager = (manager: PortalManager | undefined | null) => { |
| 72 | this.manager = manager; |
| 73 | }; |
| 74 | |
| 75 | private mount = (children: React.ReactNode) => { |
| 76 | const key = this.nextKey++; |
| 77 | |
| 78 | if (this.manager) { |
| 79 | this.manager.mount(key, children); |
| 80 | } else { |
| 81 | this.queue.push({ type: 'mount', key, children }); |
| 82 | } |
| 83 | |
| 84 | return key; |
| 85 | }; |
| 86 | |
| 87 | private update = (key: number, children: React.ReactNode) => { |
| 88 | if (this.manager) { |
| 89 | this.manager.update(key, children); |
| 90 | } else { |
| 91 | const op: Operation = { type: 'mount', key, children }; |
| 92 | const index = this.queue.findIndex( |
| 93 | (o) => o.type === 'mount' || (o.type === 'update' && o.key === key) |
| 94 | ); |
| 95 | |
| 96 | if (index > -1) { |
| 97 | this.queue[index] = op; |
| 98 | } else { |
| 99 | this.queue.push(op as Operation); |
| 100 | } |
| 101 | } |
| 102 | }; |
nothing calls this directly
no test coverage detected
searching dependent graphs…