@joist/element 4.0.0-next.4 → 4.0.0-next.41

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 (71) hide show
  1. package/README.md +102 -15
  2. package/package.json +7 -9
  3. package/src/lib/attr-changed.test.ts +34 -0
  4. package/src/lib/attr-changed.ts +15 -0
  5. package/src/lib/attr.test.ts +159 -97
  6. package/src/lib/attr.ts +26 -33
  7. package/src/lib/element.test.ts +95 -71
  8. package/src/lib/element.ts +107 -51
  9. package/src/lib/lifecycle.test.ts +31 -0
  10. package/src/lib/lifecycle.ts +9 -0
  11. package/src/lib/listen.test.ts +104 -0
  12. package/src/lib/listen.ts +27 -7
  13. package/src/lib/metadata.ts +21 -8
  14. package/src/lib/query.test.ts +21 -47
  15. package/src/lib/query.ts +2 -8
  16. package/src/lib/result.ts +1 -21
  17. package/src/lib/tags.ts +27 -12
  18. package/src/lib/template.test.ts +123 -0
  19. package/src/lib/template.ts +118 -0
  20. package/src/lib.ts +3 -2
  21. package/target/lib/attr-changed.d.ts +1 -0
  22. package/target/lib/attr-changed.js +10 -0
  23. package/target/lib/attr-changed.js.map +1 -0
  24. package/target/lib/attr-changed.test.d.ts +1 -0
  25. package/target/lib/attr-changed.test.js +54 -0
  26. package/target/lib/attr-changed.test.js.map +1 -0
  27. package/target/lib/attr.d.ts +3 -1
  28. package/target/lib/attr.js +23 -25
  29. package/target/lib/attr.js.map +1 -1
  30. package/target/lib/attr.test.js +373 -251
  31. package/target/lib/attr.test.js.map +1 -1
  32. package/target/lib/element.d.ts +8 -339
  33. package/target/lib/element.js +81 -39
  34. package/target/lib/element.js.map +1 -1
  35. package/target/lib/element.test.js +154 -178
  36. package/target/lib/element.test.js.map +1 -1
  37. package/target/lib/lifecycle.d.ts +1 -0
  38. package/target/lib/lifecycle.js +8 -0
  39. package/target/lib/lifecycle.js.map +1 -0
  40. package/target/lib/lifecycle.test.d.ts +1 -0
  41. package/target/lib/lifecycle.test.js +48 -0
  42. package/target/lib/lifecycle.test.js.map +1 -0
  43. package/target/lib/listen.d.ts +2 -2
  44. package/target/lib/listen.js +18 -3
  45. package/target/lib/listen.js.map +1 -1
  46. package/target/lib/listen.test.d.ts +1 -0
  47. package/target/lib/listen.test.js +167 -0
  48. package/target/lib/listen.test.js.map +1 -0
  49. package/target/lib/metadata.d.ts +20 -10
  50. package/target/lib/metadata.js +8 -2
  51. package/target/lib/metadata.js.map +1 -1
  52. package/target/lib/query.d.ts +1 -1
  53. package/target/lib/query.js +1 -6
  54. package/target/lib/query.js.map +1 -1
  55. package/target/lib/query.test.js +35 -74
  56. package/target/lib/query.test.js.map +1 -1
  57. package/target/lib/result.d.ts +1 -8
  58. package/target/lib/result.js +1 -14
  59. package/target/lib/result.js.map +1 -1
  60. package/target/lib/tags.d.ts +10 -6
  61. package/target/lib/tags.js +20 -11
  62. package/target/lib/tags.js.map +1 -1
  63. package/target/lib/template.d.ts +11 -0
  64. package/target/lib/template.js +87 -0
  65. package/target/lib/template.js.map +1 -0
  66. package/target/lib/template.test.d.ts +1 -0
  67. package/target/lib/template.test.js +91 -0
  68. package/target/lib/template.test.js.map +1 -0
  69. package/target/lib.d.ts +3 -2
  70. package/target/lib.js +3 -2
  71. package/target/lib.js.map +1 -1
