@dhis2-ui/portal 10.16.1 → 10.16.3-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/src/index.js +1 -0
- package/src/portal.js +37 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dhis2-ui/portal",
|
|
3
|
-
"version": "10.16.1",
|
|
3
|
+
"version": "10.16.3-alpha.1",
|
|
4
4
|
"description": "UI Portal",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -37,7 +37,8 @@
|
|
|
37
37
|
},
|
|
38
38
|
"files": [
|
|
39
39
|
"build",
|
|
40
|
-
"types"
|
|
40
|
+
"types",
|
|
41
|
+
"src"
|
|
41
42
|
],
|
|
42
43
|
"devDependencies": {
|
|
43
44
|
"react": "^18.3.1",
|
package/src/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Portal } from './portal.js'
|
package/src/portal.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import PropTypes from 'prop-types'
|
|
2
|
+
import { useState, useEffect } from 'react'
|
|
3
|
+
import { createPortal } from 'react-dom'
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* #dhis2-portal-root is provided by the App Platform and will be used by
|
|
7
|
+
* default.
|
|
8
|
+
*
|
|
9
|
+
* For non-platform apps, the mount node can be added manually to the HTML
|
|
10
|
+
* template.
|
|
11
|
+
*
|
|
12
|
+
* As a fallback, portals will be attached to the document body.
|
|
13
|
+
*
|
|
14
|
+
* This needs to be a function so that it works in tests as well.
|
|
15
|
+
*/
|
|
16
|
+
const getDefaultNode = () =>
|
|
17
|
+
document.getElementById('dhis2-portal-root') || document.body
|
|
18
|
+
|
|
19
|
+
export const Portal = ({ children, node, disable }) => {
|
|
20
|
+
const [mountNode, setMountNode] = useState(null)
|
|
21
|
+
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
setMountNode(node || getDefaultNode())
|
|
24
|
+
}, [node])
|
|
25
|
+
|
|
26
|
+
if (disable) {
|
|
27
|
+
return children
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return mountNode ? createPortal(children, mountNode) : mountNode
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
Portal.propTypes = {
|
|
34
|
+
children: PropTypes.node,
|
|
35
|
+
disable: PropTypes.bool,
|
|
36
|
+
node: PropTypes.node,
|
|
37
|
+
}
|