rsnext/packages/next/client/portal/index.tsx
Tim Neutkens fdabeb7098
Use SWC to compile Next.js core server files (#27167)
- Use SWC to compile Next.js core server files
- Ensure only @babel/runtime/helpers/interopRequireDefault helper is used

Just an initial comparison to compare size difference of this change.



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes
2021-07-14 18:12:04 +00:00

24 lines
674 B
TypeScript

import React from 'react'
import { createPortal } from 'react-dom'
type PortalProps = {
children: React.ReactNode
type: string
}
export const Portal: React.FC<PortalProps> = ({ children, type }) => {
let portalNode = React.useRef<HTMLElement | null>(null)
let [, forceUpdate] = React.useState<{}>()
React.useEffect(() => {
portalNode.current = document.createElement(type)
document.body.appendChild(portalNode.current)
forceUpdate({})
return () => {
if (portalNode.current) {
document.body.removeChild(portalNode.current)
}
}
}, [type])
return portalNode.current ? createPortal(children, portalNode.current) : null
}