Skip to content

Commit 923ba80

Browse files
committed
refactor(SLB-385): simplify focalpoint typing
So its compatible with a simple `[Int!]` GraphQL type.
1 parent d98d95d commit 923ba80

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

packages/npm/@amazeelabs/image/src/client.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
type ImageProps,
1616
type ImageSettings as ImageSettingsType,
1717
inferTargetDimensions,
18+
validateFocus,
1819
} from './lib.js';
1920

2021
function base64(content: string) {
@@ -95,7 +96,7 @@ export const Image = forwardRef(function Image(
9596
ref.current.style.backgroundPosition = calculateFocusPosition(
9697
ref.current.naturalWidth,
9798
ref.current.naturalHeight,
98-
focalPoint || [
99+
validateFocus(focalPoint) || [
99100
ref.current.naturalWidth / 2,
100101
ref.current.naturalHeight / 2,
101102
],

packages/npm/@amazeelabs/image/src/lib.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export type ImageProps = Omit<
3030
* one is the x-coordinate, the second one is the y-coordinate. Both are
3131
* relative to the top-left corner of the *original* image.
3232
*/
33-
focalPoint?: [number, number];
33+
focalPoint?: Array<number>;
3434
/**
3535
* A single property to switch between important and lazy-loaded images.
3636
* It controls the `loading` and `decoding` attributes.
@@ -113,6 +113,15 @@ export function inferTargetDimensions(
113113

114114
export type Focus = [number, number];
115115

116+
export function validateFocus(
117+
focus: Array<number> | undefined,
118+
): Focus | undefined {
119+
if (!focus || focus.length === 2) {
120+
return focus as Focus;
121+
}
122+
console.warn('Invalid focus point:', focus);
123+
}
124+
116125
export function calculateFocusExtraction(
117126
source: readonly [number, number],
118127
target: readonly [number, number],

packages/npm/@amazeelabs/image/src/server.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
ImageProps,
1919
ImageSettings as ImageSettingsType,
2020
inferTargetDimensions,
21+
validateFocus,
2122
} from './lib.js';
2223

2324
async function prepareFile(src: string) {
@@ -170,7 +171,12 @@ export async function Image({
170171
decoding={priority ? 'async' : 'auto'}
171172
// eslint-disable-next-line react/no-unknown-property
172173
fetchPriority={priority ? 'high' : 'auto'}
173-
src={await transformSrc(filename, source, target, focalPoint)}
174+
src={await transformSrc(
175+
filename,
176+
source,
177+
target,
178+
validateFocus(focalPoint),
179+
)}
174180
srcSet={await transformSrcSet(
175181
filename,
176182
source,
@@ -180,7 +186,7 @@ export async function Image({
180186
target.width * 2,
181187
...getSettings().resolutions.filter((w) => w < target.width),
182188
],
183-
focalPoint,
189+
validateFocus(focalPoint),
184190
)}
185191
sizes={props.sizes || `(min-width: ${width}px) ${width}px, 100vw`}
186192
data-src={props.src}

0 commit comments

Comments
 (0)