hikari-components 0.2.2

Core UI components (40+) for the Hikari design system
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
// hi-components/src/feedback/glow.rs
// Unified glow effect component with mouse-following spotlight and acrylic blur
//
// Animation architecture:
// - Uses CSS variables updated via mouse events for position tracking
// - CSS transitions provide smooth interpolation between positions
// - WASI-compatible: single implementation for both browser and server
// - Server environments stub out mouse-related APIs automatically
//
// # CSS Value Type Integration
//
// This component can use the type-safe CSS value system from tairitsu-css-values:
//
// ```rust
// use hikari_components::style_builder::{StyleStringBuilder, CssLength};
//
// // Type-safe approach (recommended for new code):
// let style = StyleStringBuilder::new()
//     .add_var_with_length("glow-x", CssLength::percent(50))
//     .add_var_with_length("glow-y", CssLength::percent(50))
//     .build();
//
// // Traditional string approach (still supported):
// let style = StyleStringBuilder::new()
//     .add_var("glow-x", "50%")
//     .add_var("glow-y", "50%")
//     .build();
// ```

use hikari_palette::classes::{ClassesBuilder, GlowClass};
use tairitsu_vdom::IntoAttrValue;

use crate::prelude::*;
use crate::style_builder::StyleStringBuilder;

#[derive(Clone, Copy, PartialEq, Debug, Default)]
pub enum GlowBlur {
    None,
    Light,
    #[default]
    Medium,
    Heavy,
}

#[derive(Clone, Copy, PartialEq, Debug, Default)]
pub enum GlowColor {
    #[default]
    Ghost,
    Primary,
    Secondary,
    Danger,
    Success,
    Warning,
    Info,
}

#[derive(Clone, Copy, PartialEq, Debug, Default)]
pub enum GlowIntensity {
    Dim,
    #[default]
    Soft,
    Bright,
}

impl std::fmt::Display for GlowBlur {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            GlowBlur::None => write!(f, "none"),
            GlowBlur::Light => write!(f, "light"),
            GlowBlur::Medium => write!(f, "medium"),
            GlowBlur::Heavy => write!(f, "heavy"),
        }
    }
}

impl std::fmt::Display for GlowColor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            GlowColor::Ghost => write!(f, "ghost"),
            GlowColor::Primary => write!(f, "primary"),
            GlowColor::Secondary => write!(f, "secondary"),
            GlowColor::Danger => write!(f, "danger"),
            GlowColor::Success => write!(f, "success"),
            GlowColor::Warning => write!(f, "warning"),
            GlowColor::Info => write!(f, "info"),
        }
    }
}

impl std::fmt::Display for GlowIntensity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            GlowIntensity::Dim => write!(f, "dim"),
            GlowIntensity::Soft => write!(f, "soft"),
            GlowIntensity::Bright => write!(f, "bright"),
        }
    }
}

impl IntoAttrValue for GlowBlur {
    fn into_attr_value(self) -> Option<String> {
        Some(self.to_string())
    }
}

impl IntoAttrValue for GlowColor {
    fn into_attr_value(self) -> Option<String> {
        Some(self.to_string())
    }
}

impl IntoAttrValue for GlowIntensity {
    fn into_attr_value(self) -> Option<String> {
        Some(self.to_string())
    }
}

/// Glow animation presets for continuous animation effects
#[derive(Clone, Copy, PartialEq, Debug, Default)]
pub enum GlowPreset {
    #[default]
    None,
    Pulse,
    Breathe,
    Shimmer,
}

impl std::fmt::Display for GlowPreset {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            GlowPreset::None => write!(f, ""),
            GlowPreset::Pulse => write!(f, "pulse"),
            GlowPreset::Breathe => write!(f, "breathe"),
            GlowPreset::Shimmer => write!(f, "shimmer"),
        }
    }
}

impl IntoAttrValue for GlowPreset {
    fn into_attr_value(self) -> Option<String> {
        if self == GlowPreset::None {
            None
        } else {
            Some(self.to_string())
        }
    }
}

/// Props for the [`Glow`] component, controlling blur, color, intensity, and animation preset.
#[define_props]
pub struct GlowProps {
    pub children: Element,
    pub blur: GlowBlur,
    pub color: GlowColor,
    pub intensity: GlowIntensity,
    pub active_intensity: Option<GlowIntensity>,
    pub preset: GlowPreset,
    pub class: String,
    pub block: bool,
    #[default("inherit".to_string())]
    pub radius: String,
    #[default("100".to_string())]
    pub transition_duration: String,
}

