@rhtml/custom-attributes 0.0.112 → 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.
@@ -0,0 +1,92 @@
1
+ import { Attribute } from './attribute';
2
+
3
+ /**
4
+ * Media query Attribute
5
+ * for performance reasons it is key value pair
6
+ */
7
+ export const MediaMatchers = new Map([
8
+ ['screen and (max-width: 599px)', 'xs'],
9
+ ['screen and (min-width: 600px) and (max-width: 959px)', 'sm'],
10
+ ['screen and (min-width: 960px) and (max-width: 1279px)', 'md'],
11
+ ['screen and (min-width: 1280px) and (max-width: 1919px)', 'lg'],
12
+ ['screen and (min-width: 1920px) and (max-width: 5000px)', 'xl'],
13
+ ['screen and (max-width: 959px)', 'lt-md'],
14
+ ['screen and (max-width: 1279px)', 'lt-lg'],
15
+ ['screen and (max-width: 1919px)', 'lt-xl'],
16
+ ['screen and (min-width: 600px)', 'gt-xs'],
17
+ ['screen and (min-width: 960px)', 'gt-sm'],
18
+ ['screen and (min-width: 1280px)', 'gt-md'],
19
+ ['screen and (min-width: 1920px)', 'gt-lg']
20
+ ]);
21
+ type MediaEvent = MediaQueryList | MediaQueryListEvent;
22
+ export type EnterMediaQueryAttributes = [MediaEvent, Attr];
23
+ export type ExitMediaQueryAttributes = [MediaEvent, string];
24
+
25
+ export interface OnUpdateMediaQuery {
26
+ OnEnterMediaQuery(tuple: [MediaEvent, Attr]): void;
27
+ OnExitMediaQuery(tuple: [MediaEvent, string]): void;
28
+ }
29
+
30
+ export const Breakpoints = [...MediaMatchers.values()];
31
+
32
+ export const createFiltersFromSelector = (selector: string) => [
33
+ ...Breakpoints.map(breakpoint => `${selector}.${breakpoint}`),
34
+ selector
35
+ ];
36
+
37
+ export abstract class MediaQueryAttribute<T> extends Attribute<T>
38
+ implements OnUpdateMediaQuery {
39
+ prevValue: string;
40
+ originalValue: string;
41
+
42
+ private matchers: Map<MediaQueryList, MediaQueryList> = new Map();
43
+ private cachedAttributes: Map<string, Attr> = new Map();
44
+
45
+ listener = (event: MediaQueryList | MediaQueryListEvent) => {
46
+ const key = `${this.selector.toLowerCase()}.${MediaMatchers.get(
47
+ event.media
48
+ )}`;
49
+ const attribute = this.cachedAttributes.get(key);
50
+
51
+ if (event.matches && attribute) {
52
+ return this.OnEnterMediaQuery([event, attribute]);
53
+ }
54
+ return this.OnExitMediaQuery([event, key]);
55
+ };
56
+
57
+ OnInit() {
58
+ if (this.OnEnterMediaQuery || this.OnExitMediaQuery) {
59
+ this.originalValue = this.value;
60
+ for (const query of MediaMatchers.keys()) {
61
+ const matcher = window.matchMedia(query);
62
+
63
+ const attr = Object.values(this.element.attributes).find(
64
+ v =>
65
+ v.name ===
66
+ `${this.selector.toLowerCase()}.${MediaMatchers.get(query)}`
67
+ );
68
+
69
+ if (attr) {
70
+ this.cachedAttributes.set(attr.name, attr);
71
+ matcher.addEventListener('change', this.listener);
72
+ }
73
+
74
+ if (attr && matcher.matches) {
75
+ this.listener(matcher);
76
+ }
77
+ this.matchers.set(matcher, matcher);
78
+ }
79
+ }
80
+ }
81
+
82
+ OnDestroy() {
83
+ for (const matcher of this.matchers.values()) {
84
+ matcher.removeEventListener('change', this.listener);
85
+ }
86
+ this.cachedAttributes.clear();
87
+ this.matchers.clear();
88
+ }
89
+
90
+ abstract OnEnterMediaQuery(tuple: [MediaEvent, Attr]): void;
91
+ abstract OnExitMediaQuery(tuple: [MediaEvent, string]): void;
92
+ }
package/src/types.ts ADDED
@@ -0,0 +1,34 @@
1
+ import { CustomAttributeRegistry } from './custom-registry';
2
+
3
+ export type C<T> = new (...args: never[]) => T;
4
+
5
+ export interface Constructor<T> extends C<T> {
6
+ options: ModifierOptions;
7
+ }
8
+
9
+ export interface ModifierOptions {
10
+ /**
11
+ * Main selector of the attribute
12
+ */
13
+ selector: string;
14
+ /**
15
+ * Define custom attribute registry
16
+ */
17
+ registry?(this: HTMLElement): CustomAttributeRegistry;
18
+ /**
19
+ * Specify attributes to be listened
20
+ */
21
+ observedAttributes?: string[];
22
+ /**
23
+ * Define MutationObserver to listen for parent element changes
24
+ * Defining property will attach observer to this.element
25
+ * */
26
+ observe?: MutationObserverInit;
27
+ }
28
+
29
+ export interface InputOptions {
30
+ /**
31
+ * If enabled will trigger OnUpdate method on the Attribute
32
+ * */
33
+ observe: true;
34
+ }