lumen-dioxus 6.0.0

Lumen UI components for Dioxus: web, desktop, mobile, and Dioxus Native (Blitz).
Documentation
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
//! Structural layout: `AspectRatio`, `Container`, `Direction`, `Grid`, `Stack`, `Prose`,
//! `VisuallyHidden`, `GradientDivider`, `ScrollArea`, `Toolbar`, `Affix`, and `Watermark`.

use dioxus::prelude::*;

use super::util::{
    ContainerElement, GridElement, StackElement, attr, element, encode_uri_component, escape_xml,
    join_style, js_number,
};
use crate::class::{Glass, Orientation, compose};

/// Props for [`AspectRatio`].
#[derive(Props, Clone, PartialEq)]
pub struct AspectRatioProps {
    /// CSS `aspect-ratio` value. Defaults to `16 / 9`.
    #[props(default = "16 / 9".to_string(), into)]
    pub ratio: String,
    /// Inline style appended after the ratio, so it can override it.
    #[props(into)]
    pub style: Option<String>,
    /// Extra classes appended after the Lumen class.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<div>` or global attribute.
    #[props(extends = GlobalAttributes, extends = div)]
    pub attributes: Vec<Attribute>,
    /// Media or content kept at the ratio.
    pub children: Element,
}

/// Keeps its content at a fixed width-to-height ratio.
#[component]
pub fn AspectRatio(props: AspectRatioProps) -> Element {
    let class = compose([Some("ui-aspect-ratio"), props.class.as_deref()]);
    let ratio = format!("aspect-ratio: {}", props.ratio);
    let style = join_style([Some(ratio.as_str()), props.style.as_deref()]);

    rsx! {
        div { class, style, ..props.attributes, {props.children} }
    }
}

/// Maximum width of a [`Container`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ContainerSize {
    /// Narrow reading width.
    Sm,
    /// Medium width.
    Md,
    /// The default page width.
    #[default]
    Lg,
    /// No maximum width.
    Full,
}

impl ContainerSize {
    /// The class suffix and `data-size` value.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Sm => "sm",
            Self::Md => "md",
            Self::Lg => "lg",
            Self::Full => "full",
        }
    }
}

/// Props for [`Container`].
#[derive(Props, Clone, PartialEq)]
pub struct ContainerProps {
    /// Maximum width.
    #[props(default)]
    pub size: ContainerSize,
    /// The rendered element. Defaults to `<div>`.
    #[props(default)]
    pub r#as: ContainerElement,
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other global attribute.
    #[props(extends = GlobalAttributes, extends = div)]
    pub attributes: Vec<Attribute>,
    /// Page content.
    pub children: Element,
}

/// Centers page content and caps its width.
#[component]
pub fn Container(props: ContainerProps) -> Element {
    let size = props.size.as_str();
    let modifier = format!("ui-container--{size}");
    let class = compose([
        Some("ui-container"),
        Some(modifier.as_str()),
        props.class.as_deref(),
    ]);
    let mut attributes = vec![
        attr("class", class),
        attr("data-size", size),
        attr("data-ui-container", "true"),
    ];
    attributes.extend(props.attributes);

    element(props.r#as, attributes, props.children)
}

/// Text direction set by [`Direction`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum DirectionValue {
    /// Left to right.
    #[default]
    Ltr,
    /// Right to left.
    Rtl,
    /// Decided by the content.
    Auto,
}

impl DirectionValue {
    /// The `dir` attribute value.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Ltr => "ltr",
            Self::Rtl => "rtl",
            Self::Auto => "auto",
        }
    }

    /// Whether arrow keys and drag deltas flip horizontally.
    pub fn is_rtl(self) -> bool {
        self == Self::Rtl
    }
}

#[derive(Clone, Copy)]
struct DirectionContext(Signal<DirectionValue>);

/// The direction of the nearest [`Direction`] ancestor, or [`DirectionValue::Ltr`] without one.
///
/// `Resizable`, `KanbanBoard`, and `Carousel` read it to flip horizontal arrow keys in
/// right-to-left layouts, the way `@santi020k/lumen-react` reads the computed `direction`.
pub fn use_direction() -> DirectionValue {
    try_use_context::<DirectionContext>().map_or(DirectionValue::Ltr, |context| (context.0)())
}