package/README.md CHANGED
@@ -1,39 +1,126 @@
1
1
  # Element
2
2
 
3
- Create a shadow root and apply styles and html as defined
3
+ Utilities for building web compnennts. Especially targeted at
4
4
 
5
- #### Installation:
5
+ ## Table of Contents
6
+
7
+ - [Installation](#installation)
8
+ - [Custom Element](#custom-element)
9
+ - [Attributes](#attributes)
10
+ - [Styles](#styles)
11
+ - [Listeners](#listeners)
12
+ - [Queries](#queries)
13
+
14
+ ## Installation
6
15
 
7
16
  ```BASH
8
- npm i @joist/element
17
+ npm i @joist/element@next
18
+ ```
19
+
20
+ ## Custom Element
21
+
22
+ To define a custom element decorate your custom element class and add a tagName
23
+
24
+ ```ts
25
+ @element({
26
+ tagName: 'my-element'
27
+ })
28
+ export class MyElement extends HTMLElement {}
29
+ ```
30
+
31
+ ## Attributes
32
+
33
+ Attributes can be managed using the `@attr` decorator. This decorator will read attribute values and and write properties back to attributes;
34
+
35
+ ```ts
36
+ @element({
37
+ tagName: 'my-element'
38
+ })
39
+ export class MyElement extends HTMLElement {
40
+ @attr()
41
+ accessor greeting = 'Hello World';
42
+ }
9
43
  ```
10
44
 
11
- #### Example:
45
+ ## HTML and CSS
12
46
 
13
- ```TS
14
- import { tagName, css, html, attr, listen, element } from '@joist/element';
47
+ HTML templates can be applied by passing the result of the `html` tag to the shaodw list.
48
+ CSS can be applied by passing the result of the `css` tag to the shadow list.
15
49
 
50
+ ```ts
16
51
  @element({
17
52
  tagName: 'my-element',
18
- shadow: [
53
+ shadowDom: [
19
54
  css`
20
- :host {
21
- display: block;
55
+ h1 {
22
56
  color: red;
23
57
  }
24
58
  `,
59
+ html`<h1>Hello World</h1>`
60
+ ]
61
+ })
62
+ export class MyElement extends HTMLElement {}
63
+ ```
64
+
65
+ ## Listeners
66
+
67
+ The `@listen` decorator allows you to easy setup event listeners. By default the listener will be attached to the shadow root if it exists or the host element if it doesn't. This can be customized by pass a selector function to the decorator
68
+
69
+ ```ts
70
+ @element({
71
+ tagName: 'my-element',
72
+ shadowDom: []
73
+ })
74
+ export class MyElement extends HTMLElement {
75
+ @listen('eventname')
76
+ onEventName1() {
77
+ // all listener to the shadow root
78
+ }
79
+
80
+ @listen('eventname', (host) => host)
81
+ onEventName2() {
82
+ // all listener to the host element
83
+ }
84
+
85
+ @listen('eventname', (host) => host.querySelector('button'))
86
+ onEventName3() {
87
+ // add listener to a button found in the light dom
88
+ }
89
+
90
+ @listen('eventname', '#test')
91
+ onEventName4() {
92
+ // add listener to element with the id of "test" that is found in the shadow dom
93
+ }
94
+ }
95
+ ```
96
+
97
+ ## Queries
98
+
99
+ The `query` function will query for a particular element and allow you to easily patch that element with new properties.
100
+
101
+ ```ts
102
+ @element({
103
+ tagName: 'my-element',
104
+ shadowDom: [
25
105
  html`
26
- <slot></slot>
106
+ <label for="my-input">
107
+ <slot></slot>
108
+ </label>
109
+
110
+ <input id="my-input" />
27
111
  `
28
112
  ]
29
113
  })
30
114
  export class MyElement extends HTMLElement {
31
- @attr()
32
- accessor value = 0;
115
+ @observe()
116
+ value: string;
117
+
118
+ #input = query('input');
33
119
 
34
- @listen('click')
35
- onClick() {
36
- console.log('clicked!')
120
+ @effect()
121
+ onChange() {
122
+ const input = this.#input();
123
+ input.value = this.value;
37
124
  }
38
125
  }
39
126
  ```
package/package.json CHANGED
@@ -1,16 +1,13 @@
1
1
  {
2
2
  "name": "@joist/element",
3
- "version": "4.0.0-next.4",
3
+ "version": "4.0.0-next.41",
4
4
  "type": "module",
5
5
  "main": "./target/lib.js",
6
6
  "module": "./target/lib.js",
7
7
  "exports": {
8
- ".": {
9
- "import": "./target/lib.js"
10
- },
11
- "./*": {
12
- "import": "./target/lib/*.js"
13
- }
8
+ ".": "./target/lib.js",
9
+ "./*": "./target/lib/*",
10
+ "./package.json": "./package.json"
14
11
  },
15
12
  "files": [
16
13
  "src",
@@ -20,7 +17,7 @@
20
17
  "description": "Intelligently apply styles to WebComponents",
21
18
  "repository": {
22
19
  "type": "git",
23
- "url": "git+https://github.com/deebloo/joist.git"
20
+ "url": "git+https://github.com/joist-framework/joist.git"
24
21
  },
25
22
  "keywords": [
26
23
  "TypeScript",
@@ -31,7 +28,7 @@
31
28
  "author": "deebloo",
32
29
  "license": "MIT",
33
30
  "bugs": {
34
- "url": "https://github.com/deebloo/joist/issues"
31
+ "url": "https://github.com/joist-framework/joist/issues"
35
32
  },
36
33
  "publishConfig": {
37
34
  "access": "public"
@@ -57,6 +54,7 @@
57
54
  "test": {
58
55
  "command": "wtr --config wtr.config.mjs",
59
56
  "files": [
57
+ "vitest.config.js",
60
58
  "target/**"
61
59
  ],
62
60
  "output": [],
@@ -0,0 +1,34 @@
1
+ import { assert } from 'chai';
2
+
3
+ import { attrChanged } from './attr-changed.js';
4
+ import { attr } from './attr.js';
5
+ import { element } from './element.js';
6
+
7
+ it('should call specific attrbute callback', () => {
8
+ let args: string[] = [];
9
+
10
+ @element({
11
+ tagName: 'attr-changed-1'
12
+ })
13
+ class MyElement extends HTMLElement {
14
+ @attr()
15
+ accessor test = 'hello';
16
+
17
+ @attrChanged('test')
18
+ onTestChanged(oldValue: string, newValue: string) {
19
+ args = [oldValue, newValue];
20
+ }
21
+ }
22
+
23
+ const el = new MyElement();
24
+
25
+ document.body.append(el);
26
+
27
+ assert.deepEqual(args, [null, 'hello']);
28
+
29
+ el.setAttribute('test', 'world');
30
+
31
+ assert.deepEqual(args, ['hello', 'world']);
32
+
33
+ el.remove();
34
+ });
@@ -0,0 +1,15 @@
1
+ import { metadataStore } from './metadata.js';
2
+
3
+ export function attrChanged(name: string) {
4
+ return function attrChangedDecorator<This extends HTMLElement>(
5
+ cb: Function,
6
+ ctx: ClassMethodDecoratorContext<This>
7
+ ): void {
8
+ const meta = metadataStore.read(ctx.metadata);
9
+ const val = meta.attrChanges.get(name) ?? new Set();
10
+
11
+ val.add(cb);
12
+
13
+ meta.attrChanges.set(name, val);
14
+ };
15
+ }
@@ -1,129 +1,191 @@
1
- import { expect, fixture, html } from '@open-wc/testing';
1
+ import { expect } from 'chai';
2
2
 
3
3
  import { attr } from './attr.js';
4
4
  import { element } from './element.js';
5
5
 
6
- describe('@attr()', () => {
7
- it('should read and parse the correct values', async () => {
8
- @element({
9
- tagName: 'attr-test-1'
10
- })
11
- class MyElement extends HTMLElement {
12
- @attr()
13
- accessor value1 = 100; // no attribute
6
+ it('should read and parse the correct values', () => {
7
+ @element({
8
+ tagName: 'attr-test-1'
9
+ })
10
+ class MyElement extends HTMLElement {
11
+ @attr()
12
+ accessor value1 = 100; // no attribute
14
13
 
15
- @attr()
16
- accessor value2 = 0; // number
14
+ @attr()
15
+ accessor value2 = 0; // number
17
16
 
18
- @attr()
19
- accessor value3 = false; // boolean
17
+ @attr()
18
+ accessor value3 = false; // boolean
20
19
 
21
- @attr()
22
- accessor value4 = 'hello'; // string
23
- }
20
+ @attr()
21
+ accessor value4 = 'hello'; // string
22
+ }
24
23
 
25
- const el = await fixture<MyElement>(
26
- html`<attr-test-1 value2="2" value3 value4="world"></attr-test-1>`
27
- );
24
+ const container = document.createElement('div');
25
+ container.innerHTML = /*html*/ `
26
+ <attr-test-1 value2="2" value3 value4="world"></attr-test-1>
27
+ `;
28
28
 
29
- expect(el.value1).to.equal(100);
30
- expect(el.value2).to.equal(2);
31
- expect(el.value3).to.equal(true);
32
- expect(el.value4).to.equal('world');
33
- });
29
+ document.body.append(container);
34
30
 
35
- it('should not write falsy props to attributes', async () => {
36
- @element({
37
- tagName: 'attr-test-2'
38
- })
39
- class MyElement extends HTMLElement {
40
- @attr()
41
- accessor value1 = undefined;
31
+ const el = document.querySelector('attr-test-1') as MyElement;
42
32
 
43
- @attr()
44
- accessor value2 = null;
33
+ expect(el.value1).to.equal(100);
34
+ expect(el.value2).to.equal(2);
35
+ expect(el.value3).to.equal(true);
36
+ expect(el.value4).to.equal('world');
45
37
 
46
- @attr()
47
- accessor value3 = '';
48
- }
38
+ container.remove();
39
+ });
49
40
 
50
- const el = await fixture<MyElement>(html`<attr-test-2></attr-test-2>`);
41
+ it('should not write falsy props to attributes', async () => {
42
+ @element({
43
+ tagName: 'attr-test-2'
44
+ })
45
+ class MyElement extends HTMLElement {
46
+ @attr()
47
+ accessor value1 = undefined;
51
48
 
52
- expect(el.hasAttribute('value1')).to.be.false;
53
- expect(el.hasAttribute('value2')).to.be.false;
54
- expect(el.hasAttribute('value3')).to.be.false;
55
- });
49
+ @attr()
50
+ accessor value2 = null;
56
51
 
57
- it('should update attributes when props are changed', async () => {
58
- @element({
59
- tagName: 'attr-test-3'
60
- })
61
- class MyElement extends HTMLElement {
62
- @attr()
63
- accessor value1 = 'hello'; // no attribute
52
+ @attr()
53
+ accessor value3 = '';
54
+ }
64
55
 
65
- @attr()
66
- accessor value2 = 0; // number
56
+ const el = new MyElement();
67
57
 
68
- @attr()
69
- accessor value3 = true; // boolean
58
+ expect(el.hasAttribute('value1')).to.be.false;
59
+ expect(el.hasAttribute('value2')).to.be.false;
60
+ expect(el.hasAttribute('value3')).to.be.false;
61
+ });
70
62
 
71
- @attr()
72
- accessor value4 = false; // boolean
73
- }
63
+ it('should update attributes when props are changed', async () => {
64
+ @element({
65
+ tagName: 'attr-test-3'
66
+ })
67
+ class MyElement extends HTMLElement {
68
+ @attr()
69
+ accessor value1 = 'hello'; // no attribute
70
+
71
+ @attr()
72
+ accessor value2 = 0; // number
73
+
74
+ @attr()
75
+ accessor value3 = true; // boolean
76
+
77
+ @attr()
78
+ accessor value4 = false; // boolean
79
+ }
74
80
 
75
- const el = await fixture<MyElement>(html`<attr-test-3></attr-test-3>`);
81
+ const el = new MyElement();
76
82
 
77
- el.value1 = 'world';
78
- el.value2 = 100;
79
- el.value3 = false;
80
- el.value4 = true;
83
+ el.value1 = 'world';
84
+ el.value2 = 100;
85
+ el.value3 = false;
86
+ el.value4 = true;
81
87
 
82
- expect(el.getAttribute('value1')).to.equal('world');
83
- expect(el.getAttribute('value2')).to.equal('100');
84
- expect(el.hasAttribute('value3')).to.be.false;
85
- expect(el.hasAttribute('value4')).to.be.true;
86
- });
88
+ expect(el.getAttribute('value1')).to.equal('world');
89
+ expect(el.getAttribute('value2')).to.equal('100');
90
+ expect(el.hasAttribute('value3')).to.be.false;
91
+ expect(el.hasAttribute('value4')).to.be.true;
92
+ });
93
+
94
+ it('should normalize attribute names', async () => {
95
+ const value3 = Symbol('Value from SYMBOL');
96
+
97
+ @element({
98
+ tagName: 'attr-test-4'
99
+ })
100
+ class MyElement extends HTMLElement {
101
+ @attr()
102
+ accessor Value1 = 'hello';
103
+
104
+ @attr()
105
+ accessor ['Value 2'] = 0;
106
+
107
+ @attr()
108
+ accessor [value3] = true;
109
+ }
110
+
111
+ const el = new MyElement();
112
+
113
+ document.body.append(el);
87
114
 
88
- it('should normalize attribute names', async () => {
89
- const value3 = Symbol('Value from SYMBOL');
115
+ expect([...el.attributes].map((attr) => attr.name)).to.deep.equal([
116
+ 'value1',
117
+ 'value-2',
118
+ 'value-from-symbol'
119
+ ]);
120
+
121
+ el.remove();
122
+ });
123
+
124
+ it('should throw an error for symbols with no description', async () => {
125
+ expect(() => {
126
+ const value = Symbol();
90
127
 
91
128
  @element({
92
129
  tagName: 'attr-test-4'
93
130
  })
94
131
  class MyElement extends HTMLElement {
95
132
  @attr()
96
- accessor Value1 = 'hello';
133
+ accessor [value] = true;
134
+ }
97
135
 
98
- @attr()
99
- accessor ['Value 2'] = 0;
136
+ new MyElement();
137
+ }).to.throw('Cannot handle Symbol property without description');
138
+ });
100
139
 
101
- @attr()
102
- accessor [value3] = true;
103
- }
140
+ it('should not reflect property to attribute', async () => {
141
+ @element({
142
+ tagName: 'attr-test-5'
143
+ })
144
+ class MyElement extends HTMLElement {
145
+ @attr({ reflect: false })
146
+ accessor value = 'foo';
147
+ }
148
+
149
+ const el = new MyElement();
150
+ el.value = 'bar';
151
+
152
+ expect(el.value).to.equal('bar');
153
+
154
+ expect(el.hasAttribute('value')).to.be.false;
155
+ });
156
+
157
+ it('non reflective attributes should still read new attribute values', async () => {
158
+ @element({
159
+ tagName: 'attr-test-6'
160
+ })
161
+ class MyElement extends HTMLElement {
162
+ @attr({ reflect: false })
163
+ accessor value = 'foo';
164
+ }
165
+
166
+ const el = new MyElement();
167
+ el.setAttribute('value', 'bar');
168
+
169
+ expect(el.value).to.equal('bar');
170
+ });
171
+
172
+ it('should allow a manually defined attribute name', async () => {
173
+ @element({
174
+ tagName: 'attr-test-7'
175
+ })
176
+ class MyElement extends HTMLElement {
177
+ @attr({
178
+ name: 'aria-label'
179
+ })
180
+ accessor value = '';
181
+ }
182
+
183
+ const el = new MyElement();
184
+ el.setAttribute('aria-label', 'TEST');
185
+
186
+ document.body.append(el);
187
+
188
+ expect(el.value).to.equal('TEST');
104
189
 
105
- const el = await fixture<MyElement>(html`<attr-test-4></attr-test-4>`);
106
-
107
- expect([...el.attributes].map((attr) => attr.name)).to.deep.equal([
108
- 'value1',
109
- 'value-2',
110
- 'value-from-symbol'
111
- ]);
112
- });
113
-
114
- it('should throw an error for symbols with no description', async () => {
115
- expect(() => {
116
- const value = Symbol();
117
-
118
- @element({
119
- tagName: 'attr-test-4'
120
- })
121
- class MyElement extends HTMLElement {
122
- @attr()
123
- accessor [value] = true;
124
- }
125
-
126
- new MyElement();
127
- }).to.throw('Cannot handle Symbol property without description');
128
- });
190
+ el.remove();
129
191
  });
package/src/lib/attr.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import { metadataStore } from './metadata.js';
2
2
 
3
3
  export interface AttrOpts {
4
- observe?: boolean;
4
+ name?: string;
5
+ observed?: boolean;
6
+ reflect?: boolean;
5
7
  }
6
8
 
7
9
  export function attr(opts?: AttrOpts) {
@@ -9,48 +11,39 @@ export function attr(opts?: AttrOpts) {
9
11
  { get, set }: ClassAccessorDecoratorTarget<This, unknown>,
10
12
  ctx: ClassAccessorDecoratorContext<This>
11
13
  ): ClassAccessorDecoratorResult<This, any> {
12
- const attrName = parseAttrName(ctx.name);
13
- const meta = metadataStore.read(ctx.metadata);
14
+ const attrName = opts?.name ?? parseAttrName(ctx.name);
15
+ const meta = metadataStore.read<This>(ctx.metadata);
16
+ const reflect = opts?.reflect ?? true;
14
17
 
15
- meta.attrs.push({
18
+ meta.attrs.set(attrName, {
16
19
  propName: ctx.name,
17
- attrName,
18
- observe: opts?.observe ?? true
20
+ observe: opts?.observed ?? true,
21
+ reflect,
22
+ getPropValue: get,
23
+ setPropValue: set
19
24
  });
20
25
 
21
26
  return {
22
27
  set(value: unknown) {
23
- if (value === true) {
24
- this.setAttribute(attrName, '');
25
- } else if (value === false) {
26
- this.removeAttribute(attrName);
27
- } else {
28
- this.setAttribute(attrName, String(value));
29
- }
30
-
31
- set.call(this, value);
32
- },
33
- get() {
34
- const ogValue = get.call(this);
35
- const attr = this.getAttribute(attrName);
28
+ if (reflect) {
29
+ if (value === true) {
30
+ if (!this.hasAttribute(attrName)) {
31
+ this.setAttribute(attrName, '');
32
+ }
33
+ } else if (value === false) {
34
+ if (this.hasAttribute(attrName)) {
35
+ this.removeAttribute(attrName);
36
+ }
37
+ } else {
38
+ const strValue = String(value);
36
39
 
37
- if (attr !== null) {
38
- // treat as boolean
39
- if (attr === '') {
40
- return true;
40
+ if (this.getAttribute(attrName) !== strValue) {
41
+ this.setAttribute(attrName, strValue);
42
+ }
41
43
  }
42
-
43
- // treat as number
44
- if (typeof ogValue === 'number') {
45
- return Number(attr);
46
- }
47
-
48
- // treat as string
49
- return attr;
50
44
  }
51
45
 
52
- // no readable value return original
53
- return ogValue;
46
+ set.call(this, value);
54
47
  }
55
48
  };
56
49
  };