@ouroboros/browser 0.4.0 → 0.5.0

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
@@ -72,7 +72,7 @@ or
72
72
  import { network } from '@ouroboros/browser';
73
73
  ```
74
74
 
75
- default (network)
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 (pageVisibility)
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
- xs, sm, md, lg, xl, compare(), get(), greaterThan(), lessThan()
123
+ size (Subscribe), compare(), greaterThan(), lessThan()
@@ -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';
@@ -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';
@@ -0,0 +1,141 @@
1
+ /**
2
+ * Size
3
+ *
4
+ * JS Library to manage the current client width of the screen in specific sizes
5
+ *
6
+ * @author Chris Nasr <chris@ouroboroscoding.com>
7
+ * @copyright Ouroboros Coding Inc.
8
+ * @created 2024-09-19
9
+ */
10
+ import Subscribe, { SubscribeCallback, SubscribeReturn } from '@ouroboros/subscribe';
11
+ export type SIZE = 'xs' | 'sm' | 'md' | 'lg' | 'xl';
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;
105
+ /**
106
+ * Compare
107
+ *
108
+ * Compares two widths and returns the standard -1,0,1 values as a result. -1
109
+ * if `a` is less than `b, 0 for equality, and 1 if `a` is greater than `b`.
110
+ *
111
+ * @name compare
112
+ * @access public
113
+ * @param a The size to compare against `b`
114
+ * @param b The size to compare against `a`
115
+ * @returns -1 || 0 || 1
116
+ */
117
+ export declare function compare(a: SIZE, b: SIZE): 0 | 1 | -1;
118
+ /**
119
+ * Greater Than
120
+ *
121
+ * Returns true only if `a` is a larger width than `b`
122
+ *
123
+ * @name greaterThan
124
+ * @access public
125
+ * @param a The size to compare as greater than `b`
126
+ * @param b The size to compare against `a`
127
+ * @returns boolean
128
+ */
129
+ export declare function greaterThan(a: SIZE, b: SIZE): boolean;
130
+ /**
131
+ * Less Than
132
+ *
133
+ * Returns true only if `a` is a smaller width than `b`
134
+ *
135
+ * @name greaterThan
136
+ * @access public
137
+ * @param a The size to compare as less than `b`
138
+ * @param b The size to compare against `a`
139
+ * @returns boolean
140
+ */
141
+ export declare function lessThan(a: SIZE, b: SIZE): boolean;
package/build/size.js ADDED
@@ -0,0 +1,223 @@
1
+ /**
2
+ * Size
3
+ *
4
+ * JS Library to manage the current client width of the screen in specific sizes
5
+ *
6
+ * @author Chris Nasr <chris@ouroboroscoding.com>
7
+ * @copyright Ouroboros Coding Inc.
8
+ * @created 2024-09-19
9
+ */
10
+ // Ouroboros modules
11
+ import Subscribe from '@ouroboros/subscribe';
12
+ // The minimum pixels necessary for each size type
13
+ export const SIZES = {
14
+ xs: 0, sm: 600, md: 900, lg: 1200, xl: 1536
15
+ };
16
+ /**
17
+ * Calculate
18
+ *
19
+ * Uses the current client width to calculate the string representation
20
+ *
21
+ * @name calculate
22
+ * @access private
23
+ * @returns string
24
+ */
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);
96
+ }
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);
142
+ }
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);
163
+ }
164
+ // Return
165
+ return bRet;
166
+ }
167
+ }
168
+ // Create an instance of the class
169
+ const size = new Size();
170
+ // Default export
171
+ export default size;
172
+ /**
173
+ * Compare
174
+ *
175
+ * Compares two widths and returns the standard -1,0,1 values as a result. -1
176
+ * if `a` is less than `b, 0 for equality, and 1 if `a` is greater than `b`.
177
+ *
178
+ * @name compare
179
+ * @access public
180
+ * @param a The size to compare against `b`
181
+ * @param b The size to compare against `a`
182
+ * @returns -1 || 0 || 1
183
+ */
184
+ export function compare(a, b) {
185
+ // If the current size is the same as the passed value
186
+ if (a === b)
187
+ return 0;
188
+ // Else, if the current value is less than the passed value
189
+ if (SIZES[a] < SIZES[b])
190
+ return -1;
191
+ // Else, the current value is more than the passed value
192
+ return 1;
193
+ }
194
+ /**
195
+ * Greater Than
196
+ *
197
+ * Returns true only if `a` is a larger width than `b`
198
+ *
199
+ * @name greaterThan
200
+ * @access public
201
+ * @param a The size to compare as greater than `b`
202
+ * @param b The size to compare against `a`
203
+ * @returns boolean
204
+ */
205
+ export function greaterThan(a, b) {
206
+ // If the current value is greater than the passed value
207
+ return SIZES[a] > SIZES[b];
208
+ }
209
+ /**
210
+ * Less Than
211
+ *
212
+ * Returns true only if `a` is a smaller width than `b`
213
+ *
214
+ * @name greaterThan
215
+ * @access public
216
+ * @param a The size to compare as less than `b`
217
+ * @param b The size to compare against `a`
218
+ * @returns boolean
219
+ */
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];
223
+ }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@ouroboros/browser",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Package to handle common browser functionality.",
5
- "main": "index.js",
6
- "types": "index.d.ts",
5
+ "main": "build/index.js",
6
+ "types": "build/index.d.ts",
7
7
  "scripts": {
8
8
  "build": "tslint -p tsconfig.json && tsc",
9
- "clean": "rm *.js *.d.ts",
9
+ "clean": "rm build/*",
10
10
  "for_local": "npm install; tslint -p tsconfig.json && tsc; rm -Rf node_modules",
11
11
  "test": "echo \"Error: no test specified.\" && exit 1"
12
12
  },
