Skip to content

Commit 143d1e0

Browse files
docs: update type declarations and improve documentation across multiple files
1 parent d7ac90a commit 143d1e0

File tree

14 files changed

+65
-151
lines changed

14 files changed

+65
-151
lines changed

docs/api/auto-sizes.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,7 @@ To lazy load images, refer to the [`lazyLoad`](/api/lazy-load) method.
88

99
## Type Declarations
1010

11-
```ts
12-
function autoSizes<T extends HTMLImageElement | HTMLSourceElement>(
13-
/**
14-
* A CSS selector, a DOM element, a list of DOM elements, or an array of DOM elements to calculate the `sizes` attribute for.
15-
*
16-
* @default 'img[data-sizes="auto"], source[data-sizes="auto"]'
17-
*/
18-
selectorsOrElements?: string | T | NodeListOf<T> | T[]
19-
): void
20-
```
11+
<<< @/../packages/core/src/lazyLoad.ts#autoSizes{ts}
2112

2213
## Example
2314

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

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,9 @@ The function uses the `fast-blurhash` library for decoding and generates a compa
1515

1616
## Type Declarations
1717

18-
```ts
19-
interface BlurHashOptions {
20-
/**
21-
* Aspect ratio (width / height) of the BlurHash image to be decoded.
22-
*
23-
* @default 1 (square aspect ratio)
24-
*/
25-
ratio?: number
26-
27-
/**
28-
* The size of the longer edge (width or height) of the BlurHash image to be
29-
* decoded, depending on the aspect ratio.
30-
*
31-
* @default 32
32-
*/
33-
size?: number
34-
}
35-
36-
function createPngDataUri(
37-
hash: string,
38-
options?: BlurhashOptions
39-
): string
40-
```
18+
<<< @/../packages/core/src/blurhash.ts#BlurHashOptions{ts}
19+
20+
<<< @/../packages/core/src/blurhash.ts#createPngDataUri{ts}
4121

4222
## Example
4323

docs/api/create-placeholder-from-hash.md

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,7 @@ Generates a PNG data URI placeholder from a BlurHash or ThumbHash string. This f
44

55
## Type Declarations
66

7-
```ts
8-
function createPlaceholderFromHash(options?: {
9-
/** If present, hash and ratio are extracted from element's data attributes */
10-
image?: HTMLImageElement
11-
/** Hash string to decode */
12-
hash?: string
13-
/** Type of hash algorithm */
14-
hashType?: 'blurhash' | 'thumbhash'
15-
/** Size of longer edge for BlurHash decoding (ignored for ThumbHash) */
16-
size?: number
17-
/** Aspect ratio (width/height) for BlurHash */
18-
ratio?: number
19-
}): string | undefined
20-
```
7+
<<< @/../packages/core/src/lazyLoad.ts#createPlaceholderFromHash{ts}
218

229
## Parameters
2310

docs/api/lazy-load.md

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -36,65 +36,6 @@ cleanup()
3636

3737
## Type Declarations
3838

39-
```ts
40-
interface UnLazyLoadOptions {
41-
/**
42-
* Whether to generate a blurry placeholder from a [BlurHash](https://blurha.sh)
43-
* or [ThumbHash](https://github.com/evanw/thumbhash) string. The placeholder
44-
* image will be inlined as a data URI in the `src` attribute.
45-
*
46-
* @remarks
47-
* If this option is set to `true`, the `data-blurhash` or `data-thumbhash`
48-
* attributes will be used for the hash string. If you pass a single element
49-
* as the `selectorsOrElements` argument, you can also pass a string to this
50-
* option to override the hash string.
51-
*/
52-
hash?: string | boolean
53-
54-
/**
55-
* Specify the hash type to use for generating the blurry placeholder.
56-
*
57-
* @remarks
58-
* This option is ignored if the `hash` option is set to a boolean value.
59-
* In these cases, the `data-blurhash` or `data-thumbhash` attributes will
60-
* be used to determine the hash type.
61-
*
62-
* @default 'blurhash'
63-
*/
64-
hashType?: 'blurhash' | 'thumbhash'
65-
66-
/**
67-
* The size of the longer edge (width or height) of the BlurHash image to be
68-
* decoded, depending on the aspect ratio.
69-
*
70-
* @remarks
71-
* This option is ignored if the `hashType` option is set to `thumbhash`.
72-
*
73-
* @default 32
74-
*/
75-
placeholderSize?: number
76-
77-
/**
78-
* Whether to update the `sizes` attribute on resize events with the current image width.
79-
*
80-
* @default false
81-
*/
82-
updateSizesOnResize?: boolean
39+
<<< @/../packages/core/src/types.ts#UnLazyLoadOptions{ts}
8340

84-
/**
85-
* A callback function to run when an image is loaded.
86-
*/
87-
onImageLoad?: (image: HTMLImageElement) => void
88-
}
89-
90-
function lazyLoad<T extends HTMLImageElement>(
91-
/**
92-
* A CSS selector, a DOM element, a list of DOM elements, or an array of DOM elements to lazy-load.
93-
*
94-
* @default 'img[loading="lazy"]'
95-
*/
96-
selectorsOrElements?: string | T | NodeListOf<T> | T[],
97-
98-
options?: UnLazyLoadOptions
99-
): () => void
100-
```
41+
<<< @/../packages/core/src/lazyLoad.ts#lazyLoad{ts}