/// Get opacity value for intensity level
fn get_opacity_for_intensity(intensity: GlowIntensity) -> f32 {
    match intensity {
        GlowIntensity::Dim => 0.07,
        GlowIntensity::Soft => 0.15,
        GlowIntensity::Bright => 0.30,
    }
}

/// Animation state for glow effect
#[derive(Clone, Copy, PartialEq, Debug, Default)]
struct GlowState {
    mouse_x: f64,
    mouse_y: f64,
    is_inside: bool,
    interaction_level: f32, // 0 = idle, 0.5 = hover, 1.0 = active
}

/// A mouse-following glow spotlight with acrylic blur, wrapping children in an interactive overlay.
#[component]
pub fn Glow(props: GlowProps) -> Element {
    let blur_class = match props.blur {
        GlowBlur::None => GlowClass::GlowBlurNone,
        GlowBlur::Light => GlowClass::GlowBlurLight,
        GlowBlur::Medium => GlowClass::GlowBlurMedium,
        GlowBlur::Heavy => GlowClass::GlowBlurHeavy,
    };

    let intensity_class = match props.intensity {
        GlowIntensity::Dim => GlowClass::GlowDim,
        GlowIntensity::Soft => GlowClass::GlowSoft,
        GlowIntensity::Bright => GlowClass::GlowBright,
    };

    let glow_classes = ClassesBuilder::new()
        .add_typed(GlowClass::GlowWrapper)
        .add_typed_if(GlowClass::GlowWrapperBlock, props.block)
        .add_typed(blur_class)
        .add_typed(intensity_class)
        .add(&props.class)
        .add(&props.preset.to_string())
        .build();

    let glow_color = match props.color {
        GlowColor::Ghost => "var(--hi-ghost-glow, rgba(128, 128, 128, 0.5))",
        GlowColor::Primary => "var(--hi-glow-button-primary)",
        GlowColor::Secondary => "var(--hi-glow-button-secondary)",
        GlowColor::Danger => "var(--hi-glow-button-danger)",
        GlowColor::Success => "var(--hi-glow-button-success)",
        GlowColor::Warning => "var(--hi-glow-button-warning)",
        GlowColor::Info => "var(--hi-glow-button-info)",
    };

    let base_opacity = get_opacity_for_intensity(props.intensity);
    let active_opacity = props.active_intensity.map(get_opacity_for_intensity);

    // Animation state
    let glow_state = use_signal(GlowState::default);

    // Build initial style using StyleStringBuilder
    let glow_style = use_signal(|| {
        StyleStringBuilder::new()
            .add_var("glow-x", "50%")
            .add_var("glow-y", "50%")
            .add_var("hi-glow-color", glow_color)
            .add_var("glow-opacity", &base_opacity.to_string())
            .add_var("glow-intensity-scale", "0")
            .add_var("glow-radius", &props.radius)
            .build()
    });

    let build_style =
        |interaction_level: f32, opacity: f32, glow_x: &str, glow_y: &str| -> String {
            StyleStringBuilder::new()
                .add_var("glow-x", glow_x)
                .add_var("glow-y", glow_y)
                .add_var("glow-intensity-scale", &interaction_level.to_string())
                .add_var("glow-opacity", &format!("{:.3}", opacity))
                .build()
        };

    fn calc_glow_percent(event: &MouseEvent) -> (String, String) {
        if let Some(target) = event.target {
            let rect =
                tairitsu_vdom::get_bounding_client_rect(tairitsu_vdom::DomHandle::from_raw(target));
            if rect.width > 0.0 && rect.height > 0.0 {
                let px = (event.offset_x as f64 / rect.width * 100.0).clamp(0.0, 100.0);
                let py = (event.offset_y as f64 / rect.height * 100.0).clamp(0.0, 100.0);
                return (format!("{:.1}%", px), format!("{:.1}%", py));
            }
        }
        ("50%".to_string(), "50%".to_string())
    }

    // Clone values for event handlers
    let base_opacity_clone = base_opacity;
    let active_opacity_clone = active_opacity;

    // Mouse move handler - update interaction level and style
    let onmousemove_handler = {
        let state = glow_state.clone();
        let style = glow_style.clone();

        move |event: MouseEvent| {
            let (glow_x, glow_y) = calc_glow_percent(&event);

            let current = state.read();
            let new_state = GlowState {
                mouse_x: event.offset_x as f64,
                mouse_y: event.offset_y as f64,
                is_inside: true,
                interaction_level: current.interaction_level,
            };
            state.set(new_state);

            let current_opacity = base_opacity_clone
                + (active_opacity_clone.unwrap_or(base_opacity_clone * 2.0) - base_opacity_clone)
                    * new_state.interaction_level;

            let new_style = build_style(
                new_state.interaction_level,
                current_opacity,
                &glow_x,
                &glow_y,
            );
            style.set(new_style);
        }
    };

    // Mouse enter handler
    let onmouseenter_handler = {
        let state = glow_state.clone();
        let style = glow_style.clone();
        let base_op = base_opacity_clone;

        move |event: MouseEvent| {
            let (glow_x, glow_y) = calc_glow_percent(&event);

            let current = state.read();
            let new_state = GlowState {
                mouse_x: event.offset_x as f64,
                mouse_y: event.offset_y as f64,
                interaction_level: 0.5,
                is_inside: true,
            };
            state.set(new_state);

            let current_opacity = base_op
                + (active_opacity_clone.unwrap_or(base_op * 2.0) - base_op)
                    * new_state.interaction_level;
            let new_style = build_style(
                new_state.interaction_level,
                current_opacity,
                &glow_x,
                &glow_y,
            );
            style.set(new_style);
        }
    };
    // Mouse leave handler
    let onmouseleave_handler = {
        let state = glow_state.clone();
        let style = glow_style.clone();

        move |_: MouseEvent| {
            let current = state.read();
            let new_state = GlowState {
                is_inside: false,
                interaction_level: 0.0,
                ..current
            };
            state.set(new_state);

            let new_style = build_style(0.0, 0.0, "50%", "50%");
            style.set(new_style);
        }
    };

    // Mouse down handler
    let onmousedown_handler = {
        let state = glow_state.clone();
        let style = glow_style.clone();
        let base_op = base_opacity_clone;

        move |_: MouseEvent| {
            let current = state.read();
            let new_state = GlowState {
                interaction_level: 1.0,
                ..current
            };
            state.set(new_state);

            let current_opacity = base_op
                + (active_opacity_clone.unwrap_or(base_op * 2.0) - base_op)
                    * new_state.interaction_level;
            let new_style = build_style(new_state.interaction_level, current_opacity, "50%", "50%");
            style.set(new_style);
        }
    };

    // Mouse up handler
    let onmouseup_handler = {
        let state = glow_state.clone();
        let style = glow_style.clone();
        let base_op = base_opacity_clone;

        move |_: MouseEvent| {
            let current = state.read();
            let interaction_level = if current.is_inside { 0.5 } else { 0.0 };
            let new_state = GlowState {
                interaction_level,
                ..current
            };
            state.set(new_state);

            let current_opacity = base_op
                + (active_opacity_clone.unwrap_or(base_op * 2.0) - base_op)
                    * new_state.interaction_level;
            let new_style = build_style(new_state.interaction_level, current_opacity, "50%", "50%");
            style.set(new_style);
        }
    };

    rsx! {
        div {
            class: glow_classes,
            "data-glow": "true",
            style: "{glow_style}",
            onmousemove: onmousemove_handler,
            onmouseenter: onmouseenter_handler,
            onmouseleave: onmouseleave_handler,
            onmousedown: onmousedown_handler,
            onmouseup: onmouseup_handler,
            {props.children}
        }
    }
}

pub struct GlowComponent;

impl crate::styled::StyledComponent for GlowComponent {
    fn styles() -> &'static str {
        include_str!(concat!(env!("OUT_DIR"), "/styles/glow.css"))
    }

    fn name() -> &'static str {
        "glow"
    }
}

#[derive(Clone, Copy, PartialEq, Debug, Default)]
pub struct GlowConfig {
    pub glow: bool,
    pub blur: GlowBlur,
    pub color: GlowColor,
    pub intensity: GlowIntensity,
}

#[define_props]
pub struct ConditionalGlowProps {
    pub glow: bool,

    pub children: Element,

    pub blur: GlowBlur,

    pub color: GlowColor,

    pub intensity: GlowIntensity,

    #[default(false)]
    pub block: bool,
}

#[component]
pub fn ConditionalGlow(props: ConditionalGlowProps) -> Element {
    if props.glow {
        rsx! {
            Glow {
                blur: props.blur,
                color: props.color,
                intensity: props.intensity,
                block: props.block,
                {props.children}
            }
        }
    } else {
        props.children
    }
}