Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Commit 3346577

Browse files
joaopedrodcfSoaresMG
joaopedrodcf
authored andcommitted
feat: add mobile breakpoint to contex
1 parent 9cda1b1 commit 3346577

File tree

8 files changed

+79
-49
lines changed

8 files changed

+79
-49
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ When possible, use the `withIsMobile` and `useIsMobile` for mobile devices detec
4141
| children | node | yes | - | React component |
4242
| breakpoints | { xs: string, sm: string, md: string, lg: string, xl: string } | no | - | Min breakpoints |
4343
| breakpointsMax | { xs: string, sm: string, md: string, lg: string, xl: string } | no | - | Max breakpoints |
44-
| mediaQueries | { _initial: string, xs: string, sm: string, md: string, lg: string, xl: string } | no | - | Represents the screen media queries `(If this is passed breakpoints and breakpointsMax is obsolete)` |
44+
| mediaQueries | { _initial: string, xs: string, sm: string, md: string, lg: string, xl: string } | no | - | Represents the screen media queries `(If this is passed, breakpoints and breakpointsMax props are obsolete)` |
45+
| mobileBreakpoint | '_initial' <br>&#124;&nbsp; 'xs' <br>&#124;&nbsp; 'sm' <br>&#124;&nbsp; 'md' <br>&#124;&nbsp; 'lg' <br>&#124;&nbsp; 'xl' | no | - | It's considered mobile until this breakpoint |
4546

4647
## Object returned by the useResponsive / withResponsive / Responsive:
4748

@@ -58,7 +59,7 @@ When possible, use the `withIsMobile` and `useIsMobile` for mobile devices detec
5859

5960
| Key | Type | Description |
6061
|------------------------|---------|----------------------------------------------------------------------------------------------|
61-
| isMobile | boolean | If its below the md breakpoint |
62+
| isMobile | boolean | If it's below the mobile breakpoint defined by mobileBreakpoint |
6263
| isCalculated | boolean | False on first render. Once true, it means all breakpoints values are based on the window. |
6364

6465
## Usage and examples
@@ -73,8 +74,7 @@ The component has five different exported consumption APIs:
7374
- `withResponsive`: A HoC which passes the responsive data to the `responsive` prop
7475
- `withIsMobile`: A HoC which passes `isMobile` and `isCalculated` props only
7576

76-
77-
### How setup the package
77+
### How to setup
7878

7979
There are two possible options to configure your responsive provider with `breakpoints` or with `mediaQueries`
8080

src/ResponsiveContext.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const ResponsiveContext = createContext({
77
greaterThan: {},
88
is: {},
99
orientation: null,
10+
mobileBreakpoint: 'md',
1011
});
1112

1213
export default ResponsiveContext;

src/ResponsiveProvider/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const ResponsiveProvider = ({
1313
breakpoints,
1414
breakpointsMax,
1515
mediaQueries,
16+
mobileBreakpoint,
1617
}) => {
1718
const breakpointsWithInitialValue = {
1819
_initial: '0em',
@@ -52,9 +53,16 @@ const ResponsiveProvider = ({
5253
mediaType: currentMediaType,
5354
orientation: currentOrientation,
5455
isCalculated,
56+
mobileBreakpoint,
5557
...queriesObjects,
5658
}),
57-
[currentMediaType, currentOrientation, isCalculated, queriesObjects]
59+
[
60+
currentMediaType,
61+
currentOrientation,
62+
isCalculated,
63+
mobileBreakpoint,
64+
queriesObjects,
65+
]
5866
);
5967

6068
useDebugResponsive(contextObject, currentMediaType);
@@ -77,6 +85,7 @@ const breakpointsPropTypes = {
7785
ResponsiveProvider.defaultProps = {
7886
initialMediaType: 'xs',
7987
defaultOrientation: null,
88+
mobileBreakpoint: 'md',
8089
};
8190

8291
ResponsiveProvider.propTypes = {
@@ -99,6 +108,7 @@ ResponsiveProvider.propTypes = {
99108
...breakpointsPropTypes,
100109
_initial: PropTypes.string,
101110
}),
111+
mobileBreakpoint: PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl']),
102112
};
103113

104114
export default ResponsiveProvider;

src/useIsMobile.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { useContext, useMemo } from 'react';
2-
import ResponsiveContext from './ResponsiveContext';
1+
import { useMemo } from 'react';
2+
import useResponsive from './useResponsive';
33

44
const useIsMobile = () => {
5-
const { isCalculated, lessThan } = useContext(ResponsiveContext);
6-
const isMobile = lessThan.md;
5+
const { isCalculated, lessThan, mobileBreakpoint } = useResponsive();
6+
const isMobile = lessThan[mobileBreakpoint];
77

88
return useMemo(() => ({ isMobile, isCalculated }), [
99
isMobile,

tests/__fixtures__/mockContextContent.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ export default {
2525
xs: false,
2626
},
2727
mediaType: 'md',
28+
mobileBreakpoint: 'md',
2829
orientation: 'landscape',
2930
};

tests/__snapshots__/ResponsiveProvider.test.js.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Object {
2828
"xs": true,
2929
},
3030
"mediaType": "_initial",
31+
"mobileBreakpoint": "md",
3132
"orientation": "portrait",
3233
}
3334
`;
@@ -60,6 +61,7 @@ Object {
6061
"xs": false,
6162
},
6263
"mediaType": "xs",
64+
"mobileBreakpoint": "md",
6365
"orientation": "landscape",
6466
}
6567
`;
@@ -92,6 +94,7 @@ Object {
9294
"xs": false,
9395
},
9496
"mediaType": "xs",
97+
"mobileBreakpoint": "md",
9598
"orientation": "portrait",
9699
}
97100
`;
@@ -124,6 +127,7 @@ Object {
124127
"xs": false,
125128
},
126129
"mediaType": "xs",
130+
"mobileBreakpoint": "md",
127131
"orientation": "portrait",
128132
}
129133
`;
@@ -156,6 +160,7 @@ Object {
156160
"xs": false,
157161
},
158162
"mediaType": "sm",
163+
"mobileBreakpoint": "md",
159164
"orientation": "portrait",
160165
}
161166
`;
@@ -188,6 +193,7 @@ Object {
188193
"xs": false,
189194
},
190195
"mediaType": "sm",
196+
"mobileBreakpoint": "md",
191197
"orientation": "landscape",
192198
}
193199
`;
@@ -220,6 +226,7 @@ Object {
220226
"xs": false,
221227
},
222228
"mediaType": "md",
229+
"mobileBreakpoint": "md",
223230
"orientation": "landscape",
224231
}
225232
`;
@@ -252,6 +259,7 @@ Object {
252259
"xs": false,
253260
},
254261
"mediaType": "md",
262+
"mobileBreakpoint": "md",
255263
"orientation": "portrait",
256264
}
257265
`;
@@ -284,6 +292,7 @@ Object {
284292
"xs": false,
285293
},
286294
"mediaType": "lg",
295+
"mobileBreakpoint": "md",
287296
"orientation": "landscape",
288297
}
289298
`;
@@ -316,6 +325,7 @@ Object {
316325
"xs": false,
317326
},
318327
"mediaType": "lg",
328+
"mobileBreakpoint": "md",
319329
"orientation": "landscape",
320330
}
321331
`;
@@ -348,6 +358,7 @@ Object {
348358
"xs": false,
349359
},
350360
"mediaType": "xl",
361+
"mobileBreakpoint": "md",
351362
"orientation": "landscape",
352363
}
353364
`;
@@ -380,6 +391,7 @@ Object {
380391
"xs": false,
381392
},
382393
"mediaType": "md",
394+
"mobileBreakpoint": "md",
383395
"orientation": "landscape",
384396
}
385397
`;
@@ -412,6 +424,7 @@ Object {
412424
"xs": false,
413425
},
414426
"mediaType": "xs",
427+
"mobileBreakpoint": "md",
415428
"orientation": null,
416429
}
417430
`;
@@ -444,6 +457,7 @@ Object {
444457
"xs": false,
445458
},
446459
"mediaType": "xl",
460+
"mobileBreakpoint": "md",
447461
"orientation": "portrait",
448462
}
449463
`;

tests/useIsMobile.test.js

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ describe('useIsMobile()', () => {
2828
unmockMatchMedia();
2929
});
3030

31-
test('should return the values from the hook correctly, if is desktop', () => {
32-
window.resizeTo(1024, 768);
31+
test('should return not isMobile when window size md and mobile breakpoint md', () => {
32+
const windowSizeMd = 960;
33+
window.resizeTo(windowSizeMd, 768);
3334

3435
render(
35-
<ResponsiveProvider {...props}>
36+
<ResponsiveProvider {...props} mobileBreakpoint="md">
3637
<MockComponent>{mockRenderProp}</MockComponent>
3738
</ResponsiveProvider>
3839
);
@@ -44,11 +45,46 @@ describe('useIsMobile()', () => {
4445
});
4546
});
4647

47-
test('should return the values from the hook correctly, if is mobile', () => {
48-
window.resizeTo(375, 812);
48+
test('should return isMobile when window size sm and mobile breakpoint md', () => {
49+
const windowSizeSm = 576;
50+
window.resizeTo(windowSizeSm, 768);
4951

5052
render(
51-
<ResponsiveProvider {...props}>
53+
<ResponsiveProvider {...props} mobileBreakpoint="md">
54+
<MockComponent>{mockRenderProp}</MockComponent>
55+
</ResponsiveProvider>
56+
);
57+
58+
expect(mockRenderProp).toHaveBeenCalledTimes(2);
59+
expect(mockRenderProp).toHaveBeenLastCalledWith({
60+
isMobile: true,
61+
isCalculated: true,
62+
});
63+
});
64+
65+
test('should return not isMobile when window size sm and mobile breakpoint sm', () => {
66+
const windowSizeSm = 576;
67+
window.resizeTo(windowSizeSm, 768);
68+
69+
render(
70+
<ResponsiveProvider {...props} mobileBreakpoint="sm">
71+
<MockComponent>{mockRenderProp}</MockComponent>
72+
</ResponsiveProvider>
73+
);
74+
75+
expect(mockRenderProp).toHaveBeenCalledTimes(2);
76+
expect(mockRenderProp).toHaveBeenLastCalledWith({
77+
isMobile: false,
78+
isCalculated: true,
79+
});
80+
});
81+
82+
test('should return isMobile when window size sm and mobile breakpoint sm', () => {
83+
const windowSizeXs = 320;
84+
window.resizeTo(windowSizeXs, 768);
85+
86+
render(
87+
<ResponsiveProvider {...props} mobileBreakpoint="sm">
5288
<MockComponent>{mockRenderProp}</MockComponent>
5389
</ResponsiveProvider>
5490
);

yarn.lock

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3021,7 +3021,7 @@ debug@^3.1.0:
30213021
dependencies:
30223022
ms "^2.1.1"
30233023

3024-
debuglog@*, debuglog@^1.0.1:
3024+
debuglog@^1.0.1:
30253025
version "1.0.1"
30263026
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
30273027
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
@@ -4547,7 +4547,7 @@ import-local@^2.0.0:
45474547
pkg-dir "^3.0.0"
45484548
resolve-cwd "^2.0.0"
45494549

4550-
imurmurhash@*, imurmurhash@^0.1.4:
4550+
imurmurhash@^0.1.4:
45514551
version "0.1.4"
45524552
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
45534553
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
@@ -5977,11 +5977,6 @@ lockfile@^1.0.4:
59775977
dependencies:
59785978
signal-exit "^3.0.2"
59795979

5980-
lodash._baseindexof@*:
5981-
version "3.1.0"
5982-
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
5983-
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=
5984-
59855980
lodash._baseuniq@~4.6.0:
59865981
version "4.6.0"
59875982
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
@@ -5990,33 +5985,11 @@ lodash._baseuniq@~4.6.0:
59905985
lodash._createset "~4.0.0"
59915986
lodash._root "~3.0.0"
59925987

5993-
lodash._bindcallback@*:
5994-
version "3.0.1"
5995-
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
5996-
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=
5997-
5998-
lodash._cacheindexof@*:
5999-
version "3.0.2"
6000-
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
6001-
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=
6002-
6003-
lodash._createcache@*:
6004-
version "3.1.2"
6005-
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
6006-
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
6007-
dependencies:
6008-
lodash._getnative "^3.0.0"
6009-
60105988
lodash._createset@~4.0.0:
60115989
version "4.0.3"
60125990
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
60135991
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=
60145992

6015-
lodash._getnative@*, lodash._getnative@^3.0.0:
6016-
version "3.9.1"
6017-
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
6018-
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=
6019-
60205993
lodash._reinterpolate@^3.0.0:
60215994
version "3.0.0"
60225995
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
@@ -6057,11 +6030,6 @@ lodash.isstring@^4.0.1:
60576030
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
60586031
integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=
60596032

6060-
lodash.restparam@*:
6061-
version "3.6.1"
6062-
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
6063-
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=
6064-
60656033
lodash.sortby@^4.7.0:
60666034
version "4.7.0"
60676035
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"

0 commit comments

Comments
 (0)