@csedl/svelte-on-rails 0.0.4 → 0.0.5

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.
Files changed (3) hide show
  1. package/README.md +15 -10
  2. package/index.js +3 -4
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -15,37 +15,42 @@ npm i @csedl/svelte-on-rails
15
15
  A file like `initializeSvelte.js` that is imported into your entrypoint file
16
16
 
17
17
  ```javascript
18
- const components = import.meta.glob('/javascript/**/*.svelte', { eager: true })
19
- const componentsRoot = '/javascript'
18
+ import { initializeSvelteComponents, cleanupSvelteComponents } from 'svelte-on-rails';
20
19
 
21
- // hydrate
20
+ const components = import.meta.glob('/javascript/**/*.svelte', { eager: true });
21
+ const componentsRoot = '/javascript';
22
22
 
23
- import {initializeSvelteComponents} from "../javascript/node-module";
24
- initializeSvelteComponents(componentsRoot, components, true)
23
+ // Initialize Svelte components
24
+ initializeSvelteComponents(componentsRoot, components, true);
25
25
 
26
- // Turbo-Event-Listener
26
+ // Turbo event listener for page load
27
27
  document.addEventListener('turbo:load', () => {
28
- initializeSvelteComponents(componentsRoot, components, true);
28
+ initializeSvelteComponents(componentsRoot, components, true);
29
+ });
30
+
31
+ // Turbo event listener for cleanup before page cache
32
+ document.addEventListener('turbo:before-cache', () => {
33
+ cleanupSvelteComponents(true);
29
34
  });
30
35
  ```
31
36
 
37
+ => the latter boolean is for debugging logs to the console
38
+
32
39
  ## How it works
33
40
 
34
41
  The helper from the `svelte-on-rails` gem adds attributes like
35
42
 
36
43
  - class `svelte-on-rails-not-initialized-component`
37
44
  - attribute `data-svelte-component="HelloWorld"`
38
- - attribute `data-svelte-on-rails-initialize-action="hydrate"` if the element is SSR, otherwise `mount`
39
45
  - attribute `data-props={items:['one','two','three']}`
40
46
 
41
47
  to the wrapping element. Then, this lib
42
48
 
43
49
  - picks up these elements
44
50
  - picks the props, parses them by `JSON.parse(..)` and provides them to the element as svelte props
45
- - hydrates or mounts the element
51
+ - If the element is empty, it mountes, otherwise it hydrates the element
46
52
  - adds the attribute `data-svelte-initialized='true'`
47
53
  - removes the class `svelte-on-rails-not-initialized-component`
48
- - removes the attribute `data-svelte-on-rails-initialize-action`
49
54
 
50
55
  ## Requirements
51
56
 
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { mount, hydrate, unmount } from 'svelte';
1
+ import {mount, hydrate, unmount} from 'svelte';
2
2
 
3
3
  // Store for tracking initialized Svelte component instances
4
4
  const svelteInstances = new WeakMap();
@@ -20,7 +20,7 @@ export function initializeSvelteComponents(componentsRoot, components, debug = f
20
20
 
21
21
  // Iterate over the static array
22
22
  for (const element of elementsArray) {
23
- const action = element.getAttribute('data-svelte-on-rails-initialize-action');
23
+ const action = element.innerHTML ? 'hydrate' : 'mount';
24
24
  const componentName = element.getAttribute('data-svelte-component');
25
25
 
26
26
  // Check component
@@ -58,7 +58,7 @@ export function initializeSvelteComponents(componentsRoot, components, debug = f
58
58
  hydrate: true,
59
59
  props: props,
60
60
  });
61
- debugLog2(`Hydrated successfully ${componentName}, with parsed props:`, debug);
61
+ debugLog2(`Hydrated successfully ${componentName}, with parsed props:`, props, debug);
62
62
  } else {
63
63
  console.error(`[initializeSvelteComponents] Unknown action: "${action}" for component ${componentName}`);
64
64
  continue; // Proceed to the next element
@@ -68,7 +68,6 @@ export function initializeSvelteComponents(componentsRoot, components, debug = f
68
68
  svelteInstances.set(element, instance);
69
69
  element.classList.remove(virginClass);
70
70
  element.setAttribute('data-svelte-initialized', 'true');
71
- element.removeAttribute('data-svelte-on-rails-initialize-action');
72
71
  } catch (e) {
73
72
  console.error(`[initializeSvelteComponents] Error ${action} ${componentName}:`, e);
74
73
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@csedl/svelte-on-rails",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "Hydrates or mounts svelte components that are rendered from ruby-gem svelte-on-rails.",
5
5
  "main": "index.js",
6
6
  "exports": {