@@ -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",
@@ -35,8 +36,8 @@
35
36
  "typescript": "^4.8.3"
36
37
  },
37
38
  "dependencies": {
38
- "@ouroboros/clone": "^1.0.1",
39
- "@ouroboros/subscribe": "^1.2.2",
40
- "@ouroboros/tools": "^0.5.3"
39
+ "@ouroboros/clone": "^1.1.0",
40
+ "@ouroboros/subscribe": "^1.3.1",
41
+ "@ouroboros/tools": "^0.6.1"
41
42
  }
42
43
  }
@@ -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/src/size.ts ADDED
@@ -0,0 +1,249 @@
1
+ /**
2
+ * Size
3
+ *
4
+ * JS Library to manage the current client width of the screen in specific sizes
5
+ *
6
+ * @author Chris Nasr <chris@ouroboroscoding.com>
7
+ * @copyright Ouroboros Coding Inc.
8
+ * @created 2024-09-19
9
+ */
10
+
11
+ // Ouroboros modules
12
+ import Subscribe, {
13
+ SubscribeCallback,
14
+ SubscribeReturn
15
+ } from '@ouroboros/subscribe';
16
+
17
+ // Types
18
+ export type SIZE = 'xs' | 'sm' | 'md' | 'lg' | 'xl';
19
+
20
+ // The minimum pixels necessary for each size type
21
+ export const SIZES: Record<SIZE, number> = {
22
+ xs: 0, sm: 600, md: 900, lg: 1200, xl: 1536
23
+ }
24
+
25
+ /**
26
+ * Calculate
27
+ *
28
+ * Uses the current client width to calculate the string representation
29
+ *
30
+ * @name calculate
31
+ * @access private
32
+ * @returns string
33
+ */
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);
159
+ }
160
+
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;
185
+ }
186
+ }
187
+
188
+ // Create an instance of the class
189
+ const size = new Size();
190
+ // Default export
191
+ export default size;
192
+
193
+ /**
194
+ * Compare
195
+ *
196
+ * Compares two widths and returns the standard -1,0,1 values as a result. -1
197
+ * if `a` is less than `b, 0 for equality, and 1 if `a` is greater than `b`.
198
+ *
199
+ * @name compare
200
+ * @access public
201
+ * @param a The size to compare against `b`
202
+ * @param b The size to compare against `a`
203
+ * @returns -1 || 0 || 1
204
+ */
205
+ export function compare(a: SIZE, b: SIZE) {
206
+
207
+ // If the current size is the same as the passed value
208
+ if(a === b) return 0;
209
+
210
+ // Else, if the current value is less than the passed value
211
+ if(SIZES[a] < SIZES[b]) return -1;
212
+
213
+ // Else, the current value is more than the passed value
214
+ return 1;
215
+ }
216
+
217
+ /**
218
+ * Greater Than
219
+ *
220
+ * Returns true only if `a` is a larger width than `b`
221
+ *
222
+ * @name greaterThan
223
+ * @access public
224
+ * @param a The size to compare as greater than `b`
225
+ * @param b The size to compare against `a`
226
+ * @returns boolean
227
+ */
228
+ export function greaterThan(a: SIZE, b: SIZE): boolean {
229
+
230
+ // If the current value is greater than the passed value
231
+ return SIZES[a] > SIZES[b];
232
+ }
233
+
234
+ /**
235
+ * Less Than
236
+ *
237
+ * Returns true only if `a` is a smaller width than `b`
238
+ *
239
+ * @name greaterThan
240
+ * @access public
241
+ * @param a The size to compare as less than `b`
242
+ * @param b The size to compare against `a`
243
+ * @returns boolean
244
+ */
245
+ export function lessThan(a: SIZE, b: SIZE): boolean {
246
+
247
+ // If the `a` minimum value is less than the `b` minimum value
248
+ return SIZES[a] < SIZES[b];
249
+ }
package/size.d.ts DELETED
@@ -1,72 +0,0 @@
1
- /**
2
- * Size
3
- *
4
- * JS Library to manage the current client width of the screen in specific sizes
5
- *
6
- * @author Chris Nasr <chris@ouroboroscoding.com>
7
- * @copyright Ouroboros Coding Inc.
8
- * @created 2024-09-19
9
- */
10
- export declare const xs = "xs";
11
- export declare const sm = "sm";
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;
16
- export declare const SIZES: Record<SIZE, number>;
17
- /**
18
- * Compare
19
- *
20
- * Compares two widths and returns the standard -1,0,1 values as a result. -1
21
- * if `a` is less than `b, 0 for equality, and 1 if `a` is greater than `b`.
22
- *
23
- * @name compare
24
- * @access public
25
- * @param against The size to compare the current size against
26
- * @returns -1 || 0 || 1
27
- */
28
- export declare function compare(against: SIZE): 0 | 1 | -1;
29
- /**
30
- * Greater Than
31
- *
32
- * Returns true only if `a` is a larger width than `b`
33
- *
34
- * @name greaterThan
35
- * @access public
36
- * @param against The size to compare the current size against
37
- * @returns boolean
38
- */
39
- export declare function greaterThan(against: SIZE): boolean;
40
- /**
41
- * Less Than
42
- *
43
- * Returns true only if `a` is a smaller width than `b`
44
- *
45
- * @name greaterThan
46
- * @access public
47
- * @param against The size to compare the current size against
48
- * @returns boolean
49
- */
50
- export declare function lessThan(against: SIZE): boolean;
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;
package/size.js DELETED
@@ -1,132 +0,0 @@
1
- /**
2
- * Size
3
- *
4
- * JS Library to manage the current client width of the screen in specific sizes
5
- *
6
- * @author Chris Nasr <chris@ouroboroscoding.com>
7
- * @copyright Ouroboros Coding Inc.
8
- * @created 2024-09-19
9
- */
10
- // Constants
11
- export const xs = 'xs';
12
- export const sm = 'sm';
13
- export const md = 'md';
14
- export const lg = 'lg';
15
- export const xl = 'xl';
16
- // The minimum pixels necessary for each size type
17
- export const SIZES = {
18
- [xs]: 0, [sm]: 600, [md]: 900,
19
- [lg]: 1200, [xl]: 1536
20
- };
21
- // The current size
22
- let _current = null;
23
- /**
24
- * Init
25
- *
26
- * Called internally to setup the module
27
- *
28
- * @name _init
29
- * @access private
30
- */
31
- function _init() {
32
- // Const private function to calculate size from client width
33
- const _calculate = () => {
34
- if (document.documentElement.clientWidth < 600) {
35
- return xs;
36
- }
37
- if (document.documentElement.clientWidth < 900) {
38
- return sm;
39
- }
40
- if (document.documentElement.clientWidth < 1200) {
41
- return md;
42
- }
43
- if (document.documentElement.clientWidth < 1536) {
44
- return lg;
45
- }
46
- return xl;
47
- };
48
- // If we don't have the size
49
- if (_current === null) {
50
- _current = _calculate();
51
- window.addEventListener('resize', () => {
52
- _current = _calculate();
53
- });
54
- }
55
- }
56
- /**
57
- * Compare
58
- *
59
- * Compares two widths and returns the standard -1,0,1 values as a result. -1
60
- * if `a` is less than `b, 0 for equality, and 1 if `a` is greater than `b`.
61
- *
62
- * @name compare
63
- * @access public
64
- * @param against The size to compare the current size against
65
- * @returns -1 || 0 || 1
66
- */
67
- export function compare(against) {
68
- // Init if necessary
69
- if (_current === null)
70
- _init();
71
- // If the current size is the same as the passed value
72
- if (_current === against)
73
- return 0;
74
- // Else, if the current value is less than the passed value
75
- if (SIZES[_current] < SIZES[against])
76
- return -1;
77
- // Else, the current value is more than the passed value
78
- return 1;
79
- }
80
- /**
81
- * Greater Than
82
- *
83
- * Returns true only if `a` is a larger width than `b`
84
- *
85
- * @name greaterThan
86
- * @access public
87
- * @param against The size to compare the current size against
88
- * @returns boolean
89
- */
90
- export function greaterThan(against) {
91
- // Init if necessary
92
- if (_current === null)
93
- _init();
94
- // If the current value is greater than the passed value
95
- return SIZES[_current] > SIZES[against];
96
- }
97
- /**
98
- * Less Than
99
- *
100
- * Returns true only if `a` is a smaller width than `b`
101
- *
102
- * @name greaterThan
103
- * @access public
104
- * @param against The size to compare the current size against
105
- * @returns boolean
106
- */
107
- export function lessThan(against) {
108
- // Init if necessary
109
- if (_current === null)
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;
129
- }
130
- // Default export
131
- const width = { xs, sm, md, lg, xl, compare, greaterThan, lessThan, get };
132
- export default width;
package/size.ts DELETED
@@ -1,142 +0,0 @@
1
- /**
2
- * Size
3
- *
4
- * JS Library to manage the current client width of the screen in specific sizes
5
- *
6
- * @author Chris Nasr <chris@ouroboroscoding.com>
7
- * @copyright Ouroboros Coding Inc.
8
- * @created 2024-09-19
9
- */
10
-
11
- // Constants
12
- export const xs = 'xs';
13
- export const sm = 'sm';
14
- export const md = 'md';
15
- export const lg = 'lg';
16
- export const xl = 'xl';
17
-
18
- // Types
19
- export type SIZE = typeof xs | typeof sm | typeof md | typeof lg | typeof xl;
20
-
21
- // The minimum pixels necessary for each size type
22
- export const SIZES: Record<SIZE, number> = {
23
- [xs]: 0, [sm]: 600, [md]: 900,
24
- [lg]: 1200, [xl]: 1536
25
- }
26
-
27
- // The current size
28
- let _current: SIZE | null = null;
29
-
30
- /**
31
- * Init
32
- *
33
- * Called internally to setup the module
34
- *
35
- * @name _init
36
- * @access private
37
- */
38
- function _init(): void {
39
-
40
- // Const private function to calculate size from client width
41
- const _calculate = () => {
42
- if(document.documentElement.clientWidth < 600) { return xs }
43
- if(document.documentElement.clientWidth < 900) { return sm }
44
- if(document.documentElement.clientWidth < 1200) { return md }
45
- if(document.documentElement.clientWidth < 1536) { return lg }
46
- return xl;
47
- }
48
-
49
- // If we don't have the size
50
- if(_current === null) {
51
- _current = _calculate();
52
- window.addEventListener('resize', () => {
53
- _current = _calculate();
54
- });
55
- }
56
- }
57
-
58
- /**
59
- * Compare
60
- *
61
- * Compares two widths and returns the standard -1,0,1 values as a result. -1
62
- * if `a` is less than `b, 0 for equality, and 1 if `a` is greater than `b`.
63
- *
64
- * @name compare
65
- * @access public
66
- * @param against The size to compare the current size against
67
- * @returns -1 || 0 || 1
68
- */
69
- export function compare(against: SIZE) {
70
-
71
- // Init if necessary
72
- if(_current === null) _init();
73
-
74
- // If the current size is the same as the passed value
75
- if(_current === against) return 0;
76
-
77
- // Else, if the current value is less than the passed value
78
- if(SIZES[_current as SIZE] < SIZES[against]) return -1;
79
-
80
- // Else, the current value is more than the passed value
81
- return 1;
82
- }
83
-
84
- /**
85
- * Greater Than
86
- *
87
- * Returns true only if `a` is a larger width than `b`
88
- *
89
- * @name greaterThan
90
- * @access public
91
- * @param against The size to compare the current size against
92
- * @returns boolean
93
- */
94
- export function greaterThan(against: SIZE): boolean {
95
-
96
- // Init if necessary
97
- if(_current === null) _init();
98
-
99
- // If the current value is greater than the passed value
100
- return SIZES[_current as SIZE] > SIZES[against];
101
- }
102
-
103
- /**
104
- * Less Than
105
- *
106
- * Returns true only if `a` is a smaller width than `b`
107
- *
108
- * @name greaterThan
109
- * @access public
110
- * @param against The size to compare the current size against
111
- * @returns boolean
112
- */
113
- export function lessThan(against: SIZE): boolean {
114
-
115
- // Init if necessary
116
- if(_current === null) _init();
117
-
118
- // If the current value is less than the passed value
119
- return SIZES[_current as SIZE] < SIZES[against];
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;
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes