forked from ton-org/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.jsx
More file actions
113 lines (103 loc) · 2.96 KB
/
image.jsx
File metadata and controls
113 lines (103 loc) · 2.96 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* @param {{
* src: string,
* darkSrc?: string,
* alt?: string,
* darkAlt?: string,
* href?: string,
* target?: '_self' | '_blank' | '_parent' | '_top' | '_unfencedTop',
* height?: string | number,
* width?: string | number,
* }} props
*/
export const Image = ({ src, darkSrc, alt = "", darkAlt, href, target, height = 342, width = 608 }) => {
const isSVG = src.match(/\.svg(?:[#?].*?)?$/i) !== null;
const shouldInvert = isSVG && !darkSrc;
const shouldCreateLink = href !== undefined;
const minPx = 9;
const maxPx = 608;
const expectedPx = `a number or a string with a number that is greater than ${minPx - 1} and less than or equal to ${maxPx}`;
/**
* @param title {string}
* @param received {string | number}
* @param expected {string | number}
*/
const createInvalidPropCallout = (title, received, expected) => {
return (
// @ts-ignore
<Danger>
<span className="font-bold">
Invalid <code>{title.toString()}</code> passed!
</span>
<br />
<span className="font-bold">Received: </span>
{received.toString()}
<br />
<span className="font-bold">Expected: </span>
{expected.toString()}
{/* @ts-ignore */}
</Danger>
);
};
/** @param value {string | number} */
const checkValidDimensionValue = (value) => {
switch (typeof value) {
case "string":
case "number":
const num = Number(value);
return Number.isSafeInteger(num) && num >= minPx && num <= maxPx;
default:
return false;
}
};
// Collect error callouts
let callouts = [];
// Invalid image height (in pixels)
if (height && !checkValidDimensionValue(height)) {
callouts.push(createInvalidPropCallout("height", height, expectedPx));
}
// Invalid image width (in pixels)
if (width && !checkValidDimensionValue(width)) {
callouts.push(createInvalidPropCallout("width", width, expectedPx));
}
// Display all errors
if (callouts.length !== 0) {
return callouts;
}
// Resulting pixel dimensions
const heightPx = Number(height);
const widthPx = Number(width);
// Resulting images
const images = (
<>
<img
className="block dark:hidden"
src={src}
alt={alt}
{...(height && { height: heightPx })}
{...(width && { width: widthPx })}
// @ts-ignore
{...((shouldCreateLink || shouldInvert) && { noZoom: "true" })}
/>
<img
className={`hidden dark:block ${shouldInvert ? "invert" : ""}`}
src={darkSrc ?? src}
alt={darkAlt ?? alt}
{...(height && { height: heightPx })}
{...(width && { width: widthPx })}
// @ts-ignore
{...((shouldCreateLink || shouldInvert) && { noZoom: "true" })}
/>
</>
);
// Is a clickable link
if (shouldCreateLink) {
return (
<a href={href} target={target ?? "_self"}>
{images}
</a>
);
}
// Not a link
return images;
};