@hoangyell/gallery 1.2.0 → 1.2.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/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.2",
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
@@ -12,6 +12,8 @@ export interface GalleryItemInput {
12
12
  src: string;
13
13
  alt?: string;
14
14
  fit?: ObjectFit;
15
+ width?: number;
16
+ height?: number;
15
17
  }
16
18
 
17
19
  export interface Props {
@@ -27,7 +29,7 @@ export interface Props {
27
29
  orientation?: "landscape" | "portrait";
28
30
  /** Scale factor to reduce gallery width (1 = full). */
29
31
  scale?: number;
30
- /** Global object-fit for all items unless overridden per-item. Defaults to "contain". */
32
+ /** Global object-fit for all items unless overridden per-item. Auto: "contain" for 1 item, "cover" for multiple items. */
31
33
  fit?: ObjectFit;
32
34
  /** Enable zoom gestures (pinch, double-tap, pan, wheel) in lightbox. Defaults to true. */
33
35
  zoom?: boolean;
@@ -40,7 +42,7 @@ const {
40
42
  rows,
41
43
  orientation = "landscape",
42
44
  scale = 1,
43
- fit = "contain",
45
+ fit: explicitFit,
44
46
  zoom = true,
45
47
  zoomControls = false,
46
48
  } = Astro.props;
@@ -48,7 +50,23 @@ const {
48
50
  const rawItems = Array.isArray(Astro.props.items) ? Astro.props.items : [];
49
51
  const rawAlts = Array.isArray(Astro.props.alts) ? Astro.props.alts : [];
50
52
 
51
- type GalleryItem = { src: string; alt: string; fit: string };
53
+ type GalleryItem = {
54
+ src: string;
55
+ alt: string;
56
+ fit: string;
57
+ width: number;
58
+ height: number;
59
+ };
60
+
61
+ function defaultDimensions(
62
+ fitMode: ObjectFit,
63
+ orient: "landscape" | "portrait",
64
+ ): { width: number; height: number } {
65
+ if (orient === "portrait") return { width: 900, height: 1200 };
66
+ return fitMode === "contain"
67
+ ? { width: 1600, height: 900 }
68
+ : { width: 1200, height: 900 };
69
+ }
52
70
 
53
71
  function altFromUrl(url: string): string {
54
72
  try {
@@ -59,20 +77,32 @@ function altFromUrl(url: string): string {
59
77
  }
60
78
  }
61
79
 
80
+ const validItemCount = rawItems.filter((item) => {
81
+ if (typeof item === "string") return item.trim() !== "";
82
+ return Boolean(item?.src);
83
+ }).length;
84
+ const defaultFit: ObjectFit = validItemCount <= 1 ? "contain" : "cover";
85
+ const resolvedFit: ObjectFit = explicitFit || defaultFit;
86
+
62
87
  const galleryItems: GalleryItem[] = rawItems
63
88
  .map((item, i) => {
64
89
  if (typeof item === "string") {
90
+ const dims = defaultDimensions(resolvedFit, orientation);
65
91
  return {
66
92
  src: item,
67
93
  alt: rawAlts[i] || altFromUrl(item),
68
- fit: String(fit),
94
+ fit: String(resolvedFit),
95
+ ...dims,
69
96
  };
70
97
  }
71
- const itemFit = item?.fit || fit;
98
+ const itemFit = item?.fit || resolvedFit;
99
+ const dims = defaultDimensions(itemFit, orientation);
72
100
  return {
73
101
  src: item?.src || "",
74
102
  alt: item?.alt || rawAlts[i] || altFromUrl(item?.src || ""),
75
103
  fit: String(itemFit),
104
+ width: item?.width && item.width > 0 ? item.width : dims.width,
105
+ height: item?.height && item.height > 0 ? item.height : dims.height,
76
106
  };
77
107
  })
78
108
  .filter((item) => item.src !== "");
@@ -106,7 +136,7 @@ if (resolvedRows <= 1 || galleryItems.length <= 1) {
106
136
  class={`hy-gallery ${className}`}
107
137
  data-hy-gallery
108
138
  data-orientation={orientation}
109
- data-fit={fit}
139
+ data-fit={resolvedFit}
110
140
  data-zoom={zoom ? "true" : "false"}
111
141
  data-zoom-controls={zoomControls ? "true" : "false"}
112
142
  style={scale !== 1 ? `width: ${scale * 100}%; margin: 2rem auto;` : undefined}
@@ -141,6 +171,8 @@ if (resolvedRows <= 1 || galleryItems.length <= 1) {
141
171
  <img
142
172
  src={item.src}
143
173
  alt={item.alt}
174
+ width={item.width}
175
+ height={item.height}
144
176
  loading="lazy"
145
177
  decoding="async"
146
178
  class="hy-gallery-img"
@@ -159,6 +191,7 @@ if (resolvedRows <= 1 || galleryItems.length <= 1) {
159
191
  <style>
160
192
  /* ── Custom properties (override on .hy-gallery or :root) ── */
161
193
  .hy-gallery {
194
+ color-scheme: light dark;
162
195
  --hy-gallery-gap: 4px;
163
196
  --hy-gallery-radius: 1rem;
164
197
  --hy-gallery-bg: #f8fafc;
@@ -184,6 +217,7 @@ if (resolvedRows <= 1 || galleryItems.length <= 1) {
184
217
  /* Explicit light theme on any ancestor wins over OS preference.
185
218
  Supports Tailwind-style `.light` class and `[data-theme="light"]`. */
186
219
  :where(.light, [data-theme="light"]) .hy-gallery {
220
+ color-scheme: light;
187
221
  --hy-gallery-bg: #f8fafc;
188
222
  --hy-gallery-overlay-bg: rgba(255, 255, 255, 0.7);
189
223
  --hy-gallery-accent: #2563eb;
@@ -195,6 +229,7 @@ if (resolvedRows <= 1 || galleryItems.length <= 1) {
195
229
  /* Explicit dark theme on any ancestor wins over OS preference.
196
230
  Supports Tailwind-style `.dark` class and `[data-theme="dark"]`. */
197
231
  :where(.dark, [data-theme="dark"]) .hy-gallery {
232
+ color-scheme: dark;
198
233
  --hy-gallery-bg: #0f172a;
199
234
  --hy-gallery-overlay-bg: rgba(2, 6, 23, 0.8);
200
235
  --hy-gallery-accent: #60a5fa;
@@ -277,7 +312,7 @@ if (resolvedRows <= 1 || galleryItems.length <= 1) {
277
312
  Drops the forced 4:3 / 1:1 container so diagrams, charts, screenshots,
278
313
  and tall portraits render fully without cropping or letterboxing.
279
314
  Applied either at the wrapper (global) or per-item via data-fit. */
280
- .hy-gallery[data-fit="contain"] .hy-gallery-item,
315
+ .hy-gallery[data-fit="contain"] .hy-gallery-item:not([data-fit="cover"]),
281
316
  .hy-gallery .hy-gallery-item[data-fit="contain"] {
282
317
  aspect-ratio: auto;
283
318
  background: transparent;
@@ -301,7 +336,7 @@ if (resolvedRows <= 1 || galleryItems.length <= 1) {
301
336
 
302
337
  /* In contain mode the image is the primary content — let it drive height
303
338
  instead of being absolutely positioned inside a fixed-aspect box. */
304
- .hy-gallery[data-fit="contain"] .hy-gallery-img,
339
+ .hy-gallery[data-fit="contain"] .hy-gallery-item:not([data-fit="cover"]) .hy-gallery-img,
305
340
  .hy-gallery .hy-gallery-item[data-fit="contain"] .hy-gallery-img {
306
341
  position: static;
307
342
  width: 100%;