@rc-component/portal 1.0.0-0
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/LICENSE +21 -0
- package/README.md +54 -0
- package/es/Context.d.ts +4 -0
- package/es/Context.js +3 -0
- package/es/Portal.d.ts +11 -0
- package/es/Portal.js +43 -0
- package/es/index.d.ts +4 -0
- package/es/index.js +2 -0
- package/es/useDom.d.ts +6 -0
- package/es/useDom.js +69 -0
- package/es/useScrollLocker.d.ts +1 -0
- package/es/useScrollLocker.js +43 -0
- package/lib/Context.d.ts +4 -0
- package/lib/Context.js +14 -0
- package/lib/Portal.d.ts +11 -0
- package/lib/Portal.js +58 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +13 -0
- package/lib/useDom.d.ts +6 -0
- package/lib/useDom.js +85 -0
- package/lib/useScrollLocker.d.ts +1 -0
- package/lib/useScrollLocker.js +57 -0
- package/package.json +72 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019-present react-component
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# rc-portal
|
|
2
|
+
|
|
3
|
+
React 18 supported Portal Component.
|
|
4
|
+
|
|
5
|
+
[![NPM version][npm-image]][npm-url] [](https://github.com/umijs/dumi) [![build status][github-actions-image]][github-actions-url] [![Codecov][codecov-image]][codecov-url] [![npm download][download-image]][download-url]
|
|
6
|
+
|
|
7
|
+
[npm-image]: http://img.shields.io/npm/v/rc-portal.svg?style=flat-square
|
|
8
|
+
[npm-url]: http://npmjs.org/package/rc-portal
|
|
9
|
+
[github-actions-image]: https://github.com/react-component/portal/workflows/CI/badge.svg
|
|
10
|
+
[github-actions-url]: https://github.com/react-component/portal/actions
|
|
11
|
+
[codecov-image]: https://img.shields.io/codecov/c/github/react-component/portal/master.svg?style=flat-square
|
|
12
|
+
[codecov-url]: https://codecov.io/gh/react-component/portal/branch/master
|
|
13
|
+
[download-image]: https://img.shields.io/npm/dm/rc-portal.svg?style=flat-square
|
|
14
|
+
[download-url]: https://npmjs.org/package/rc-portal
|
|
15
|
+
|
|
16
|
+
## Development
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install
|
|
20
|
+
npm start
|
|
21
|
+
open http://localhost:8000
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Feature
|
|
25
|
+
|
|
26
|
+
- React life cycle support portal component
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
[](https://npmjs.org/package/rc-portal)
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
```js | pure
|
|
35
|
+
import Portal from 'rc-portal';
|
|
36
|
+
|
|
37
|
+
const Demo = () => {
|
|
38
|
+
return <Portal open>Hello World</Portal>;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export default Demo;
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## 🔥 API
|
|
45
|
+
|
|
46
|
+
We use typescript to create the Type definition. You can view directly in IDE. But you can still check the type definition [here](https://github.com/react-component/portal/blob/master/src/interface.ts).
|
|
47
|
+
|
|
48
|
+
### Portal
|
|
49
|
+
|
|
50
|
+
| Prop | Description | Type | Default |
|
|
51
|
+
| ------------ | ---------------------------------- | ------------------------ | ------------- |
|
|
52
|
+
| getContainer | Customize portal container element | Element \| () => Element | document.body |
|
|
53
|
+
| open | Show the portal | boolean | false |
|
|
54
|
+
| autoLock | Lock screen scroll when open | boolean | false |
|
package/es/Context.d.ts
ADDED
package/es/Context.js
ADDED
package/es/Portal.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
export interface PortalProps {
|
|
3
|
+
/** Customize container element. Default will create a div in document.body when `open` */
|
|
4
|
+
getContainer?: () => Element | DocumentFragment;
|
|
5
|
+
children?: React.ReactNode;
|
|
6
|
+
/** Show the portal children */
|
|
7
|
+
open?: boolean;
|
|
8
|
+
/** Lock screen scroll when open */
|
|
9
|
+
autoLock?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export default function Portal(props: PortalProps): JSX.Element;
|
package/es/Portal.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { createPortal } from 'react-dom';
|
|
4
|
+
import OrderContext from "./Context";
|
|
5
|
+
import useDom from "./useDom";
|
|
6
|
+
import useScrollLocker from "./useScrollLocker"; // ZombieJ: Since React 18 strict mode logic change.
|
|
7
|
+
// We should rewrite for compatible.
|
|
8
|
+
|
|
9
|
+
export default function Portal(props) {
|
|
10
|
+
var open = props.open,
|
|
11
|
+
autoLock = props.autoLock,
|
|
12
|
+
getContainer = props.getContainer,
|
|
13
|
+
children = props.children;
|
|
14
|
+
|
|
15
|
+
var _React$useState = React.useState(open),
|
|
16
|
+
_React$useState2 = _slicedToArray(_React$useState, 2),
|
|
17
|
+
mergedRender = _React$useState2[0],
|
|
18
|
+
setMergedRender = _React$useState2[1];
|
|
19
|
+
|
|
20
|
+
useScrollLocker(autoLock && open); // ====================== Should Render ======================
|
|
21
|
+
|
|
22
|
+
React.useEffect(function () {
|
|
23
|
+
setMergedRender(open);
|
|
24
|
+
}, [open]); // ======================== Container ========================
|
|
25
|
+
|
|
26
|
+
var customizeContainer = getContainer === null || getContainer === void 0 ? void 0 : getContainer();
|
|
27
|
+
|
|
28
|
+
var _useDom = useDom(mergedRender && !customizeContainer),
|
|
29
|
+
_useDom2 = _slicedToArray(_useDom, 2),
|
|
30
|
+
defaultContainer = _useDom2[0],
|
|
31
|
+
queueCreate = _useDom2[1];
|
|
32
|
+
|
|
33
|
+
var mergedContainer = customizeContainer || defaultContainer; // ========================= Render ==========================
|
|
34
|
+
// Do not render when nothing need render
|
|
35
|
+
|
|
36
|
+
if (!mergedRender) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return /*#__PURE__*/React.createElement(OrderContext.Provider, {
|
|
41
|
+
value: queueCreate
|
|
42
|
+
}, /*#__PURE__*/createPortal(children, mergedContainer));
|
|
43
|
+
}
|
package/es/index.d.ts
ADDED
package/es/index.js
ADDED
package/es/useDom.d.ts
ADDED
package/es/useDom.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
|
|
2
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import useLayoutEffect from "rc-util/es/hooks/useLayoutEffect";
|
|
5
|
+
import OrderContext from "./Context";
|
|
6
|
+
var EMPTY_LIST = [];
|
|
7
|
+
/**
|
|
8
|
+
* Will add `div` to document. Nest call will keep order
|
|
9
|
+
* @param render Render DOM in document
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export default function useDom(render) {
|
|
13
|
+
var _React$useState = React.useState(function () {
|
|
14
|
+
var defaultEle = document.createElement('div');
|
|
15
|
+
return defaultEle;
|
|
16
|
+
}),
|
|
17
|
+
_React$useState2 = _slicedToArray(_React$useState, 1),
|
|
18
|
+
ele = _React$useState2[0]; // ========================== Order ==========================
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
var queueCreate = React.useContext(OrderContext);
|
|
22
|
+
|
|
23
|
+
var _React$useState3 = React.useState(EMPTY_LIST),
|
|
24
|
+
_React$useState4 = _slicedToArray(_React$useState3, 2),
|
|
25
|
+
queue = _React$useState4[0],
|
|
26
|
+
setQueue = _React$useState4[1];
|
|
27
|
+
|
|
28
|
+
var mergedQueueCreate = queueCreate || function (appendFn) {
|
|
29
|
+
setQueue(function (origin) {
|
|
30
|
+
return [appendFn].concat(_toConsumableArray(origin));
|
|
31
|
+
});
|
|
32
|
+
}; // =========================== DOM ===========================
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
function append() {
|
|
36
|
+
if (!ele.parentElement) {
|
|
37
|
+
document.body.appendChild(ele);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function cleanup() {
|
|
42
|
+
var _ele$parentElement;
|
|
43
|
+
|
|
44
|
+
(_ele$parentElement = ele.parentElement) === null || _ele$parentElement === void 0 ? void 0 : _ele$parentElement.removeChild(ele);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
useLayoutEffect(function () {
|
|
48
|
+
if (render) {
|
|
49
|
+
if (queueCreate) {
|
|
50
|
+
queueCreate(append);
|
|
51
|
+
} else {
|
|
52
|
+
append();
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
cleanup();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return cleanup;
|
|
59
|
+
}, [render]);
|
|
60
|
+
useLayoutEffect(function () {
|
|
61
|
+
if (queue.length) {
|
|
62
|
+
queue.forEach(function (appendFn) {
|
|
63
|
+
return appendFn();
|
|
64
|
+
});
|
|
65
|
+
setQueue(EMPTY_LIST);
|
|
66
|
+
}
|
|
67
|
+
}, [queue]);
|
|
68
|
+
return [ele, mergedQueueCreate];
|
|
69
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function useScrollLocker(lock?: boolean): void;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { updateCSS, removeCSS } from "rc-util/es/Dom/dynamicCSS";
|
|
3
|
+
import getScrollBarSize from "rc-util/es/getScrollBarSize";
|
|
4
|
+
var lockCount = 0;
|
|
5
|
+
var locked = false;
|
|
6
|
+
var UNIQUE_ID = "rc-util-locker-".concat(Date.now());
|
|
7
|
+
|
|
8
|
+
function syncLocker() {
|
|
9
|
+
var nextLocked = lockCount > 0;
|
|
10
|
+
|
|
11
|
+
if (locked !== nextLocked) {
|
|
12
|
+
locked = nextLocked;
|
|
13
|
+
|
|
14
|
+
if (locked) {
|
|
15
|
+
var scrollbarSize = getScrollBarSize();
|
|
16
|
+
updateCSS("\nhtml body {\n overflow-y: hidden;\n width: calc(100% - ".concat(scrollbarSize, "px);\n}"), UNIQUE_ID);
|
|
17
|
+
} else {
|
|
18
|
+
removeCSS(UNIQUE_ID);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default function useScrollLocker(lock) {
|
|
24
|
+
React.useLayoutEffect(function () {
|
|
25
|
+
if (lock) {
|
|
26
|
+
lockCount += 1;
|
|
27
|
+
syncLocker();
|
|
28
|
+
} else {
|
|
29
|
+
lockCount -= 1;
|
|
30
|
+
syncLocker();
|
|
31
|
+
}
|
|
32
|
+
}, [lock]);
|
|
33
|
+
var lockRef = React.useRef(lock);
|
|
34
|
+
lockRef.current = lock;
|
|
35
|
+
React.useLayoutEffect(function () {
|
|
36
|
+
return function () {
|
|
37
|
+
if (lockRef.current) {
|
|
38
|
+
lockCount -= 1;
|
|
39
|
+
syncLocker();
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}, []);
|
|
43
|
+
}
|
package/lib/Context.d.ts
ADDED
package/lib/Context.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.default = void 0;
|
|
9
|
+
|
|
10
|
+
var React = _interopRequireWildcard(require("react"));
|
|
11
|
+
|
|
12
|
+
var OrderContext = /*#__PURE__*/React.createContext(null);
|
|
13
|
+
var _default = OrderContext;
|
|
14
|
+
exports.default = _default;
|
package/lib/Portal.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
export interface PortalProps {
|
|
3
|
+
/** Customize container element. Default will create a div in document.body when `open` */
|
|
4
|
+
getContainer?: () => Element | DocumentFragment;
|
|
5
|
+
children?: React.ReactNode;
|
|
6
|
+
/** Show the portal children */
|
|
7
|
+
open?: boolean;
|
|
8
|
+
/** Lock screen scroll when open */
|
|
9
|
+
autoLock?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export default function Portal(props: PortalProps): JSX.Element;
|
package/lib/Portal.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
4
|
+
|
|
5
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
6
|
+
|
|
7
|
+
Object.defineProperty(exports, "__esModule", {
|
|
8
|
+
value: true
|
|
9
|
+
});
|
|
10
|
+
exports.default = Portal;
|
|
11
|
+
|
|
12
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
13
|
+
|
|
14
|
+
var React = _interopRequireWildcard(require("react"));
|
|
15
|
+
|
|
16
|
+
var _reactDom = require("react-dom");
|
|
17
|
+
|
|
18
|
+
var _Context = _interopRequireDefault(require("./Context"));
|
|
19
|
+
|
|
20
|
+
var _useDom3 = _interopRequireDefault(require("./useDom"));
|
|
21
|
+
|
|
22
|
+
var _useScrollLocker = _interopRequireDefault(require("./useScrollLocker"));
|
|
23
|
+
|
|
24
|
+
function Portal(props) {
|
|
25
|
+
var open = props.open,
|
|
26
|
+
autoLock = props.autoLock,
|
|
27
|
+
getContainer = props.getContainer,
|
|
28
|
+
children = props.children;
|
|
29
|
+
|
|
30
|
+
var _React$useState = React.useState(open),
|
|
31
|
+
_React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
|
|
32
|
+
mergedRender = _React$useState2[0],
|
|
33
|
+
setMergedRender = _React$useState2[1];
|
|
34
|
+
|
|
35
|
+
(0, _useScrollLocker.default)(autoLock && open); // ====================== Should Render ======================
|
|
36
|
+
|
|
37
|
+
React.useEffect(function () {
|
|
38
|
+
setMergedRender(open);
|
|
39
|
+
}, [open]); // ======================== Container ========================
|
|
40
|
+
|
|
41
|
+
var customizeContainer = getContainer === null || getContainer === void 0 ? void 0 : getContainer();
|
|
42
|
+
|
|
43
|
+
var _useDom = (0, _useDom3.default)(mergedRender && !customizeContainer),
|
|
44
|
+
_useDom2 = (0, _slicedToArray2.default)(_useDom, 2),
|
|
45
|
+
defaultContainer = _useDom2[0],
|
|
46
|
+
queueCreate = _useDom2[1];
|
|
47
|
+
|
|
48
|
+
var mergedContainer = customizeContainer || defaultContainer; // ========================= Render ==========================
|
|
49
|
+
// Do not render when nothing need render
|
|
50
|
+
|
|
51
|
+
if (!mergedRender) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return /*#__PURE__*/React.createElement(_Context.default.Provider, {
|
|
56
|
+
value: queueCreate
|
|
57
|
+
}, /*#__PURE__*/(0, _reactDom.createPortal)(children, mergedContainer));
|
|
58
|
+
}
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.default = void 0;
|
|
9
|
+
|
|
10
|
+
var _Portal = _interopRequireDefault(require("./Portal"));
|
|
11
|
+
|
|
12
|
+
var _default = _Portal.default;
|
|
13
|
+
exports.default = _default;
|
package/lib/useDom.d.ts
ADDED
package/lib/useDom.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
4
|
+
|
|
5
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
6
|
+
|
|
7
|
+
Object.defineProperty(exports, "__esModule", {
|
|
8
|
+
value: true
|
|
9
|
+
});
|
|
10
|
+
exports.default = useDom;
|
|
11
|
+
|
|
12
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
13
|
+
|
|
14
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
15
|
+
|
|
16
|
+
var React = _interopRequireWildcard(require("react"));
|
|
17
|
+
|
|
18
|
+
var _useLayoutEffect = _interopRequireDefault(require("rc-util/lib/hooks/useLayoutEffect"));
|
|
19
|
+
|
|
20
|
+
var _Context = _interopRequireDefault(require("./Context"));
|
|
21
|
+
|
|
22
|
+
var EMPTY_LIST = [];
|
|
23
|
+
/**
|
|
24
|
+
* Will add `div` to document. Nest call will keep order
|
|
25
|
+
* @param render Render DOM in document
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
function useDom(render) {
|
|
29
|
+
var _React$useState = React.useState(function () {
|
|
30
|
+
var defaultEle = document.createElement('div');
|
|
31
|
+
return defaultEle;
|
|
32
|
+
}),
|
|
33
|
+
_React$useState2 = (0, _slicedToArray2.default)(_React$useState, 1),
|
|
34
|
+
ele = _React$useState2[0]; // ========================== Order ==========================
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
var queueCreate = React.useContext(_Context.default);
|
|
38
|
+
|
|
39
|
+
var _React$useState3 = React.useState(EMPTY_LIST),
|
|
40
|
+
_React$useState4 = (0, _slicedToArray2.default)(_React$useState3, 2),
|
|
41
|
+
queue = _React$useState4[0],
|
|
42
|
+
setQueue = _React$useState4[1];
|
|
43
|
+
|
|
44
|
+
var mergedQueueCreate = queueCreate || function (appendFn) {
|
|
45
|
+
setQueue(function (origin) {
|
|
46
|
+
return [appendFn].concat((0, _toConsumableArray2.default)(origin));
|
|
47
|
+
});
|
|
48
|
+
}; // =========================== DOM ===========================
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
function append() {
|
|
52
|
+
if (!ele.parentElement) {
|
|
53
|
+
document.body.appendChild(ele);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function cleanup() {
|
|
58
|
+
var _ele$parentElement;
|
|
59
|
+
|
|
60
|
+
(_ele$parentElement = ele.parentElement) === null || _ele$parentElement === void 0 ? void 0 : _ele$parentElement.removeChild(ele);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
(0, _useLayoutEffect.default)(function () {
|
|
64
|
+
if (render) {
|
|
65
|
+
if (queueCreate) {
|
|
66
|
+
queueCreate(append);
|
|
67
|
+
} else {
|
|
68
|
+
append();
|
|
69
|
+
}
|
|
70
|
+
} else {
|
|
71
|
+
cleanup();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return cleanup;
|
|
75
|
+
}, [render]);
|
|
76
|
+
(0, _useLayoutEffect.default)(function () {
|
|
77
|
+
if (queue.length) {
|
|
78
|
+
queue.forEach(function (appendFn) {
|
|
79
|
+
return appendFn();
|
|
80
|
+
});
|
|
81
|
+
setQueue(EMPTY_LIST);
|
|
82
|
+
}
|
|
83
|
+
}, [queue]);
|
|
84
|
+
return [ele, mergedQueueCreate];
|
|
85
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function useScrollLocker(lock?: boolean): void;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
6
|
+
|
|
7
|
+
Object.defineProperty(exports, "__esModule", {
|
|
8
|
+
value: true
|
|
9
|
+
});
|
|
10
|
+
exports.default = useScrollLocker;
|
|
11
|
+
|
|
12
|
+
var React = _interopRequireWildcard(require("react"));
|
|
13
|
+
|
|
14
|
+
var _dynamicCSS = require("rc-util/lib/Dom/dynamicCSS");
|
|
15
|
+
|
|
16
|
+
var _getScrollBarSize = _interopRequireDefault(require("rc-util/lib/getScrollBarSize"));
|
|
17
|
+
|
|
18
|
+
var lockCount = 0;
|
|
19
|
+
var locked = false;
|
|
20
|
+
var UNIQUE_ID = "rc-util-locker-".concat(Date.now());
|
|
21
|
+
|
|
22
|
+
function syncLocker() {
|
|
23
|
+
var nextLocked = lockCount > 0;
|
|
24
|
+
|
|
25
|
+
if (locked !== nextLocked) {
|
|
26
|
+
locked = nextLocked;
|
|
27
|
+
|
|
28
|
+
if (locked) {
|
|
29
|
+
var scrollbarSize = (0, _getScrollBarSize.default)();
|
|
30
|
+
(0, _dynamicCSS.updateCSS)("\nhtml body {\n overflow-y: hidden;\n width: calc(100% - ".concat(scrollbarSize, "px);\n}"), UNIQUE_ID);
|
|
31
|
+
} else {
|
|
32
|
+
(0, _dynamicCSS.removeCSS)(UNIQUE_ID);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function useScrollLocker(lock) {
|
|
38
|
+
React.useLayoutEffect(function () {
|
|
39
|
+
if (lock) {
|
|
40
|
+
lockCount += 1;
|
|
41
|
+
syncLocker();
|
|
42
|
+
} else {
|
|
43
|
+
lockCount -= 1;
|
|
44
|
+
syncLocker();
|
|
45
|
+
}
|
|
46
|
+
}, [lock]);
|
|
47
|
+
var lockRef = React.useRef(lock);
|
|
48
|
+
lockRef.current = lock;
|
|
49
|
+
React.useLayoutEffect(function () {
|
|
50
|
+
return function () {
|
|
51
|
+
if (lockRef.current) {
|
|
52
|
+
lockCount -= 1;
|
|
53
|
+
syncLocker();
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
}, []);
|
|
57
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rc-component/portal",
|
|
3
|
+
"version": "1.0.0-0",
|
|
4
|
+
"description": "React Portal Component",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react",
|
|
7
|
+
"react-component",
|
|
8
|
+
"react-portal"
|
|
9
|
+
],
|
|
10
|
+
"homepage": "https://github.com/react-component/portal",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/react-component/portal/issues"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/react-component/portal.git"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"author": "smith3816@gmail.com",
|
|
20
|
+
"main": "./lib/index",
|
|
21
|
+
"module": "./es/index",
|
|
22
|
+
"typings": "es/index.d.ts",
|
|
23
|
+
"files": [
|
|
24
|
+
"lib",
|
|
25
|
+
"es",
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"compile": "father build",
|
|
30
|
+
"deploy": "npm run docs:build && npm run docs:deploy",
|
|
31
|
+
"docs:build": "dumi build",
|
|
32
|
+
"docs:deploy": "gh-pages -d docs-dist",
|
|
33
|
+
"lint": "eslint src/ --ext .tsx,.ts",
|
|
34
|
+
"lint:tsc": "tsc -p tsconfig.json --noEmit",
|
|
35
|
+
"now-build": "npm run docs:build",
|
|
36
|
+
"prepublishOnly": "npm run compile && np --no-cleanup --yolo --no-publish",
|
|
37
|
+
"prettier": "prettier --write \"**/*.{js,jsx,tsx,ts,less,md,json}\"",
|
|
38
|
+
"start": "dumi dev",
|
|
39
|
+
"test": "umi-test",
|
|
40
|
+
"test:coverage": "umi-test --coverage",
|
|
41
|
+
"watch": "father dev"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@babel/runtime": "^7.18.0",
|
|
45
|
+
"rc-util": "^5.8.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@testing-library/jest-dom": "^5.16.4",
|
|
49
|
+
"@testing-library/react": "^13.0.0",
|
|
50
|
+
"@types/jest": "^26.0.20",
|
|
51
|
+
"@types/react": "^18.0.0",
|
|
52
|
+
"@types/react-dom": "^18.0.0",
|
|
53
|
+
"@umijs/fabric": "^2.5.2",
|
|
54
|
+
"dumi": "^1.1.0",
|
|
55
|
+
"eslint": "^7.18.0",
|
|
56
|
+
"father": "^4.0.0-rc.8",
|
|
57
|
+
"gh-pages": "^3.1.0",
|
|
58
|
+
"np": "^5.0.3",
|
|
59
|
+
"prettier": "^2.1.2",
|
|
60
|
+
"react": "^18.0.0",
|
|
61
|
+
"react-dom": "^18.0.0",
|
|
62
|
+
"typescript": "^4.6.3",
|
|
63
|
+
"umi-test": "^1.9.7"
|
|
64
|
+
},
|
|
65
|
+
"peerDependencies": {
|
|
66
|
+
"react": ">=16.9.0",
|
|
67
|
+
"react-dom": ">=16.9.0"
|
|
68
|
+
},
|
|
69
|
+
"engines": {
|
|
70
|
+
"node": ">=8.x"
|
|
71
|
+
}
|
|
72
|
+
}
|