blinc_cn 0.5.1

Blinc Component Library - shadcn-style themed components built on blinc_layout primitives
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! Card component for content containers
//!
//! A styled container with shadow and border for grouping related content.
//!
//! # Example
//!
//! ```ignore
//! use blinc_cn::prelude::*;
//!
//! // Simple card with content
//! cn::card()
//!     .child(text("Card content"))
//!
//! // Card with structured content using CardHeader and CardFooter
//! cn::card()
//!     .child(cn::card_header().title("Card Title").description("Description"))
//!     .child(text("Main content goes here"))
//!     .child(cn::card_footer().child(cn::button("Action")))
//!
//! // Card with custom styling (via Deref to Div)
//! cn::card()
//!     .shadow_lg()  // Larger shadow
//!     .p(32.0)      // Custom padding
//!     .child(text("Custom styled card"))
//! ```

use std::ops::{Deref, DerefMut};

use blinc_layout::div::{Div, ElementBuilder, ElementTypeId};
use blinc_layout::prelude::*;
use blinc_theme::{ColorToken, SpacingToken, ThemeState};

/// Card component for content containers
///
/// Implements `Deref` to `Div` for full customization.
pub struct Card {
    inner: Div,
}

impl Card {
    /// Create a new empty card
    pub fn new() -> Self {
        // All visual props from CSS: .cn-card { background, border, border-radius, padding, gap }
        let inner = div().class("cn-card").shadow_sm().flex_col().items_start();

        Self { inner }
    }

    /// Add content to the card body
    pub fn child(mut self, content: impl ElementBuilder + 'static) -> Self {
        self.inner = self.inner.child(content);
        self
    }

    // Forwarding methods for common Div operations

    /// Set width
    pub fn w(mut self, width: f32) -> Self {
        self.inner = self.inner.w(width);
        self
    }

    /// Set height
    pub fn h(mut self, height: f32) -> Self {
        self.inner = self.inner.h(height);
        self
    }

    /// Set full width
    pub fn w_full(mut self) -> Self {
        self.inner = self.inner.w_full();
        self
    }

    /// Set padding on all sides
    pub fn p(mut self, padding: f32) -> Self {
        self.inner = self.inner.p(padding);
        self
    }

    /// Set horizontal padding
    pub fn px(mut self, padding: f32) -> Self {
        self.inner = self.inner.px(padding);
        self
    }

    /// Set vertical padding
    pub fn py(mut self, padding: f32) -> Self {
        self.inner = self.inner.py(padding);
        self
    }

    /// Set margin on all sides
    pub fn m(mut self, margin: f32) -> Self {
        self.inner = self.inner.m(margin);
        self
    }

    /// Apply large shadow
    pub fn shadow_lg(mut self) -> Self {
        self.inner = self.inner.shadow_lg();
        self
    }

    /// Apply medium shadow
    pub fn shadow_md(mut self) -> Self {
        self.inner = self.inner.shadow_md();
        self
    }

    /// Set background color
    pub fn bg(mut self, color: blinc_core::Color) -> Self {
        self.inner = self.inner.bg(color);
        self
    }

    /// Add a CSS class for selector matching
    pub fn class(mut self, name: impl Into<String>) -> Self {
        self.inner = self.inner.class(name);
        self
    }

    /// Set the element ID for CSS selector matching
    pub fn id(mut self, id: &str) -> Self {
        self.inner = self.inner.id(id);
        self
    }
}

impl Default for Card {
    fn default() -> Self {
        Self::new()
    }
}

impl Deref for Card {
    type Target = Div;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl DerefMut for Card {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

impl ElementBuilder for Card {
    fn build(&self, tree: &mut blinc_layout::tree::LayoutTree) -> blinc_layout::tree::LayoutNodeId {
        self.inner.build(tree)
    }

    fn render_props(&self) -> blinc_layout::element::RenderProps {
        self.inner.render_props()
    }

    fn children_builders(&self) -> &[Box<dyn ElementBuilder>] {
        self.inner.children_builders()
    }

    fn event_handlers(&self) -> Option<&blinc_layout::event_handler::EventHandlers> {
        ElementBuilder::event_handlers(&self.inner)
    }

    fn layout_style(&self) -> Option<&taffy::Style> {
        ElementBuilder::layout_style(&self.inner)
    }

    fn element_type_id(&self) -> ElementTypeId {
        ElementBuilder::element_type_id(&self.inner)
    }

    fn element_classes(&self) -> &[String] {
        self.inner.element_classes()
    }

    fn element_id(&self) -> Option<&str> {
        self.inner.element_id()
    }
}

/// Create an empty card
///
/// # Example
///
/// ```ignore
/// use blinc_cn::prelude::*;
///
/// cn::card()
///     .child(text("Content"))
/// ```
pub fn card() -> Card {
    Card::new()
}

// ============================================================================
// Card subcomponents for structured content
// ============================================================================

/// Card header section
pub struct CardHeader {
    inner: Div,
}

impl CardHeader {
    /// Create a new card header
    pub fn new() -> Self {
        let theme = ThemeState::get();
        let gap = theme.spacing_value(SpacingToken::Space1_5); // 6px
        let inner = div()
            .class("cn-card-header")
            .flex_col()
            .items_start()
            .w_full()
            .gap_px(gap);

        Self { inner }
    }

    /// Add a title
    pub fn title(mut self, title: impl Into<String>) -> Self {
        let theme = ThemeState::get();
        self.inner = self.inner.child(
            text(title)
                .size(18.0)
                .semibold()
                .color(theme.color(ColorToken::TextPrimary)),
        );
        self
    }

    /// Add a description
    pub fn description(mut self, desc: impl Into<String>) -> Self {
        let theme = ThemeState::get();
        self.inner = self.inner.child(
            text(desc)
                .size(14.0)
                .color(theme.color(ColorToken::TextSecondary)),
        );
        self
    }
}

impl Default for CardHeader {
    fn default() -> Self {
        Self::new()
    }
}

impl Deref for CardHeader {
    type Target = Div;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl DerefMut for CardHeader {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

impl ElementBuilder for CardHeader {
    fn build(&self, tree: &mut blinc_layout::tree::LayoutTree) -> blinc_layout::tree::LayoutNodeId {
        self.inner.build(tree)
    }

    fn render_props(&self) -> blinc_layout::element::RenderProps {
        self.inner.render_props()
    }

    fn children_builders(&self) -> &[Box<dyn ElementBuilder>] {
        self.inner.children_builders()
    }

    fn event_handlers(&self) -> Option<&blinc_layout::event_handler::EventHandlers> {
        ElementBuilder::event_handlers(&self.inner)
    }

    fn layout_style(&self) -> Option<&taffy::Style> {
        ElementBuilder::layout_style(&self.inner)
    }

    fn element_type_id(&self) -> ElementTypeId {
        ElementBuilder::element_type_id(&self.inner)
    }

    fn element_classes(&self) -> &[String] {
        self.inner.element_classes()
    }

    fn element_id(&self) -> Option<&str> {
        self.inner.element_id()
    }
}

/// Create a card header
pub fn card_header() -> CardHeader {
    CardHeader::new()
}

/// Card content section - grows to fill available space
pub struct CardContent {
    inner: Div,
}

impl CardContent {
    /// Create a new card content section
    pub fn new() -> Self {
        let inner = div()
            .flex_col()
            .flex_1() // Grow to fill available space
            .w_full();

        Self { inner }
    }

    /// Add a child element
    pub fn child(mut self, content: impl ElementBuilder + 'static) -> Self {
        self.inner = self.inner.child(content);
        self
    }
}

impl Default for CardContent {
    fn default() -> Self {
        Self::new()
    }
}

impl Deref for CardContent {
    type Target = Div;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl DerefMut for CardContent {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

impl ElementBuilder for CardContent {
    fn build(&self, tree: &mut blinc_layout::tree::LayoutTree) -> blinc_layout::tree::LayoutNodeId {
        self.inner.build(tree)
    }

    fn render_props(&self) -> blinc_layout::element::RenderProps {
        self.inner.render_props()
    }

    fn children_builders(&self) -> &[Box<dyn ElementBuilder>] {
        self.inner.children_builders()
    }

    fn event_handlers(&self) -> Option<&blinc_layout::event_handler::EventHandlers> {
        ElementBuilder::event_handlers(&self.inner)
    }

    fn layout_style(&self) -> Option<&taffy::Style> {
        ElementBuilder::layout_style(&self.inner)
    }

    fn element_type_id(&self) -> ElementTypeId {
        ElementBuilder::element_type_id(&self.inner)
    }

    fn element_classes(&self) -> &[String] {
        self.inner.element_classes()
    }

    fn element_id(&self) -> Option<&str> {
        self.inner.element_id()
    }
}

/// Create a card content section
pub fn card_content() -> CardContent {
    CardContent::new()
}

/// Card footer section
pub struct CardFooter {
    inner: Div,
}

impl CardFooter {
    /// Create a new card footer
    pub fn new() -> Self {
        let theme = ThemeState::get();
        let gap = theme.spacing_value(SpacingToken::Space2); // 8px
        let inner = div()
            .class("cn-card-footer")
            .flex_row()
            .w_full()
            .gap_px(gap)
            .justify_end();

        Self { inner }
    }

    /// Add a child element
    pub fn child(mut self, content: impl ElementBuilder + 'static) -> Self {
        self.inner = self.inner.child(content);
        self
    }
}

impl Default for CardFooter {
    fn default() -> Self {
        Self::new()
    }
}

impl Deref for CardFooter {
    type Target = Div;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl DerefMut for CardFooter {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

impl ElementBuilder for CardFooter {
    fn build(&self, tree: &mut blinc_layout::tree::LayoutTree) -> blinc_layout::tree::LayoutNodeId {
        self.inner.build(tree)
    }

    fn render_props(&self) -> blinc_layout::element::RenderProps {
        self.inner.render_props()
    }

    fn children_builders(&self) -> &[Box<dyn ElementBuilder>] {
        self.inner.children_builders()
    }

    fn event_handlers(&self) -> Option<&blinc_layout::event_handler::EventHandlers> {
        ElementBuilder::event_handlers(&self.inner)
    }

    fn layout_style(&self) -> Option<&taffy::Style> {
        ElementBuilder::layout_style(&self.inner)
    }

    fn element_type_id(&self) -> ElementTypeId {
        ElementBuilder::element_type_id(&self.inner)
    }

    fn element_classes(&self) -> &[String] {
        self.inner.element_classes()
    }

    fn element_id(&self) -> Option<&str> {
        self.inner.element_id()
    }
}

/// Create a card footer
pub fn card_footer() -> CardFooter {
    CardFooter::new()
}

#[cfg(test)]
mod tests {
    use super::*;

    fn init_theme() {
        let _ = ThemeState::try_get().unwrap_or_else(|| {
            ThemeState::init_default();
            ThemeState::get()
        });
    }

    #[test]
    fn test_card_default() {
        init_theme();
        let _ = card();
    }

    #[test]
    fn test_card_with_content() {
        init_theme();
        let _ = card().child(text("Content"));
    }

    #[test]
    fn test_card_header() {
        init_theme();
        let _ = card_header().title("Title").description("Description");
    }

    #[test]
    fn test_card_footer() {
        init_theme();
        let _ = card_footer();
    }
}