/// Props for [`Direction`].
#[derive(Props, Clone, PartialEq)]
pub struct DirectionProps {
    /// Text direction for the subtree. Defaults to left to right.
    #[props(default)]
    pub dir: DirectionValue,
    /// Extra classes appended after the Lumen class.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<div>` or global attribute.
    #[props(extends = GlobalAttributes, extends = div)]
    pub attributes: Vec<Attribute>,
    /// Content laid out in the direction.
    pub children: Element,
}

/// Sets `dir` on a wrapper and shares it with descendants through [`use_direction`].
#[component]
pub fn Direction(props: DirectionProps) -> Element {
    let mut current = use_signal(|| props.dir);
    use_context_provider(|| DirectionContext(current));

    if *current.peek() != props.dir {
        current.set(props.dir);
    }

    let class = compose([Some("ui-direction"), props.class.as_deref()]);

    rsx! {
        div { class, dir: props.dir.as_str(), ..props.attributes, {props.children} }
    }
}

/// Column count of a [`Grid`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum GridColumns {
    /// One column.
    One,
    /// Two columns.
    Two,
    /// Three columns.
    Three,
    /// Four columns.
    Four,
    /// Six columns.
    Six,
    /// Twelve columns.
    Twelve,
    /// As many columns as fit the minimum item width.
    #[default]
    Auto,
}

impl GridColumns {
    /// The class suffix and `data-columns` value.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::One => "1",
            Self::Two => "2",
            Self::Three => "3",
            Self::Four => "4",
            Self::Six => "6",
            Self::Twelve => "12",
            Self::Auto => "auto",
        }
    }
}

/// Gap between [`Grid`] and [`Stack`] children, from Lumen's spacing scale.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum GridGap {
    /// No gap.
    None,
    /// Extra small.
    Xs,
    /// Small.
    Sm,
    /// Medium.
    Md,
    /// Large.
    Lg,
    /// Extra large.
    Xl,
    /// Two times extra large.
    Xl2,
    /// Three times extra large.
    Xl3,
    /// Space between closely related items.
    Related,
    /// Space between groups. The default.
    #[default]
    Group,
    /// Space between sections.
    Section,
}

impl GridGap {
    /// The class suffix.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::None => "none",
            Self::Xs => "xs",
            Self::Sm => "sm",
            Self::Md => "md",
            Self::Lg => "lg",
            Self::Xl => "xl",
            Self::Xl2 => "2xl",
            Self::Xl3 => "3xl",
            Self::Related => "related",
            Self::Group => "group",
            Self::Section => "section",
        }
    }
}

/// Gap between [`Stack`] children.
pub type StackGap = GridGap;

/// Props for [`Grid`].
#[derive(Props, Clone, PartialEq)]
pub struct GridProps {
    /// Column count.
    #[props(default)]
    pub columns: GridColumns,
    /// Gap between items.
    #[props(default)]
    pub gap: GridGap,
    /// Minimum item width for automatic columns, as a CSS length.
    #[props(into)]
    pub min_item_width: Option<String>,
    /// The rendered element. Defaults to `<div>`.
    #[props(default)]
    pub r#as: GridElement,
    /// Inline style. The minimum item width is appended after it.
    #[props(into)]
    pub style: Option<String>,
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other global attribute.
    #[props(extends = GlobalAttributes, extends = div)]
    pub attributes: Vec<Attribute>,
    /// Grid items.
    pub children: Element,
}

/// A responsive CSS grid on Lumen's spacing scale.
#[component]
pub fn Grid(props: GridProps) -> Element {
    let columns = props.columns.as_str();
    let columns_class = format!("ui-grid--columns-{columns}");
    let gap_class = format!("ui-grid--gap-{}", props.gap.as_str());
    let class = compose([
        Some("ui-grid"),
        Some(columns_class.as_str()),
        Some(gap_class.as_str()),
        props.class.as_deref(),
    ]);
    let min = props
        .min_item_width
        .as_deref()
        .map(|width| format!("--ui-grid-min: {width}"));
    let mut attributes = vec![
        attr("class", class),
        attr("data-columns", columns),
        attr("data-ui-grid", "true"),
    ];

    if let Some(style) = join_style([props.style.as_deref(), min.as_deref()]) {
        attributes.push(attr("style", style));
    }

    attributes.extend(props.attributes);

    element(props.r#as, attributes, props.children)
}

/// Cross-axis alignment of [`Stack`] children.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum StackAlign {
    /// Align to the start.
    Start,
    /// Center.
    Center,
    /// Align to the end.
    End,
    /// Stretch to fill. The default.
    #[default]
    Stretch,
}

