react-components-rails 1.0.0.beta1 → 1.0.0.beta2

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.
@@ -6,7 +6,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "react-components-rails"
8
8
  spec.licenses = ["MIT"]
9
- spec.version = "1.0.0.beta1"
9
+ spec.version = "1.0.0.beta2"
10
10
  spec.authors = ["Renaud Chaput"]
11
11
  spec.email = ["renchap@gmail.com"]
12
12
 
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.required_ruby_version = ">= 2.7.0"
25
25
 
26
26
  spec.add_development_dependency "bundler", "~> 2.3"
27
- spec.add_development_dependency "rake", "~> 13.0"
27
+ # spec.add_development_dependency "rake", "~> 12.0"
28
28
  # spec.add_development_dependency "minitest", "~> 5.0"
29
29
  # spec.add_development_dependency "capybara"
30
30
  # spec.add_development_dependency "selenium-webdriver"
data/src/index.ts CHANGED
@@ -1,9 +1,7 @@
1
1
  import React from "react"
2
2
  import ReactDOM from "react-dom"
3
- import intersection from "lodash/intersection"
4
- import keys from "lodash/keys"
5
- import assign from "lodash/assign"
6
- import omit from "lodash/omit"
3
+ import ReactDOMClient, { Root } from "react-dom/client"
4
+ import { intersection, keys, assign, omit } from "lodash"
7
5
  // import ujs from './ujs'
8
6
 
9
7
  const CLASS_ATTRIBUTE_NAME = "data-react-class"
@@ -17,6 +15,11 @@ declare global {
17
15
 
18
16
  const ReactComponentsRails = {
19
17
  registeredComponents: {} as { [name: string]: React.ComponentType },
18
+ mountedRoots: [] as Root[],
19
+ ReactDOMClient: undefined as
20
+ | typeof ReactDOM
21
+ | typeof ReactDOMClient
22
+ | undefined,
20
23
 
21
24
  render(node: Element, component: React.ComponentType) {
22
25
  const propsJson = node.getAttribute(PROPS_ATTRIBUTE_NAME)
@@ -24,7 +27,15 @@ const ReactComponentsRails = {
24
27
 
25
28
  const reactElement = React.createElement(component, props)
26
29
 
27
- ReactDOM.render(reactElement, node)
30
+ if (this.ReactDOMClient) {
31
+ if ("createRoot" in this.ReactDOMClient) {
32
+ const root = this.ReactDOMClient.createRoot(node)
33
+ root.render(reactElement)
34
+ this.mountedRoots.push(root)
35
+ } else {
36
+ this.ReactDOMClient.render(reactElement, node)
37
+ }
38
+ }
28
39
  },
29
40
 
30
41
  registerComponents(components: { [name: string]: React.Component }) {
@@ -43,9 +54,16 @@ const ReactComponentsRails = {
43
54
  },
44
55
 
45
56
  unmountComponents() {
46
- const mounted = document.querySelectorAll(`[${CLASS_ATTRIBUTE_NAME}]`)
47
- for (let i = 0; i < mounted.length; i += 1) {
48
- ReactDOM.unmountComponentAtNode(mounted[i])
57
+ if (this.ReactDOMClient) {
58
+ if ("createRoot" in this.ReactDOMClient) {
59
+ this.mountedRoots.forEach((root) => root.unmount())
60
+ this.mountedRoots = []
61
+ } else {
62
+ const mounted = document.querySelectorAll(`[${CLASS_ATTRIBUTE_NAME}]`)
63
+ for (let i = 0; i < mounted.length; i += 1) {
64
+ this.ReactDOMClient.unmountComponentAtNode(mounted[i])
65
+ }
66
+ }
49
67
  }
50
68
  },
51
69
 
@@ -76,7 +94,8 @@ const ReactComponentsRails = {
76
94
  }
77
95
  },
78
96
 
79
- setup(components = {}) {
97
+ async setup(components = {}) {
98
+ await this.loadReactDOMClient()
80
99
  if (typeof window.ReactComponentsRails === "undefined") {
81
100
  window.ReactComponentsRails = this
82
101
  // ujs.setup(this.mountComponents.bind(this), this.unmountComponents.bind(this))
@@ -85,6 +104,16 @@ const ReactComponentsRails = {
85
104
  window.ReactComponentsRails.registerComponents(components)
86
105
  window.ReactComponentsRails.mountComponents()
87
106
  },
107
+
108
+ async loadReactDOMClient() {
109
+ if (this.ReactDOMClient) return
110
+
111
+ try {
112
+ this.ReactDOMClient = await import("react-dom/client")
113
+ } catch (e) {
114
+ this.ReactDOMClient = ReactDOM
115
+ }
116
+ },
88
117
  }
89
118
 
90
119
  export default ReactComponentsRails
data/tsconfig.json CHANGED
@@ -11,8 +11,11 @@
11
11
  // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
12
12
 
13
13
  /* Language and Environment */
14
- "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
15
- "lib": ["DOM", "ES2016"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
14
+ "target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
15
+ "lib": [
16
+ "DOM",
17
+ "ES2016"
18
+ ] /* Specify a set of bundled library declaration files that describe the target runtime environment. */,
16
19
  // "jsx": "preserve", /* Specify what JSX code is generated. */
17
20
  // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
18
21
  // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
@@ -24,8 +27,8 @@
24
27
  // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
25
28
 
26
29
  /* Modules */
27
- "module": "commonjs", /* Specify what module code is generated. */
28
- // "rootDir": "./", /* Specify the root folder within your source files. */
30
+ "module": "CommonJS" /* Specify what module code is generated. */,
31
+ "rootDir": "./src" /* Specify the root folder within your source files. */,
29
32
  // "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
30
33
  // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
31
34
  // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
@@ -69,12 +72,12 @@
69
72
  /* Interop Constraints */
70
73
  // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
71
74
  // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
72
- "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
75
+ "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
73
76
  // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
74
- "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
77
+ "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
75
78
 
76
79
  /* Type Checking */
77
- "strict": true, /* Enable all strict type-checking options. */
80
+ "strict": true /* Enable all strict type-checking options. */,
78
81
  // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
79
82
  // "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
80
83
  // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
@@ -96,6 +99,6 @@
96
99
 
97
100
  /* Completeness */
98
101
  // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
99
- "skipLibCheck": true /* Skip type checking all .d.ts files. */
102
+ "skipLibCheck": true /* Skip type checking all .d.ts files. */
100
103
  }
101
104
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: react-components-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.beta1
4
+ version: 1.0.0.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Renaud Chaput
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-27 00:00:00.000000000 Z
11
+ date: 2022-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.3'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '13.0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '13.0'
41
27
  description:
42
28
  email:
43
29
  - renchap@gmail.com
@@ -56,12 +42,13 @@ files:
56
42
  - lib/component.rb
57
43
  - lib/helpers.rb
58
44
  - lib/railtie.rb
45
+ - lib/react-components-rails.rb
59
46
  - package.json
47
+ - pnpm-lock.yaml
60
48
  - react-components-rails.gemspec
61
49
  - src/index.ts
62
50
  - src/ujs.ts
63
51
  - tsconfig.json
64
- - yarn.lock
65
52
  homepage: https://github.com/renchap/webpacker-react
66
53
  licenses:
67
54
  - MIT