@hoangyell/gallery 1.2.0 → 1.2.1

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/README.md CHANGED
@@ -27,7 +27,7 @@
27
27
  ## Features
28
28
 
29
29
  - **Pyramid layout** — distribute images across rows with `rows` prop
30
- - **Two fit modes** — `contain` (default, natural aspect, for diagrams, code screenshots, and tall portraits) or `cover` (uniform photo grid)
30
+ - **Adaptive fit modes** - auto-detects `contain` for single images (natural aspect, no cropping) and `cover` for multi-image galleries (clean uniform grid)
31
31
  - **Lightbox & pinch-to-zoom** — click to open, smooth multi-touch pinch-to-zoom & pan, double-tap zoom, mouse wheel, keyboard navigation (`+`, `-`, `0`, arrows, ESC)
32
32
  - **Corner rounding** — only the four outer corners of each group are rounded
33
33
  - **Hover zoom** — image scales inside its cell without changing layout
@@ -80,7 +80,7 @@ import { Gallery } from "@hoangyell/gallery";
80
80
  | `rows` | `number` | auto (~3 per row) | Number of visual rows |
81
81
  | `orientation` | `"landscape" \| "portrait"` | `"landscape"` | Item aspect ratio hint |
82
82
  | `scale` | `number` | `1` | Gallery width scale (0–1) |
83
- | `fit` | `ObjectFit` | `"contain"` | Default CSS object-fit — see Fit modes below |
83
+ | `fit` | `ObjectFit` | auto (`"contain"` for 1 item, `"cover"` for 2+) | CSS object-fit: see Fit modes below |
84
84
  | `zoom` | `boolean` | `true` | Enable pinch-to-zoom, pan, double-tap, and wheel zoom |
85
85
  | `zoomControls` | `boolean` | `false` | Show visible zoom in / zoom out / reset buttons in lightbox |
86
86
  | `class` | `string` | — | Extra wrapper class |
@@ -88,12 +88,11 @@ import { Gallery } from "@hoangyell/gallery";
88
88
  ## Fit modes
89
89
 
90
90
  The `fit` prop controls **both** the image's `object-fit` **and** the
91
- container's aspect ratio. Pick the right one for your content:
91
+ container's aspect ratio.
92
92
 
93
- | Mode | What you get | When to use |
94
- |---|---|---|
95
- | `"contain"` (default) | Container hugs each image's natural aspect ratio. No cropping. | Diagrams, charts, screenshots, comics, memes, portraits. |
96
- | `"cover"` | Uniform 4:3 grid (1:1 on mobile). Images are cropped to fill. | Photo galleries — travel, products, faces. The Pinterest aesthetic. |
93
+ - **Auto-adaptive (default)**: single images auto-default to `"contain"` so diagrams, screenshots, memes, and tall portraits render at full natural aspect ratio without cropping. Multi-image sets auto-default to `"cover"` so rows stay perfectly uniform without jagged heights.
94
+ - `"contain"`: Container hugs each image's natural aspect ratio. No cropping. Ideal for standalone diagrams, charts, screenshots, comics, memes, and portraits.
95
+ - `"cover"`: Uniform 4:3 grid (1:1 on mobile). Images are cropped to fill cells neatly. Ideal for multi-photo sets.
97
96
 
98
97
  Set it at the gallery level:
99
98
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hoangyell/gallery",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Astro image gallery with lightbox, pyramid multi-row layout, and accessible controls.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/Gallery.astro CHANGED
@@ -27,7 +27,7 @@ export interface Props {
27
27
  orientation?: "landscape" | "portrait";
28
28
  /** Scale factor to reduce gallery width (1 = full). */
29
29
  scale?: number;
30
- /** Global object-fit for all items unless overridden per-item. Defaults to "contain". */
30
+ /** Global object-fit for all items unless overridden per-item. Auto: "contain" for 1 item, "cover" for multiple items. */
31
31
  fit?: ObjectFit;
32
32
  /** Enable zoom gestures (pinch, double-tap, pan, wheel) in lightbox. Defaults to true. */
33
33
  zoom?: boolean;
@@ -40,7 +40,7 @@ const {
40
40
  rows,
41
41
  orientation = "landscape",
42
42
  scale = 1,
43
- fit = "contain",
43
+ fit: explicitFit,
44
44
  zoom = true,
45
45
  zoomControls = false,
46
46
  } = Astro.props;
@@ -59,16 +59,23 @@ function altFromUrl(url: string): string {
59
59
  }
60
60
  }
61
61
 
62
+ const validItemCount = rawItems.filter((item) => {
63
+ if (typeof item === "string") return item.trim() !== "";
64
+ return Boolean(item?.src);
65
+ }).length;
66
+ const defaultFit: ObjectFit = validItemCount <= 1 ? "contain" : "cover";
67
+ const resolvedFit: ObjectFit = explicitFit || defaultFit;
68
+
62
69
  const galleryItems: GalleryItem[] = rawItems
63
70
  .map((item, i) => {
64
71
  if (typeof item === "string") {
65
72
  return {
66
73
  src: item,
67
74
  alt: rawAlts[i] || altFromUrl(item),
68
- fit: String(fit),
75
+ fit: String(resolvedFit),
69
76
  };
70
77
  }
71
- const itemFit = item?.fit || fit;
78
+ const itemFit = item?.fit || resolvedFit;
72
79
  return {
73
80
  src: item?.src || "",
74
81
  alt: item?.alt || rawAlts[i] || altFromUrl(item?.src || ""),
@@ -106,7 +113,7 @@ if (resolvedRows <= 1 || galleryItems.length <= 1) {
106
113
  class={`hy-gallery ${className}`}
107
114
  data-hy-gallery
108
115
  data-orientation={orientation}
109
- data-fit={fit}
116
+ data-fit={resolvedFit}
110
117
  data-zoom={zoom ? "true" : "false"}
111
118
  data-zoom-controls={zoomControls ? "true" : "false"}
112
119
  style={scale !== 1 ? `width: ${scale * 100}%; margin: 2rem auto;` : undefined}
@@ -277,7 +284,7 @@ if (resolvedRows <= 1 || galleryItems.length <= 1) {
277
284
  Drops the forced 4:3 / 1:1 container so diagrams, charts, screenshots,
278
285
  and tall portraits render fully without cropping or letterboxing.
279
286
  Applied either at the wrapper (global) or per-item via data-fit. */
280
- .hy-gallery[data-fit="contain"] .hy-gallery-item,
287
+ .hy-gallery[data-fit="contain"] .hy-gallery-item:not([data-fit="cover"]),
281
288
  .hy-gallery .hy-gallery-item[data-fit="contain"] {
282
289
  aspect-ratio: auto;
283
290
  background: transparent;
@@ -301,7 +308,7 @@ if (resolvedRows <= 1 || galleryItems.length <= 1) {
301
308
 
302
309
  /* In contain mode the image is the primary content — let it drive height
303
310
  instead of being absolutely positioned inside a fixed-aspect box. */
304
- .hy-gallery[data-fit="contain"] .hy-gallery-img,
311
+ .hy-gallery[data-fit="contain"] .hy-gallery-item:not([data-fit="cover"]) .hy-gallery-img,
305
312
  .hy-gallery .hy-gallery-item[data-fit="contain"] .hy-gallery-img {
306
313
  position: static;
307
314
  width: 100%;