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

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c8ba858d748c6f96a4537c328a9181cf4a16a4b73d951f5623993cf0c38f4998
4
- data.tar.gz: 3c9516b00e4d060d1ab45b4cf57e8721dffdc9f683e8fd469f8960ce435b8b34
3
+ metadata.gz: b0c3195d86d0560cf12133385cef8ea31ab04781fef04127f548d8aa57586ef0
4
+ data.tar.gz: 7c733c62ce9dc6592afa462563ab8882aaf9a003e6aec95c403cfbeb1479ea3c
5
5
  SHA512:
6
- metadata.gz: 52b8286cec7712a22c21fa99f7b06a3520e7e97f97afa1e655d90f44d45dd7c04470aaa37c1cf04ae4d01d6eb5facd1e42406ebe0a9571f9225f95d68c46a0c1
7
- data.tar.gz: 69b40924f14cbef646d6b4081e24693482359b5140a860422c62bbae889095bc58e9cd2acf6255a538590012f2d3123b96f87bf81e67abde233f779fe6093d02
6
+ metadata.gz: 17d30edf163397119ab79c3119d43c44be8d8851b0b35a7025ca7002ea77a79c258f0d2ff92bbf01b1085e7b36fa8e131e8b724a12955aa84b6137da1def3aa4
7
+ data.tar.gz: e967f7206ba91fdd642addeae9f581277300d2ddc30df20d7eecf4588757ef063e14e0139dde49e8d0cdece71f835e0e55954cf2728a407398c35a53aa385d70
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- react-components-rails (1.0.0.beta2)
4
+ react-components-rails (1.0.0.beta3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-components-rails",
3
- "version": "1.0.0-beta.2",
3
+ "version": "1.0.0-beta.3",
4
4
  "homepage": "https://github.com/renchap/react-components-rails",
5
5
  "repository": "renchap/react-components-rails",
6
6
  "author": {
@@ -8,13 +8,13 @@
8
8
  "email": "renchap@gmail.com"
9
9
  },
10
10
  "license": "MIT",
11
- "type": "module",
12
11
  "source": "src/index.ts",
13
12
  "exports": {
14
- "require": "./dist/index.cjs",
15
- "default": "./dist/index.js"
13
+ "import": "./dist/index.mjs",
14
+ "require": "./dist/index.js",
15
+ "default": "./dist/index.mjs"
16
16
  },
17
- "main": "./dist/index.cjs",
17
+ "main": "./dist/index.js",
18
18
  "types": "dist/index.d.ts",
19
19
  "files": [
20
20
  "dist/",
@@ -24,10 +24,7 @@
24
24
  "build": "tsup src/index.ts --dts --sourcemap --format esm --target es6 --minify && tsup src/index.ts --sourcemap --format cjs --target es5 --minify",
25
25
  "dev": "tsup watch"
26
26
  },
27
- "dependencies": {
28
- "@types/lodash": "^4.14.182",
29
- "lodash": "^4.17.21"
30
- },
27
+ "dependencies": {},
31
28
  "peerDependencies": {
32
29
  "react": ">= 0.14",
33
30
  "react-dom": ">= 0.14"
data/pnpm-lock.yaml CHANGED
@@ -2,20 +2,14 @@ lockfileVersion: 5.4
2
2
 
3
3
  specifiers:
4
4
  '@swc/core': ^1.2.231
5
- '@types/lodash': ^4.14.182
6
5
  '@types/react': ^18.0.17
7
6
  '@types/react-dom': ^18.0.6
8
- lodash: ^4.17.21
9
7
  prettier: ^2.7.1
10
8
  react: ^18.2.0
11
9
  react-dom: ^18.2.0
12
10
  tsup: ^6.2.2
13
11
  typescript: ^4.7.4
14
12
 
15
- dependencies:
16
- '@types/lodash': 4.14.182
17
- lodash: 4.17.21
18
-
19
13
  devDependencies:
20
14
  '@swc/core': 1.2.231
21
15
  '@types/react': 18.0.17
@@ -220,10 +214,6 @@ packages:
220
214
  dev: true
221
215
  optional: true
222
216
 
223
- /@types/lodash/4.14.182:
224
- resolution: {integrity: sha512-/THyiqyQAP9AfARo4pF+aCGcyiQ94tX/Is2I7HofNRqoYLgN1PBoOWu2/zTA5zMxzP5EFutMtWtGAFRKUe961Q==}
225
- dev: false
226
-
227
217
  /@types/prop-types/15.7.5:
228
218
  resolution: {integrity: sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==}
229
219
  dev: true
@@ -733,10 +723,6 @@ packages:
733
723
  resolution: {integrity: sha512-HDWXG8isMntAyRF5vZ7xKuEvOhT4AhlRt/3czTSjvGUxjYCBVRQY48ViDHyfYz9VIoBkW4TMGQNapx+l3RUwdA==}
734
724
  dev: true
735
725
 
736
- /lodash/4.17.21:
737
- resolution: {integrity: sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==}
738
- dev: false
739
-
740
726
  /loose-envify/1.4.0:
741
727
  resolution: {integrity: sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==}
742
728
  hasBin: true
@@ -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.beta2"
9
+ spec.version = "1.0.0.beta3"
10
10
  spec.authors = ["Renaud Chaput"]
11
11
  spec.email = ["renchap@gmail.com"]
12
12
 
data/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import React from "react"
2
2
  import ReactDOM from "react-dom"
3
3
  import ReactDOMClient, { Root } from "react-dom/client"
4
- import { intersection, keys, assign, omit } from "lodash"
4
+
5
5
  // import ujs from './ujs'
6
6
 
7
7
  const CLASS_ATTRIBUTE_NAME = "data-react-class"
@@ -9,66 +9,86 @@ const PROPS_ATTRIBUTE_NAME = "data-react-props"
9
9
 
10
10
  declare global {
11
11
  interface Window {
12
- ReactComponentsRails: typeof ReactComponentsRails
12
+ ReactComponentsRails: ReactComponentsRails
13
13
  }
14
14
  }
15
15
 
16
- const ReactComponentsRails = {
17
- registeredComponents: {} as { [name: string]: React.ComponentType },
18
- mountedRoots: [] as Root[],
19
- ReactDOMClient: undefined as
16
+ class ReactComponentsRails {
17
+ #registeredComponents = {} as { [name: string]: React.ComponentType }
18
+ #mountedRoots = [] as Root[]
19
+ #ReactDOMClient = undefined as
20
20
  | typeof ReactDOM
21
21
  | typeof ReactDOMClient
22
- | undefined,
22
+ | undefined
23
+
24
+ static getInstance() {
25
+ if (typeof window.ReactComponentsRails === "undefined") {
26
+ const instance = new ReactComponentsRails()
27
+ window.ReactComponentsRails = instance
28
+
29
+ // ujs.setup(this.mountComponents.bind(this), this.unmountComponents.bind(this))
30
+ }
31
+
32
+ return window.ReactComponentsRails
33
+ }
23
34
 
24
- render(node: Element, component: React.ComponentType) {
35
+ private render(node: Element, component: React.ComponentType) {
25
36
  const propsJson = node.getAttribute(PROPS_ATTRIBUTE_NAME)
26
37
  const props = propsJson && JSON.parse(propsJson)
27
38
 
28
39
  const reactElement = React.createElement(component, props)
29
40
 
30
- if (this.ReactDOMClient) {
31
- if ("createRoot" in this.ReactDOMClient) {
32
- const root = this.ReactDOMClient.createRoot(node)
41
+ if (this.#ReactDOMClient) {
42
+ if ("createRoot" in this.#ReactDOMClient) {
43
+ const root = this.#ReactDOMClient.createRoot(node)
33
44
  root.render(reactElement)
34
- this.mountedRoots.push(root)
45
+ this.#mountedRoots.push(root)
35
46
  } else {
36
- this.ReactDOMClient.render(reactElement, node)
47
+ this.#ReactDOMClient.render(reactElement, node)
37
48
  }
38
49
  }
39
- },
40
-
41
- registerComponents(components: { [name: string]: React.Component }) {
42
- const collisions = intersection(
43
- keys(this.registeredComponents),
44
- keys(components)
45
- )
46
- if (collisions.length > 0) {
50
+ }
51
+
52
+ private registerComponents(components: {
53
+ [name: string]: React.ComponentType
54
+ }) {
55
+ const alreadyExisting: string[] = []
56
+
57
+ Object.keys(components).forEach((key) => {
58
+ if (this.#registeredComponents[key]) alreadyExisting.push(key)
59
+ else {
60
+ const comp = components[key]
61
+ this.#registeredComponents[key] = comp
62
+ }
63
+ })
64
+
65
+ if (alreadyExisting.length > 0) {
47
66
  console.error(
48
- `react-components-rails: can not register components. Following components are already registered: ${collisions}`
67
+ `react-components-rails: can not register components. Following components are already registered: ${alreadyExisting.join(
68
+ ", "
69
+ )}`
49
70
  )
50
71
  }
51
72
 
52
- assign(this.registeredComponents, omit(components, collisions))
53
73
  return true
54
- },
55
-
56
- unmountComponents() {
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
- }
67
- }
68
- },
74
+ }
69
75
 
70
- mountComponents() {
71
- const { registeredComponents } = this
76
+ // Not used for now, useful for UJS
77
+ // private unmountComponents() {
78
+ // if (this.#ReactDOMClient) {
79
+ // if ("createRoot" in this.#ReactDOMClient) {
80
+ // this.#mountedRoots.forEach((root) => root.unmount())
81
+ // this.#mountedRoots = []
82
+ // } else {
83
+ // const mounted = document.querySelectorAll(`[${CLASS_ATTRIBUTE_NAME}]`)
84
+ // for (let i = 0; i < mounted.length; i += 1) {
85
+ // this.#ReactDOMClient.unmountComponentAtNode(mounted[i])
86
+ // }
87
+ // }
88
+ // }
89
+ // }
90
+
91
+ private mountComponents() {
72
92
  const toMount = document.querySelectorAll(`[${CLASS_ATTRIBUTE_NAME}]`)
73
93
 
74
94
  for (let i = 0; i < toMount.length; i += 1) {
@@ -82,7 +102,7 @@ const ReactComponentsRails = {
82
102
  continue
83
103
  }
84
104
 
85
- const component = registeredComponents[className]
105
+ const component = this.#registeredComponents[className]
86
106
 
87
107
  if (component) {
88
108
  if (node.innerHTML.length === 0) this.render(node, component)
@@ -92,28 +112,31 @@ const ReactComponentsRails = {
92
112
  )
93
113
  }
94
114
  }
95
- },
96
-
97
- async setup(components = {}) {
98
- await this.loadReactDOMClient()
99
- if (typeof window.ReactComponentsRails === "undefined") {
100
- window.ReactComponentsRails = this
101
- // ujs.setup(this.mountComponents.bind(this), this.unmountComponents.bind(this))
102
- }
103
-
104
- window.ReactComponentsRails.registerComponents(components)
105
- window.ReactComponentsRails.mountComponents()
106
- },
115
+ }
107
116
 
108
- async loadReactDOMClient() {
109
- if (this.ReactDOMClient) return
117
+ setup(components = {}) {
118
+ this.loadReactDOMClient().then(() => {
119
+ window.ReactComponentsRails.registerComponents(components)
120
+ window.ReactComponentsRails.mountComponents()
121
+ })
122
+ }
110
123
 
111
- try {
112
- this.ReactDOMClient = await import("react-dom/client")
113
- } catch (e) {
114
- this.ReactDOMClient = ReactDOM
115
- }
116
- },
124
+ private loadReactDOMClient() {
125
+ return new Promise<void>((resolve) => {
126
+ if (this.#ReactDOMClient) resolve()
127
+
128
+ import("react-dom/client")
129
+ .then((i) => {
130
+ this.#ReactDOMClient = i
131
+ resolve()
132
+ })
133
+ .catch(() => {
134
+ this.#ReactDOMClient = ReactDOM
135
+ })
136
+ })
137
+ }
117
138
  }
118
139
 
119
- export default ReactComponentsRails
140
+ const instance = ReactComponentsRails.getInstance()
141
+
142
+ export default instance
data/tsconfig.json CHANGED
@@ -1,4 +1,5 @@
1
1
  {
2
+ "exclude": ["dist/**/*"],
2
3
  "compilerOptions": {
3
4
  /* Visit https://aka.ms/tsconfig.json to read more about this file */
4
5
 
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.beta2
4
+ version: 1.0.0.beta3
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-08-15 00:00:00.000000000 Z
11
+ date: 2022-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler