@ouroboros/browser 0.4.0 → 0.4.1
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 +3 -3
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/index.ts +1 -1
- package/package.json +3 -2
- package/size.d.ts +103 -34
- package/size.js +163 -72
- package/size.ts +180 -73
package/README.md
CHANGED
|
@@ -72,7 +72,7 @@ or
|
|
|
72
72
|
import { network } from '@ouroboros/browser';
|
|
73
73
|
```
|
|
74
74
|
|
|
75
|
-
default (
|
|
75
|
+
default (Subscribe)
|
|
76
76
|
|
|
77
77
|
## pageVisibility
|
|
78
78
|
|
|
@@ -88,7 +88,7 @@ or
|
|
|
88
88
|
import { pageVisibility } from '@ouroboros/browser';
|
|
89
89
|
```
|
|
90
90
|
|
|
91
|
-
default (
|
|
91
|
+
default (Subscribe)
|
|
92
92
|
|
|
93
93
|
## safeLocalStorage
|
|
94
94
|
|
|
@@ -120,4 +120,4 @@ or
|
|
|
120
120
|
import { size } from '@ouroboros/browser';
|
|
121
121
|
```
|
|
122
122
|
|
|
123
|
-
|
|
123
|
+
size (Subscribe), compare(), greaterThan(), lessThan()
|
package/index.d.ts
CHANGED
|
@@ -4,4 +4,4 @@ export { default as hash } from './hash';
|
|
|
4
4
|
export { default as network } from './network';
|
|
5
5
|
export { default as pageVisibility } from './pageVisibility';
|
|
6
6
|
export { default as safeLocalStorage } from './safeLocalStorage';
|
|
7
|
-
export { default as size } from './size';
|
|
7
|
+
export { default as size, compare, greaterThan, lessThan } from './size';
|
package/index.js
CHANGED
|
@@ -4,4 +4,4 @@ export { default as hash } from './hash';
|
|
|
4
4
|
export { default as network } from './network';
|
|
5
5
|
export { default as pageVisibility } from './pageVisibility';
|
|
6
6
|
export { default as safeLocalStorage } from './safeLocalStorage';
|
|
7
|
-
export { default as size } from './size';
|
|
7
|
+
export { default as size, compare, greaterThan, lessThan } from './size';
|
package/index.ts
CHANGED
|
@@ -4,4 +4,4 @@ export { default as hash } from './hash';
|
|
|
4
4
|
export { default as network } from './network';
|
|
5
5
|
export { default as pageVisibility } from './pageVisibility';
|
|
6
6
|
export { default as safeLocalStorage } from './safeLocalStorage';
|
|
7
|
-
export { default as size } from './size';
|
|
7
|
+
export { default as size, compare, greaterThan, lessThan } from './size';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ouroboros/browser",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Package to handle common browser functionality.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -22,7 +22,8 @@
|
|
|
22
22
|
"hash",
|
|
23
23
|
"local storage",
|
|
24
24
|
"network",
|
|
25
|
-
"page visibility"
|
|
25
|
+
"page visibility",
|
|
26
|
+
"browser size"
|
|
26
27
|
],
|
|
27
28
|
"author": "Chris Nasr <chris@ouroboroscoding.com>",
|
|
28
29
|
"license": "MIT",
|
package/size.d.ts
CHANGED
|
@@ -7,13 +7,101 @@
|
|
|
7
7
|
* @copyright Ouroboros Coding Inc.
|
|
8
8
|
* @created 2024-09-19
|
|
9
9
|
*/
|
|
10
|
-
|
|
11
|
-
export
|
|
12
|
-
export declare const md = "md";
|
|
13
|
-
export declare const lg = "lg";
|
|
14
|
-
export declare const xl = "xl";
|
|
15
|
-
export type SIZE = typeof xs | typeof sm | typeof md | typeof lg | typeof xl;
|
|
10
|
+
import Subscribe, { SubscribeCallback, SubscribeReturn } from '@ouroboros/subscribe';
|
|
11
|
+
export type SIZE = 'xs' | 'sm' | 'md' | 'lg' | 'xl';
|
|
16
12
|
export declare const SIZES: Record<SIZE, number>;
|
|
13
|
+
/**
|
|
14
|
+
* Size
|
|
15
|
+
*
|
|
16
|
+
* Extends the Subscribe class to be created once and exported
|
|
17
|
+
*
|
|
18
|
+
* @name Size
|
|
19
|
+
* @extends Subscribe
|
|
20
|
+
*/
|
|
21
|
+
declare class Size extends Subscribe {
|
|
22
|
+
_callback: () => void;
|
|
23
|
+
/**
|
|
24
|
+
* Constructor
|
|
25
|
+
*
|
|
26
|
+
* Creates a new instance and returns it
|
|
27
|
+
*
|
|
28
|
+
* @name Size
|
|
29
|
+
* @access private
|
|
30
|
+
* @returns Size
|
|
31
|
+
*/
|
|
32
|
+
constructor();
|
|
33
|
+
/**
|
|
34
|
+
* Compare
|
|
35
|
+
*
|
|
36
|
+
* Calls the module's `compare` method with the current size and the
|
|
37
|
+
* `against` param.
|
|
38
|
+
*
|
|
39
|
+
* @name compare
|
|
40
|
+
* @access public
|
|
41
|
+
* @param against The size to compare the current size against
|
|
42
|
+
* @returns number
|
|
43
|
+
*/
|
|
44
|
+
compare(against: SIZE): number;
|
|
45
|
+
/**
|
|
46
|
+
* Get
|
|
47
|
+
*
|
|
48
|
+
* Returns the current size. Overrides the built in Subscribe.get to make
|
|
49
|
+
* sure we always have the latest, even if no one has ever subscribed
|
|
50
|
+
*
|
|
51
|
+
* @name get
|
|
52
|
+
* @access public
|
|
53
|
+
* @returns string
|
|
54
|
+
*/
|
|
55
|
+
get(): SIZE;
|
|
56
|
+
/**
|
|
57
|
+
* Greater Than
|
|
58
|
+
*
|
|
59
|
+
* Calls the module's `greaterThan` method with the current size and the
|
|
60
|
+
* `against` param.
|
|
61
|
+
*
|
|
62
|
+
* @name greaterThan
|
|
63
|
+
* @access public
|
|
64
|
+
* @param against The size to compare the current size against
|
|
65
|
+
* @returns number
|
|
66
|
+
*/
|
|
67
|
+
greaterThan(against: SIZE): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Less Than
|
|
70
|
+
*
|
|
71
|
+
* Calls the module's `lessThan` method with the current size and the
|
|
72
|
+
* `against` param.
|
|
73
|
+
*
|
|
74
|
+
* @name lessThan
|
|
75
|
+
* @access public
|
|
76
|
+
* @param against The size to compare the current size against
|
|
77
|
+
* @returns number
|
|
78
|
+
*/
|
|
79
|
+
lessThan(against: SIZE): boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Subscribe
|
|
82
|
+
*
|
|
83
|
+
* Overrides parent subscribe to handle adding the window event
|
|
84
|
+
*
|
|
85
|
+
* @name subscribe
|
|
86
|
+
* @access public
|
|
87
|
+
* @param callback The function to call when the state changes
|
|
88
|
+
* @returns object
|
|
89
|
+
*/
|
|
90
|
+
subscribe(callback: SubscribeCallback): SubscribeReturn;
|
|
91
|
+
/**
|
|
92
|
+
* Unsubscribe
|
|
93
|
+
*
|
|
94
|
+
* Overrides parent unsubscribe to handle removing the window event
|
|
95
|
+
*
|
|
96
|
+
* @name unsubscribe
|
|
97
|
+
* @access public
|
|
98
|
+
* @param callback The callback to remove from the list
|
|
99
|
+
* @returns boolean
|
|
100
|
+
*/
|
|
101
|
+
unsubscribe(callback: SubscribeCallback): boolean;
|
|
102
|
+
}
|
|
103
|
+
declare const size: Size;
|
|
104
|
+
export default size;
|
|
17
105
|
/**
|
|
18
106
|
* Compare
|
|
19
107
|
*
|
|
@@ -22,10 +110,11 @@ export declare const SIZES: Record<SIZE, number>;
|
|
|
22
110
|
*
|
|
23
111
|
* @name compare
|
|
24
112
|
* @access public
|
|
25
|
-
* @param
|
|
113
|
+
* @param a The size to compare against `b`
|
|
114
|
+
* @param b The size to compare against `a`
|
|
26
115
|
* @returns -1 || 0 || 1
|
|
27
116
|
*/
|
|
28
|
-
export declare function compare(
|
|
117
|
+
export declare function compare(a: SIZE, b: SIZE): 0 | 1 | -1;
|
|
29
118
|
/**
|
|
30
119
|
* Greater Than
|
|
31
120
|
*
|
|
@@ -33,10 +122,11 @@ export declare function compare(against: SIZE): 0 | 1 | -1;
|
|
|
33
122
|
*
|
|
34
123
|
* @name greaterThan
|
|
35
124
|
* @access public
|
|
36
|
-
* @param
|
|
125
|
+
* @param a The size to compare as greater than `b`
|
|
126
|
+
* @param b The size to compare against `a`
|
|
37
127
|
* @returns boolean
|
|
38
128
|
*/
|
|
39
|
-
export declare function greaterThan(
|
|
129
|
+
export declare function greaterThan(a: SIZE, b: SIZE): boolean;
|
|
40
130
|
/**
|
|
41
131
|
* Less Than
|
|
42
132
|
*
|
|
@@ -44,29 +134,8 @@ export declare function greaterThan(against: SIZE): boolean;
|
|
|
44
134
|
*
|
|
45
135
|
* @name greaterThan
|
|
46
136
|
* @access public
|
|
47
|
-
* @param
|
|
137
|
+
* @param a The size to compare as less than `b`
|
|
138
|
+
* @param b The size to compare against `a`
|
|
48
139
|
* @returns boolean
|
|
49
140
|
*/
|
|
50
|
-
export declare function lessThan(
|
|
51
|
-
/**
|
|
52
|
-
* Get
|
|
53
|
-
*
|
|
54
|
-
* Returns the current width type based on the current client width
|
|
55
|
-
*
|
|
56
|
-
* @name get
|
|
57
|
-
* @access public
|
|
58
|
-
* @returns The current width
|
|
59
|
-
*/
|
|
60
|
-
export declare function get(): SIZE;
|
|
61
|
-
declare const width: {
|
|
62
|
-
xs: string;
|
|
63
|
-
sm: string;
|
|
64
|
-
md: string;
|
|
65
|
-
lg: string;
|
|
66
|
-
xl: string;
|
|
67
|
-
compare: typeof compare;
|
|
68
|
-
greaterThan: typeof greaterThan;
|
|
69
|
-
lessThan: typeof lessThan;
|
|
70
|
-
get: typeof get;
|
|
71
|
-
};
|
|
72
|
-
export default width;
|
|
141
|
+
export declare function lessThan(a: SIZE, b: SIZE): boolean;
|
package/size.js
CHANGED
|
@@ -7,52 +7,168 @@
|
|
|
7
7
|
* @copyright Ouroboros Coding Inc.
|
|
8
8
|
* @created 2024-09-19
|
|
9
9
|
*/
|
|
10
|
-
//
|
|
11
|
-
|
|
12
|
-
export const sm = 'sm';
|
|
13
|
-
export const md = 'md';
|
|
14
|
-
export const lg = 'lg';
|
|
15
|
-
export const xl = 'xl';
|
|
10
|
+
// Ouroboros modules
|
|
11
|
+
import Subscribe from '@ouroboros/subscribe';
|
|
16
12
|
// The minimum pixels necessary for each size type
|
|
17
13
|
export const SIZES = {
|
|
18
|
-
|
|
19
|
-
[lg]: 1200, [xl]: 1536
|
|
14
|
+
xs: 0, sm: 600, md: 900, lg: 1200, xl: 1536
|
|
20
15
|
};
|
|
21
|
-
// The current size
|
|
22
|
-
let _current = null;
|
|
23
16
|
/**
|
|
24
|
-
*
|
|
17
|
+
* Calculate
|
|
25
18
|
*
|
|
26
|
-
*
|
|
19
|
+
* Uses the current client width to calculate the string representation
|
|
27
20
|
*
|
|
28
|
-
* @name
|
|
21
|
+
* @name calculate
|
|
29
22
|
* @access private
|
|
23
|
+
* @returns string
|
|
30
24
|
*/
|
|
31
|
-
function
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
25
|
+
function calculate() {
|
|
26
|
+
if (document.documentElement.clientWidth < 600) {
|
|
27
|
+
return 'xs';
|
|
28
|
+
}
|
|
29
|
+
if (document.documentElement.clientWidth < 900) {
|
|
30
|
+
return 'sm';
|
|
31
|
+
}
|
|
32
|
+
if (document.documentElement.clientWidth < 1200) {
|
|
33
|
+
return 'md';
|
|
34
|
+
}
|
|
35
|
+
if (document.documentElement.clientWidth < 1536) {
|
|
36
|
+
return 'lg';
|
|
37
|
+
}
|
|
38
|
+
return 'xl';
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Size
|
|
42
|
+
*
|
|
43
|
+
* Extends the Subscribe class to be created once and exported
|
|
44
|
+
*
|
|
45
|
+
* @name Size
|
|
46
|
+
* @extends Subscribe
|
|
47
|
+
*/
|
|
48
|
+
class Size extends Subscribe {
|
|
49
|
+
// The window event callback
|
|
50
|
+
_callback;
|
|
51
|
+
/**
|
|
52
|
+
* Constructor
|
|
53
|
+
*
|
|
54
|
+
* Creates a new instance and returns it
|
|
55
|
+
*
|
|
56
|
+
* @name Size
|
|
57
|
+
* @access private
|
|
58
|
+
* @returns Size
|
|
59
|
+
*/
|
|
60
|
+
constructor() {
|
|
61
|
+
// Get the current values and init the instance with them
|
|
62
|
+
super(calculate());
|
|
63
|
+
// Store the subscription callback
|
|
64
|
+
this._callback = () => {
|
|
65
|
+
this.set(calculate());
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Compare
|
|
70
|
+
*
|
|
71
|
+
* Calls the module's `compare` method with the current size and the
|
|
72
|
+
* `against` param.
|
|
73
|
+
*
|
|
74
|
+
* @name compare
|
|
75
|
+
* @access public
|
|
76
|
+
* @param against The size to compare the current size against
|
|
77
|
+
* @returns number
|
|
78
|
+
*/
|
|
79
|
+
compare(against) {
|
|
80
|
+
return compare(this.subscribeData, against);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Get
|
|
84
|
+
*
|
|
85
|
+
* Returns the current size. Overrides the built in Subscribe.get to make
|
|
86
|
+
* sure we always have the latest, even if no one has ever subscribed
|
|
87
|
+
*
|
|
88
|
+
* @name get
|
|
89
|
+
* @access public
|
|
90
|
+
* @returns string
|
|
91
|
+
*/
|
|
92
|
+
get() {
|
|
93
|
+
const s = calculate();
|
|
94
|
+
if (s !== this.subscribeData) {
|
|
95
|
+
this.set(s);
|
|
39
96
|
}
|
|
40
|
-
|
|
41
|
-
|
|
97
|
+
return s;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Greater Than
|
|
101
|
+
*
|
|
102
|
+
* Calls the module's `greaterThan` method with the current size and the
|
|
103
|
+
* `against` param.
|
|
104
|
+
*
|
|
105
|
+
* @name greaterThan
|
|
106
|
+
* @access public
|
|
107
|
+
* @param against The size to compare the current size against
|
|
108
|
+
* @returns number
|
|
109
|
+
*/
|
|
110
|
+
greaterThan(against) {
|
|
111
|
+
return greaterThan(this.subscribeData, against);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Less Than
|
|
115
|
+
*
|
|
116
|
+
* Calls the module's `lessThan` method with the current size and the
|
|
117
|
+
* `against` param.
|
|
118
|
+
*
|
|
119
|
+
* @name lessThan
|
|
120
|
+
* @access public
|
|
121
|
+
* @param against The size to compare the current size against
|
|
122
|
+
* @returns number
|
|
123
|
+
*/
|
|
124
|
+
lessThan(against) {
|
|
125
|
+
return lessThan(this.subscribeData, against);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Subscribe
|
|
129
|
+
*
|
|
130
|
+
* Overrides parent subscribe to handle adding the window event
|
|
131
|
+
*
|
|
132
|
+
* @name subscribe
|
|
133
|
+
* @access public
|
|
134
|
+
* @param callback The function to call when the state changes
|
|
135
|
+
* @returns object
|
|
136
|
+
*/
|
|
137
|
+
subscribe(callback) {
|
|
138
|
+
// If we have no current subscribers
|
|
139
|
+
if (this.subscribeCallbacks.length === 0) {
|
|
140
|
+
// Add the event listener
|
|
141
|
+
window.addEventListener('resize', this._callback);
|
|
42
142
|
}
|
|
43
|
-
|
|
44
|
-
|
|
143
|
+
// Call the parent subscribe and return
|
|
144
|
+
return super.subscribe(callback);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Unsubscribe
|
|
148
|
+
*
|
|
149
|
+
* Overrides parent unsubscribe to handle removing the window event
|
|
150
|
+
*
|
|
151
|
+
* @name unsubscribe
|
|
152
|
+
* @access public
|
|
153
|
+
* @param callback The callback to remove from the list
|
|
154
|
+
* @returns boolean
|
|
155
|
+
*/
|
|
156
|
+
unsubscribe(callback) {
|
|
157
|
+
// Call the parent unsubscribe
|
|
158
|
+
const bRet = super.unsubscribe(callback);
|
|
159
|
+
// If it was successful and we have no more callbacks
|
|
160
|
+
if (bRet && this.subscribeCallbacks.length === 0) {
|
|
161
|
+
// Remove the event listener
|
|
162
|
+
window.removeEventListener('resize', this._callback);
|
|
45
163
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
// If we don't have the size
|
|
49
|
-
if (_current === null) {
|
|
50
|
-
_current = _calculate();
|
|
51
|
-
window.addEventListener('resize', () => {
|
|
52
|
-
_current = _calculate();
|
|
53
|
-
});
|
|
164
|
+
// Return
|
|
165
|
+
return bRet;
|
|
54
166
|
}
|
|
55
167
|
}
|
|
168
|
+
// Create an instance of the class
|
|
169
|
+
const size = new Size();
|
|
170
|
+
// Default export
|
|
171
|
+
export default size;
|
|
56
172
|
/**
|
|
57
173
|
* Compare
|
|
58
174
|
*
|
|
@@ -61,18 +177,16 @@ function _init() {
|
|
|
61
177
|
*
|
|
62
178
|
* @name compare
|
|
63
179
|
* @access public
|
|
64
|
-
* @param
|
|
180
|
+
* @param a The size to compare against `b`
|
|
181
|
+
* @param b The size to compare against `a`
|
|
65
182
|
* @returns -1 || 0 || 1
|
|
66
183
|
*/
|
|
67
|
-
export function compare(
|
|
68
|
-
// Init if necessary
|
|
69
|
-
if (_current === null)
|
|
70
|
-
_init();
|
|
184
|
+
export function compare(a, b) {
|
|
71
185
|
// If the current size is the same as the passed value
|
|
72
|
-
if (
|
|
186
|
+
if (a === b)
|
|
73
187
|
return 0;
|
|
74
188
|
// Else, if the current value is less than the passed value
|
|
75
|
-
if (SIZES[
|
|
189
|
+
if (SIZES[a] < SIZES[b])
|
|
76
190
|
return -1;
|
|
77
191
|
// Else, the current value is more than the passed value
|
|
78
192
|
return 1;
|
|
@@ -84,15 +198,13 @@ export function compare(against) {
|
|
|
84
198
|
*
|
|
85
199
|
* @name greaterThan
|
|
86
200
|
* @access public
|
|
87
|
-
* @param
|
|
201
|
+
* @param a The size to compare as greater than `b`
|
|
202
|
+
* @param b The size to compare against `a`
|
|
88
203
|
* @returns boolean
|
|
89
204
|
*/
|
|
90
|
-
export function greaterThan(
|
|
91
|
-
// Init if necessary
|
|
92
|
-
if (_current === null)
|
|
93
|
-
_init();
|
|
205
|
+
export function greaterThan(a, b) {
|
|
94
206
|
// If the current value is greater than the passed value
|
|
95
|
-
return SIZES[
|
|
207
|
+
return SIZES[a] > SIZES[b];
|
|
96
208
|
}
|
|
97
209
|
/**
|
|
98
210
|
* Less Than
|
|
@@ -101,32 +213,11 @@ export function greaterThan(against) {
|
|
|
101
213
|
*
|
|
102
214
|
* @name greaterThan
|
|
103
215
|
* @access public
|
|
104
|
-
* @param
|
|
216
|
+
* @param a The size to compare as less than `b`
|
|
217
|
+
* @param b The size to compare against `a`
|
|
105
218
|
* @returns boolean
|
|
106
219
|
*/
|
|
107
|
-
export function lessThan(
|
|
108
|
-
//
|
|
109
|
-
|
|
110
|
-
_init();
|
|
111
|
-
// If the current value is less than the passed value
|
|
112
|
-
return SIZES[_current] < SIZES[against];
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Get
|
|
116
|
-
*
|
|
117
|
-
* Returns the current width type based on the current client width
|
|
118
|
-
*
|
|
119
|
-
* @name get
|
|
120
|
-
* @access public
|
|
121
|
-
* @returns The current width
|
|
122
|
-
*/
|
|
123
|
-
export function get() {
|
|
124
|
-
// Init if necessary
|
|
125
|
-
if (_current === null)
|
|
126
|
-
_init();
|
|
127
|
-
// Return the current size
|
|
128
|
-
return _current;
|
|
220
|
+
export function lessThan(a, b) {
|
|
221
|
+
// If the `a` minimum value is less than the `b` minimum value
|
|
222
|
+
return SIZES[a] < SIZES[b];
|
|
129
223
|
}
|
|
130
|
-
// Default export
|
|
131
|
-
const width = { xs, sm, md, lg, xl, compare, greaterThan, lessThan, get };
|
|
132
|
-
export default width;
|
package/size.ts
CHANGED
|
@@ -8,53 +8,188 @@
|
|
|
8
8
|
* @created 2024-09-19
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
//
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
export const xl = 'xl';
|
|
11
|
+
// Ouroboros modules
|
|
12
|
+
import Subscribe, {
|
|
13
|
+
SubscribeCallback,
|
|
14
|
+
SubscribeReturn
|
|
15
|
+
} from '@ouroboros/subscribe';
|
|
17
16
|
|
|
18
17
|
// Types
|
|
19
|
-
export type SIZE =
|
|
18
|
+
export type SIZE = 'xs' | 'sm' | 'md' | 'lg' | 'xl';
|
|
20
19
|
|
|
21
20
|
// The minimum pixels necessary for each size type
|
|
22
21
|
export const SIZES: Record<SIZE, number> = {
|
|
23
|
-
|
|
24
|
-
[lg]: 1200, [xl]: 1536
|
|
22
|
+
xs: 0, sm: 600, md: 900, lg: 1200, xl: 1536
|
|
25
23
|
}
|
|
26
24
|
|
|
27
|
-
// The current size
|
|
28
|
-
let _current: SIZE | null = null;
|
|
29
|
-
|
|
30
25
|
/**
|
|
31
|
-
*
|
|
26
|
+
* Calculate
|
|
32
27
|
*
|
|
33
|
-
*
|
|
28
|
+
* Uses the current client width to calculate the string representation
|
|
34
29
|
*
|
|
35
|
-
* @name
|
|
30
|
+
* @name calculate
|
|
36
31
|
* @access private
|
|
32
|
+
* @returns string
|
|
37
33
|
*/
|
|
38
|
-
function
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
34
|
+
function calculate(): SIZE {
|
|
35
|
+
if(document.documentElement.clientWidth < 600) { return 'xs' }
|
|
36
|
+
if(document.documentElement.clientWidth < 900) { return 'sm' }
|
|
37
|
+
if(document.documentElement.clientWidth < 1200) { return 'md' }
|
|
38
|
+
if(document.documentElement.clientWidth < 1536) { return 'lg' }
|
|
39
|
+
return 'xl';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Size
|
|
44
|
+
*
|
|
45
|
+
* Extends the Subscribe class to be created once and exported
|
|
46
|
+
*
|
|
47
|
+
* @name Size
|
|
48
|
+
* @extends Subscribe
|
|
49
|
+
*/
|
|
50
|
+
class Size extends Subscribe {
|
|
51
|
+
|
|
52
|
+
// The window event callback
|
|
53
|
+
_callback: () => void;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Constructor
|
|
57
|
+
*
|
|
58
|
+
* Creates a new instance and returns it
|
|
59
|
+
*
|
|
60
|
+
* @name Size
|
|
61
|
+
* @access private
|
|
62
|
+
* @returns Size
|
|
63
|
+
*/
|
|
64
|
+
constructor() {
|
|
65
|
+
|
|
66
|
+
// Get the current values and init the instance with them
|
|
67
|
+
super(calculate());
|
|
68
|
+
|
|
69
|
+
// Store the subscription callback
|
|
70
|
+
this._callback = () => {
|
|
71
|
+
this.set(calculate());
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Compare
|
|
77
|
+
*
|
|
78
|
+
* Calls the module's `compare` method with the current size and the
|
|
79
|
+
* `against` param.
|
|
80
|
+
*
|
|
81
|
+
* @name compare
|
|
82
|
+
* @access public
|
|
83
|
+
* @param against The size to compare the current size against
|
|
84
|
+
* @returns number
|
|
85
|
+
*/
|
|
86
|
+
compare(against: SIZE): number {
|
|
87
|
+
return compare(this.subscribeData, against);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Get
|
|
92
|
+
*
|
|
93
|
+
* Returns the current size. Overrides the built in Subscribe.get to make
|
|
94
|
+
* sure we always have the latest, even if no one has ever subscribed
|
|
95
|
+
*
|
|
96
|
+
* @name get
|
|
97
|
+
* @access public
|
|
98
|
+
* @returns string
|
|
99
|
+
*/
|
|
100
|
+
get() {
|
|
101
|
+
const s = calculate();
|
|
102
|
+
if(s !== this.subscribeData) {
|
|
103
|
+
this.set(s);
|
|
104
|
+
}
|
|
105
|
+
return s;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Greater Than
|
|
110
|
+
*
|
|
111
|
+
* Calls the module's `greaterThan` method with the current size and the
|
|
112
|
+
* `against` param.
|
|
113
|
+
*
|
|
114
|
+
* @name greaterThan
|
|
115
|
+
* @access public
|
|
116
|
+
* @param against The size to compare the current size against
|
|
117
|
+
* @returns number
|
|
118
|
+
*/
|
|
119
|
+
greaterThan(against: SIZE): boolean {
|
|
120
|
+
return greaterThan(this.subscribeData, against);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Less Than
|
|
125
|
+
*
|
|
126
|
+
* Calls the module's `lessThan` method with the current size and the
|
|
127
|
+
* `against` param.
|
|
128
|
+
*
|
|
129
|
+
* @name lessThan
|
|
130
|
+
* @access public
|
|
131
|
+
* @param against The size to compare the current size against
|
|
132
|
+
* @returns number
|
|
133
|
+
*/
|
|
134
|
+
lessThan(against: SIZE): boolean {
|
|
135
|
+
return lessThan(this.subscribeData, against);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Subscribe
|
|
140
|
+
*
|
|
141
|
+
* Overrides parent subscribe to handle adding the window event
|
|
142
|
+
*
|
|
143
|
+
* @name subscribe
|
|
144
|
+
* @access public
|
|
145
|
+
* @param callback The function to call when the state changes
|
|
146
|
+
* @returns object
|
|
147
|
+
*/
|
|
148
|
+
subscribe(callback: SubscribeCallback): SubscribeReturn {
|
|
149
|
+
|
|
150
|
+
// If we have no current subscribers
|
|
151
|
+
if(this.subscribeCallbacks.length === 0) {
|
|
152
|
+
|
|
153
|
+
// Add the event listener
|
|
154
|
+
window.addEventListener('resize', this._callback);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Call the parent subscribe and return
|
|
158
|
+
return super.subscribe(callback);
|
|
47
159
|
}
|
|
48
160
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
161
|
+
/**
|
|
162
|
+
* Unsubscribe
|
|
163
|
+
*
|
|
164
|
+
* Overrides parent unsubscribe to handle removing the window event
|
|
165
|
+
*
|
|
166
|
+
* @name unsubscribe
|
|
167
|
+
* @access public
|
|
168
|
+
* @param callback The callback to remove from the list
|
|
169
|
+
* @returns boolean
|
|
170
|
+
*/
|
|
171
|
+
unsubscribe(callback: SubscribeCallback): boolean {
|
|
172
|
+
|
|
173
|
+
// Call the parent unsubscribe
|
|
174
|
+
const bRet = super.unsubscribe(callback);
|
|
175
|
+
|
|
176
|
+
// If it was successful and we have no more callbacks
|
|
177
|
+
if(bRet && this.subscribeCallbacks.length === 0) {
|
|
178
|
+
|
|
179
|
+
// Remove the event listener
|
|
180
|
+
window.removeEventListener('resize', this._callback);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Return
|
|
184
|
+
return bRet;
|
|
55
185
|
}
|
|
56
186
|
}
|
|
57
187
|
|
|
188
|
+
// Create an instance of the class
|
|
189
|
+
const size = new Size();
|
|
190
|
+
// Default export
|
|
191
|
+
export default size;
|
|
192
|
+
|
|
58
193
|
/**
|
|
59
194
|
* Compare
|
|
60
195
|
*
|
|
@@ -63,19 +198,17 @@ function _init(): void {
|
|
|
63
198
|
*
|
|
64
199
|
* @name compare
|
|
65
200
|
* @access public
|
|
66
|
-
* @param
|
|
201
|
+
* @param a The size to compare against `b`
|
|
202
|
+
* @param b The size to compare against `a`
|
|
67
203
|
* @returns -1 || 0 || 1
|
|
68
204
|
*/
|
|
69
|
-
export function compare(
|
|
70
|
-
|
|
71
|
-
// Init if necessary
|
|
72
|
-
if(_current === null) _init();
|
|
205
|
+
export function compare(a: SIZE, b: SIZE) {
|
|
73
206
|
|
|
74
207
|
// If the current size is the same as the passed value
|
|
75
|
-
if(
|
|
208
|
+
if(a === b) return 0;
|
|
76
209
|
|
|
77
210
|
// Else, if the current value is less than the passed value
|
|
78
|
-
if(SIZES[
|
|
211
|
+
if(SIZES[a] < SIZES[b]) return -1;
|
|
79
212
|
|
|
80
213
|
// Else, the current value is more than the passed value
|
|
81
214
|
return 1;
|
|
@@ -88,16 +221,14 @@ export function compare(against: SIZE) {
|
|
|
88
221
|
*
|
|
89
222
|
* @name greaterThan
|
|
90
223
|
* @access public
|
|
91
|
-
* @param
|
|
224
|
+
* @param a The size to compare as greater than `b`
|
|
225
|
+
* @param b The size to compare against `a`
|
|
92
226
|
* @returns boolean
|
|
93
227
|
*/
|
|
94
|
-
export function greaterThan(
|
|
95
|
-
|
|
96
|
-
// Init if necessary
|
|
97
|
-
if(_current === null) _init();
|
|
228
|
+
export function greaterThan(a: SIZE, b: SIZE): boolean {
|
|
98
229
|
|
|
99
230
|
// If the current value is greater than the passed value
|
|
100
|
-
return SIZES[
|
|
231
|
+
return SIZES[a] > SIZES[b];
|
|
101
232
|
}
|
|
102
233
|
|
|
103
234
|
/**
|
|
@@ -107,36 +238,12 @@ export function greaterThan(against: SIZE): boolean {
|
|
|
107
238
|
*
|
|
108
239
|
* @name greaterThan
|
|
109
240
|
* @access public
|
|
110
|
-
* @param
|
|
241
|
+
* @param a The size to compare as less than `b`
|
|
242
|
+
* @param b The size to compare against `a`
|
|
111
243
|
* @returns boolean
|
|
112
244
|
*/
|
|
113
|
-
export function lessThan(
|
|
114
|
-
|
|
115
|
-
// Init if necessary
|
|
116
|
-
if(_current === null) _init();
|
|
245
|
+
export function lessThan(a: SIZE, b: SIZE): boolean {
|
|
117
246
|
|
|
118
|
-
// If the
|
|
119
|
-
return SIZES[
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Get
|
|
124
|
-
*
|
|
125
|
-
* Returns the current width type based on the current client width
|
|
126
|
-
*
|
|
127
|
-
* @name get
|
|
128
|
-
* @access public
|
|
129
|
-
* @returns The current width
|
|
130
|
-
*/
|
|
131
|
-
export function get(): SIZE {
|
|
132
|
-
|
|
133
|
-
// Init if necessary
|
|
134
|
-
if(_current === null) _init();
|
|
135
|
-
|
|
136
|
-
// Return the current size
|
|
137
|
-
return _current as SIZE;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// Default export
|
|
141
|
-
const width = { xs, sm, md, lg, xl, compare, greaterThan, lessThan, get };
|
|
142
|
-
export default width;
|
|
247
|
+
// If the `a` minimum value is less than the `b` minimum value
|
|
248
|
+
return SIZES[a] < SIZES[b];
|
|
249
|
+
}
|