| 1 |
import React, { useEffect } from 'react' |
| 2 |
|
| 3 |
const SLOT_ID = 'snippets-screen-meta-slot' |
| 4 |
|
| 5 |
export interface ScreenMetaSlotProps { |
| 6 |
hidden?: boolean |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Relocates the WordPress Screen Options and Help tabs (and their panels) |
| 11 |
* from above the page content into this slot, so they appear directly below |
| 12 |
* the page's subnavigation bar. The slot div is rendered empty by React and |
| 13 |
* never reconciled with children, so adopting the foreign nodes is safe. |
| 14 |
* The nodes are returned to the top of the page when the slot unmounts. |
| 15 |
* |
| 16 |
* Stays mounted (with `hidden`) rather than being conditionally rendered by |
| 17 |
* the caller: unmounting hands the adopted nodes back to their native, |
| 18 |
* visible position at the top of the page instead of hiding them. |
| 19 |
*/ |
| 20 |
export const ScreenMetaSlot: React.FC<ScreenMetaSlotProps> = ({ hidden = false }) => { |
| 21 |
useEffect(() => { |
| 22 |
const slot = document.getElementById(SLOT_ID) |
| 23 |
const meta = document.getElementById('screen-meta') |
| 24 |
const links = document.getElementById('screen-meta-links') |
| 25 |
|
| 26 |
if (!slot || !meta || !links) { |
| 27 |
return undefined |
| 28 |
} |
| 29 |
|
| 30 |
const parent = meta.parentElement |
| 31 |
slot.append(meta, links) |
| 32 |
|
| 33 |
return () => { |
| 34 |
parent?.prepend(meta, links) |
| 35 |
} |
| 36 |
}, []) |
| 37 |
|
| 38 |
return <div id={SLOT_ID} className="snippets-screen-meta-slot" hidden={hidden} /> |
| 39 |
} |
| 40 |
|