-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add responsive support to picture component (#12423)
* feat: add responsive support to picture component * Add picture tests * Fix test * Implement own define vars * Share logic * Prevent extra astro-* class * Use dataset scoping * Revert unit test * Revert "Fix test" This reverts commit f9ec6e2. * Changes from review
- Loading branch information
Showing
10 changed files
with
400 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[data-astro-image] { | ||
width: 100%; | ||
height: auto; | ||
object-fit: var(--fit); | ||
object-position: var(--pos); | ||
aspect-ratio: var(--w) / var(--h); | ||
} | ||
/* Styles for responsive layout */ | ||
[data-astro-image='responsive'] { | ||
max-width: calc(var(--w) * 1px); | ||
max-height: calc(var(--h) * 1px); | ||
} | ||
/* Styles for fixed layout */ | ||
[data-astro-image='fixed'] { | ||
width: calc(var(--w) * 1px); | ||
height: calc(var(--h) * 1px); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { toStyleString } from '../../runtime/server/render/util.js'; | ||
import type { AstroConfig } from '../../types/public/config.js'; | ||
import type { GetImageResult, ImageLayout, LocalImageProps, RemoteImageProps } from '../types.js'; | ||
|
||
export function addCSSVarsToStyle( | ||
vars: Record<string, string | false | undefined>, | ||
styles?: string | Record<string, any>, | ||
) { | ||
const cssVars = Object.entries(vars) | ||
.filter(([_, value]) => value !== undefined && value !== false) | ||
.map(([key, value]) => `--${key}: ${value};`) | ||
.join(' '); | ||
|
||
if (!styles) { | ||
return cssVars; | ||
} | ||
const style = typeof styles === 'string' ? styles : toStyleString(styles); | ||
|
||
return `${cssVars} ${style}`; | ||
} | ||
|
||
const cssFitValues = ['fill', 'contain', 'cover', 'scale-down']; | ||
|
||
export function applyResponsiveAttributes< | ||
T extends LocalImageProps<unknown> | RemoteImageProps<unknown>, | ||
>({ | ||
layout, | ||
image, | ||
props, | ||
additionalAttributes | ||
}: { | ||
layout: Exclude<ImageLayout, 'none'>; | ||
image: GetImageResult; | ||
additionalAttributes: Record<string, any>; | ||
props: T; | ||
}) { | ||
const attributes = { ...additionalAttributes, ...image.attributes }; | ||
attributes.style = addCSSVarsToStyle( | ||
{ | ||
w: image.attributes.width ?? props.width ?? image.options.width, | ||
h: image.attributes.height ?? props.height ?? image.options.height, | ||
fit: cssFitValues.includes(props.fit ?? '') && props.fit, | ||
pos: props.position, | ||
}, | ||
attributes.style, | ||
); | ||
attributes['data-astro-image'] = layout; | ||
return attributes; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.