1
1
'use client' ;
2
- import { imageDimensionsFromData } from 'image-dimensions' ;
3
2
import {
4
3
createContext ,
5
4
forwardRef ,
6
5
PropsWithChildren ,
7
6
useContext ,
8
7
useEffect ,
9
8
useRef ,
9
+ useState ,
10
10
} from 'react' ;
11
11
12
12
import {
@@ -31,6 +31,7 @@ export function ImageSettings({
31
31
children,
32
32
...settings
33
33
} : PropsWithChildren < Partial < ImageSettingsType > > ) {
34
+ console . log ( 'settings' , settings . alterSrc ) ;
34
35
return (
35
36
< ImageSettingsContext . Provider
36
37
value = { { ...defaultImageSettings , ...settings } }
@@ -40,69 +41,49 @@ export function ImageSettings({
40
41
) ;
41
42
}
42
43
43
- async function imageDimensions ( src : string ) {
44
- const response = await fetch ( src ) ;
45
- if ( ! response . body ) {
46
- throw new Error ( 'Failed to fetch image' ) ;
47
- }
48
- const buffer = await response . arrayBuffer ( ) ;
49
- const data = new Uint8Array ( buffer ) ;
50
- const result = imageDimensionsFromData ( data ) ;
51
- return result ;
52
- }
53
-
54
44
function sizerImage ( width : number , height : number ) {
55
45
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${ width } " height="${ height } " viewBox="0 0 ${ width } ${ height } "></svg>` ;
56
46
return `data:image/svg+xml;base64,${ base64 ( svg ) } ` ;
57
47
}
58
48
59
49
export const Image = forwardRef ( function Image (
60
- { focalPoint, ...props } : ImageProps ,
50
+ { src , focalPoint, ...props } : ImageProps ,
61
51
ref ,
62
52
) {
63
53
const alterSrc = useContext ( ImageSettingsContext ) . alterSrc ;
54
+ const alteredSrc = alterSrc && src ? alterSrc ( src ) : src ;
64
55
const imageRef = useRef < HTMLImageElement | null > ( null ) ;
65
56
useEffect ( ( ) => {
66
57
const r = imageRef || ref ;
67
- if ( r . current && props . src ) {
68
- const url = alterSrc ? alterSrc ( props . src ) : props . src ;
69
- imageDimensions ( url )
70
- . then ( ( source ) => {
71
- if ( ! r . current || ! source ) {
72
- return ;
73
- }
74
- const target = inferTargetDimensions (
75
- source ,
76
- props . width ,
77
- props . height ,
78
- ) ;
79
- r . current . style . backgroundImage = `url(${ url } )` ;
80
- r . current . style . backgroundSize = 'cover' ;
81
- r . current . style . maxWidth = '100%' ;
58
+ if ( r . current && alteredSrc ) {
59
+ const source = {
60
+ width : r . current . naturalWidth ,
61
+ height : r . current . naturalHeight ,
62
+ } ;
63
+ if ( ! r . current || ! source ) {
64
+ return ;
65
+ }
66
+ const target = inferTargetDimensions ( source , props . width , props . height ) ;
67
+ r . current . style . backgroundImage = `url(${ alteredSrc } )` ;
68
+ r . current . style . backgroundSize = 'cover' ;
69
+ r . current . style . maxWidth = '100%' ;
82
70
83
- r . current . style . backgroundPosition = calculateFocusPosition (
84
- r . current . naturalWidth ,
85
- r . current . naturalHeight ,
86
- focalPoint || [
87
- r . current . naturalWidth / 2 ,
88
- r . current . naturalHeight / 2 ,
89
- ] ,
90
- ) ;
91
- r . current . src = sizerImage ( target . width , target . height ) ;
92
- return ;
93
- } )
94
- . catch ( ( error ) => {
95
- throw error ;
96
- } ) ;
71
+ r . current . style . backgroundPosition = calculateFocusPosition (
72
+ r . current . naturalWidth ,
73
+ r . current . naturalHeight ,
74
+ focalPoint || [ r . current . naturalWidth / 2 , r . current . naturalHeight / 2 ] ,
75
+ ) ;
76
+ r . current . src = sizerImage ( target . width , target . height ) ;
77
+ return ;
97
78
}
98
79
} , [
99
80
imageRef ,
100
81
ref ,
101
82
focalPoint ,
102
83
props . width ,
103
84
props . height ,
104
- props . src ,
85
+ alteredSrc ,
105
86
alterSrc ,
106
87
] ) ;
107
- return < img ref = { imageRef || ref } { ...props } /> ;
88
+ return < img src = { alteredSrc } ref = { imageRef || ref } { ...props } /> ;
108
89
} ) ;
0 commit comments