e4348a4eb4
* Added `background` option and prop. This optional color specifies which background to use when removing the alpha channel if the output format doesn't support transparency. * Modified existing tests * Fixed wrong dimensions in tests * Fixing a few instances of jpeg vs jpg * Added color checking * working on the tests * tests are now passing * Adding tests * Added tests for background color * no need to test with subpath * Added fixture * Renamed test fixture for background-color * skipping test until fixed * Typo * Working on tests * tests are passing * Updated readme and added changeset * Updated lockfile * Updated lockfile * Updated lockfile Co-authored-by: Tony Sullivan <tony.f.sullivan@outlook.com>
64 lines
1.9 KiB
Text
64 lines
1.9 KiB
Text
---
|
|
import { getPicture } from '../dist/index.js';
|
|
import { warnForMissingAlt } from './index.js';
|
|
import type { ImgHTMLAttributes, HTMLAttributes } from './index.js';
|
|
import type { ImageMetadata, OutputFormat, TransformOptions } from '../dist/index.js';
|
|
|
|
interface LocalImageProps
|
|
extends Omit<HTMLAttributes, 'src' | 'width' | 'height'>,
|
|
Omit<TransformOptions, 'src'>,
|
|
Pick<astroHTML.JSX.ImgHTMLAttributes, 'loading' | 'decoding'> {
|
|
src: ImageMetadata | Promise<{ default: ImageMetadata }>;
|
|
/** Defines an alternative text description of the image. Set to an empty string (alt="") if the image is not a key part of the content (it's decoration or a tracking pixel). */
|
|
alt: string;
|
|
sizes: HTMLImageElement['sizes'];
|
|
widths: number[];
|
|
formats?: OutputFormat[];
|
|
}
|
|
|
|
interface RemoteImageProps
|
|
extends Omit<HTMLAttributes, 'src' | 'width' | 'height'>,
|
|
TransformOptions,
|
|
Pick<ImgHTMLAttributes, 'loading' | 'decoding'> {
|
|
src: string;
|
|
/** Defines an alternative text description of the image. Set to an empty string (alt="") if the image is not a key part of the content (it's decoration or a tracking pixel). */
|
|
alt: string;
|
|
sizes: HTMLImageElement['sizes'];
|
|
widths: number[];
|
|
aspectRatio: TransformOptions['aspectRatio'];
|
|
formats?: OutputFormat[];
|
|
background: TransformOptions['background'];
|
|
}
|
|
|
|
export type Props = LocalImageProps | RemoteImageProps;
|
|
|
|
const {
|
|
src,
|
|
alt,
|
|
sizes,
|
|
widths,
|
|
aspectRatio,
|
|
background,
|
|
formats = ['avif', 'webp'],
|
|
loading = 'lazy',
|
|
decoding = 'async',
|
|
...attrs
|
|
} = Astro.props as Props;
|
|
|
|
if (alt === undefined || alt === null) {
|
|
warnForMissingAlt();
|
|
}
|
|
|
|
const { image, sources } = await getPicture({ src, widths, formats, aspectRatio, background });
|
|
---
|
|
|
|
<picture {...attrs}>
|
|
{sources.map((attrs) => <source {...attrs} {sizes} />)}
|
|
<img {...image} {loading} {decoding} {alt} />
|
|
</picture>
|
|
|
|
<style>
|
|
img {
|
|
content-visibility: auto;
|
|
}
|
|
</style>
|