-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor Props
of Image
and Picture
component to support type checking
#5788
Changes from 5 commits
a912cbd
8f9311a
d90ef3d
8e65030
76438d4
0b1f0f3
8bdf53d
c245aca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@astrojs/image': patch | ||
--- | ||
|
||
- Refactor types to support props auto-completion for the `Image` and `Picture` components. | ||
- Pass previously missing `alt` prop to the `getPicture` function |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,9 @@ | ||
--- | ||
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'; | ||
import type { PictureComponentLocalImageProps, PictureComponentRemoteImageProps } from './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; | ||
export type Props = PictureComponentLocalImageProps | PictureComponentRemoteImageProps; | ||
|
||
const { | ||
src, | ||
|
@@ -45,7 +18,7 @@ const { | |
loading = 'lazy', | ||
decoding = 'async', | ||
...attrs | ||
} = Astro.props as Props; | ||
} = Astro.props; | ||
|
||
if (alt === undefined || alt === null) { | ||
warnForMissingAlt(); | ||
|
@@ -59,6 +32,7 @@ const { image, sources } = await getPicture({ | |
fit, | ||
background, | ||
position, | ||
alt, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ommiting |
||
}); | ||
|
||
delete image.width; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,57 @@ | ||
/// <reference types="astro/astro-jsx" /> | ||
export { default as Image } from './Image.astro'; | ||
export { default as Picture } from './Picture.astro'; | ||
import type { HTMLAttributes as AllHTMLAttributes } from '../../../astro/types'; | ||
MoustaphaDev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// TODO: should these directives be removed from astroHTML.JSX? | ||
export type ImgHTMLAttributes = Omit< | ||
astroHTML.JSX.ImgHTMLAttributes, | ||
'client:list' | 'set:text' | 'set:html' | 'is:raw' | ||
>; | ||
export type HTMLAttributes = Omit< | ||
astroHTML.JSX.HTMLAttributes, | ||
'client:list' | 'set:text' | 'set:html' | 'is:raw' | ||
>; | ||
import type { TransformOptions, OutputFormat } from '../dist/loaders/index.js'; | ||
import type { ImageMetadata } from '../dist/vite-plugin-astro-image.js'; | ||
|
||
export interface ImageComponentLocalImageProps | ||
extends Omit<TransformOptions, 'src'>, | ||
Omit<ImgHTMLAttributes, 'src' | 'width' | 'height'> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sounds good to me! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Probably not? I don't think we want astro directives here |
||
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; | ||
} | ||
|
||
export interface ImageComponentRemoteImageProps extends TransformOptions, ImgHTMLAttributes { | ||
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; | ||
format?: OutputFormat; | ||
width: number; | ||
height: number; | ||
} | ||
|
||
export interface PictureComponentLocalImageProps | ||
extends Omit<HTMLAttributes, 'src' | 'width' | 'height'>, | ||
Omit<TransformOptions, 'src'>, | ||
Pick<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[]; | ||
} | ||
|
||
export interface PictureComponentRemoteImageProps | ||
extends Omit<HTMLAttributes, 'src' | 'width' | 'height'>, | ||
Princesseuh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 ImgHTMLAttributes = AllHTMLAttributes<'img'>; | ||
|
||
export type HTMLAttributes = Omit<astroHTML.JSX.HTMLAttributes, 'set:text' | 'set:html' | 'is:raw'>; | ||
Princesseuh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let altWarningShown = false; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason, when generics are used directly inside the astro component, its props type become
Record<string, any>
. When extracted to another file, it works as expectedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our language server doesn't support generics at the moment, but it will soon. Your workaround seems valid, though!
cc @Princesseuh for a TS review 😄