@rhtml/decorators 0.0.134 → 0.0.136

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.
package/README.md CHANGED
@@ -13,7 +13,7 @@ import { Attribute, Options, Input } from '@rhtml/custom-attributes';
13
13
  import { HostListener } from '@rhtml/decorators';
14
14
 
15
15
  @Modifier({
16
- selector: 'test'
16
+ selector: 'test',
17
17
  })
18
18
  export class TestDirective extends Attribute {
19
19
  @Input()
@@ -34,9 +34,7 @@ export class TestDirective extends Attribute {
34
34
  ```
35
35
 
36
36
  ```html
37
- <div test myProperty="12312">
38
- 111
39
- </div>
37
+ <div test myProperty="12312">111</div>
40
38
  ```
41
39
 
42
40
  #### Usage inside @rxdi/lit-html
@@ -52,10 +50,8 @@ import { HostListener } from '@rhtml/decorators';
52
50
  @Component({
53
51
  selector: 'home-component',
54
52
  template(this) {
55
- return html`
56
- Home Component
57
- `;
58
- }
53
+ return html` Home Component `;
54
+ },
59
55
  })
60
56
  export class HomeComponent extends LitElement {
61
57
  @HostListener('mouseenter') onEnter() {
@@ -68,6 +64,35 @@ export class HomeComponent extends LitElement {
68
64
  }
69
65
  ```
70
66
 
67
+ #### Add window event listener
68
+
69
+ ```typescript
70
+ import { Component, html, LitElement } from '@rxdi/lit-html';
71
+
72
+ import { HostListener } from '@rhtml/decorators';
73
+
74
+ /**
75
+ * @customElement home-component
76
+ */
77
+ @Component({
78
+ selector: 'home-component',
79
+ template(this) {
80
+ return html` Home Component `;
81
+ },
82
+ })
83
+ export class HomeComponent extends LitElement {
84
+ @HostListener('window:mouseenter')
85
+ onEnter() {
86
+ console.log('Enter home');
87
+ }
88
+
89
+ @HostListener('window:mouseleave')
90
+ onLeave() {
91
+ console.log('Leave home');
92
+ }
93
+ }
94
+ ```
95
+
71
96
  #### @HostBinding and @Input decorators
72
97
 
73
98
  Specifiyng Input and HostBinding decorator gives you an reactive ability to assign styles directly to input properties
@@ -79,7 +104,7 @@ import { Attribute, Input, Modifier } from '@rhtml/custom-attributes';
79
104
  import { HostBinding } from '@rhtml/decorators';
80
105
 
81
106
  @Modifier({
82
- selector: 'layout'
107
+ selector: 'layout',
83
108
  })
84
109
  export class CustomLayout extends Attribute {
85
110
  @Input({ observe: true })
@@ -1,3 +1,3 @@
1
- export declare const HostListener: (event: keyof DocumentEventMap) => (target: unknown, key: string, propertyDescriptor: PropertyDescriptor) => {
1
+ export declare const HostListener: (event: keyof DocumentEventMap | `window:${keyof DocumentEventMap}`) => (target: unknown, key: string, propertyDescriptor: PropertyDescriptor) => {
2
2
  get: () => (...args: unknown[]) => any;
3
3
  };
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HostListener = void 0;
4
4
  const HostListener = (event) => (target, key, propertyDescriptor) => {
5
+ const [type, eventName] = event.split(':');
5
6
  const wrapper = function (...args) {
6
7
  return propertyDescriptor.value.apply(this, args);
7
8
  };
@@ -16,8 +17,9 @@ const HostListener = (event) => (target, key, propertyDescriptor) => {
16
17
  Object.defineProperty(target, 'OnInit', {
17
18
  value: function (...args) {
18
19
  var _a;
19
- const element = (_a = this.element) !== null && _a !== void 0 ? _a : this;
20
- element.addEventListener(event, (...args) => wrapper.apply(this, args));
20
+ const element = type === 'window' ? window : (_a = this.element) !== null && _a !== void 0 ? _a : this;
21
+ const e = type === 'window' ? eventName : event;
22
+ element.addEventListener(e, (...args) => wrapper.apply(this, args));
21
23
  return OnInit.apply(this, args);
22
24
  },
23
25
  configurable: true,
@@ -26,8 +28,9 @@ const HostListener = (event) => (target, key, propertyDescriptor) => {
26
28
  Object.defineProperty(target, 'OnDestroy', {
27
29
  value: function (...args) {
28
30
  var _a;
29
- const element = (_a = this.element) !== null && _a !== void 0 ? _a : this;
30
- element.removeEventListener(event, (...args) => wrapper.apply(this, args));
31
+ const element = type === 'window' ? window : (_a = this.element) !== null && _a !== void 0 ? _a : this;
32
+ const e = type === 'window' ? eventName : event;
33
+ element.removeEventListener(e, (...args) => wrapper.apply(this, args));
31
34
  return OnDestroy.apply(this, args);
32
35
  },
33
36
  configurable: true,
@@ -1 +1 @@
1
- {"version":3,"file":"host-listener.js","sourceRoot":"","sources":["../src/host-listener.ts"],"names":[],"mappings":";;;AAAO,MAAM,YAAY,GACvB,CAAC,KAA6B,EAAE,EAAE,CAClC,CAAC,MAAe,EAAE,GAAW,EAAE,kBAAsC,EAAE,EAAE;IACvE,MAAM,OAAO,GAAG,UAAU,GAAG,IAAe;QAC1C,OAAO,kBAAkB,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IACpD,CAAC,CAAC;IACF,MAAM,MAAM,GACV,MAAM,CAAC,QAAQ,CAAC;QAChB;YACE,MAAM;QACR,CAAC,CAAC;IACJ,MAAM,SAAS,GACb,MAAM,CAAC,WAAW,CAAC;QACnB;YACE,MAAM;QACR,CAAC,CAAC;IAEJ,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,QAAQ,EAAE;QACtC,KAAK,EAAE,UAAU,GAAG,IAAe;;YACjC,MAAM,OAAO,GAAG,MAAA,IAAI,CAAC,OAAO,mCAAI,IAAI,CAAC;YACrC,OAAO,CAAC,gBAAgB,CAAC,KAAK,EAAE,CAAC,GAAG,IAAe,EAAE,EAAE,CACrD,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAC1B,CAAC;YACF,OAAO,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAClC,CAAC;QACD,YAAY,EAAE,IAAI;QAClB,QAAQ,EAAE,IAAI;KACf,CAAC,CAAC;IAEH,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,WAAW,EAAE;QACzC,KAAK,EAAE,UAAU,GAAG,IAAe;;YACjC,MAAM,OAAO,GAAG,MAAA,IAAI,CAAC,OAAO,mCAAI,IAAI,CAAC;YACrC,OAAO,CAAC,mBAAmB,CAAC,KAAK,EAAE,CAAC,GAAG,IAAe,EAA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AO,CAAC,gBAAgB,CAAC,CAAC,EAAE,CAAC,GAAG,IAAe,EAAE,EAAE,CACjD,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAC1B,CAAC;YACF,OAAO,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAClC,CAAC;QACD,YAAY,EAAE,IAAI;QAClB,QAAQ,EAAE,IAAI;KACf,CAAC,CAAC;IAEH,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,WAAW,EAAE;QACzC,KAAK,EAAE,UAAU,GAAG,IAAe;;YACjC,MAAM,OAAO,GAAG,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAA,IAAI,CAAC,OAAO,mCAAI,IAAI,CAAC;YAClE,MAAM,CAAC,GAAG,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC;YAEhD,OAAO,CAAC,mBAAmB,CAAC,CAAC,EAAE,CAAC,GAAG,IAAe,EAAE,EAAE,CACpD,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAC1B,CAAC;YACF,OAAO,SAAS,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACrC,CAAC;QACD,YAAY,EAAE,IAAI;QAClB,QAAQ,EAAE,IAAI;KACf,CAAC,CAAC;IACH,OAAO;QACL,GAAG,EAAE,GAAG,EAAE,CAAC,OAAO;KACnB,CAAC;AACJ,CAAC,CAAC;AAjDS,QAAA,YAAY,gBAiDrB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rhtml/decorators",
3
- "version": "0.0.134",
3
+ "version": "0.0.136",
4
4
  "description": "Reactive HyperText Markup Language",
5
5
  "scripts": {
6
6
  "start": "npx parcel ./examples/index.html --out-dir build/examples",
@@ -1,6 +1,8 @@
1
1
  export const HostListener =
2
- (event: keyof DocumentEventMap) =>
2
+ (event: keyof DocumentEventMap | `window:${keyof DocumentEventMap}`) =>
3
3
  (target: unknown, key: string, propertyDescriptor: PropertyDescriptor) => {
4
+ const [type, eventName] = event.split(':');
5
+
4
6
  const wrapper = function (...args: unknown[]) {
5
7
  return propertyDescriptor.value.apply(this, args);
6
8
  };
@@ -17,8 +19,10 @@ export const HostListener =
17
19
 
18
20
  Object.defineProperty(target, 'OnInit', {
19
21
  value: function (...args: unknown[]) {
20
- const element = this.element ?? this;
21
- element.addEventListener(event, (...args: unknown[]) =>
22
+ const element = type === 'window' ? window : this.element ?? this;
23
+ const e = type === 'window' ? eventName : event;
24
+
25
+ element.addEventListener(e, (...args: unknown[]) =>
22
26
  wrapper.apply(this, args)
23
27
  );
24
28
  return OnInit.apply(this, args);
@@ -29,8 +33,10 @@ export const HostListener =
29
33
 
30
34
  Object.defineProperty(target, 'OnDestroy', {
31
35
  value: function (...args: unknown[]) {
32
- const element = this.element ?? this;
33
- element.removeEventListener(event, (...args: unknown[]) =>
36
+ const element = type === 'window' ? window : this.element ?? this;
37
+ const e = type === 'window' ? eventName : event;
38
+
39
+ element.removeEventListener(e, (...args: unknown[]) =>
34
40
  wrapper.apply(this, args)
35
41
  );
36
42
  return OnDestroy.apply(this, args);