Skip to content

Commit da24b7d

Browse files
authored
feat(Header, ContentLayout): calculate isMobile by screen size (#1228)
* fix(Header, ContentLayout): calculate isMobile by screen size * fix: requested changes
1 parent 5f16f66 commit da24b7d

File tree

5 files changed

+57
-15
lines changed

5 files changed

+57
-15
lines changed

src/blocks/ContentLayout/ContentLayout.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import * as React from 'react';
22

33
import {BackgroundImage, FileLink} from '../../components';
4-
import {MobileContext} from '../../context/mobileContext';
54
import {useTheme} from '../../context/theme';
65
import {Col} from '../../grid';
76
import {ContentLayoutBlockProps, ContentSize, ContentTextSize} from '../../models';
87
import {Content} from '../../sub-blocks';
98
import {block, getThemedValue} from '../../utils';
109

1110
import './ContentLayout.scss';
11+
import {useWindowWidth} from '../../context/windowWidthContext';
12+
import {BREAKPOINTS} from '../../constants';
1213

1314
const b = block('content-layout-block');
1415

@@ -35,7 +36,9 @@ function getTextWidth(size: ContentTextSize) {
3536
}
3637

3738
export const ContentLayoutBlock = (props: ContentLayoutBlockProps) => {
38-
const isMobile = React.useContext(MobileContext);
39+
const windowWidth = useWindowWidth();
40+
const isMobile = windowWidth <= BREAKPOINTS.sm;
41+
3942
const {
4043
textContent,
4144
fileContent,

src/blocks/Header/Header.tsx

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {Button, Media, RouterLink} from '../../components';
66
import HeaderBreadcrumbs from '../../components/HeaderBreadcrumbs/HeaderBreadcrumbs';
77
import {getMediaImage} from '../../components/Media/Image/utils';
88
import YFMWrapper from '../../components/YFMWrapper/YFMWrapper';
9-
import {MobileContext} from '../../context/mobileContext';
109
import {useTheme} from '../../context/theme';
1110
import {Col, Grid, Row} from '../../grid';
1211
import {ClassNameProps, HeaderBlockBackground, HeaderBlockProps} from '../../models';
@@ -15,6 +14,8 @@ import {mergeVideoMicrodata} from '../../utils/microdata';
1514

1615
import {getImageSize, getTitleSizes, titleWithImageSizes} from './utils';
1716
import './Header.scss';
17+
import {useWindowWidth} from '../../context/windowWidthContext';
18+
import {BREAKPOINTS} from '../../constants';
1819

1920
const b = block('header-block');
2021

@@ -85,7 +86,8 @@ export const HeaderBlock = (props: React.PropsWithChildren<HeaderBlockFullProps>
8586
centered,
8687
additionalInfo,
8788
} = props;
88-
const isMobile = React.useContext(MobileContext);
89+
const windowWidth = useWindowWidth();
90+
const isMobile = windowWidth <= BREAKPOINTS.sm;
8991
const theme = useTheme();
9092
const hasRightSideImage = Boolean((image || video) && !centered);
9193
const curImageSize = imageSize || getImageSize(width);
@@ -101,10 +103,7 @@ export const HeaderBlock = (props: React.PropsWithChildren<HeaderBlockFullProps>
101103
const videoThemed = video && getThemedValue(video, theme);
102104
const mediaWithMicrodata = mergeVideoMicrodata(
103105
{video: videoThemed, image: imageThemed},
104-
{
105-
name: title,
106-
description,
107-
},
106+
{name: title, description},
108107
);
109108
const fullWidth = backgroundThemed?.fullWidth || backgroundThemed?.fullWidthMedia;
110109
const titleId = useUniqId();
@@ -149,9 +148,7 @@ export const HeaderBlock = (props: React.PropsWithChildren<HeaderBlockFullProps>
149148
tagName="div"
150149
className={b('overtitle')}
151150
content={overtitle}
152-
modifiers={{
153-
constructor: true,
154-
}}
151+
modifiers={{constructor: true}}
155152
/>
156153
) : (
157154
overtitle
@@ -162,10 +159,7 @@ export const HeaderBlock = (props: React.PropsWithChildren<HeaderBlockFullProps>
162159
content={title}
163160
contentClassName={b('title')}
164161
className={b('title-container')}
165-
modifiers={{
166-
constructor: true,
167-
constructorTheme: textTheme,
168-
}}
162+
modifiers={{constructor: true, constructorTheme: textTheme}}
169163
tagName="h1"
170164
contentPosition="end"
171165
>

src/containers/PageConstructor/Provider.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
import {SSRContext, SSRContextProps} from '../../context/ssrContext';
2020
import {ThemeContext} from '../../context/theme';
2121
import {Theme} from '../../models';
22+
import {WindowWidthProvider} from '../../context/windowWidthContext';
2223

2324
export interface PageConstructorProviderProps {
2425
isMobile?: boolean;
@@ -62,6 +63,7 @@ export const PageConstructorProvider = (
6263
<AnalyticsContext.Provider value={analytics} />,
6364
<FormsContext.Provider value={forms} />,
6465
<SSRContext.Provider value={{isServer: ssrConfig?.isServer}} />,
66+
<WindowWidthProvider />,
6567
].reduceRight((prev, provider) => React.cloneElement(provider, {}, prev), children);
6668
/* eslint-enable react/jsx-key */
6769

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as React from 'react';
2+
import throttle from 'lodash/throttle';
3+
import {SSRContext} from '../ssrContext';
4+
import {BREAKPOINTS} from '../../constants';
5+
6+
const DEFAULT_WIDTH = BREAKPOINTS.xl;
7+
const UPDATE_FREQUENCY_MS = 100;
8+
9+
export const WindowWidthContext = React.createContext<number>(DEFAULT_WIDTH);
10+
11+
export const WindowWidthProvider = ({children}: React.PropsWithChildren) => {
12+
const {isServer} = React.useContext(SSRContext);
13+
14+
const [windowWidth, setWindowWidth] = React.useState(DEFAULT_WIDTH);
15+
16+
React.useEffect(() => {
17+
if (isServer) {
18+
return;
19+
}
20+
21+
const handleResize = throttle(
22+
() => {
23+
setWindowWidth(window.innerWidth);
24+
},
25+
UPDATE_FREQUENCY_MS,
26+
{leading: true},
27+
);
28+
29+
handleResize();
30+
31+
window.addEventListener('resize', handleResize, {passive: true});
32+
33+
// eslint-disable-next-line consistent-return
34+
return () => window.removeEventListener('resize', handleResize);
35+
}, [isServer]);
36+
37+
return (
38+
<WindowWidthContext.Provider value={windowWidth}>{children}</WindowWidthContext.Provider>
39+
);
40+
};
41+
42+
export const useWindowWidth = () => React.useContext(WindowWidthContext);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './BreakpointContext';

0 commit comments

Comments
 (0)