@mschop/alpine-web-components 1.0.10 → 2.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.
@@ -20,6 +20,7 @@ export interface State {
20
20
  abstract class AlpineWebComponent extends HTMLElement {
21
21
 
22
22
  public static isAlpineInitStarted: boolean = false
23
+ private isInitialized: boolean = false
23
24
  protected state: State = {attributes: {}}
24
25
  protected abstract template: string
25
26
  protected styles: () => string = () => ``
@@ -33,15 +34,11 @@ abstract class AlpineWebComponent extends HTMLElement {
33
34
  }
34
35
  }
35
36
 
36
- public connectedCallback() {
37
- if (AlpineWebComponent.isAlpineInitStarted) {
38
- this.init()
39
- } else {
40
- document.addEventListener('alpine:init', this.alpineInitHandler);
41
- }
42
- }
43
-
44
37
  public init() {
38
+ if (this.isInitialized) {
39
+ return;
40
+ }
41
+ this.isInitialized = true;
45
42
  AlpineWebComponent.isAlpineInitStarted = true
46
43
 
47
44
  this.loadTemplate();
@@ -147,6 +144,21 @@ abstract class AlpineWebComponent extends HTMLElement {
147
144
  })
148
145
  }
149
146
 
147
+ protected initChildren(): void {
148
+ const root = this.getRoot();
149
+ if (root === null) {
150
+ return;
151
+ }
152
+ root.querySelectorAll('*').forEach((node) => {
153
+ if (node === this) {
154
+ return;
155
+ }
156
+ if (node instanceof AlpineWebComponent) {
157
+ node.init();
158
+ }
159
+ });
160
+ }
161
+
150
162
  protected updateAttribute(key: string): void
151
163
  {
152
164
  const newValue = this.castToAttribute(key, this.state.attributes[key]);
package/README.md CHANGED
@@ -5,7 +5,7 @@ This library helps you with using AlpineJS with web components. This includes we
5
5
  ## Install
6
6
 
7
7
  1. Make sure you have AlpineJS installed and ready.
8
- 2. Install this library by running `npm i alpine-web-components`
8
+ 2. Install this library by running `npm i @mschop/alpine-web-components`
9
9
  3. Use classes AlpineWebComponent or AlpineComponent as base classes.
10
10
 
11
11
  ## How to use it
@@ -45,7 +45,9 @@ class Counter extends AlpineWebComponent {
45
45
  window.customElements.define('my-counter', Counter);
46
46
  ```
47
47
 
48
- You should now be able to reference the counter with `<my-counter></my-counter>`
48
+ You should now be able to reference the counter with `<my-counter></my-counter>`.
49
+
50
+ Note: components do not auto-initialize on connect. Call `init()` on your top-level component when you want Alpine to boot. Sub-components will be automatically initialized.
49
51
 
50
52
  The component instance is attached as `state.component` but marked raw, so Alpine won't proxy the full custom element. You can call component methods from the Alpine scope:
51
53
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mschop/alpine-web-components",
3
- "version": "1.0.10",
3
+ "version": "2.0.0",
4
4
  "description": "Use alpinejs with web components and shadow root",
5
5
  "main": "index.js",
6
6
  "scripts": {