@mschop/alpine-web-components 1.0.1 → 1.0.2

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.
@@ -1,8 +1,8 @@
1
1
  import Alpine from 'alpinejs';
2
2
 
3
3
  export interface AttributeCast<T> {
4
- fromAttribute: (value: string) => T
5
- toAttribute: (value: T) => string
4
+ fromAttribute: (value: null|string) => T
5
+ toAttribute: (value: T) => null|string
6
6
  }
7
7
 
8
8
  export interface AttributeCasts {
@@ -104,19 +104,15 @@ abstract class AlpineWebComponent extends HTMLElement {
104
104
  this.constructor.observedAttributes?.forEach((key: string) => {
105
105
  const value = this.getAttribute(key);
106
106
 
107
- if (value === null) {
108
- return;
109
- }
110
-
111
107
  this.state.attributes[key] = this.castFromAttribute(key, value)
112
108
  })
113
109
  }
114
110
 
115
- private castToAttribute(name: string, value: any) {
111
+ private castToAttribute(name: string, value: any): null|string {
116
112
  return this.attributeCasts[name] === undefined ? value : this.attributeCasts[name].toAttribute(value);
117
113
  }
118
114
 
119
- private castFromAttribute(name: string, value: string): any {
115
+ private castFromAttribute(name: string, value: null|string): any {
120
116
  return this.attributeCasts[name] === undefined ? value : this.attributeCasts[name].fromAttribute(value);
121
117
  }
122
118
 
@@ -137,7 +133,11 @@ abstract class AlpineWebComponent extends HTMLElement {
137
133
  if (newValue === currentValue) {
138
134
  return;
139
135
  }
140
- this.setAttribute(key, this.state.attributes[key])
136
+ if (newValue === null) {
137
+ this.removeAttribute(key)
138
+ } else {
139
+ this.setAttribute(key, this.state.attributes[key])
140
+ }
141
141
  this.dispatchEvent(
142
142
  new CustomEvent(
143
143
  'updated-' + key,
package/README.md CHANGED
@@ -42,7 +42,7 @@ class Counter extends AlpineWebComponent {
42
42
  }
43
43
  }
44
44
 
45
- window.define('my-counter', Counter);
45
+ window.customElements.define('my-counter', Counter);
46
46
  ```
47
47
 
48
48
  You should now be able to reference the counter with `<my-counter></my-counter>`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mschop/alpine-web-components",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Use alpinejs with web components and shadow root",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -10,7 +10,7 @@
10
10
  "type": "git",
11
11
  "url": "git+ssh://git@gitlab.com/mschop/alpine-web-components.git"
12
12
  },
13
- "author": "",
13
+ "author": "mschop",
14
14
  "license": "MIT",
15
15
  "bugs": {
16
16
  "url": "https://gitlab.com/mschop/alpine-web-components/issues"