Commit 31aa62d4 by Moorthy G

refactor(shared/responsive-image): extract out source as a seperate component

parent b871d0ab
import ResponsiveImage from './ResponsiveImage'; import { ResponsiveImage, Source } from './';
import type { ResponsiveImageProps } from './ResponsiveImage'; import type { ResponsiveImageProps } from './ResponsiveImage';
export default { export default {
...@@ -28,42 +28,42 @@ export const ArtDirection = { ...@@ -28,42 +28,42 @@ export const ArtDirection = {
}, },
render: (args: ResponsiveImageProps) => ( render: (args: ResponsiveImageProps) => (
<ResponsiveImage {...args}> <ResponsiveImage {...args}>
<ResponsiveImage.Source <Source
media="xxl" media="xxl"
src="https://picsum.photos/1920/540?jpg" src="https://picsum.photos/1920/540?jpg"
width={1920} width={1920}
height={540} height={540}
sizes="100vw" sizes="100vw"
/> />
<ResponsiveImage.Source <Source
media="xl" media="xl"
src="https://picsum.photos/1536/540?jpg" src="https://picsum.photos/1536/540?jpg"
width={1536} width={1536}
height={540} height={540}
sizes="100vw" sizes="100vw"
/> />
<ResponsiveImage.Source <Source
media="lg" media="lg"
src="https://picsum.photos/1280/540?jpg" src="https://picsum.photos/1280/540?jpg"
width={1280} width={1280}
height={540} height={540}
sizes="100vw" sizes="100vw"
/> />
<ResponsiveImage.Source <Source
media="md" media="md"
src="https://picsum.photos/1024/540?jpg" src="https://picsum.photos/1024/540?jpg"
width={1024} width={1024}
height={540} height={540}
sizes="100vw" sizes="100vw"
/> />
<ResponsiveImage.Source <Source
media="sm" media="sm"
src="https://picsum.photos/768/540?jpg" src="https://picsum.photos/768/540?jpg"
width={768} width={768}
height={540} height={540}
sizes="100vw" sizes="100vw"
/> />
<ResponsiveImage.Source <Source
media="(min-width: 576px)" media="(min-width: 576px)"
src="https://picsum.photos/640/540?jpg" src="https://picsum.photos/640/540?jpg"
width={640} width={640}
......
import React, { forwardRef, memo } from 'react'; import React, { forwardRef } from 'react';
import { getImageProps } from 'next/image'; import { getImageProps } from 'next/image';
import breakpoints from './breakpoints';
export type Image = { export type Image = {
url: string; url: string;
...@@ -21,31 +20,14 @@ export interface ResponsiveImageProps ...@@ -21,31 +20,14 @@ export interface ResponsiveImageProps
children?: React.ReactNode; children?: React.ReactNode;
} }
export interface SourceProps extends React.HTMLAttributes<HTMLSourceElement> { export const ResponsiveImage = forwardRef<
src: string; HTMLImageElement,
width: number; ResponsiveImageProps
height: number; >(
media: string; (
sizes: string; { src, width, height, alt, sizes, quality, priority, children, ...rest },
quality?: number; ref
} ) => {
type ResponsiveImageCompoundComponent = React.ForwardRefExoticComponent<
React.PropsWithoutRef<ResponsiveImageProps> & React.RefAttributes<HTMLImageElement>
> & { Source: React.FC<SourceProps> };
const ResponsiveImage = forwardRef<HTMLImageElement, ResponsiveImageProps>(
({
src,
width,
height,
alt,
sizes,
quality,
priority,
children,
...rest
}, ref) => {
if (src) { if (src) {
const { props } = getImageProps({ const { props } = getImageProps({
src, src,
...@@ -66,39 +48,3 @@ const ResponsiveImage = forwardRef<HTMLImageElement, ResponsiveImageProps>( ...@@ -66,39 +48,3 @@ const ResponsiveImage = forwardRef<HTMLImageElement, ResponsiveImageProps>(
} }
} }
); );
const ResponsiveImageWithStatics = ResponsiveImage as ResponsiveImageCompoundComponent;
ResponsiveImageWithStatics.displayName = 'ResponsiveImage';
ResponsiveImageWithStatics.Source = memo(
({ src, width, height, sizes, media, quality, ...rest }: SourceProps) => {
if (src) {
const query = breakpoints[media] || media;
const { props } = getImageProps({
width,
height,
src,
sizes,
quality,
alt: ''
});
return (
<source
media={query}
src={props.src}
srcSet={props.srcSet}
width={props.width}
height={props.height}
sizes={props.sizes}
{...rest}
/>
);
}
}
);
ResponsiveImageWithStatics.Source.displayName = 'ResponsiveImageSource';
export { ResponsiveImageWithStatics as ResponsiveImage };
export default ResponsiveImageWithStatics;
import React, { memo } from 'react';
import { getImageProps } from 'next/image';
import breakpoints from './breakpoints';
export type IResponsiveImageSourceProps =
React.HTMLAttributes<HTMLSourceElement> & {
src: string;
width: number;
height: number;
media: string;
sizes: string;
quality?: number;
};
export const Source = memo(
({
src,
width,
height,
sizes,
media,
quality,
...rest
}: IResponsiveImageSourceProps) => {
if (src) {
const query = breakpoints[media] || media;
const { props } = getImageProps({
width,
height,
src,
sizes,
quality,
alt: ''
});
return (
<source
media={query}
src={props.src}
srcSet={props.srcSet}
width={props.width}
height={props.height}
sizes={props.sizes}
{...rest}
/>
);
}
return null;
}
);
Source.displayName = 'ResponsiveImageSource';
export * from './ResponsiveImage'; export * from './ResponsiveImage';
export * from './Source';
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment