@rhtml/custom-attributes 0.0.112 → 0.0.115

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,90 @@
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
+
22
+ export type MediaEvent = MediaQueryList | MediaQueryListEvent;
23
+ export type MediaQueryEvent = [MediaEvent, Attr];
24
+
25
+ export interface OnUpdateMediaQuery {
26
+ OnEnterMediaQuery(tuple: MediaQueryEvent): void;
27
+ OnExitMediaQuery(tuple: MediaQueryEvent): 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>
38
+ extends Attribute<T>
39
+ implements OnUpdateMediaQuery
40
+ {
41
+ private matchers: Map<MediaQueryList, MediaQueryList> = new Map();
42
+ private cachedAttributes: Map<string, Attr> = new Map();
43
+
44
+ listener = (event: MediaQueryList | MediaQueryListEvent) => {
45
+ const key = `${this.selector.toLowerCase()}.${MediaMatchers.get(
46
+ event.media
47
+ )}`;
48
+ const attribute = this.cachedAttributes.get(key);
49
+
50
+ if (event.matches && attribute) {
51
+ return this.OnEnterMediaQuery([event, attribute]);
52
+ }
53
+ return this.OnExitMediaQuery([event, attribute]);
54
+ };
55
+
56
+ OnInit() {
57
+ if (this.OnEnterMediaQuery || this.OnExitMediaQuery) {
58
+ for (const query of MediaMatchers.keys()) {
59
+ const matcher = window.matchMedia(query);
60
+
61
+ const attr = Object.values(this.element.attributes).find(
62
+ (v) =>
63
+ v.name ===
64
+ `${this.selector.toLowerCase()}.${MediaMatchers.get(query)}`
65
+ );
66
+
67
+ if (attr) {
68
+ this.cachedAttributes.set(attr.name, attr);
69
+ matcher.addEventListener('change', this.listener);
70
+ }
71
+
72
+ if (attr && matcher.matches) {
73
+ this.listener(matcher);
74
+ }
75
+ this.matchers.set(matcher, matcher);
76
+ }
77
+ }
78
+ }
79
+
80
+ OnDestroy() {
81
+ for (const matcher of this.matchers.values()) {
82
+ matcher.removeEventListener('change', this.listener);
83
+ }
84
+ this.cachedAttributes.clear();
85
+ this.matchers.clear();
86
+ }
87
+
88
+ abstract OnEnterMediaQuery(tuple: MediaQueryEvent): void;
89
+ abstract OnExitMediaQuery(tuple: MediaQueryEvent): void;
90
+ }
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
+ }
package/.eslintrc.js DELETED
@@ -1,26 +0,0 @@
1
- module.exports = {
2
- // Specifies the ESLint parser
3
- parser: "@typescript-eslint/parser",
4
- extends: [
5
- // Uses the recommended rules from the @typescript-eslint/eslint-plugin
6
- "plugin:@typescript-eslint/recommended",
7
- // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
8
- "prettier/@typescript-eslint",
9
- // Enables eslint-plugin-prettier and eslint-config-prettier. This will display prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
10
- "plugin:prettier/recommended"
11
- ],
12
- parserOptions: {
13
- // Allows for the parsing of modern ECMAScript features
14
- ecmaVersion: 2018,
15
- // Allows for the use of imports
16
- sourceType: "module"
17
- },
18
- rules: {
19
- "@typescript-eslint/explicit-function-return-type": 0,
20
- "simple-import-sort/sort": "error",
21
- "sort-imports": "off",
22
- "import/order": "off",
23
- "@typescript-eslint/camelcase": 0
24
- },
25
- plugins: ["simple-import-sort"]
26
- };
package/.prettierrc DELETED
@@ -1,4 +0,0 @@
1
- {
2
- "singleQuote": true,
3
- "printWidth": 80
4
- }
package/jest.config.js DELETED
@@ -1,16 +0,0 @@
1
- module.exports = {
2
- testEnvironment: 'node',
3
- testPathIgnorePatterns: ['/node_modules/'],
4
- coverageReporters: ['lcov', 'html'],
5
- rootDir: './',
6
- moduleFileExtensions: ['ts', 'tsx', 'js', 'json', 'node'],
7
- globals: {
8
- __DEV__: true
9
- },
10
- transform: {
11
- '\\.(ts|tsx)$': 'ts-jest'
12
- },
13
- testRegex: '/src/.*\\.spec.(ts|tsx|js)$',
14
- verbose: true,
15
- collectCoverage: true
16
- };