impl StackAlign {
    /// The class suffix.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Start => "start",
            Self::Center => "center",
            Self::End => "end",
            Self::Stretch => "stretch",
        }
    }
}

/// Main-axis distribution of [`Stack`] children.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum StackJustify {
    /// Pack at the start. The default.
    #[default]
    Start,
    /// Center.
    Center,
    /// Pack at the end.
    End,
    /// Spread with space between.
    Between,
}

impl StackJustify {
    /// The class suffix.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Start => "start",
            Self::Center => "center",
            Self::End => "end",
            Self::Between => "between",
        }
    }
}

/// Props for [`Stack`].
#[derive(Props, Clone, PartialEq)]
pub struct StackProps {
    /// Main axis. Defaults to vertical.
    #[props(default = Orientation::Vertical)]
    pub direction: Orientation,
    /// Gap between children.
    #[props(default)]
    pub gap: StackGap,
    /// Cross-axis alignment.
    #[props(default)]
    pub align: StackAlign,
    /// Main-axis distribution.
    #[props(default)]
    pub justify: StackJustify,
    /// Wraps children onto new lines.
    #[props(default)]
    pub wrap: bool,
    /// The rendered element. Defaults to `<div>`.
    #[props(default)]
    pub r#as: StackElement,
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other global attribute.
    #[props(extends = GlobalAttributes, extends = div)]
    pub attributes: Vec<Attribute>,
    /// Stacked children.
    pub children: Element,
}

/// A one-dimensional flex layout on Lumen's spacing scale.
#[component]
pub fn Stack(props: StackProps) -> Element {
    let direction = props.direction.as_str();
    let direction_class = format!("ui-stack--{direction}");
    let gap_class = format!("ui-stack--gap-{}", props.gap.as_str());
    let align_class = format!("ui-stack--align-{}", props.align.as_str());
    let justify_class = format!("ui-stack--justify-{}", props.justify.as_str());
    let class = compose([
        Some("ui-stack"),
        Some(direction_class.as_str()),
        Some(gap_class.as_str()),
        Some(align_class.as_str()),
        Some(justify_class.as_str()),
        props.wrap.then_some("ui-stack--wrap"),
        props.class.as_deref(),
    ]);
    let mut attributes = vec![
        attr("class", class),
        attr("data-direction", direction),
        attr("data-ui-stack", "true"),
    ];
    attributes.extend(props.attributes);

    element(props.r#as, attributes, props.children)
}

/// Typography treatment of [`Prose`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ProseAppearance {
    /// Product documentation rhythm.
    #[default]
    Default,
    /// Long-form editorial rhythm.
    Editorial,
}

/// Props for [`Prose`].
#[derive(Props, Clone, PartialEq)]
pub struct ProseProps {
    /// Typography treatment.
    #[props(default)]
    pub appearance: ProseAppearance,
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<div>` or global attribute.
    #[props(extends = GlobalAttributes, extends = div)]
    pub attributes: Vec<Attribute>,
    /// Rich text such as headings, paragraphs, and lists.
    pub children: Element,
}

/// Styles plain HTML content such as rendered Markdown.
#[component]
pub fn Prose(props: ProseProps) -> Element {
    let class = compose([
        Some("ui-prose"),
        (props.appearance == ProseAppearance::Editorial).then_some("ui-prose--editorial"),
        props.class.as_deref(),
    ]);

    rsx! {
        div { class, "data-slot": "prose", ..props.attributes, {props.children} }
    }
}

/// Props for [`VisuallyHidden`].
#[derive(Props, Clone, PartialEq)]
pub struct VisuallyHiddenProps {
    /// Shows the content while it has keyboard focus, for skip links.
    #[props(default)]
    pub focusable: bool,
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<span>` or global attribute.
    #[props(extends = GlobalAttributes, extends = span)]
    pub attributes: Vec<Attribute>,
    /// Text for assistive technology.
    pub children: Element,
}

