@mschop/alpine-web-components 1.0.1 → 1.0.3

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,25 @@ 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;
107
+ if (value === null && this.state.attributes[key] !== undefined) {
108
+ const attribute = this.castToAttribute(
109
+ key,
110
+ this.castToAttribute(key, this.state.attributes[key])
111
+ );
112
+ if (attribute !== null) {
113
+ this.setAttribute(key, attribute)
114
+ }
115
+ } else {
116
+ this.state.attributes[key] = this.castFromAttribute(key, value)
109
117
  }
110
-
111
- this.state.attributes[key] = this.castFromAttribute(key, value)
112
118
  })
113
119
  }
114
120
 
115
- private castToAttribute(name: string, value: any) {
121
+ private castToAttribute(name: string, value: any): null|string {
116
122
  return this.attributeCasts[name] === undefined ? value : this.attributeCasts[name].toAttribute(value);
117
123
  }
118
124
 
119
- private castFromAttribute(name: string, value: string): any {
125
+ private castFromAttribute(name: string, value: null|string): any {
120
126
  return this.attributeCasts[name] === undefined ? value : this.attributeCasts[name].fromAttribute(value);
121
127
  }
122
128
 
@@ -137,7 +143,11 @@ abstract class AlpineWebComponent extends HTMLElement {
137
143
  if (newValue === currentValue) {
138
144
  return;
139
145
  }
140
- this.setAttribute(key, this.state.attributes[key])
146
+ if (newValue === null) {
147
+ this.removeAttribute(key)
148
+ } else {
149
+ this.setAttribute(key, this.state.attributes[key])
150
+ }
141
151
  this.dispatchEvent(
142
152
  new CustomEvent(
143
153
  '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.3",
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"