@rhtml/custom-attributes 0.0.110 → 0.0.113

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
@@ -306,3 +306,124 @@ By changing `delay` attribute because we use `observe: true` method `OnUpdateAtt
306
306
  My Animated Element
307
307
  </h2>
308
308
  ```
309
+
310
+ #### Media query matcher
311
+
312
+ By extending `MediaQueryAttribute` we gain two more events to handle media matches `OnEnterMediaQuery` and `OnExitMediaQuery`
313
+
314
+ For example when we define attribute selector `color` we can use available media breakpoints `xs`, `sm`, `md`,`lg`,`xl`.
315
+
316
+ Extending `MediaQueryAttribute` will help you to track values from specific resolutions
317
+
318
+ ```typescript
319
+ import {
320
+ EnterMediaQueryAttributes,
321
+ ExitMediaQueryAttributes,
322
+ MediaQueryAttribute,
323
+ Modifier
324
+ } from '@rhtml/custom-attributes';
325
+
326
+ interface Styles {
327
+ color: string;
328
+ }
329
+
330
+ @Modifier({
331
+ selector: 'color'
332
+ })
333
+ export class Color extends MediaQueryAttribute<Styles> {
334
+ OnInit() {
335
+ this.modify();
336
+ /* Executing media matcher init */
337
+ super.OnInit();
338
+ }
339
+
340
+ OnDestroy() {
341
+ this.clean();
342
+ /* Executing media matcher destroy */
343
+ super.OnDestroy();
344
+ }
345
+
346
+ OnUpdate() {
347
+ this.modify();
348
+ }
349
+
350
+ OnEnterMediaQuery([event, attribute]: EnterMediaQueryAttributes) {
351
+ console.log(event, attribute.value);
352
+ this.prevValue = this.value;
353
+ this.value = attribute.value ?? this.value;
354
+ this.modify();
355
+ }
356
+
357
+ OnExitMediaQuery([event, selector]: ExitMediaQueryAttributes) {
358
+ this.value = this.prevValue ?? this.originalValue;
359
+ this.modify();
360
+ }
361
+
362
+ clean() {
363
+ this.setStyles({ color: null })(this.element);
364
+ }
365
+
366
+ modify() {
367
+ this.setStyles({ color: this.value || null })(this.element);
368
+ }
369
+ }
370
+ ```
371
+
372
+ ##### Usage
373
+
374
+ ```html
375
+ <div color="red" color.xs="green" color.md="gray">
376
+ My text
377
+ </div>
378
+ ```
379
+
380
+ ##### Available breakpoints
381
+
382
+ | Breakpoint | Media Query |
383
+ | ---------- | ------------------------------------------------------ |
384
+ | xs | screen and (max-width: 599px) |
385
+ | sm | screen and (min-width: 600px) and (max-width: 959px) |
386
+ | md | screen and (min-width: 960px) and (max-width: 1279px) |
387
+ | lg | screen and (min-width: 1280px) and (max-width: 1919px) |
388
+ | xl | screen and (min-width: 1920px) and (max-width: 5000px) |
389
+ | | |
390
+ | lt-sm | screen and (max-width: 599px) (use xs) |
391
+ | lt-md | screen and (max-width: 959px) |
392
+ | lt-lg | screen and (max-width: 1279px) |
393
+ | lt-xl | screen and (max-width: 1919px) |
394
+ | | |
395
+ | gt-xs | screen and (min-width: 600px) |
396
+ | gt-sm | screen and (min-width: 960px) |
397
+ | gt-md | screen and (min-width: 1280px) |
398
+ | gt-lg | screen and (min-width: 1920px) |
399
+
400
+ #### Defining custom MutationObserver which is attached on the element used by the Attribute
401
+
402
+ ```typescript
403
+ import { Attribute, Modifier } from '@rhtml/custom-attributes';
404
+
405
+ @Modifier({
406
+ selector: 'color',
407
+ observe: {
408
+ childList: true,
409
+ subtree: true,
410
+ attributes: true,
411
+ attributeFilter: ['my-attribute']
412
+ }
413
+ })
414
+ export class Color extends Attribute {
415
+ OnChange(records: MutationRecord[]) {
416
+ console.log(records);
417
+ const attributeValue = this.element.getAttribute('my-attribute');
418
+ console.log(attributeValue); // 'test'
419
+ }
420
+ }
421
+ ```
422
+
423
+ ```html
424
+ <div color="red" my-attribute="test">
425
+ My element
426
+ </div>
427
+ ```
428
+
429
+ If we change `my-attribute` value we will trigger `OnChange` which will give us mutation records
@@ -0,0 +1,15 @@
1
+ import { ModifierOptions } from './types';
2
+ export declare abstract class Attribute<T = {}> {
3
+ static options: ModifierOptions;
4
+ element?: HTMLElement;
5
+ value?: string;
6
+ selector?: string;
7
+ parent?: HTMLElement;
8
+ observer?: MutationObserver;
9
+ setStyles(keys: T): (div: HTMLElement | Element | HTMLDivElement) => void;
10
+ OnInit(): void;
11
+ OnDestroy(): void;
12
+ OnUpdate(_oldValue: string, _newValue: string): void;
13
+ OnUpdateAttribute(_name: string, _value: string): void;
14
+ OnChange(_records: MutationRecord[]): void;
15
+ }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Attribute = void 0;
4
+ /* */
5
+ class Attribute {
6
+ setStyles(keys) {
7
+ return (div) => {
8
+ for (const [key, value] of Object.entries(keys)) {
9
+ div['style'][key] = value;
10
+ }
11
+ };
12
+ }
13
+ OnInit() {
14
+ /* */
15
+ }
16
+ OnDestroy() {
17
+ /* */
18
+ }
19
+ OnUpdate(_oldValue, _newValue) {
20
+ /* */
21
+ }
22
+ OnUpdateAttribute(_name, _value) {
23
+ /* */
24
+ }
25
+ OnChange(_records) {
26
+ /* */
27
+ }
28
+ }
29
+ exports.Attribute = Attribute;
30
+ //# sourceMappingURL=attribute.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attribute.js","sourceRoot":"","sources":["../src/attribute.ts"],"names":[],"mappings":";;;AAGA,MAAM;AACN,MAAsB,SAAS;IAO7B,SAAS,CAAC,IAAO;QACf,OAAO,CAAC,GAA2C,EAAE,EAAE;YACrD,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;gBAC/C,GAAG,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;aAC3B;QACH,CAAC,CAAC;IACJ,CAAC;IACD,MAAM;QACJ,KAAK;IACP,CAAC;IACD,SAAS;QACP,KAAK;IACP,CAAC;IACD,QAAQ,CAAC,SAAiB,EAAE,SAAiB;QAC3C,KAAK;IACP,CAAC;IACD,iBAAiB,CAAC,KAAa,EAAE,MAAc;QAC7C,KAAK;IACP,CAAC;IAED,QAAQ,CAAC,QAA0B;QACjC,KAAK;IACP,CAAC;CACF;AA9BD,8BA8BC"}
@@ -0,0 +1,18 @@
1
+ import { Attribute } from './attribute';
2
+ import { Constructor } from './types';
3
+ export declare class CustomAttributeRegistry {
4
+ private parent;
5
+ private _attrMap;
6
+ private _elementMap;
7
+ private observer;
8
+ constructor(parent: HTMLElement);
9
+ define(attrName: string, Constructor: Constructor<Attribute>): void;
10
+ get(element: HTMLElement, attrName: string): Attribute<{}>;
11
+ private getConstructor;
12
+ private observe;
13
+ unsubscribe(): void;
14
+ private upgradeAttribute;
15
+ private upgradeElement;
16
+ private downgrade;
17
+ private found;
18
+ }
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CustomAttributeRegistry = void 0;
4
+ const helpers_1 = require("./helpers");
5
+ class CustomAttributeRegistry {
6
+ constructor(parent) {
7
+ this.parent = parent;
8
+ this._attrMap = new Map();
9
+ this._elementMap = new Map();
10
+ if (!parent) {
11
+ throw new Error('Must be given a parent element');
12
+ }
13
+ this.observe();
14
+ }
15
+ define(attrName, Constructor) {
16
+ this._attrMap.set(attrName.toLowerCase(), Constructor);
17
+ this.upgradeAttribute(attrName);
18
+ }
19
+ get(element, attrName) {
20
+ const map = this._elementMap.get(element);
21
+ if (!map)
22
+ return;
23
+ return map.get(attrName.toLowerCase());
24
+ }
25
+ getConstructor(attrName) {
26
+ return this._attrMap.get(attrName.toLowerCase());
27
+ }
28
+ observe() {
29
+ var _a, _b;
30
+ this.observer = new MutationObserver(mutations => {
31
+ for (const mutation of mutations) {
32
+ if (mutation.type === 'attributes') {
33
+ const attr = this.getConstructor(mutation.attributeName);
34
+ if (attr) {
35
+ this.found(mutation.attributeName, mutation.target, mutation.oldValue);
36
+ }
37
+ }
38
+ else {
39
+ for (const node of mutation.removedNodes) {
40
+ this.downgrade(node);
41
+ }
42
+ for (const node of mutation.addedNodes) {
43
+ this.upgradeElement(node);
44
+ }
45
+ }
46
+ }
47
+ });
48
+ this.observer.observe((_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a['shadowRoot']) !== null && _b !== void 0 ? _b : this.parent, {
49
+ childList: true,
50
+ subtree: true,
51
+ attributes: true,
52
+ attributeOldValue: true
53
+ });
54
+ }
55
+ unsubscribe() {
56
+ var _a;
57
+ (_a = this.observer) === null || _a === void 0 ? void 0 : _a.disconnect();
58
+ const values = [...this._elementMap.values()];
59
+ for (const elModifiers of values) {
60
+ const modifiers = [...elModifiers.values()];
61
+ for (const modifier of modifiers) {
62
+ modifier.OnDestroy();
63
+ if (modifier.observer) {
64
+ modifier.observer.disconnect();
65
+ }
66
+ }
67
+ elModifiers.clear();
68
+ }
69
+ this._elementMap.clear();
70
+ }
71
+ upgradeAttribute(attrName, doc) {
72
+ const parent = doc || this.parent;
73
+ const matches = parent.querySelectorAll('[' + attrName + ']');
74
+ for (const match of [...matches]) {
75
+ this.found(attrName, match);
76
+ }
77
+ }
78
+ upgradeElement(element) {
79
+ if (element.nodeType !== 1) {
80
+ return;
81
+ }
82
+ for (const attr of element.attributes) {
83
+ if (this.getConstructor(attr.name)) {
84
+ this.found(attr.name, element);
85
+ }
86
+ }
87
+ for (const [attr] of this._attrMap) {
88
+ this.upgradeAttribute(attr, element);
89
+ }
90
+ }
91
+ downgrade(element) {
92
+ const map = this._elementMap.get(element);
93
+ if (!map) {
94
+ return;
95
+ }
96
+ for (const [, instance] of map) {
97
+ if (instance.OnDestroy) {
98
+ instance.OnDestroy();
99
+ }
100
+ }
101
+ this._elementMap.delete(element);
102
+ }
103
+ found(attributeName, el, oldVal) {
104
+ var _a, _b;
105
+ let map = this._elementMap.get(el);
106
+ if (!map) {
107
+ map = new Map();
108
+ this._elementMap.set(el, map);
109
+ }
110
+ let modifier = map.get(attributeName);
111
+ const attribute = el.getAttribute(attributeName);
112
+ if (!modifier) {
113
+ const Modifier = this.getConstructor(attributeName);
114
+ modifier = new Modifier();
115
+ if ((_b = (_a = Modifier.options) === null || _a === void 0 ? void 0 : _a.observedAttributes) === null || _b === void 0 ? void 0 : _b.length) {
116
+ for (const observedAttribute of Modifier.options.observedAttributes) {
117
+ helpers_1.observe(modifier, observedAttribute);
118
+ }
119
+ }
120
+ modifier.element = el;
121
+ modifier.selector = attributeName;
122
+ modifier.value = attribute || modifier.value;
123
+ modifier.parent = this.parent;
124
+ if (modifier.OnInit) {
125
+ modifier.OnInit();
126
+ }
127
+ if (Modifier.options.observe) {
128
+ modifier.observer = new MutationObserver(records => modifier.OnChange(records));
129
+ modifier.observer.observe(modifier.element, Modifier.options.observe);
130
+ }
131
+ map.set(attributeName, modifier);
132
+ return;
133
+ }
134
+ if (attribute == null && !!modifier.value) {
135
+ modifier.value = attribute;
136
+ if (modifier.OnDestroy) {
137
+ modifier.OnDestroy();
138
+ }
139
+ if (modifier.observer) {
140
+ modifier.observer.disconnect();
141
+ }
142
+ map.delete(attributeName);
143
+ return;
144
+ }
145
+ if (attribute !== modifier.value) {
146
+ modifier.value = attribute;
147
+ if (modifier.OnUpdate) {
148
+ modifier.OnUpdate(oldVal, attribute);
149
+ }
150
+ return;
151
+ }
152
+ }
153
+ }
154
+ exports.CustomAttributeRegistry = CustomAttributeRegistry;
155
+ //# sourceMappingURL=custom-registry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"custom-registry.js","sourceRoot":"","sources":["../src/custom-registry.ts"],"names":[],"mappings":";;;AACA,uCAAoC;AAGpC,MAAa,uBAAuB;IAKlC,YAAoB,MAAmB;QAAnB,WAAM,GAAN,MAAM,CAAa;QAJ/B,aAAQ,GAAwC,IAAI,GAAG,EAAE,CAAC;QAC1D,gBAAW,GAA6C,IAAI,GAAG,EAAE,CAAC;QAIxE,IAAI,CAAC,MAAM,EAAE;YACX,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;SACnD;QACD,IAAI,CAAC,OAAO,EAAE,CAAC;IACjB,CAAC;IAED,MAAM,CAAC,QAAgB,EAAE,WAAmC;QAC1D,IAAI,CAAC,QAAQ,CAAC,GA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,QAAQ,CAAC,KAAK,GAAG,SAAS,CAAC;YAC3B,IAAI,QAAQ,CAAC,QAAQ,EAAE;gBACrB,QAAQ,CAAC,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;aACtC;YACD,OAAO;SACR;IACH,CAAC;CACF;AAtKD,0DAsKC"}
@@ -0,0 +1,21 @@
1
+ import { InputOptions, ModifierOptions } from '../types';
2
+ /**
3
+ * Decorator @Input
4
+ * Used to get attribute from element
5
+ */
6
+ export declare const Input: (options?: InputOptions) => (target: any, memberName: string) => void;
7
+ /**
8
+ * Decorator @Modifier
9
+ * Accepts parameter options with selector and registry
10
+ */
11
+ export declare const Modifier: (options: ModifierOptions) => (target: Function) => void;
12
+ /**
13
+ * Decorator @CustomAttribute
14
+ * Accepts parameter options with selector and registry
15
+ */
16
+ export declare const CustomAttribute: (options: ModifierOptions) => (target: Function) => void;
17
+ /**
18
+ * Decorator @Directive
19
+ * Accepts parameter options with selector and registry
20
+ */
21
+ export declare const Directive: (options: ModifierOptions) => (target: Function) => void;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Directive = exports.CustomAttribute = exports.Modifier = exports.Input = void 0;
4
+ const helpers_1 = require("../helpers");
5
+ /**
6
+ * Decorator @Input
7
+ * Used to get attribute from element
8
+ */
9
+ exports.Input = (options) => (target, memberName) => {
10
+ const OnInit = target.OnInit || helpers_1.noop;
11
+ Object.defineProperty(target, 'OnInit', {
12
+ value() {
13
+ var _a;
14
+ let originalValue = this[memberName];
15
+ const element = (_a = this.element) !== null && _a !== void 0 ? _a : this;
16
+ Object.defineProperty(this, memberName, {
17
+ get: function () {
18
+ originalValue = element.getAttribute(memberName.toLowerCase());
19
+ return originalValue;
20
+ },
21
+ set(value) {
22
+ element.setAttribute(memberName.toLowerCase(), value);
23
+ originalValue = value;
24
+ },
25
+ configurable: true
26
+ });
27
+ return OnInit.call(this);
28
+ },
29
+ configurable: true
30
+ });
31
+ if (options === null || options === void 0 ? void 0 : options.observe) {
32
+ helpers_1.observe(target, memberName);
33
+ }
34
+ };
35
+ /**
36
+ * Decorator @Modifier
37
+ * Accepts parameter options with selector and registry
38
+ */
39
+ exports.Modifier = (options) => {
40
+ return (target) => {
41
+ target['options'] = options;
42
+ };
43
+ };
44
+ /**
45
+ * Decorator @CustomAttribute
46
+ * Accepts parameter options with selector and registry
47
+ */
48
+ exports.CustomAttribute = exports.Modifier;
49
+ /**
50
+ * Decorator @Directive
51
+ * Accepts parameter options with selector and registry
52
+ */
53
+ exports.Directive = exports.Modifier;
54
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/decorators/index.ts"],"names":[],"mappings":";;;AAAA,wCAA2C;AAG3C;;;GAGG;AACU,QAAA,KAAK,GAAG,CAAC,OAAsB,EAAE,EAAE,CAAC,CAC/C,MAAM,EACN,UAAkB,EAClB,EAAE;IACF,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,IAAI,cAAI,CAAC;IACrC,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,QAAQ,EAAE;QACtC,KAAK;;YACH,IAAI,aAAa,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC;YAErC,MAAM,OAAO,SAAG,IAAI,CAAC,OAAO,mCAAI,IAAI,CAAC;YACrC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,UAAU,EAAE;gBACtC,GAAG,EAAE;oBACH,aAAa,GAAG,OAAO,CAAC,YAAY,CAAC,UAAU,CAAC,WAAW,EAAE,CAAC,CAAC;oBAC/D,OAAO,aAAa,CAAC;gBACvB,CAAC;gBACD,GAAG,CAAC,KAAK;oBACP,OAAO,CAAC,YAAY,CAAC,UAAU,CAAC,WAAW,EAAE,EAAE,KAAK,CAAC,CAAC;oBACtD,aAAa,GAAG,KAAK,CAAC;gBACxB,CAAC;gBACD,YAAY,EAAE,IAAI;aACnB,CAAC,CAAC;YACH,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC3B,CAAC;QACD,YAAY,EAAE,IAAI;KACnB,CAAC,CAAC;IAEH,IAAI,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,EAAE;QACpB,iBAAO,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;KAC7B;AACH,CAAC,CAAC;AAEF;;;GAGG;AACU,QAAA,QAAQ,GAAG,CAAC,OAAwB,EAAE,EAAE;IACnD,OAAO,CAAC,MAAgB,EAAE,EAAE;QAC1B,MAAM,CAAC,SAAS,CAAC,GAAG,OAAO,CAAC;IAC9B,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF;;;GAGG;AACU,QAAA,eAAe,GAAG,gBAAQ,CAAC;AACxC;;;GAGG;AACU,QAAA,SAAS,GAAG,gBAAQ,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare const noop: () => void;
2
+ export declare const observe: (target: unknown, memberName: string) => void;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.observe = exports.noop = void 0;
4
+ exports.noop = function () {
5
+ /* */
6
+ };
7
+ exports.observe = (target, memberName) => {
8
+ const prototype = target.constructor.prototype;
9
+ const OnInit = prototype.OnInit || exports.noop;
10
+ const OnDestroy = prototype.OnDestroy || exports.noop;
11
+ const OnUpdateAttribute = prototype.OnUpdateAttribute || exports.noop;
12
+ let observer;
13
+ prototype.OnInit = function () {
14
+ var _a;
15
+ const element = (_a = this.element) !== null && _a !== void 0 ? _a : this;
16
+ if (observer) {
17
+ observer.disconnect();
18
+ }
19
+ observer = new MutationObserver(() => {
20
+ OnUpdateAttribute.call(this, memberName, element.getAttribute(memberName));
21
+ target[memberName] = element.getAttribute(memberName);
22
+ });
23
+ observer.observe(element, {
24
+ attributeFilter: [memberName],
25
+ attributes: true
26
+ });
27
+ return OnInit.call(this);
28
+ };
29
+ prototype.OnDestroy = function () {
30
+ observer.disconnect();
31
+ return OnDestroy.call(this);
32
+ };
33
+ };
34
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/helpers/index.ts"],"names":[],"mappings":";;;AAAa,QAAA,IAAI,GAAG;IAClB,MAAM;AACR,CAAC,CAAC;AAEW,QAAA,OAAO,GAAG,CAAC,MAAe,EAAE,UAAkB,EAAE,EAAE;IAC7D,MAAM,SAAS,GAAG,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC;IAC/C,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,IAAI,YAAI,CAAC;IACxC,MAAM,SAAS,GAAG,SAAS,CAAC,SAAS,IAAI,YAAI,CAAC;IAC9C,MAAM,iBAAiB,GAAG,SAAS,CAAC,iBAAiB,IAAI,YAAI,CAAC;IAE9D,IAAI,QAA0B,CAAC;IAC/B,SAAS,CAAC,MAAM,GAAG;;QACjB,MAAM,OAAO,SAAG,IAAI,CAAC,OAAO,mCAAI,IAAI,CAAC;QACrC,IAAI,QAAQ,EAAE;YACZ,QAAQ,CAAC,UAAU,EAAE,CAAC;SACvB;QACD,QAAQ,GAAG,IAAI,gBAAgB,CAAC,GAAG,EAAE;YACnC,iBAAiB,CAAC,IAAI,CACpB,IAAI,EACJ,UAAU,EACV,OAAO,CAAC,YAAY,CAAC,UAAU,CAAC,CACjC,CAAC;YACF,MAAM,CAAC,UAAU,CAAC,GAAG,OAAO,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC;QACxD,CAAC,CAAC,CAAC;QACH,QAAQ,CAAC,OAAO,CAAC,OAAO,EAAE;YACxB,eAAe,EAAE,CAAC,UAAU,CAAC;YAC7B,UAAU,EAAE,IAAI;SACjB,CAAC,CAAC;QACH,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC3B,CAAC,CAAC;IACF,SAAS,CAAC,SAAS,GAAG;QACpB,QAAQ,CAAC,UAAU,EAAE,CAAC;QACtB,OAAO,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC9B,CAAC,CAAC;AACJ,CAAC,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,55 +1,6 @@
1
- export declare type C<T> = new (...args: never[]) => T;
2
- export interface Constructor<T> extends C<T> {
3
- options: ModifierOptions;
4
- }
5
- interface ModifierOptions {
6
- selector: string;
7
- registry?(this: HTMLElement): CustomAttributeRegistry;
8
- observedAttributes?: string[];
9
- }
10
- interface InputOptions {
11
- /**
12
- * If enabled will trigger OnUpdate method on the Attribute
13
- * */
14
- observe: true;
15
- }
16
- /**
17
- * Decorator @Input
18
- * Used to get attribute from element
19
- */
20
- export declare const Input: (options?: InputOptions) => (target: unknown, memberName: string) => void;
21
- /**
22
- * Decorator @Modifier
23
- * Accepts parameter options with selector and registry
24
- */
25
- export declare const Modifier: (options: ModifierOptions) => (target: Function) => void;
26
- export declare const CustomAttribute: (options: ModifierOptions) => (target: Function) => void;
27
- export declare abstract class Attribute<T = {}> {
28
- static options: ModifierOptions;
29
- element?: HTMLElement;
30
- value?: string;
31
- selector?: string;
32
- parent?: HTMLElement;
33
- setStyles(keys: T): (div: HTMLElement | Element | HTMLDivElement) => void;
34
- OnInit(): void;
35
- OnDestroy(): void;
36
- OnUpdate(_oldValue: string, _newValue: string): void;
37
- OnUpdateAttribute(_name: string, _value: string): void;
38
- }
39
- export declare class CustomAttributeRegistry {
40
- private parent;
41
- private _attrMap;
42
- private _elementMap;
43
- private observer;
44
- constructor(parent: HTMLElement);
45
- define(attrName: string, Constructor: Constructor<Attribute>): void;
46
- get(element: HTMLElement, attrName: string): Attribute<{}>;
47
- private getConstructor;
48
- private observe;
49
- unsubscribe(): void;
50
- private upgradeAttribute;
51
- private upgradeElement;
52
- private downgrade;
53
- private found;
54
- }
55
- export {};
1
+ export * from './attribute';
2
+ export * from './custom-registry';
3
+ export * from './decorators';
4
+ export * from './helpers';
5
+ export * from './media-attribute';
6
+ export * from './types';