-
Notifications
You must be signed in to change notification settings - Fork 255
/
image.tsx
69 lines (63 loc) · 1.39 KB
/
image.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import * as React from "react";
import { connect, useConnect } from "frontity";
import { Frontity } from "frontity/types";
/**
* Props for the {@link Image} component.
*/
export interface ImageProps extends React.ImgHTMLAttributes<HTMLImageElement> {
/**
* CSS class(es).
*/
className?: string;
/**
* The `rootMargin` of useInView.
*
* @deprecated This property was used in the previous version of the Image
* component and has no effect anymore as the Image component relies only on
* native lazy-loading.
*/
rootMargin?: number;
}
/**
* The Image component.
*
* @param props - Defined in {@link ImageProps}.
* @returns A React component.
*/
const Image: React.FC<ImageProps> = ({
loading = "lazy",
className,
width,
height,
alt,
...props
}) => {
const { state } = useConnect<Frontity>();
if (state.frontity.mode === "amp") {
let layout = "fill";
if (height && width) {
layout = "responsive";
}
return (
<amp-img
alt={alt}
class={className}
width={width}
height={height}
layout={layout}
{...props}
/>
);
}
return (
<img
alt={alt}
className={"frontity-lazy-image".concat(className ? ` ${className}` : "")}
loading={loading}
width={width}
height={height}
{...props}
/>
);
};
export default connect(Image, { injectProps: false });