@rc-component/util 1.1.0 → 1.2.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.
@@ -83,8 +83,11 @@ export function injectCSS(css, option = {}) {
83
83
  return styleNode;
84
84
  }
85
85
  function findExistNode(key, option = {}) {
86
- const container = getContainer(option);
87
- return (option.styles || findStyles(container)).find(node => node.getAttribute(getMark(option)) === key);
86
+ let {
87
+ styles
88
+ } = option;
89
+ styles ||= findStyles(getContainer(option));
90
+ return styles.find(node => node.getAttribute(getMark(option)) === key);
88
91
  }
89
92
  export function removeCSS(key, option = {}) {
90
93
  const existNode = findExistNode(key, option);
@@ -4,10 +4,6 @@ declare const MARK = "__rc_react_root__";
4
4
  type ContainerType = (Element | DocumentFragment) & {
5
5
  [MARK]?: Root;
6
6
  };
7
- /** @private Test usage. Not work in prod */
8
- export declare function _r(node: React.ReactElement, container: ContainerType): void;
9
7
  export declare function render(node: React.ReactElement, container: ContainerType): void;
10
- /** @private Test usage. Not work in prod */
11
- export declare function _u(container: ContainerType): void;
12
8
  export declare function unmount(container: ContainerType): Promise<void>;
13
9
  export {};
@@ -1,83 +1,19 @@
1
- import * as ReactDOM from 'react-dom';
2
- // Let compiler not to search module usage
3
- const fullClone = {
4
- ...ReactDOM
5
- };
6
- const {
7
- version,
8
- render: reactRender,
9
- unmountComponentAtNode
10
- } = fullClone;
11
- let createRoot;
12
- try {
13
- const mainVersion = Number((version || '').split('.')[0]);
14
- if (mainVersion >= 18) {
15
- ({
16
- createRoot
17
- } = fullClone);
18
- }
19
- } catch (e) {
20
- // Do nothing;
21
- }
22
- function toggleWarning(skip) {
23
- const {
24
- __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
25
- } = fullClone;
26
- if (__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED && typeof __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED === 'object') {
27
- __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.usingClientEntryPoint = skip;
28
- }
29
- }
1
+ import { createRoot } from 'react-dom/client';
30
2
  const MARK = '__rc_react_root__';
31
3
 
32
4
  // ========================== Render ==========================
33
5
 
34
- function modernRender(node, container) {
35
- toggleWarning(true);
6
+ export function render(node, container) {
36
7
  const root = container[MARK] || createRoot(container);
37
- toggleWarning(false);
38
8
  root.render(node);
39
9
  container[MARK] = root;
40
10
  }
41
- function legacyRender(node, container) {
42
- reactRender?.(node, container);
43
- }
44
-
45
- /** @private Test usage. Not work in prod */
46
- export function _r(node, container) {
47
- if (process.env.NODE_ENV !== 'production') {
48
- return legacyRender(node, container);
49
- }
50
- }
51
- export function render(node, container) {
52
- if (createRoot) {
53
- modernRender(node, container);
54
- return;
55
- }
56
- legacyRender(node, container);
57
- }
58
11
 
59
12
  // ========================= Unmount ==========================
60
- async function modernUnmount(container) {
13
+ export async function unmount(container) {
61
14
  // Delay to unmount to avoid React 18 sync warning
62
15
  return Promise.resolve().then(() => {
63
16
  container[MARK]?.unmount();
64
17
  delete container[MARK];
65
18
  });
66
- }
67
- function legacyUnmount(container) {
68
- unmountComponentAtNode(container);
69
- }
70
-
71
- /** @private Test usage. Not work in prod */
72
- export function _u(container) {
73
- if (process.env.NODE_ENV !== 'production') {
74
- return legacyUnmount(container);
75
- }
76
- }
77
- export async function unmount(container) {
78
- if (createRoot !== undefined) {
79
- // Delay to unmount to avoid React 18 sync warning
80
- return modernUnmount(container);
81
- }
82
- legacyUnmount(container);
83
19
  }
package/es/ref.js CHANGED
@@ -1,7 +1,8 @@
1
- import { isValidElement } from 'react';
1
+ import { isValidElement, version } from 'react';
2
2
  import { ForwardRef, isMemo } from 'react-is';
3
3
  import useMemo from "./hooks/useMemo";
4
4
  import isFragment from "./React/isFragment";
5
+ const ReactMajorVersion = Number(version.split('.')[0]);
5
6
  export const fillRef = (ref, node) => {
6
7
  if (typeof ref === 'function') {
7
8
  ref(node);
@@ -35,7 +36,7 @@ export const supportRef = nodeOrComponent => {
35
36
  }
36
37
 
37
38
  // React 19 no need `forwardRef` anymore. So just pass if is a React element.
38
- if (isReactElement(nodeOrComponent) && nodeOrComponent.props.propertyIsEnumerable('ref')) {
39
+ if (isReactElement(nodeOrComponent) && ReactMajorVersion >= 19) {
39
40
  return true;
40
41
  }
41
42
  const type = isMemo(nodeOrComponent) ? nodeOrComponent.type.type : nodeOrComponent.type;
@@ -93,8 +93,11 @@ function injectCSS(css, option = {}) {
93
93
  return styleNode;
94
94
  }
95
95
  function findExistNode(key, option = {}) {
96
- const container = getContainer(option);
97
- return (option.styles || findStyles(container)).find(node => node.getAttribute(getMark(option)) === key);
96
+ let {
97
+ styles
98
+ } = option;
99
+ styles ||= findStyles(getContainer(option));
100
+ return styles.find(node => node.getAttribute(getMark(option)) === key);
98
101
  }
99
102
  function removeCSS(key, option = {}) {
100
103
  const existNode = findExistNode(key, option);
@@ -4,10 +4,6 @@ declare const MARK = "__rc_react_root__";
4
4
  type ContainerType = (Element | DocumentFragment) & {
5
5
  [MARK]?: Root;
6
6
  };
7
- /** @private Test usage. Not work in prod */
8
- export declare function _r(node: React.ReactElement, container: ContainerType): void;
9
7
  export declare function render(node: React.ReactElement, container: ContainerType): void;
10
- /** @private Test usage. Not work in prod */
11
- export declare function _u(container: ContainerType): void;
12
8
  export declare function unmount(container: ContainerType): Promise<void>;
13
9
  export {};
@@ -3,92 +3,24 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports._r = _r;
7
- exports._u = _u;
8
6
  exports.render = render;
9
7
  exports.unmount = unmount;
10
- var ReactDOM = _interopRequireWildcard(require("react-dom"));
11
- function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
12
- function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
13
- // Let compiler not to search module usage
14
- const fullClone = {
15
- ...ReactDOM
16
- };
17
- const {
18
- version,
19
- render: reactRender,
20
- unmountComponentAtNode
21
- } = fullClone;
22
- let createRoot;
23
- try {
24
- const mainVersion = Number((version || '').split('.')[0]);
25
- if (mainVersion >= 18) {
26
- ({
27
- createRoot
28
- } = fullClone);
29
- }
30
- } catch (e) {
31
- // Do nothing;
32
- }
33
- function toggleWarning(skip) {
34
- const {
35
- __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
36
- } = fullClone;
37
- if (__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED && typeof __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED === 'object') {
38
- __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.usingClientEntryPoint = skip;
39
- }
40
- }
8
+ var _client = require("react-dom/client");
41
9
  const MARK = '__rc_react_root__';
42
10
 
43
11
  // ========================== Render ==========================
44
12
 
45
- function modernRender(node, container) {
46
- toggleWarning(true);
47
- const root = container[MARK] || createRoot(container);
48
- toggleWarning(false);
13
+ function render(node, container) {
14
+ const root = container[MARK] || (0, _client.createRoot)(container);
49
15
  root.render(node);
50
16
  container[MARK] = root;
51
17
  }
52
- function legacyRender(node, container) {
53
- reactRender?.(node, container);
54
- }
55
-
56
- /** @private Test usage. Not work in prod */
57
- function _r(node, container) {
58
- if (process.env.NODE_ENV !== 'production') {
59
- return legacyRender(node, container);
60
- }
61
- }
62
- function render(node, container) {
63
- if (createRoot) {
64
- modernRender(node, container);
65
- return;
66
- }
67
- legacyRender(node, container);
68
- }
69
18
 
70
19
  // ========================= Unmount ==========================
71
- async function modernUnmount(container) {
20
+ async function unmount(container) {
72
21
  // Delay to unmount to avoid React 18 sync warning
73
22
  return Promise.resolve().then(() => {
74
23
  container[MARK]?.unmount();
75
24
  delete container[MARK];
76
25
  });
77
- }
78
- function legacyUnmount(container) {
79
- unmountComponentAtNode(container);
80
- }
81
-
82
- /** @private Test usage. Not work in prod */
83
- function _u(container) {
84
- if (process.env.NODE_ENV !== 'production') {
85
- return legacyUnmount(container);
86
- }
87
- }
88
- async function unmount(container) {
89
- if (createRoot !== undefined) {
90
- // Delay to unmount to avoid React 18 sync warning
91
- return modernUnmount(container);
92
- }
93
- legacyUnmount(container);
94
26
  }
package/lib/ref.js CHANGED
@@ -9,6 +9,7 @@ var _reactIs = require("react-is");
9
9
  var _useMemo = _interopRequireDefault(require("./hooks/useMemo"));
10
10
  var _isFragment = _interopRequireDefault(require("./React/isFragment"));
11
11
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
+ const ReactMajorVersion = Number(_react.version.split('.')[0]);
12
13
  const fillRef = (ref, node) => {
13
14
  if (typeof ref === 'function') {
14
15
  ref(node);
@@ -45,7 +46,7 @@ const supportRef = nodeOrComponent => {
45
46
  }
46
47
 
47
48
  // React 19 no need `forwardRef` anymore. So just pass if is a React element.
48
- if (isReactElement(nodeOrComponent) && nodeOrComponent.props.propertyIsEnumerable('ref')) {
49
+ if (isReactElement(nodeOrComponent) && ReactMajorVersion >= 19) {
49
50
  return true;
50
51
  }
51
52
  const type = (0, _reactIs.isMemo)(nodeOrComponent) ? nodeOrComponent.type.type : nodeOrComponent.type;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rc-component/util",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "Common Utils For React Component",
5
5
  "keywords": [
6
6
  "react",
@@ -27,7 +27,7 @@
27
27
  "coverage": "npm test -- --coverage",
28
28
  "lint": "eslint src/ --ext .tsx,.ts & eslint tests/ --ext .tsx,.ts",
29
29
  "prepare": "husky install",
30
- "prepublishOnly": "npm run compile && np --yolo --no-publish",
30
+ "prepublishOnly": "npm run compile && rc-np",
31
31
  "start": "dumi dev",
32
32
  "test": "rc-test"
33
33
  },
@@ -42,6 +42,7 @@
42
42
  },
43
43
  "devDependencies": {
44
44
  "@rc-component/father-plugin": "^2.0.1",
45
+ "@rc-component/np": "^1.0.3",
45
46
  "@testing-library/react": "^16.0.0",
46
47
  "@types/jest": "^29.4.0",
47
48
  "@types/node": "^22.5.5",
@@ -60,7 +61,6 @@
60
61
  "glob": "^9.2.1",
61
62
  "husky": "^9.1.6",
62
63
  "lint-staged": "^15.1.0",
63
- "np": "^10.0.2",
64
64
  "prettier": "^3.3.2",
65
65
  "rc-test": "^7.0.14",
66
66
  "react": "^18.0.0",