|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { forwardRef } from 'react' |
| 4 | + |
| 5 | +import Box from '@mui/material/Box' |
| 6 | +import { alpha, useTheme } from '@mui/material/styles' |
| 7 | +import { LazyLoadImage as ReactLazyLoadImage } from 'react-lazy-load-image-component' |
| 8 | + |
| 9 | +import { LazyLoadImageProps } from './types' |
| 10 | +import { getRatio } from './utils' |
| 11 | + |
| 12 | +export type * from './types' |
| 13 | + |
| 14 | +const LazyLoadImage = forwardRef<HTMLSpanElement, LazyLoadImageProps>( |
| 15 | + ( |
| 16 | + { |
| 17 | + ratio, |
| 18 | + overlay, |
| 19 | + disabledEffect = false, |
| 20 | + alt, |
| 21 | + src, |
| 22 | + afterLoad, |
| 23 | + delayTime, |
| 24 | + threshold, |
| 25 | + beforeLoad, |
| 26 | + delayMethod, |
| 27 | + placeholder, |
| 28 | + wrapperProps, |
| 29 | + scrollPosition, |
| 30 | + effect = 'blur', |
| 31 | + visibleByDefault, |
| 32 | + wrapperClassName, |
| 33 | + useIntersectionObserver, |
| 34 | + sx, |
| 35 | + ...other |
| 36 | + }, |
| 37 | + ref, |
| 38 | + ) => { |
| 39 | + const theme = useTheme() |
| 40 | + |
| 41 | + const overlayStyles = !!overlay && { |
| 42 | + '&:before': { |
| 43 | + content: "''", |
| 44 | + top: 0, |
| 45 | + left: 0, |
| 46 | + width: 1, |
| 47 | + height: 1, |
| 48 | + zIndex: 1, |
| 49 | + position: 'absolute', |
| 50 | + background: overlay || alpha(theme.palette.grey[900], 0.48), |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + const content = ( |
| 55 | + <Box |
| 56 | + component={ReactLazyLoadImage} |
| 57 | + alt={alt} |
| 58 | + src={src} |
| 59 | + afterLoad={afterLoad} |
| 60 | + delayTime={delayTime} |
| 61 | + threshold={threshold} |
| 62 | + beforeLoad={beforeLoad} |
| 63 | + delayMethod={delayMethod} |
| 64 | + placeholder={placeholder} |
| 65 | + wrapperProps={wrapperProps} |
| 66 | + scrollPosition={scrollPosition} |
| 67 | + visibleByDefault={visibleByDefault} |
| 68 | + effect={disabledEffect ? undefined : effect} |
| 69 | + useIntersectionObserver={useIntersectionObserver} |
| 70 | + wrapperClassName={wrapperClassName || 'component-image-wrapper'} |
| 71 | + placeholderSrc={disabledEffect ? '/assets/transparent.png' : '/assets/placeholder.svg'} |
| 72 | + sx={{ |
| 73 | + width: 1, |
| 74 | + height: 1, |
| 75 | + objectFit: 'cover', |
| 76 | + verticalAlign: 'bottom', |
| 77 | + ...(!!ratio && { |
| 78 | + top: 0, |
| 79 | + left: 0, |
| 80 | + position: 'absolute', |
| 81 | + }), |
| 82 | + }} |
| 83 | + /> |
| 84 | + ) |
| 85 | + |
| 86 | + return ( |
| 87 | + <Box |
| 88 | + ref={ref} |
| 89 | + component="span" |
| 90 | + className="component-image" |
| 91 | + sx={{ |
| 92 | + overflow: 'hidden', |
| 93 | + position: 'relative', |
| 94 | + verticalAlign: 'bottom', |
| 95 | + display: 'inline-block', |
| 96 | + ...(!!ratio && { |
| 97 | + width: 1, |
| 98 | + }), |
| 99 | + '& span.component-image-wrapper': { |
| 100 | + width: 1, |
| 101 | + height: 1, |
| 102 | + verticalAlign: 'bottom', |
| 103 | + backgroundSize: 'cover !important', |
| 104 | + ...(!!ratio && { |
| 105 | + pt: getRatio(ratio), |
| 106 | + }), |
| 107 | + }, |
| 108 | + ...overlayStyles, |
| 109 | + ...sx, |
| 110 | + }} |
| 111 | + {...other} |
| 112 | + > |
| 113 | + {content} |
| 114 | + </Box> |
| 115 | + ) |
| 116 | + }, |
| 117 | +) |
| 118 | + |
| 119 | +export default LazyLoadImage |
0 commit comments