Skip to content

Commit 02a365c

Browse files
refactor(core): internal utils
1 parent 47c30f7 commit 02a365c

File tree

7 files changed

+21
-26
lines changed

7 files changed

+21
-26
lines changed

docs/api/blurhash-create-png-data-uri.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# `createPngDataUri`
22

3-
Especially when using a server-side rendering framework like [Nuxt](https://nuxt.com), you might want to generate the placeholder images for the `src` attribute with SSR. This can be done with the `createPngDataUri` function:
3+
Especially when using a server-side rendering framework like [Nuxt](https://nuxt.com), you might want to generate the placeholder images for the `src` attribute during SSR. This can be done with the `createPngDataUri` function:
44

55
```ts
66
import { createPngDataUri } from 'unlazy/blurhash'

docs/api/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ unlazy provides a number of methods to help you with lazy loading images. The fo
88

99
## Server-Side Rendering
1010

11-
unlazy supports server-side rendering for BlurHash and ThumbHash strings. This means that you can generate the placeholder images for the `src` attribute with SSR and avoid the [Cumulative Layout Shift](https://web.dev/cls/) (CLS) caused by the images loading after the page has been rendered.
11+
unlazy supports server-side rendering for BlurHash and ThumbHash strings. This means that you can generate the placeholder images for the `src` attribute during SSR and avoid the [Cumulative Layout Shift](https://web.dev/cls/) (CLS) caused by the images loading after the page has been rendered.
1212

1313
Both the `unlazy/blurhash` and `unlazy/thumbhash` exports provide a `createPngDataUri` function that can be used to generate a PNG data URI for a BlurHash or ThumbHash string, respectively. This function can be used to generate the `src` attribute for the `<img>` element.
1414

docs/api/thumbhash-create-png-data-uri.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# `createPngDataUri`
22

3-
Especially when using a server-side rendering framework like [Nuxt](https://nuxt.com), you might want to generate the placeholder images for the `src` attribute with SSR. This can be done with the `createPngDataUri` function:
3+
Especially when using a server-side rendering framework like [Nuxt](https://nuxt.com), you might want to generate the placeholder images for the `src` attribute during SSR. This can be done with the `createPngDataUri` function:
44

55
```ts
66
import { createPngDataUri } from 'unlazy/thumbhash'

docs/integrations/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ That's it!
3535

3636
## Server-Side Rendering
3737

38-
unlazy supports server-side rendering for BlurHash and ThumbHash strings. This means that you can generate the placeholder images for the `src` attribute with SSR and avoid the [Cumulative Layout Shift](https://web.dev/cls/) (CLS) caused by the images loading after the page has been rendered.
38+
unlazy supports server-side rendering for BlurHash and ThumbHash strings. This means that you can generate the placeholder images for the `src` attribute during SSR and avoid the [Cumulative Layout Shift](https://web.dev/cls/) (CLS) caused by the images loading after the page has been rendered.
3939

4040
Both the `unlazy/blurhash` and `unlazy/thumbhash` exports provide a `createPngDataUri` function that can be used to generate a PNG data URI for a BlurHash or ThumbHash string, respectively. This function can be used to generate the `src` attribute for the `<img>` element.
4141

packages/core/src/blurhash.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { decodeBlurHash } from 'fast-blurhash'
22
import { DEFAULT_PLACEHOLDER_SIZE } from './constants'
3-
import { calculateAspectRatioDimensions } from './utils'
43
import { rgbaToDataUri } from './utils/dataUri'
54

65
export interface BlurHashOptions {
@@ -27,7 +26,16 @@ export function createPngDataUri(
2726
size = DEFAULT_PLACEHOLDER_SIZE,
2827
}: BlurHashOptions = {},
2928
) {
30-
const { width, height } = calculateAspectRatioDimensions(ratio, size)
29+
const { width, height } = getAspectRatioDimensions(ratio, size)
3130
const rgba = decodeBlurHash(hash, width, height)
3231
return rgbaToDataUri(width, height, rgba)
3332
}
33+
34+
function getAspectRatioDimensions(ratio: number, size: number) {
35+
const isLandscapeOrSquare = ratio >= 1
36+
37+
return {
38+
width: isLandscapeOrSquare ? size : Math.round(size * ratio),
39+
height: isLandscapeOrSquare ? Math.round(size / ratio) : size,
40+
}
41+
}

packages/core/src/lazyLoad.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,23 @@ export function loadImage(
141141

142142
export function createPlaceholderFromHash(
143143
{
144-
/** If given, the hash will be extracted from the image's `data-blurhash` or `data-thumbhash` attribute and ratio will be calculated from the image's actual dimensions */
145144
image,
146145
hash,
147146
hashType = 'blurhash',
148-
/** @default 32 */
149147
size = DEFAULT_PLACEHOLDER_SIZE,
150-
/** Will be calculated from the image's actual dimensions if not provided and image is given */
151148
ratio,
152149
}: {
150+
/** If present, the hash will be extracted from the image's `data-blurhash` or `data-thumbhash` attribute and ratio will be calculated from the image's actual dimensions. */
153151
image?: HTMLImageElement
154152
hash?: string
155153
hashType?: 'blurhash' | 'thumbhash'
154+
/** @default 32 */
156155
size?: number
156+
/** Will be calculated from the image's actual dimensions if image is provided and ratio is not. */
157157
ratio?: number
158158
} = {},
159159
) {
160-
if (!hash && image) {
160+
if (image && !hash) {
161161
const { blurhash, thumbhash } = image.dataset
162162
hash = thumbhash || blurhash
163163
hashType = thumbhash ? 'thumbhash' : 'blurhash'
@@ -169,9 +169,9 @@ export function createPlaceholderFromHash(
169169
try {
170170
if (hashType === 'blurhash') {
171171
// Preserve the original image's aspect ratio
172-
if (!ratio && image) {
173-
const actualWidth = image.width ?? image.offsetWidth ?? size
174-
const actualHeight = image.height ?? image.offsetHeight ?? size
172+
if (image && !ratio) {
173+
const actualWidth = image.width || image.offsetWidth || size
174+
const actualHeight = image.height || image.offsetHeight || size
175175
ratio = actualWidth / actualHeight
176176
}
177177

packages/core/src/utils/index.ts

-13
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,6 @@ export function createIndexedImagePlaceholder(index: number) {
2323
return DEFAULT_IMAGE_PLACEHOLDER.replace(/\s/, ` data-id='${now}-${index}' `)
2424
}
2525

26-
export function calculateAspectRatioDimensions(aspectRatio: number, referenceSize: number) {
27-
const isLandscapeOrSquare = aspectRatio >= 1
28-
29-
return {
30-
width: isLandscapeOrSquare
31-
? referenceSize
32-
: Math.round(referenceSize * aspectRatio),
33-
height: isLandscapeOrSquare
34-
? Math.round(referenceSize / aspectRatio)
35-
: referenceSize,
36-
}
37-
}
38-
3926
export function debounce<T extends (...args: any[]) => void>(
4027
fn: T,
4128
delay: number,

0 commit comments

Comments
 (0)