/// Hides content visually while keeping it available to assistive technology.
#[component]
pub fn VisuallyHidden(props: VisuallyHiddenProps) -> Element {
    let class = compose([
        Some("ui-visually-hidden"),
        props.focusable.then_some("ui-visually-hidden--focusable"),
        props.class.as_deref(),
    ]);

    rsx! {
        span { class, "data-ui-visually-hidden": "true", ..props.attributes, {props.children} }
    }
}

/// Props for [`GradientDivider`].
#[derive(Props, Clone, PartialEq)]
pub struct GradientDividerProps {
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<div>` or global attribute.
    #[props(extends = GlobalAttributes, extends = div)]
    pub attributes: Vec<Attribute>,
}

/// The utility classes `@santi020k/lumen-react` puts on the divider line, kept byte for byte.
const GRADIENT_DIVIDER_LINE: &str = "\n        via-brand/40 h-px w-full max-w-5xl bg-linear-to-r from-transparent\n        to-transparent\n      ";

/// A decorative horizontal rule that fades in from both sides.
#[component]
pub fn GradientDivider(props: GradientDividerProps) -> Element {
    let class = compose([
        Some("ui-gradient-divider pointer-events-none flex justify-center"),
        props.class.as_deref(),
    ]);

    rsx! {
        div { aria_hidden: "true", class, ..props.attributes,
            div { class: GRADIENT_DIVIDER_LINE }
        }
    }
}

/// Props for [`ScrollArea`].
#[derive(Props, Clone, PartialEq)]
pub struct ScrollAreaProps {
    /// Glass intensity.
    #[props(default, into)]
    pub glass: Glass,
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<div>` or global attribute. Name the region with `aria_label`.
    #[props(extends = GlobalAttributes, extends = div)]
    pub attributes: Vec<Attribute>,
    /// Scrollable content.
    pub children: Element,
}

/// A bordered region that scrolls its overflowing content.
#[component]
pub fn ScrollArea(props: ScrollAreaProps) -> Element {
    let glass = props.glass.class("ui-scroll-area");
    let class = compose([
        Some("ui-scroll-area"),
        glass.as_deref(),
        props.class.as_deref(),
    ]);

    rsx! {
        div { class, ..props.attributes, {props.children} }
    }
}

/// Props for [`Toolbar`].
#[derive(Props, Clone, PartialEq)]
pub struct ToolbarProps {
    /// Accessible name. Defaults to `Toolbar`.
    #[props(default = "Toolbar".to_string(), into)]
    pub aria_label: String,
    /// Layout direction.
    #[props(default)]
    pub orientation: Orientation,
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<div>` or global attribute.
    #[props(extends = GlobalAttributes, extends = div)]
    pub attributes: Vec<Attribute>,
    /// Buttons and controls.
    pub children: Element,
}

/// A labelled group of controls with the `toolbar` role.
#[component]
pub fn Toolbar(props: ToolbarProps) -> Element {
    let orientation = props.orientation.as_str();
    let class = compose([
        Some("ui-toolbar"),
        (props.orientation == Orientation::Vertical).then_some("ui-toolbar--vertical"),
        props.class.as_deref(),
    ]);

    rsx! {
        div {
            aria_label: props.aria_label,
            aria_orientation: orientation,
            class,
            "data-orientation": orientation,
            "data-ui-toolbar": "true",
            role: "toolbar",
            ..props.attributes,
            {props.children}
        }
    }
}

/// Edge an [`Affix`] sticks to.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum AffixPosition {
    /// Sticks to the top of the scroll container.
    #[default]
    Top,
    /// Sticks to the bottom of the scroll container.
    Bottom,
}

impl AffixPosition {
    /// The `data-position` value.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Top => "top",
            Self::Bottom => "bottom",
        }
    }
}

/// Props for [`Affix`].
#[derive(Props, Clone, PartialEq)]
pub struct AffixProps {
    /// Distance from the edge in pixels when neither `offset_top` nor `offset_bottom` is set.
    #[props(default)]
    pub offset: f64,
    /// Distance from the top in pixels.
    pub offset_top: Option<f64>,
    /// Distance from the bottom in pixels. Setting it sticks to the bottom.
    pub offset_bottom: Option<f64>,
    /// Edge to stick to. Defaults to the bottom when `offset_bottom` is set, else the top.
    pub position: Option<AffixPosition>,
    /// Inline style appended after the offset.
    #[props(into)]
    pub style: Option<String>,
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<div>` or global attribute.
    #[props(extends = GlobalAttributes, extends = div)]
    pub attributes: Vec<Attribute>,
    /// Content that sticks while its container scrolls.
    pub children: Element,
}

