@massimo-cassandro/minimo 1.14.1 → 1.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@massimo-cassandro/minimo",
3
- "version": "1.14.1",
3
+ "version": "1.14.2",
4
4
  "description": "Light css framework & utilities",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -54,7 +54,7 @@ import { classnames } from './classnames.js';
54
54
  *
55
55
  * @param {Object} args
56
56
  * @param {string} args.baseSrc - Image base URL (viewer endpoint); its query params are preserved.
57
- * @param {(number | [number, number])[]} [args.sizes=[250]] - Rendered image sizes, in ascending width order, one per breakpoint plus the default. Each entry is a width (px, height computed with `ratio`) or an explicit `[width, height]` pair (px), allowing a different aspect ratio per breakpoint.
57
+ * @param {(number | [number, number])[]} [args.sizes=[250]] - Rendered image sizes, in ascending width order, one per breakpoint plus the default (entries beyond `breakpoints.length + 1` are ignored in the `sizes` attribute). Each entry is a width (px, height computed with `ratio`) or an explicit `[width, height]` pair (px), allowing a different aspect ratio per breakpoint.
58
58
  * @param {number} [args.ratio=16/9] - Default aspect ratio (width / height, as in the CSS `aspect-ratio` property) used to compute the height of numeric `sizes` entries.
59
59
  * @param {number} [args.dpr=2] - Maximum pixel density (integer): `srcset` candidates are generated for every density from 1 to `dpr`.
60
60
  * @param {number[]} [args.breakpoints=[]] - `max-width` breakpoints (px) paired with `sizes` (one element less than `sizes`).
@@ -100,6 +100,11 @@ export function buildPictureTag({
100
100
  formats = formats.filter(fmt => fmt !== 'avif');
101
101
  }
102
102
 
103
+ if(sizes.length > breakpoints.length + 1) {
104
+ // eslint-disable-next-line no-console
105
+ console.warn('[buildPictureTag] `sizes` should have one entry more than `breakpoints`: extra entries are ignored in the `sizes` attribute');
106
+ }
107
+
103
108
  // densità generate: da 1 a `dpr`
104
109
  const dprList = Array.from({length: dpr}, (_, i) => i + 1);
105
110
 
@@ -121,17 +126,23 @@ export function buildPictureTag({
121
126
  .sort((a, b) => a[0] - b[0])
122
127
 
123
128
  // attributo `sizes`: ogni size è abbinata al breakpoint corrispondente (max-width),
124
- // l'ultima (senza breakpoint) è il valore di default
129
+ // quella successiva all'ultimo breakpoint è il valore di default
130
+ // (le eventuali entry oltre `breakpoints.length + 1` vengono ignorate)
125
131
  ,sizesAttr = sizePairs
132
+ .slice(0, breakpoints.length + 1)
126
133
  .map(([w], i) => i < breakpoints.length? `(max-width: ${breakpoints[i]}px) ${w}px` : `${w}px`)
127
134
  .join(', ')
128
135
 
129
- // dimensioni intrinseche dell'elemento img (la size più grande)
130
- ,[imgWidth, imgHeight] = sizePairs[sizePairs.length - 1]
136
+ // dimensioni intrinseche dell'elemento img: la size più grande,
137
+ // indipendentemente dall'ordine delle entry
138
+ ,[imgWidth, imgHeight] = sizePairs.reduce((max, pair) => pair[0] > max[0]? pair : max)
139
+
140
+ // larghezza 1x della size mediana (mediana inferiore se le size sono pari),
141
+ // indipendentemente dall'ordine delle entry
142
+ ,medianWidth = sizePairs.map(([w]) => w).sort((a, b) => a - b)[Math.floor((sizePairs.length - 1) / 2)]
131
143
 
132
- // indice del candidato srcset usato come src di default dell'img:
133
- // il candidato 1x della size mediana (mediana inferiore se le size sono pari)
134
- ,defaultSrcIdx = srcsetSizes.findIndex(([w]) => w === sizePairs[Math.floor((sizePairs.length - 1) / 2)][0])
144
+ // indice del candidato srcset usato come src di default dell'img
145
+ ,defaultSrcIdx = srcsetSizes.findIndex(([w]) => w === medianWidth)
135
146
  ;
136
147
 
137
148
  img_params.forEach(([name, value]) => searchParams.set(name, String(value)));