@@ -6,19 +6,50 @@ const DEFAULT_DEVICE_BREAKPOINTS = [640, 750, 828, 1080, 1200, 1920, 2048, 3840]
6
6
const DEFAULT_IMAGE_BREAKPOINTS = [ 16 , 32 , 48 , 64 , 96 , 128 , 256 , 384 ] as const
7
7
8
8
export interface GetImageAttributesOptions extends SrcOptions {
9
- width ?: number // explicit rendered width
10
- sizes ?: string // the HTML sizes value
11
- deviceBreakpoints ?: number [ ] // override default device break‑points
12
- imageBreakpoints ?: number [ ] // override tiny image break‑points
9
+ /**
10
+ * The intended display width (in pixels) of the image on screen.
11
+ * Used for calculating `srcSet` with a pixel-density (DPR) strategy.
12
+ * If omitted, a width-based strategy using breakpoints will be applied.
13
+ */
14
+ width ?: number
15
+
16
+ /**
17
+ * The `sizes` attribute for the image element.
18
+ * Typically used to indicate how the image will scale across different viewport sizes (e.g., "100vw").
19
+ * Presence of `sizes` triggers a width-based `srcSet` strategy.
20
+ */
21
+ sizes ?: string
22
+
23
+ /**
24
+ * An optional custom list of device width breakpoints (in pixels).
25
+ * If not specified, defaults to `[640, 750, 828, 1080, 1200, 1920, 2048, 3840]`.
26
+ * Recommended to align with your target audience's common screen widths.
27
+ */
28
+ deviceBreakpoints ?: number [ ]
29
+
30
+ /**
31
+ * An optional list of custom image breakpoints (in pixels).
32
+ * These are merged with the device breakpoints to compute the final list of candidate widths.
33
+ * Defaults to `[16, 32, 48, 64, 96, 128, 256, 384]`.
34
+ */
35
+ imageBreakpoints ?: number [ ]
13
36
}
14
37
38
+ /**
39
+ * Resulting set of attributes suitable for an HTML `<img>` element.
40
+ * Useful for enabling responsive image loading.
41
+ */
15
42
export interface ResponsiveImageAttributes {
16
43
src : string
17
44
srcSet ?: string
18
45
sizes ?: string
19
46
width ?: number
20
47
}
21
48
49
+ /**
50
+ * Generates a responsive image URL, `srcSet`, and `sizes` attributes
51
+ * based on the input options such as `width`, `sizes`, and breakpoints.
52
+ */
22
53
export function getResponsiveImageAttributes (
23
54
opts : GetImageAttributesOptions
24
55
) : ResponsiveImageAttributes {
@@ -80,7 +111,7 @@ function computeCandidateWidths(params: {
80
111
} ) : { candidates : number [ ] ; descriptorKind : 'w' | 'x' } {
81
112
const { allBreakpoints, deviceBreakpoints, explicitWidth, sizesAttr } = params
82
113
83
- /* --- sizes attribute present ----------------------------------- */
114
+ // Strategy 1: Width-based srcSet (`w`) using viewport `vw` hints
84
115
if ( sizesAttr ) {
85
116
const vwTokens = sizesAttr . match ( / ( ^ | \s ) ( 1 ? \d { 1 , 2 } ) v w / g) || [ ]
86
117
const vwPercents = vwTokens . map ( ( t ) => parseInt ( t , 10 ) )
@@ -93,16 +124,17 @@ function computeCandidateWidths(params: {
93
124
descriptorKind : 'w' ,
94
125
}
95
126
}
96
- /* no vw → give the full break‑point list */
127
+
128
+ // No usable `vw` found: fallback to all breakpoints
97
129
return { candidates : allBreakpoints , descriptorKind : 'w' }
98
130
}
99
131
100
- /* --- no sizes attr ------------------------------------------------ */
132
+ // Strategy 2: Fallback using explicit image width using device breakpoints
101
133
if ( typeof explicitWidth !== 'number' ) {
102
134
return { candidates : deviceBreakpoints , descriptorKind : 'w' }
103
135
}
104
136
105
- /* DPR strategy: 1× & 2× nearest break‑points */
137
+ // Strategy 3: Use 1x and 2x nearest breakpoints for `x` descriptor
106
138
const nearest = ( t : number ) =>
107
139
allBreakpoints . find ( ( n ) => n >= t ) || allBreakpoints [ allBreakpoints . length - 1 ]
108
140
0 commit comments