/// Keeps content stuck to an edge while its scroll container moves, with `position: sticky`.
#[component]
pub fn Affix(props: AffixProps) -> Element {
    let position = props.position.unwrap_or(if props.offset_bottom.is_some() {
        AffixPosition::Bottom
    } else {
        AffixPosition::Top
    });
    let offset = props
        .offset_bottom
        .or(props.offset_top)
        .unwrap_or(props.offset);
    let class = compose([
        Some("ui-affix"),
        (position == AffixPosition::Bottom).then_some("ui-affix--bottom"),
        props.class.as_deref(),
    ]);
    let offset = format!("--ui-affix-offset: {}px", js_number(offset));
    let style = join_style([Some(offset.as_str()), props.style.as_deref()]);

    rsx! {
        div {
            class,
            "data-position": position.as_str(),
            style,
            ..props.attributes,
            {props.children}
        }
    }
}

/// Props for [`Watermark`].
#[derive(Props, Clone, PartialEq)]
pub struct WatermarkProps {
    /// Watermark text. Takes precedence over `text`.
    #[props(into)]
    pub content: Option<String>,
    /// Watermark text when `content` is unset. Defaults to `Confidential`.
    #[props(default = "Confidential".to_string(), into)]
    pub text: String,
    /// Tile size in pixels. Values below 1 use 1.
    #[props(default = 120.0)]
    pub gap: f64,
    /// Text rotation in degrees.
    #[props(default = -22.0)]
    pub rotate: f64,
    /// Inline style appended after the watermark variables.
    #[props(into)]
    pub style: Option<String>,
    /// Extra classes appended after the Lumen class.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<div>` or global attribute.
    #[props(extends = GlobalAttributes, extends = div)]
    pub attributes: Vec<Attribute>,
    /// Protected content.
    pub children: Element,
}

/// The tiled SVG mask image a [`Watermark`] renders, matching `@santi020k/lumen-react`.
pub(crate) fn watermark_image(text: &str, gap: f64, rotate: f64) -> (String, String) {
    let tile = if gap.is_nan() { f64::NAN } else { gap.max(1.0) };
    let size = js_number(tile);
    let center = js_number(tile / 2.0);
    let rotate = js_number(rotate);
    let escaped = escape_xml(text);
    let svg = format!(
        "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{size}\" height=\"{size}\" viewBox=\"0 0 {size} {size}\">\
         <text x=\"{center}\" y=\"{center}\" dominant-baseline=\"middle\" text-anchor=\"middle\" \
         transform=\"rotate({rotate} {center} {center})\" fill=\"black\" \
         font-family=\"Inter, ui-sans-serif, system-ui, -apple-system, Segoe UI, sans-serif\" \
         font-size=\"16\" font-weight=\"500\" letter-spacing=\"1.6\">{escaped}</text></svg>"
    );

    (
        format!("{size}px"),
        format!("url(\"data:image/svg+xml,{}\")", encode_uri_component(&svg)),
    )
}

/// Tiles rotated text over its content, behind a non-interactive overlay.
///
/// The overlay uses a CSS mask image; renderers without `mask-image` support, such as the current
/// Dioxus Native engine, show the content without the tiled text.
#[component]
pub fn Watermark(props: WatermarkProps) -> Element {
    let text = props.content.clone().unwrap_or_else(|| props.text.clone());
    let (gap, image) = watermark_image(&text, props.gap, props.rotate);
    let variables = format!("--ui-watermark-gap: {gap}; --ui-watermark-image: {image}");
    let style = join_style([Some(variables.as_str()), props.style.as_deref()]);
    let class = compose([Some("ui-watermark"), props.class.as_deref()]);

    rsx! {
        div { class, "data-ui-watermark": "true", style, ..props.attributes,
            {props.children}
            div {
                aria_hidden: "true",
                class: "ui-watermark__overlay",
                "data-text": text,
            }
        }
    }
}