1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use dioxus::prelude::*;
use crate::icon::{Icon, IconKind};
/// Configurazione responsive delle colonne per la griglia Masonry.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ResponsiveCols {
pub base: u8,
pub sm: u8,
pub md: u8,
pub lg: u8,
pub xl: u8,
}
impl Default for ResponsiveCols {
fn default() -> Self {
Self {
base: 1,
sm: 2,
md: 3,
lg: 4,
xl: 4,
}
}
}
impl ResponsiveCols {
#[must_use]
pub fn fixed(cols: u8) -> Self {
Self {
base: cols,
sm: cols,
md: cols,
lg: cols,
xl: cols,
}
}
#[must_use]
pub fn responsive(base: u8, sm: u8, md: u8, lg: u8, xl: u8) -> Self {
Self { base, sm, md, lg, xl }
}
}
/// Spaziatura tokenizzata tra gli elementi della griglia.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum ComponentSpacing {
None,
Xs,
#[default]
Sm,
Md,
Lg,
Xl,
}
impl ComponentSpacing {
#[must_use]
pub fn to_px(&self) -> u32 {
match self {
Self::None => 0,
Self::Xs => 4,
Self::Sm => 8,
Self::Md => 16,
Self::Lg => 24,
Self::Xl => 32,
}
}
}
/// Singolo elemento multimediale per la galleria `ImageList`.
#[derive(Clone, PartialEq, Debug)]
pub struct MasonryItem {
pub id: String,
pub src: String,
pub title: Option<String>,
pub subtitle: Option<String>,
pub aspect_ratio: Option<f64>,
}
impl MasonryItem {
#[must_use]
pub fn new(id: impl Into<String>, src: impl Into<String>) -> Self {
Self {
id: id.into(),
src: src.into(),
title: None,
subtitle: None,
aspect_ratio: None,
}
}
#[must_use]
pub fn with_title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
#[must_use]
pub fn with_subtitle(mut self, sub: impl Into<String>) -> Self {
self.subtitle = Some(sub.into());
self
}
#[must_use]
pub fn with_aspect_ratio(mut self, ratio: f64) -> Self {
self.aspect_ratio = Some(ratio);
self
}
}
/// Griglia a cascata in stile Pinterest con altezze eterogenee distribuite fluidamente.
#[component]
pub fn MasonryGrid(
#[props(default)] cols: ResponsiveCols,
#[props(default)] gap: ComponentSpacing,
#[props(default)] class: String,
children: Element,
) -> Element {
let gap_px = gap.to_px();
rsx! {
div {
class: "sd-masonry w-full {class}",
style: "column-count: {cols.base}; column-gap: {gap_px}px;",
{children}
}
}
}
/// Galleria di immagini Masonry con anteprima ad alto contrasto e Lightbox modale interattivo.
#[component]
pub fn ImageList(
items: Vec<MasonryItem>,
#[props(default)] cols: ResponsiveCols,
#[props(default)] gap: ComponentSpacing,
#[props(default = true)] with_lightbox: bool,
#[props(default)] class: String,
) -> Element {
let mut selected_index = use_signal(|| None::<usize>);
let total = items.len();
let gap_px = gap.to_px();
rsx! {
div { class: "w-full relative {class}",
div {
class: "sd-masonry w-full",
style: "column-count: {cols.base}; column-gap: {gap_px}px;",
for (idx, item) in items.iter().enumerate() {
{
let src = item.src.clone();
let title = item.title.clone();
let sub = item.subtitle.clone();
rsx! {
div {
key: "{item.id}",
class: "break-inside-avoid mb-[{gap_px}px] group relative rounded-xl overflow-hidden border border-white/[0.08] bg-zinc-900 cursor-pointer shadow-md hover:border-purple-500/40 transition-all",
onclick: move |_| {
if with_lightbox {
selected_index.set(Some(idx));
}
},
img {
src: "{src}",
alt: "{title.clone().unwrap_or_default()}",
class: "w-full h-auto object-cover group-hover:scale-105 transition-transform duration-300 block",
loading: "lazy",
}
if title.is_some() || sub.is_some() {
div { class: "absolute inset-x-0 bottom-0 p-3 bg-gradient-to-t from-black/90 via-black/50 to-transparent opacity-0 group-hover:opacity-100 transition-opacity flex flex-col justify-end",
if let Some(t) = &title {
span { class: "text-sm font-semibold text-white truncate", "{t}" }
}
if let Some(s) = &sub {
span { class: "text-xs text-zinc-300 truncate", "{s}" }
}
}
}
}
}
}
}
}
// Lightbox Modal
if let Some(idx) = selected_index() {
if let Some(item) = items.get(idx) {
{
let cur_src = item.src.clone();
let cur_title = item.title.clone();
let cur_sub = item.subtitle.clone();
rsx! {
div {
class: "fixed inset-0 z-[1000] bg-black/95 backdrop-blur-xl flex flex-col items-center justify-center p-4 select-none",
onclick: move |_| selected_index.set(None),
// Barra superiore
div {
class: "absolute top-4 inset-x-4 flex items-center justify-between text-zinc-300 z-10",
onclick: move |e| e.stop_propagation(),
div { class: "flex items-center gap-2",
span { class: "font-mono text-xs px-2.5 py-1 rounded bg-white/10",
"{idx + 1} / {total}"
}
if let Some(t) = &cur_title {
span { class: "text-sm font-semibold text-white", "{t}" }
}
}
button {
r#type: "button",
class: "p-2 rounded-lg bg-white/10 hover:bg-white/20 text-white transition-colors cursor-pointer",
onclick: move |_| selected_index.set(None),
Icon { kind: IconKind::X, size: 20 }
}
}
// Pulsanti navigazione
button {
r#type: "button",
class: "absolute left-4 top-1/2 -translate-y-1/2 p-3 rounded-full bg-white/10 hover:bg-purple-600/80 text-white transition-all cursor-pointer z-10",
onclick: move |e| {
e.stop_propagation();
if let Some(i) = selected_index() {
if i > 0 {
selected_index.set(Some(i - 1));
} else if total > 0 {
selected_index.set(Some(total - 1));
}
}
},
Icon { kind: IconKind::ChevronLeft, size: 24 }
}
button {
r#type: "button",
class: "absolute right-4 top-1/2 -translate-y-1/2 p-3 rounded-full bg-white/10 hover:bg-purple-600/80 text-white transition-all cursor-pointer z-10",
onclick: move |e| {
e.stop_propagation();
if let Some(i) = selected_index() {
if i + 1 < total {
selected_index.set(Some(i + 1));
} else {
selected_index.set(Some(0));
}
}
},
Icon { kind: IconKind::ChevronRight, size: 24 }
}
// Immagine principale
div {
class: "max-w-5xl max-h-[80vh] flex flex-col items-center justify-center overflow-hidden rounded-xl border border-white/10 shadow-2xl",
onclick: move |e| e.stop_propagation(),
img {
src: "{cur_src}",
alt: "{cur_title.clone().unwrap_or_default()}",
class: "max-w-full max-h-[75vh] object-contain",
}
if let Some(sub_text) = cur_sub {
div { class: "w-full p-3 bg-zinc-950 text-center text-xs text-zinc-300 font-mono border-t border-white/10",
"{sub_text}"
}
}
}
}
}
}
}
}
}
}
}