Skip to content

Commit 10841dc

Browse files
authored
feat: add image context to use NextImage instead of normal img tag (#313)
* feat: add passive flag and alt to img * fix: change defer to loading attribute for iframe * feat: add image context * feat: removed passive flag from listener * feat: removed changes from another pr * feat: removed changes from another pr
1 parent b447acf commit 10841dc

File tree

8 files changed

+48
-10
lines changed

8 files changed

+48
-10
lines changed

src/blocks/Security/Security.tsx

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

3-
import {AnimateBlock, FullWidthBackground, HTML, Link, Media} from '../../components';
3+
import {AnimateBlock, FullWidthBackground, HTML, ImageBase, Link, Media} from '../../components';
44
import {Col, Grid, Row} from '../../grid';
55
import {SecurityBlockProps} from '../../models';
66
import {block} from '../../utils';
@@ -26,7 +26,7 @@ export const SecurityBlock = (props: SecurityBlockProps) => {
2626
<Row className={b('points')}>
2727
{points.map(({text, link, img}, index) => (
2828
<Col key={index} className={b('point')} sizes={{sm: 4, all: 12}}>
29-
<img className={b('point-icon')} src={img} />
29+
<ImageBase className={b('point-icon')} src={img} />
3030
<HTML className={b('point-text')} block={true}>
3131
{text}
3232
</HTML>

src/components/Button/Button.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {useMetrika} from '../../hooks/useMetrika';
88
import {Github} from '../../icons';
99
import {ButtonProps as ButtonParams, DefaultEventNames} from '../../models';
1010
import {block, setUrlTld} from '../../utils';
11+
import ImageBase from '../ImageBase/ImageBase';
1112
import {ICON_QA, OldButtonSize, OldButtonTheme, toCommonSize, toCommonView} from './utils';
1213

1314
import './Button.scss';
@@ -72,7 +73,7 @@ const Button = (props: ButtonProps) => {
7273
}
7374

7475
let icon;
75-
let image = img && <img className={b('image')} src={buttonImg.url} alt={buttonImg.alt} />;
76+
let image = img && <ImageBase className={b('image')} src={buttonImg.url} alt={buttonImg.alt} />;
7677

7778
if (theme === 'github') {
7879
icon = <Icon className={b('icon')} data={Github} size={24} qa={ICON_QA} />;

src/components/Image/Image.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {BREAKPOINTS} from '../../constants';
44
import {ProjectSettingsContext} from '../../context/projectSettingsContext';
55
import {ImageDeviceProps, ImageObjectProps} from '../../models';
66
import {isCompressible} from '../../utils/imageCompress';
7+
import ImageBase from '../ImageBase/ImageBase';
78

89
export interface ImageProps extends Partial<ImageObjectProps>, Partial<ImageDeviceProps> {
910
style?: CSSProperties;
@@ -19,7 +20,7 @@ const checkWebP = (src: string) => {
1920
const Image = (props: ImageProps) => {
2021
const projectSettings = useContext(ProjectSettingsContext);
2122
const {
22-
src,
23+
src: imageSrc,
2324
alt,
2425
disableCompress,
2526
tablet,
@@ -32,16 +33,16 @@ const Image = (props: ImageProps) => {
3233
} = props;
3334
const [imgLoadingError, setImgLoadingError] = useState(false);
3435

35-
const imageSrc = src || desktop;
36+
const src = imageSrc || desktop;
3637

37-
if (!imageSrc) {
38+
if (!src) {
3839
return null;
3940
}
4041

4142
const disableWebp =
4243
projectSettings.disableCompress ||
4344
disableCompress ||
44-
!isCompressible(imageSrc) ||
45+
!isCompressible(src) ||
4546
imgLoadingError;
4647

4748
return (
@@ -70,11 +71,11 @@ const Image = (props: ImageProps) => {
7071
<source srcSet={tablet} media={`(max-width: ${BREAKPOINTS.md}px)`} />
7172
</Fragment>
7273
)}
73-
{imageSrc && !disableWebp && <source srcSet={checkWebP(imageSrc)} type="image/webp" />}
74-
<img
74+
{src && !disableWebp && <source srcSet={checkWebP(src)} type="image/webp" />}
75+
<ImageBase
7576
className={className}
76-
src={imageSrc}
7777
alt={alt}
78+
src={src}
7879
style={style}
7980
onClick={onClick}
8081
onError={() => setImgLoadingError(true)}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React, {CSSProperties, MouseEventHandler} from 'react';
2+
3+
import {ImageContext} from '../../context/imageContext/imageContext';
4+
import {ImageObjectProps} from '../../models';
5+
6+
export interface ImageBaseProps extends Partial<ImageObjectProps> {
7+
style?: CSSProperties;
8+
className?: string;
9+
onClick?: MouseEventHandler;
10+
onError?: () => void;
11+
}
12+
13+
export const ImageBase = (props: ImageBaseProps) => {
14+
const {Image} = React.useContext(ImageContext);
15+
return Image ? <Image {...props} /> : <img {...props} />;
16+
};
17+
18+
export default ImageBase;

src/components/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export {default as FullWidthBackground} from './FullWidthBackground/FullWidthBac
1616
export {default as HeaderBreadcrumbs} from './HeaderBreadcrumbs/HeaderBreadcrumbs';
1717
export {default as HeightCalculator} from './HeightCalculator/HeightCalculator';
1818
export {default as Image} from './Image/Image';
19+
export {default as ImageBase} from './ImageBase/ImageBase';
1920
export {default as Link} from './Link/Link';
2021
export {default as Links} from './Link/Links';
2122
export {default as Media} from './Media/Media';
@@ -37,3 +38,4 @@ export {default as MetaInfo} from './MetaInfo/MetaInfo';
3738
export {default as FullScreenMedia} from './FullscreenMedia/FullScreenMedia';
3839

3940
export type {RouterLinkProps} from './RouterLink/RouterLink';
41+
export type {ImageBaseProps} from './ImageBase/ImageBase';

src/containers/PageConstructor/Provider.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, {Fragment} from 'react';
22

33
import {DEFAULT_THEME} from '../../components/constants';
44
import {AnalyticsContext, AnalyticsContextProps} from '../../context/analyticsContext';
5+
import {ImageContext, ImageContextProps} from '../../context/imageContext';
56
import {LocaleContext, LocaleContextProps} from '../../context/localeContext';
67
import {LocationContext, LocationContextProps} from '../../context/locationContext';
78
import {MapsContext, MapsContextType, initialMapValue} from '../../context/mapsContext/mapsContext';
@@ -25,6 +26,7 @@ export interface PageConstructorProviderProps {
2526
mapsContext?: MapsContextType;
2627
projectSettings?: ProjectSettingsContextProps;
2728
analytics?: AnalyticsContextProps;
29+
image?: ImageContextProps;
2830
}
2931

3032
export const PageConstructorProvider = (props: WithChildren<PageConstructorProviderProps>) => {
@@ -39,13 +41,15 @@ export const PageConstructorProvider = (props: WithChildren<PageConstructorProvi
3941
projectSettings = {},
4042
theme = DEFAULT_THEME,
4143
children,
44+
image = {},
4245
} = props;
4346

4447
/* eslint-disable react/jsx-key */
4548
const context = [
4649
<ThemeValueContext.Provider value={{themeValue: theme}} />,
4750
<ProjectSettingsContext.Provider value={projectSettings} />,
4851
<LocaleContext.Provider value={locale} />,
52+
<ImageContext.Provider value={image} />,
4953
<LocationContext.Provider value={location} />,
5054
<MobileContext.Provider value={Boolean(isMobile)} />,
5155
<MapsContext.Provider value={mapsContext} />,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
3+
import {ImageBaseProps} from '../../components';
4+
5+
export type Image = React.ComponentClass<ImageBaseProps> | React.FC<ImageBaseProps>;
6+
7+
export type ImageContextProps = {
8+
Image?: Image;
9+
};
10+
11+
export const ImageContext = React.createContext<ImageContextProps>({});

src/context/imageContext/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './imageContext';

0 commit comments

Comments
 (0)