docs/api/load-image.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,4 @@ The function performs the following operations:
1212

1313
## Type Declarations
1414

15-
```ts
16-
function loadImage(
17-
image: HTMLImageElement,
18-
onImageLoad?: (image: HTMLImageElement) => void
19-
): void
20-
```
15+
<<< @/../packages/core/src/lazyLoad.ts#loadImage{ts}

docs/integrations/nuxt.md

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,7 @@ export default defineNuxtConfig({
3333

3434
The `@unlazy/nuxt` module accepts the following options:
3535

36-
```ts
37-
export interface ModuleOptions {
38-
/**
39-
* Whether to generate the blurry placeholder on the server-side if a BlurHash
40-
* or ThumbHash is provided via the `blurhash`, respectively `thumbhash` prop.
41-
*
42-
* @default true
43-
*/
44-
ssr?: boolean
45-
46-
/**
47-
* The size of the longer edge (width or height) of the BlurHash image to be
48-
* decoded, depending on the aspect ratio.
49-
*
50-
* @remarks
51-
* This option is ignored if the a ThumbHash is used.
52-
*
53-
* @default 32
54-
*/
55-
placeholderSize?: number
56-
}
57-
```
36+
<<< @/../packages/nuxt/src/module.ts#ModuleOptions{ts}
5837

5938
Adapt the module options to your needs:
6039

packages/core/src/blurhash.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { decodeBlurHash } from 'fast-blurhash'
22
import { DEFAULT_PLACEHOLDER_SIZE } from './constants'
33
import { rgbaToDataUri } from './utils/dataUri'
44

5+
// #region BlurHashOptions
56
export interface BlurHashOptions {
67
/**
78
* Aspect ratio (width / height) of the BlurHash image to be decoded.
@@ -18,20 +19,27 @@ export interface BlurHashOptions {
1819
*/
1920
size?: number
2021
}
22+
// #endregion BlurHashOptions
2123

24+
// #region createPngDataUri
25+
export function createPngDataUri(
26+
hash: string,
27+
options?: BlurHashOptions
28+
): string
29+
// #endregion createPngDataUri
2230
export function createPngDataUri(
2331
hash: string,
2432
{
2533
ratio = 1,
2634
size = DEFAULT_PLACEHOLDER_SIZE,
2735
}: BlurHashOptions = {},
28-
) {
36+
): string {
2937
const { width, height } = getAspectRatioDimensions(ratio, size)
3038
const rgba = decodeBlurHash(hash, width, height)
3139
return rgbaToDataUri(width, height, rgba)
3240
}
3341

34-
function getAspectRatioDimensions(ratio: number, size: number) {
42+
function getAspectRatioDimensions(ratio: number, size: number): { width: number, height: number } {
3543
const isLandscapeOrSquare = ratio >= 1
3644

3745
return {

packages/core/src/lazyLoad.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ import { DEFAULT_PLACEHOLDER_SIZE } from './constants'
44
import { createPngDataUri as createPngDataUriFromThumbHash } from './thumbhash'
55
import { createIndexedImagePlaceholder, debounce, isCrawler, isLazyLoadingSupported, toElementArray } from './utils'
66

7+
// #region lazyLoad
78
export function lazyLoad<T extends HTMLImageElement>(
89
/**
910
* A CSS selector, a DOM element, a list of DOM elements, or an array of DOM elements to lazy-load.
1011
*
1112
* @default 'img[loading="lazy"]'
1213
*/
14+
selectorsOrElements?: string | T | NodeListOf<T> | T[],
15+
options?: UnLazyLoadOptions
16+
): () => void
17+
// #endregion lazyLoad
18+
export function lazyLoad<T extends HTMLImageElement>(
1319
selectorsOrElements: string | T | NodeListOf<T> | T[] = 'img[loading="lazy"]',
1420
{
1521
hash = true,
@@ -18,7 +24,7 @@ export function lazyLoad<T extends HTMLImageElement>(
1824
updateSizesOnResize = false,
1925
onImageLoad,
2026
}: UnLazyLoadOptions = {},
21-
) {
27+
): () => void {
2228
const cleanupHandlers = new Set<() => void>()
2329

2430
for (const [index, image] of toElementArray<T>(selectorsOrElements).entries()) {
@@ -89,22 +95,33 @@ export function lazyLoad<T extends HTMLImageElement>(
8995
}
9096
}
9197

98+
// #region autoSizes
9299
export function autoSizes<T extends HTMLImageElement | HTMLSourceElement>(
93100
/**
94101
* A CSS selector, a DOM element, a list of DOM elements, or an array of DOM elements to calculate the `sizes` attribute for.
95102
*
96103
* @default 'img[data-sizes="auto"], source[data-sizes="auto"]'
97104
*/
105+
selectorsOrElements?: string | T | NodeListOf<T> | T[]
106+
): void
107+
// #endregion autoSizes
108+
export function autoSizes<T extends HTMLImageElement | HTMLSourceElement>(
98109
selectorsOrElements: string | T | NodeListOf<T> | T[] = 'img[data-sizes="auto"], source[data-sizes="auto"]',
99-
) {
110+
): void {
100111
for (const image of toElementArray<T>(selectorsOrElements))
101112
updateSizesAttribute(image)
102113
}
103114

115+
// #region loadImage
116+
export function loadImage(
117+
image: HTMLImageElement,
118+
onImageLoad?: (image: HTMLImageElement) => void
119+
): void
120+
// #endregion loadImage
104121
export function loadImage(
105122
image: HTMLImageElement,
106123
onImageLoad?: (image: HTMLImageElement) => void,
107-
) {
124+
): void {
108125
// Skip preloading its `data-src` or `data-srcset` to avoid unnecessary requests
109126
if (isDescendantOfPicture(image)) {
110127
updatePictureSources(image)
@@ -139,6 +156,18 @@ export function loadImage(
139156
}, { once: true })
140157
}
141158

159+
// #region createPlaceholderFromHash
160+
export function createPlaceholderFromHash(options?: {
161+
/** 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. */
162+
image?: HTMLImageElement
163+
hash?: string
164+
hashType?: 'blurhash' | 'thumbhash'
165+
/** @default 32 */
166+
size?: number
167+
/** Will be calculated from the image's actual dimensions if image is provided and ratio is not. */
168+
ratio?: number
169+
}): string | undefined
170+
// #endregion createPlaceholderFromHash
142171
export function createPlaceholderFromHash(
143172
{
144173
image,
@@ -156,7 +185,7 @@ export function createPlaceholderFromHash(
156185
/** Will be calculated from the image's actual dimensions if image is provided and ratio is not. */
157186
ratio?: number
158187
} = {},
159-
) {
188+
): string | undefined {
160189
if (image && !hash) {
161190
const { blurhash, thumbhash } = image.dataset
162191
hash = thumbhash || blurhash
@@ -195,7 +224,7 @@ function updateSizesAttribute(
195224
updateOnResize?: boolean
196225
processSourceElements?: boolean
197226
},
198-
) {
227+
): (() => void) | undefined {
199228
if (element.dataset.sizes !== 'auto')
200229
return
201230

@@ -252,7 +281,7 @@ function updatePictureSources(image: HTMLImageElement) {
252281
}
253282
}
254283

255-
function getOffsetWidth(element: HTMLElement | HTMLSourceElement) {
284+
function getOffsetWidth(element: HTMLElement | HTMLSourceElement): number | undefined {
256285
return element instanceof HTMLSourceElement
257286
? element.parentElement?.getElementsByTagName('img')[0]?.offsetWidth
258287
: element.offsetWidth

packages/core/src/thumbhash.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import { thumbHashToRGBA } from 'thumbhash'
22
import { rgbaToDataUri } from './utils/dataUri'
33

4-
export function createPngDataUri(hash: string) {
4+
export function createPngDataUri(hash: string): string {
55
const hashArray = base64ToUint8Array(hash)
66
const { w, h, rgba } = thumbHashToRGBA(hashArray)
77

88
return rgbaToDataUri(w, h, rgba)
99
}
1010

11-
function base64ToUint8Array(base64String: string) {
11+
function base64ToUint8Array(base64String: string): Uint8Array<ArrayBuffer> {
1212
return Uint8Array.from(
1313
globalThis.atob(base64UrlToBase64(base64String)),
1414
x => x.charCodeAt(0),
1515
)
1616
}
1717

18-
function base64UrlToBase64(base64Url: string) {
18+
function base64UrlToBase64(base64Url: string): string {
1919
return base64Url.replaceAll('-', '+').replaceAll('_', '/')
2020
}

packages/core/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// #region UnLazyLoadOptions
12
export interface UnLazyLoadOptions {
23
/**
34
* Whether to generate a blurry placeholder from a [BlurHash](https://blurha.sh)
@@ -47,3 +48,4 @@ export interface UnLazyLoadOptions {
4748
*/
4849
onImageLoad?: (image: HTMLImageElement) => void
4950
}
51+
// #endregion UnLazyLoadOptions

0 commit comments

Comments
 (0)