@rhtml/decorators 0.0.109 → 0.0.112

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
@@ -12,16 +12,13 @@ npm i @rhtml/decorators
12
12
  import { Attribute, Options, Input } from '@rhtml/custom-attributes';
13
13
  import { HostListener } from '@rhtml/decorators';
14
14
 
15
+ @Modifier({
16
+ selector: 'test'
17
+ })
15
18
  export class TestDirective extends Attribute {
16
19
  @Input()
17
20
  myProperty: string;
18
21
 
19
- public static options(this: HTMLElement): Options {
20
- return {
21
- name: 'test'
22
- };
23
- }
24
-
25
22
  @HostListener('mouseenter')
26
23
  enter(event: Event) {
27
24
  console.log('ENTER', this, event);
@@ -70,3 +67,31 @@ export class HomeComponent extends LitElement {
70
67
  }
71
68
  }
72
69
  ```
70
+
71
+ #### @HostBinding and @Input decorators
72
+
73
+ Specifiyng Input and HostBinding decorator gives you an reactive ability to assign styles directly to input properties
74
+ This way by editing `padding`, `color` or `background` will reflect to the style associated with
75
+ By doing this we can skip `OnInit`, `OnDestroy`, `OnUpdate` manual assign inside hooks
76
+
77
+ ```typescript
78
+ import { Attribute, Input, Modifier } from '@rhtml/custom-attributes';
79
+ import { HostBinding } from '@rhtml/decorators';
80
+
81
+ @Modifier({
82
+ selector: 'layout'
83
+ })
84
+ export class CustomLayout extends Attribute {
85
+ @Input({ observe: true })
86
+ @HostBinding('style.padding')
87
+ padding: string;
88
+
89
+ @Input({ observe: true })
90
+ @HostBinding('style.color')
91
+ color: string;
92
+
93
+ @Input({ observe: true })
94
+ @HostBinding('style.background')
95
+ background: string;
96
+ }
97
+ ```
@@ -0,0 +1 @@
1
+ export declare const HostBinding: <T>(binding: string) => (target: any, memberName: string) => void;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ // type Class = 'class';
3
+ // type Style = 'style';
4
+ // type Type = Style | Class;
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.HostBinding = void 0;
7
+ // type Styles = keyof CSSStyleDeclaration;
8
+ // type Binding<T = Type> = T extends Style ? `${T}.${Styles}` : `${Class}.${string}`;
9
+ exports.HostBinding = (binding /* Binding<T> */) => (target, memberName) => {
10
+ const [type, key] = binding.split('.');
11
+ const OnInit = target['OnInit'] ||
12
+ function () {
13
+ /* */
14
+ };
15
+ const apply = (value) => (element) => {
16
+ if (type === 'style') {
17
+ if (!value) {
18
+ element.style[key] = null;
19
+ }
20
+ else {
21
+ element.style[key] = value;
22
+ }
23
+ }
24
+ if (type === 'class') {
25
+ if (!value) {
26
+ element.classList.remove(key);
27
+ }
28
+ else {
29
+ element.classList.add(key);
30
+ }
31
+ }
32
+ };
33
+ Object.defineProperty(target, 'OnInit', {
34
+ value: function (...args) {
35
+ var _a;
36
+ let currentValue = this[memberName];
37
+ const element = (_a = this.element) !== null && _a !== void 0 ? _a : this;
38
+ Object.defineProperty(target, memberName, {
39
+ get: function () {
40
+ return currentValue;
41
+ },
42
+ set: function (value) {
43
+ apply(value)(element);
44
+ currentValue = value;
45
+ },
46
+ configurable: true
47
+ });
48
+ if (currentValue) {
49
+ apply(currentValue)(element);
50
+ }
51
+ return OnInit.apply(this, args);
52
+ },
53
+ configurable: true,
54
+ writable: true
55
+ });
56
+ };
57
+ //# sourceMappingURL=host-binding.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"host-binding.js","sourceRoot":"","sources":["../src/host-binding.ts"],"names":[],"mappings":";AAAA,wBAAwB;AACxB,wBAAwB;AACxB,6BAA6B;;;AAE7B,2CAA2C;AAE3C,sFAAsF;AAEzE,QAAA,WAAW,GAAG,CAAI,OAAe,CAAC,gBAAgB,EAAE,EAAE,CAAC,CAClE,MAAM,EACN,UAAkB,EAClB,EAAE;IACF,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAEvC,MAAM,MAAM,GACV,MAAM,CAAC,QAAQ,CAAC;QAChB;YACE,MAAM;QACR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IAAI;KACf,CAAC,CAAC;AACL,CAAC,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from './host-listener';
2
+ export * from './host-binding';
package/dist/index.js CHANGED
@@ -11,4 +11,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  __exportStar(require("./host-listener"), exports);
14
+ __exportStar(require("./host-binding"), exports);
14
15
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,kDAAgC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,kDAAgC;AAChC,iDAA+B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rhtml/decorators",
3
- "version": "0.0.109",
3
+ "version": "0.0.112",
4
4
  "description": "Reactive HyperText Markup Language",
5
5
  "scripts": {
6
6
  "start": "npx parcel ./examples/index.html --out-dir build/examples",
@@ -0,0 +1,60 @@
1
+ // type Class = 'class';
2
+ // type Style = 'style';
3
+ // type Type = Style | Class;
4
+
5
+ // type Styles = keyof CSSStyleDeclaration;
6
+
7
+ // type Binding<T = Type> = T extends Style ? `${T}.${Styles}` : `${Class}.${string}`;
8
+
9
+ export const HostBinding = <T>(binding: string /* Binding<T> */) => (
10
+ target,
11
+ memberName: string
12
+ ) => {
13
+ const [type, key] = binding.split('.');
14
+
15
+ const OnInit =
16
+ target['OnInit'] ||
17
+ function() {
18
+ /* */
19
+ };
20
+ const apply = (value: string) => (element: HTMLElement) => {
21
+ if (type === 'style') {
22
+ if (!value) {
23
+ element.style[key] = null;
24
+ } else {
25
+ element.style[key] = value;
26
+ }
27
+ }
28
+ if (type === 'class') {
29
+ if (!value) {
30
+ element.classList.remove(key);
31
+ } else {
32
+ element.classList.add(key);
33
+ }
34
+ }
35
+ };
36
+
37
+ Object.defineProperty(target, 'OnInit', {
38
+ value: function(...args: unknown[]) {
39
+ let currentValue: string = this[memberName];
40
+ const element: HTMLElement = this.element ?? this;
41
+
42
+ Object.defineProperty(target, memberName, {
43
+ get: function() {
44
+ return currentValue;
45
+ },
46
+ set: function(value) {
47
+ apply(value)(element);
48
+ currentValue = value;
49
+ },
50
+ configurable: true
51
+ });
52
+ if (currentValue) {
53
+ apply(currentValue)(element);
54
+ }
55
+ return OnInit.apply(this, args);
56
+ },
57
+ configurable: true,
58
+ writable: true
59
+ });
60
+ };
package/src/index.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from './host-listener';
2
+ export * from './host-binding';