react-responsive-tools 1.1.4 → 2.0.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 CHANGED
@@ -1,10 +1,8 @@
1
1
  # react-responsive-tools
2
2
 
3
- This project provides tools and configurations to simplify writing responsive code for both vertical and horizontal layouts.
3
+ This project provides tools and configurations to simplify writing adaptive code for both vertical and horizontal layouts.
4
4
 
5
- The package is based on the npm package [react-responsive](https://www.npmjs.com/package/react-responsive) for use in React.js. It assumes the use of scss for compilation.
6
-
7
- In the future, the dependency on scss will be removed.
5
+ The package is based on [react-responsive](https://www.npmjs.com/package/react-responsive) for use in React. It assumes the use of SCSS for compilation.
8
6
 
9
7
  ## Installation
10
8
 
@@ -18,249 +16,609 @@ yarn add react-responsive-tools
18
16
  npm install react-responsive-tools
19
17
  ```
20
18
 
21
- To initialize the tools, you need to import the file from the library:
19
+ ### Import SCSS
20
+
21
+ To use the generated SCSS files, import the main SCSS file from the library:
22
22
 
23
23
  ```scss
24
- @import "react-responsive-tools/default"; // init breakpoints
24
+ @import 'react-responsive-tools';
25
25
  ```
26
26
 
27
- This line imports the necessary CSS variables (breakpoints) required for the tools to work.
27
+ ### Import JavaScript
28
28
 
29
- The package also includes a file to reset browser styles:
29
+ To use the components and hooks, import them from the library:
30
30
 
31
- ```scss
32
- @import "react-responsive-tools/reset"; // reset browsers styles
31
+ ```javascript
32
+ import { For, Before, useBreakpoint, useBreakpointDF, useBreakpointMF, useVBreakpoint, useVBreakpointDF, useVBreakpointMF } from 'react-responsive-tools';
33
33
  ```
34
34
 
35
- ### Default Horizontal and Vertical Breakpoints:
36
-
37
- --vbp - vertical breakpoints
35
+ ## Initialization and Configuration
38
36
 
39
- --bp - horizontal breakpoints
37
+ To initialize the tools, use the following commands:
40
38
 
41
- ```scss
42
- :root {
43
- --bp-xs: 320px;
44
- --bp-sm: 576px;
45
- --bp-md: 768px;
46
- --bp-lg: 992px;
47
- --bp-lt: 1024px;
48
- --bp-ltm: 1120px;
49
- --bp-ltl: 1440px;
50
- --bp-xl: 1920px;
51
- --bp-xxl: 2560px;
52
- --bp-qxl: 3840px;
53
-
54
- --vbp-xs: 600px;
55
- --vbp-sm: 800px;
56
- --vbp-md: 1000px;
57
- --vbp-lg: 1200px;
58
- --vbp-xl: 1600px;
59
- --vbp-xxl: 1920px;
60
- }
61
- ```
39
+ ### Re-initializing Configuration
62
40
 
63
- Breakpoints override example:
41
+ If you have changed the configuration or added new settings, you need to reinitialize the configuration files. To do this, run the following command:
64
42
 
65
- ```scss
66
- @import "react-responsive-tools/default"; // init breakpoints
67
-
68
- :root {
69
- //--bp-xxl: 2560px;
70
- --bp-xxl: 2222px; // new point
71
- }
43
+ ```sh
44
+ react-responsive-tools run reinit
72
45
  ```
73
46
 
74
- ## Using Mixins
47
+ This command will run the `reinit.sh` script, which will recreate the necessary configuration files.
75
48
 
76
- Mixins are packaged with an `include` decorator that provides descriptive instructions for breakpoints.
49
+ ### Default Values
77
50
 
78
- Example:
51
+ The following horizontal and vertical breakpoints are provided by default:
79
52
 
80
- ```scss
81
- @import "react-responsive-tools";
53
+ #### Horizontal Breakpoints
82
54
 
83
- div.supercool {
84
- @include m-xs { // for mobile to first xs breakpoint
85
- color: red;
86
- font-size: 10px;
87
- @include vm-sm {
88
- font-size: 11px;
89
- }
90
- @include vm-md {
91
- font-size: 14px;
92
- }
93
- }
94
- }
55
+ ```typescript
56
+ export const HORIZONTAL_BREAKPOINTS = {
57
+ xs: '320px',
58
+ sm: '576px',
59
+ md: '768px',
60
+ lg: '992px',
61
+ lt: '1024px',
62
+ ltm: '1200px',
63
+ ltl: '1440px',
64
+ xl: '1920px',
65
+ xxl: '2560px',
66
+ qxl: '3840px',
67
+ };
68
+ ```
95
69
 
96
- span.someclass {
97
- @include d-xxl { // for desktop to first xxl breakpoint
98
- color: white;
99
- // vertical points in horizontal
100
- @include vd-xs {
101
- font-size: 10px;
102
- }
103
- @include vd-md {
104
- font-size: 12px;
105
- }
106
- }
107
- }
70
+ #### Vertical Breakpoints
108
71
 
109
- aside.sidebar {
110
- // custom breakpoint
111
- @include mob-first(xs) {
112
- ...
113
- }
114
- // desktop to first
115
- @include desk-first(md) {
116
- ...
117
- }
72
+ ```typescript
73
+ export const VERTICAL_BREAKPOINTS = {
74
+ xs: '600px',
75
+ sm: '800px',
76
+ md: '1000px',
77
+ lg: '1200px',
78
+ xl: '1600px',
79
+ xxl: '1920px',
80
+ };
81
+ ```
118
82
 
119
- // vertical custom bp
120
- @include v-desk-first(md) {
83
+ ### Using Hooks
121
84
 
122
- }
123
- // mob first
124
- @include v-mob-first(md) {
85
+ #### Hook `useBreakpoint`
125
86
 
126
- }
127
- }
128
- ```
87
+ ```typescript
88
+ import { useBreakpoint } from 'react-responsive-tools';
129
89
 
130
- ## Hooks
90
+ // Usage Example
91
+ const MyComponent = () => {
92
+ const isMedium = useBreakpoint('md');
131
93
 
132
- ### Hook Implementations
94
+ return (
95
+ <div>
96
+ {isMedium && <p>Current breakpoint: Medium (md)</p>}
97
+ </div>
98
+ );
99
+ };
100
+ ```
133
101
 
134
- #### Hook `useBreakpoint`:
102
+ #### Hook `useBreakpointMF`
135
103
 
136
104
  ```typescript
137
- import { useMediaQuery } from 'react-responsive';
138
- import breakpoints from '../../scss/_horizontal.export.scss';
139
- import { TBreakpoint } from '../../interfaces/TBreakpoint';
140
- import { TAdaptiveVariant } from '../../interfaces/TAdaptiveVariant';
141
- import useVariant from '../useVariant';
105
+ import { useBreakpointMF } from 'react-responsive-tools';
142
106
 
143
- export default function useBreakpoint(b: TBreakpoint, variant: TAdaptiveVariant = 'MtF') {
144
- const p = breakpoints[b];
145
- const v = useVariant(variant);
146
- return useMediaQuery({ query: `(${v}-width: ${p}px)` });
147
- }
148
-
149
- export function useBreakpointMF(b: TBreakpoint) {
150
- return useBreakpoint(b, 'MtF');
151
- }
107
+ // Usage Example
108
+ const MyComponent = () => {
109
+ const isMedium = useBreakpointMF('md');
152
110
 
153
- export function useBreakpointDF(b: TBreakpoint) {
154
- return useBreakpoint(b, 'DtF');
155
- }
111
+ return (
112
+ <div>
113
+ {isMedium && <p>Current breakpoint: Medium (md) for Mobile First</p>}
114
+ </div>
115
+ );
116
+ };
156
117
  ```
157
118
 
158
- ### Usage Examples
159
-
160
- #### Hook `useBreakpoint`:
119
+ #### Hook `useBreakpointDF`
161
120
 
162
121
  ```typescript
163
- import useBreakpoint from './hooks/useBreakpoint';
122
+ import { useBreakpointDF } from 'react-responsive-tools';
164
123
 
124
+ // Usage Example
165
125
  const MyComponent = () => {
166
- const isMedium = useBreakpoint('md');
126
+ const isLarge = useBreakpointDF('lg');
167
127
 
168
128
  return (
169
129
  <div>
170
- {isMedium && <p>Current breakpoint: Medium (md)</p>}
130
+ {isLarge && <p>Current breakpoint: Large (lg) for Desktop First</p>}
171
131
  </div>
172
132
  );
173
133
  };
174
134
  ```
175
135
 
176
- #### Hook `useBreakpointMF`:
136
+ #### Hook `useVBreakpoint`
177
137
 
178
138
  ```typescript
179
- import { useBreakpointMF } from './hooks/useBreakpoint';
139
+ import { useVBreakpoint } from 'react-responsive-tools';
180
140
 
141
+ // Usage Example
181
142
  const MyComponent = () => {
182
- const isMedium = useBreakpointMF('md');
143
+ const isMedium = useVBreakpoint('md');
183
144
 
184
145
  return (
185
146
  <div>
186
- {isMedium && <p>Current breakpoint: Medium (md) for Mobile First</p>}
147
+ {isMedium && <p>Current vertical breakpoint: Medium (md)</p>}
187
148
  </div>
188
149
  );
189
150
  };
190
151
  ```
191
152
 
192
- #### Hook `useBreakpointDF`:
153
+ #### Hook `useVBreakpointMF`
193
154
 
194
155
  ```typescript
195
- import { useBreakpointDF } from './hooks/useBreakpoint';
156
+ import { useVBreakpointMF } from 'react-responsive-tools';
196
157
 
158
+ // Usage Example
197
159
  const MyComponent = () => {
198
- const isLarge = useBreakpointDF('lg');
160
+ const isMedium = useVBreakpointMF('md');
199
161
 
200
162
  return (
201
163
  <div>
202
- {isLarge && <p>Current breakpoint: Large (lg) for Desktop First</p>}
164
+ {isMedium && <p>Current vertical breakpoint: Medium (md) for Mobile First</p>}
203
165
  </div>
204
166
  );
205
167
  };
206
168
  ```
207
169
 
208
- ## Components
170
+ #### Hook `useVBreakpointDF`
209
171
 
210
- The project includes utility components to display content only at certain breakpoints.
172
+ ```typescript
173
+ import { useVBreakpointDF } from 'react-responsive-tools';
211
174
 
212
- ### Usage Examples
175
+ // Usage Example
176
+ const MyComponent = () => {
177
+ const isLarge = useVBreakpointDF('lg');
213
178
 
214
- #### Component `ForMF`:
179
+ return (
180
+ <div>
181
+ {isLarge && <p>Current vertical breakpoint: Large (lg) for Desktop First</p>}
182
+ </div>
183
+ );
184
+ };
185
+ ```
186
+
187
+ ### Using Components
188
+
189
+ #### Component `For`
215
190
 
216
191
  ```typescript
217
- import { ForMF } from './components/ForMF';
192
+ import { For } from 'react-responsive-tools';
218
193
 
194
+ // Usage Example
219
195
  const MyComponent = () => (
220
- <ForMF bp='md'>
196
+ <For p='md'>
221
197
  <p>This content is visible only at the Medium (md) breakpoint</p>
222
- </ForMF>
198
+ </For>
223
199
  );
224
200
 
225
201
  const CustomSizeComponent = () => (
226
- <ForMF bp={850}>
202
+ <For p={850}>
227
203
  <p>This content is visible only at the Custom (850px) breakpoint</p>
228
- </ForMF>
204
+ </For>
229
205
  );
230
206
  ```
231
207
 
232
- #### Component `ForDF`:
208
+ #### Component `Before`
233
209
 
234
210
  ```typescript
235
- import { ForDF } from './components/ForDF';
211
+ import { Before } from 'react-responsive-tools';
236
212
 
213
+ // Usage Example
237
214
  const MyComponent = () => (
238
- <ForDF bp='lg'>
215
+ <Before p='lg'>
239
216
  <p>This content is visible only at the Large (lg) breakpoint</p>
240
- </ForDF>
217
+ </Before>
241
218
  );
242
219
 
243
220
  const CustomSizeComponent = () => (
244
- <ForDF bp={1200}>
221
+ <Before p={1200}>
245
222
  <p>This content is visible only at the Custom (1200px) breakpoint</p>
246
- </ForDF>
223
+ </Before>
247
224
  );
248
225
  ```
249
226
 
250
- ## Project Structure
227
+ ### Breakpoint Types
228
+
229
+ ```typescript
230
+ export type TBreakpoint =
231
+ | 'xs'
232
+ | 'sm'
233
+ | 'md'
234
+ | 'lg'
235
+ | 'lt'
236
+ | 'ltm'
237
+ | 'ltl'
238
+ | 'xl'
239
+ | 'xxl'
240
+ | 'qxl'
241
+
242
+ export type TVerticalBreakpoint =
243
+ | 'xs'
244
+ | 'sm'
245
+ | 'md'
246
+ | 'lg'
247
+ | 'xl'
248
+ | 'xxl'
249
+ ```
250
+
251
+ ### Project Structure
252
+
253
+ The project includes the following directories and files:
254
+
255
+ - `scss`: directory for SCSS files.
256
+ - `_custom-breakpoints.scss`: contains custom breakpoints.
257
+ - `_horizontal.scss`: provides mixins for horizontal layouts.
258
+ - `_horizontal-breakpoints.scss`: defines horizontal breakpoints.
259
+ - `_vertical.scss`: provides mixins for vertical layouts.
260
+ - `_vertical-breakpoints.scss`: defines vertical breakpoints.
261
+ - `index.scss`: main SCSS file.
262
+
263
+ - `hooks`: directory for hooks.
264
+ - `useBreakpoint.ts`: implements the `useBreakpoint` hook.
265
+ - `useVBreakpoint.ts`: implements the `useVBreakpoint` hook.
266
+ - `useVariant.ts`: utility hook to determine the variant.
267
+
268
+ - `components`: directory for components.
269
+ - `horizontal.tsx`: utility components for horizontal layouts.
270
+ - `For.tsx`: utility component for displaying content at a specific breakpoint.
271
+ - `Before.tsx`: utility component similar to `For`, but for different breakpoints.
272
+
273
+ - `scripts`: directory for scripts.
274
+ - `createConfig.js`: script to create configuration files.
275
+ - `generateCustomBreakpointsSCSS.js`: script to generate custom SCSS breakpoints.
276
+ - `generateSCSS.js`: script to generate SCSS files.
277
+ - `generateTBreakpoint.js`: script to generate TypeScript breakpoint types.
278
+
279
+ - `interfaces`: directory for TypeScript interfaces.
280
+ - `TAdaptiveVariant.ts`: interface for adaptive variant types.
281
+ - `TBreakpoint.ts`: interface for breakpoint types.
282
+ - `TBreakpoints.ts`: interface for breakpoints.
283
+
284
+ ## Command Line Interface (CLI)
285
+
286
+ To recreate the necessary files, run the provided script using the following command:
287
+
288
+ ```sh
289
+ react-responsive-tools run reinit
290
+ ```
291
+
292
+ This command will run the `reinit.sh` script, which will create the configuration files for the project.
293
+
294
+ ### Examples of SCSS Mixins after Re-initialization
295
+
296
+ After re-generating the files, the following SCSS mixins will be available:
297
+
298
+ #### Horizontal Breakpoints
299
+
300
+ ```scss
301
+ @import "_custom-breakpoints";
302
+
303
+ @mixin mob-first($breakpoint) {
304
+ @media (min-width: map-get($horizontal-breakpoints, $breakpoint)) {
305
+ @content;
306
+ }
307
+ }
308
+
309
+ @mixin desk-first($breakpoint) {
310
+ @media (max-width: map-get($horizontal-breakpoints, $breakpoint)) {
311
+ @content;
312
+ }
313
+ }
314
+
315
+ // Example mixins
316
+ @mixin for-xs() {
317
+ @include mob-first(xs) {
318
+ @content;
319
+ }
320
+ }
321
+
322
+ @mixin for-sm() {
323
+ @include mob-first(sm) {
324
+ @content;
325
+ }
326
+ }
327
+
328
+ @mixin for-md() {
329
+ @include mob-first(md) {
330
+ @content;
331
+ }
332
+ }
333
+
334
+ @mixin for-lg() {
335
+ @include mob-first(lg) {
336
+ @content;
337
+ }
338
+ }
339
+
340
+ @mixin for-lt() {
341
+ @include mob-first(lt) {
342
+ @content;
343
+ }
344
+ }
345
+
346
+ @mixin for-ltm() {
347
+ @include mob-first(ltm) {
348
+ @content;
349
+ }
350
+ }
351
+
352
+ @mixin for-ltl() {
353
+ @include mob-first(ltl) {
354
+ @content;
355
+ }
356
+ }
357
+
358
+ @mixin for-xl() {
359
+ @include mob-first(xl) {
360
+ @content;
361
+ }
362
+ }
363
+
364
+ @mixin for-xxl() {
365
+ @include mob-first(xxl) {
366
+ @content;
367
+ }
368
+ }
369
+
370
+ @mixin for-qxl() {
371
+ @include mob-first(qxl) {
372
+ @content;
373
+ }
374
+ }
375
+
376
+ @mixin before-xs() {
377
+ @include desk-first(xs) {
378
+ @content;
379
+ }
380
+ }
381
+
382
+ @mixin before-sm() {
383
+ @include desk-first(sm) {
384
+ @content;
385
+ }
386
+ }
387
+
388
+ @mixin before-md() {
389
+ @include desk-first(md) {
390
+ @content;
391
+ }
392
+ }
393
+
394
+ @mixin before-lg() {
395
+ @include desk-first(lg) {
396
+ @content;
397
+ }
398
+ }
399
+
400
+ @mixin before-lt() {
401
+ @include desk-first(lt) {
402
+ @content;
403
+ }
404
+ }
405
+
406
+ @mixin before-ltm() {
407
+ @include desk-first(ltm) {
408
+ @content;
409
+ }
410
+ }
411
+
412
+ @mixin before-ltl() {
413
+ @include desk-first(ltl) {
414
+ @content;
415
+ }
416
+ }
417
+
418
+ @mixin before-xl() {
419
+ @include desk-first(xl) {
420
+ @content;
421
+ }
422
+ }
423
+
424
+ @mixin before-xxl() {
425
+ @include desk-first(xxl) {
426
+ @content;
427
+ }
428
+ }
429
+
430
+ @mixin before-qxl() {
431
+ @include desk-first(qxl) {
432
+ @content;
433
+ }
434
+ }
435
+ ```
436
+
437
+ #### Vertical Breakpoints
438
+
439
+ ```scss
440
+ @import "_custom-breakpoints";
441
+
442
+ @mixin v-mob-first($breakpoint) {
443
+ @media (min-height: map-get($vertical-breakpoints, $breakpoint)) {
444
+ @content;
445
+ }
446
+ }
447
+
448
+ @mixin v-desk-first($breakpoint) {
449
+ @media (max-height: map-get($vertical-breakpoints, $breakpoint)) {
450
+ @content;
451
+ }
452
+ }
453
+
454
+ // Example mixins
455
+ @mixin v-for-xs() {
456
+ @include v-mob-first(xs) {
457
+ @content;
458
+ }
459
+ }
460
+
461
+ @mixin v-for-sm() {
462
+ @include v-mob-first(sm) {
463
+ @content;
464
+ }
465
+ }
466
+
467
+ @mixin v-for-md() {
468
+ @include v-mob-first(md) {
469
+ @content;
470
+ }
471
+ }
472
+
473
+ @mixin v-for-lg() {
474
+ @include v-mob-first(lg) {
475
+ @content;
476
+ }
477
+ }
478
+
479
+ @mixin v-for-xl() {
480
+ @include v-mob-first(xl) {
481
+ @content;
482
+ }
483
+ }
484
+
485
+ @mixin v-for-xxl() {
486
+ @include v-mob-first(xxl) {
487
+ @content;
488
+ }
489
+ }
490
+
491
+ @mixin v-before-xs() {
492
+ @include v-desk-first(xs) {
493
+ @content;
494
+ }
495
+ }
496
+
497
+ @mixin v-before-sm() {
498
+ @include v-desk-first(sm) {
499
+ @content;
500
+ }
501
+ }
502
+
503
+ @mixin v-before-md() {
504
+ @include v-desk-first(md) {
505
+ @content;
506
+ }
507
+ }
508
+
509
+ @mixin v-before-lg() {
510
+ @include v-desk-first(lg) {
511
+ @content;
512
+ }
513
+ }
514
+
515
+ @mixin v-before-xl() {
516
+ @include v-desk-first(xl) {
517
+ @content;
518
+ }
519
+ }
520
+
521
+ @mixin v-before-xxl() {
522
+ @include v-desk-first(xxl) {
523
+ @content;
524
+ }
525
+ }
526
+ ```
527
+
528
+ ### Practical Example of Using These Mixins
529
+
530
+ Here is an example of how to use these mixins in your SCSS code:
531
+
532
+ #### Horizontal Layout
533
+
534
+ ```scss
535
+ .container {
536
+ // Base styles
537
+ width: 100%;
538
+ padding: 20px;
539
+
540
+ // Styles for `sm` breakpoint
541
+ @include for-sm {
542
+ width: 90%;
543
+ padding: 15px;
544
+ }
545
+
546
+ // Styles for `md` breakpoint
547
+ @include for-md {
548
+ width: 80%;
549
+ padding: 10px;
550
+ }
551
+
552
+ // Styles for `lg` breakpoint
553
+ @include for-lg {
554
+ width: 70%;
555
+ padding: 5px;
556
+ }
557
+ }
558
+
559
+ .header {
560
+ // Base styles
561
+ font-size: 16px;
562
+ background-color: white;
563
+
564
+ // Styles for `sm` breakpoint
565
+ @include before-sm {
566
+ font-size: 18px;
567
+ background-color: lightgray;
568
+ }
569
+
570
+ // Styles for `md` breakpoint
571
+ @include before-md {
572
+ font-size: 20px;
573
+ background-color: gray;
574
+ }
575
+ }
576
+ ```
577
+
578
+ #### Vertical Layout
251
579
 
252
- The project includes the `scss` directory with the following files:
580
+ ```scss
581
+ .panel {
582
+ // Base styles
583
+ height: 100%;
584
+ padding: 20px;
585
+
586
+ // Styles for `sm` vertical breakpoint
587
+ @include v-for-sm {
588
+ height: 90%;
589
+ padding: 15px;
590
+ }
253
591
 
254
- - `_data.scss`: Contains breakpoints.
255
- - `_horizontal.scss` and `_vertical.scss`: Provide mixins for horizontal and vertical layouts, respectively.
256
- - `_screen.scss`: Intermediate file for export to JS.
592
+ // Styles for `md` vertical breakpoint
593
+ @include v-for-md {
594
+ height: 80%;
595
+ padding: 10px;
596
+ }
257
597
 
258
- The `hooks` directory includes:
598
+ // Styles for `lg` vertical breakpoint
599
+ @include v-for-lg {
600
+ height: 70%;
601
+ padding: 5px;
602
+ }
603
+ }
259
604
 
260
- - `useBreakpoint.ts`: Implements the `useBreakpoint` hook to determine the current breakpoint.
261
- - `useVBreakpoint.ts`: Implements the `useVBreakpoint` hook to determine the current vertical breakpoint.
605
+ .footer {
606
+ // Base styles
607
+ font-size: 16px;
608
+ background-color: white;
609
+
610
+ // Styles for `sm` vertical breakpoint
611
+ @include v-before-sm {
612
+ font-size: 18px;
613
+ background-color: lightgray;
614
+ }
262
615
 
263
- The `components` directory includes:
616
+ // Styles for `md` vertical breakpoint
617
+ @include v-before-md {
618
+ font-size: 20px;
619
+ background-color: gray;
620
+ }
621
+ }
622
+ ```
264
623
 
265
- - `ForMF.ts`: A utility component that takes a breakpoint size and children as props. It uses the `useBreakpointMF` hook to determine if the screen size matches the given breakpoint. If it does, it renders the children; otherwise, it renders `null`.
266
- - `ForDF.ts`: Similar to `ForMF`, but uses the `useBreakpointDF` hook.
624
+ Now, the documentation is complete with examples of using the `for` and `before` mixins for both horizontal and vertical breakpoints, as well as the native mobile-first and desktop-first mixins.