/
Picture.mjs
52 lines (45 loc) · 1.48 KB
/
Picture.mjs
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
// @ts-check
import classNameProp from "class-name-prop";
import React from "react";
/** CSS dependency URLs for the React component {@linkcode Picture}. */
export const css = new Set([new URL("./Picture.css", import.meta.url).href]);
/**
* React component for a {@link HTMLPictureElement picture} with a responsive
* width and proportionate height, even while loading.
*
* It’s displayed as a block and tends to take up 100% of the available space
* unless an inline style `width` is set.
*/
const Picture = React.forwardRef(
(
/**
* @type {PictureProps
* & React.ComponentPropsWithoutRef<"picture">
* & { [dataAttribute: `data-${string}`]: unknown }}
*/
{ width, height, className, style, ...props },
/** @type {React.ForwardedRef<HTMLPictureElement>} */
ref
) =>
React.createElement("picture", {
className: classNameProp("daui-Picture", className),
...props,
style: {
...style,
"--daui-Picture-width": width,
"--daui-Picture-height": height,
},
ref,
})
);
Picture.displayName = "Picture";
export default Picture;
/**
* Props for the {@linkcode Picture} React component, excluding additional props
* for the {@linkcode HTMLPictureElement} container.
* @typedef {object} PictureProps
* @prop {number} width The picture’s ratio width; typically it’s intrinsic
* pixel width.
* @prop {number} height The picture’s ratio height; typically it’s intrinsic
* pixel height.
*/