1use dioxus::prelude::*;
5
6use super::util::{
7 ContainerElement, GridElement, StackElement, attr, element, encode_uri_component, escape_xml,
8 join_style, js_number,
9};
10use crate::class::{Glass, Orientation, compose};
11
12#[derive(Props, Clone, PartialEq)]
14pub struct AspectRatioProps {
15 #[props(default = "16 / 9".to_string(), into)]
17 pub ratio: String,
18 #[props(into)]
20 pub style: Option<String>,
21 #[props(into)]
23 pub class: Option<String>,
24 #[props(extends = GlobalAttributes, extends = div)]
26 pub attributes: Vec<Attribute>,
27 pub children: Element,
29}
30
31#[component]
33pub fn AspectRatio(props: AspectRatioProps) -> Element {
34 let class = compose([Some("ui-aspect-ratio"), props.class.as_deref()]);
35 let ratio = format!("aspect-ratio: {}", props.ratio);
36 let style = join_style([Some(ratio.as_str()), props.style.as_deref()]);
37
38 rsx! {
39 div { class, style, ..props.attributes, {props.children} }
40 }
41}
42
43#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
45#[non_exhaustive]
46pub enum ContainerSize {
47 Sm,
49 Md,
51 #[default]
53 Lg,
54 Full,
56}
57
58impl ContainerSize {
59 pub fn as_str(self) -> &'static str {
61 match self {
62 Self::Sm => "sm",
63 Self::Md => "md",
64 Self::Lg => "lg",
65 Self::Full => "full",
66 }
67 }
68}
69
70#[derive(Props, Clone, PartialEq)]
72pub struct ContainerProps {
73 #[props(default)]
75 pub size: ContainerSize,
76 #[props(default)]
78 pub r#as: ContainerElement,
79 #[props(into)]
81 pub class: Option<String>,
82 #[props(extends = GlobalAttributes, extends = div)]
84 pub attributes: Vec<Attribute>,
85 pub children: Element,
87}
88
89#[component]
91pub fn Container(props: ContainerProps) -> Element {
92 let size = props.size.as_str();
93 let modifier = format!("ui-container--{size}");
94 let class = compose([
95 Some("ui-container"),
96 Some(modifier.as_str()),
97 props.class.as_deref(),
98 ]);
99 let mut attributes = vec![
100 attr("class", class),
101 attr("data-size", size),
102 attr("data-ui-container", "true"),
103 ];
104 attributes.extend(props.attributes);
105
106 element(props.r#as, attributes, props.children)
107}
108
109#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
111#[non_exhaustive]
112pub enum DirectionValue {
113 #[default]
115 Ltr,
116 Rtl,
118 Auto,
120}
121
122impl DirectionValue {
123 pub fn as_str(self) -> &'static str {
125 match self {
126 Self::Ltr => "ltr",
127 Self::Rtl => "rtl",
128 Self::Auto => "auto",
129 }
130 }
131
132 pub fn is_rtl(self) -> bool {
134 self == Self::Rtl
135 }
136}
137
138#[derive(Clone, Copy)]
139struct DirectionContext(Signal<DirectionValue>);
140
141pub fn use_direction() -> DirectionValue {
146 try_use_context::<DirectionContext>().map_or(DirectionValue::Ltr, |context| (context.0)())
147}
148
149#[derive(Props, Clone, PartialEq)]
151pub struct DirectionProps {
152 #[props(default)]
154 pub dir: DirectionValue,
155 #[props(into)]
157 pub class: Option<String>,
158 #[props(extends = GlobalAttributes, extends = div)]
160 pub attributes: Vec<Attribute>,
161 pub children: Element,
163}
164
165#[component]
167pub fn Direction(props: DirectionProps) -> Element {
168 let mut current = use_signal(|| props.dir);
169 use_context_provider(|| DirectionContext(current));
170
171 if *current.peek() != props.dir {
172 current.set(props.dir);
173 }
174
175 let class = compose([Some("ui-direction"), props.class.as_deref()]);
176
177 rsx! {
178 div { class, dir: props.dir.as_str(), ..props.attributes, {props.children} }
179 }
180}
181
182#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
184#[non_exhaustive]
185pub enum GridColumns {
186 One,
188 Two,
190 Three,
192 Four,
194 Six,
196 Twelve,
198 #[default]
200 Auto,
201}
202
203impl GridColumns {
204 pub fn as_str(self) -> &'static str {
206 match self {
207 Self::One => "1",
208 Self::Two => "2",
209 Self::Three => "3",
210 Self::Four => "4",
211 Self::Six => "6",
212 Self::Twelve => "12",
213 Self::Auto => "auto",
214 }
215 }
216}
217
218#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
220#[non_exhaustive]
221pub enum GridGap {
222 None,
224 Xs,
226 Sm,
228 Md,
230 Lg,
232 Xl,
234 Xl2,
236 Xl3,
238 Related,
240 #[default]
242 Group,
243 Section,
245}
246
247impl GridGap {
248 pub fn as_str(self) -> &'static str {
250 match self {
251 Self::None => "none",
252 Self::Xs => "xs",
253 Self::Sm => "sm",
254 Self::Md => "md",
255 Self::Lg => "lg",
256 Self::Xl => "xl",
257 Self::Xl2 => "2xl",
258 Self::Xl3 => "3xl",
259 Self::Related => "related",
260 Self::Group => "group",
261 Self::Section => "section",
262 }
263 }
264}
265
266pub type StackGap = GridGap;
268
269#[derive(Props, Clone, PartialEq)]
271pub struct GridProps {
272 #[props(default)]
274 pub columns: GridColumns,
275 #[props(default)]
277 pub gap: GridGap,
278 #[props(into)]
280 pub min_item_width: Option<String>,
281 #[props(default)]
283 pub r#as: GridElement,
284 #[props(into)]
286 pub style: Option<String>,
287 #[props(into)]
289 pub class: Option<String>,
290 #[props(extends = GlobalAttributes, extends = div)]
292 pub attributes: Vec<Attribute>,
293 pub children: Element,
295}
296
297#[component]
299pub fn Grid(props: GridProps) -> Element {
300 let columns = props.columns.as_str();
301 let columns_class = format!("ui-grid--columns-{columns}");
302 let gap_class = format!("ui-grid--gap-{}", props.gap.as_str());
303 let class = compose([
304 Some("ui-grid"),
305 Some(columns_class.as_str()),
306 Some(gap_class.as_str()),
307 props.class.as_deref(),
308 ]);
309 let min = props
310 .min_item_width
311 .as_deref()
312 .map(|width| format!("--ui-grid-min: {width}"));
313 let mut attributes = vec![
314 attr("class", class),
315 attr("data-columns", columns),
316 attr("data-ui-grid", "true"),
317 ];
318
319 if let Some(style) = join_style([props.style.as_deref(), min.as_deref()]) {
320 attributes.push(attr("style", style));
321 }
322
323 attributes.extend(props.attributes);
324
325 element(props.r#as, attributes, props.children)
326}
327
328#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
330#[non_exhaustive]
331pub enum StackAlign {
332 Start,
334 Center,
336 End,
338 #[default]
340 Stretch,
341}
342
343impl StackAlign {
344 pub fn as_str(self) -> &'static str {
346 match self {
347 Self::Start => "start",
348 Self::Center => "center",
349 Self::End => "end",
350 Self::Stretch => "stretch",
351 }
352 }
353}
354
355#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
357#[non_exhaustive]
358pub enum StackJustify {
359 #[default]
361 Start,
362 Center,
364 End,
366 Between,
368}
369
370impl StackJustify {
371 pub fn as_str(self) -> &'static str {
373 match self {
374 Self::Start => "start",
375 Self::Center => "center",
376 Self::End => "end",
377 Self::Between => "between",
378 }
379 }
380}
381
382#[derive(Props, Clone, PartialEq)]
384pub struct StackProps {
385 #[props(default = Orientation::Vertical)]
387 pub direction: Orientation,
388 #[props(default)]
390 pub gap: StackGap,
391 #[props(default)]
393 pub align: StackAlign,
394 #[props(default)]
396 pub justify: StackJustify,
397 #[props(default)]
399 pub wrap: bool,
400 #[props(default)]
402 pub r#as: StackElement,
403 #[props(into)]
405 pub class: Option<String>,
406 #[props(extends = GlobalAttributes, extends = div)]
408 pub attributes: Vec<Attribute>,
409 pub children: Element,
411}
412
413#[component]
415pub fn Stack(props: StackProps) -> Element {
416 let direction = props.direction.as_str();
417 let direction_class = format!("ui-stack--{direction}");
418 let gap_class = format!("ui-stack--gap-{}", props.gap.as_str());
419 let align_class = format!("ui-stack--align-{}", props.align.as_str());
420 let justify_class = format!("ui-stack--justify-{}", props.justify.as_str());
421 let class = compose([
422 Some("ui-stack"),
423 Some(direction_class.as_str()),
424 Some(gap_class.as_str()),
425 Some(align_class.as_str()),
426 Some(justify_class.as_str()),
427 props.wrap.then_some("ui-stack--wrap"),
428 props.class.as_deref(),
429 ]);
430 let mut attributes = vec![
431 attr("class", class),
432 attr("data-direction", direction),
433 attr("data-ui-stack", "true"),
434 ];
435 attributes.extend(props.attributes);
436
437 element(props.r#as, attributes, props.children)
438}
439
440#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
442#[non_exhaustive]
443pub enum ProseAppearance {
444 #[default]
446 Default,
447 Editorial,
449}
450
451#[derive(Props, Clone, PartialEq)]
453pub struct ProseProps {
454 #[props(default)]
456 pub appearance: ProseAppearance,
457 #[props(into)]
459 pub class: Option<String>,
460 #[props(extends = GlobalAttributes, extends = div)]
462 pub attributes: Vec<Attribute>,
463 pub children: Element,
465}
466
467#[component]
469pub fn Prose(props: ProseProps) -> Element {
470 let class = compose([
471 Some("ui-prose"),
472 (props.appearance == ProseAppearance::Editorial).then_some("ui-prose--editorial"),
473 props.class.as_deref(),
474 ]);
475
476 rsx! {
477 div { class, "data-slot": "prose", ..props.attributes, {props.children} }
478 }
479}
480
481#[derive(Props, Clone, PartialEq)]
483pub struct VisuallyHiddenProps {
484 #[props(default)]
486 pub focusable: bool,
487 #[props(into)]
489 pub class: Option<String>,
490 #[props(extends = GlobalAttributes, extends = span)]
492 pub attributes: Vec<Attribute>,
493 pub children: Element,
495}
496
497#[component]
499pub fn VisuallyHidden(props: VisuallyHiddenProps) -> Element {
500 let class = compose([
501 Some("ui-visually-hidden"),
502 props.focusable.then_some("ui-visually-hidden--focusable"),
503 props.class.as_deref(),
504 ]);
505
506 rsx! {
507 span { class, "data-ui-visually-hidden": "true", ..props.attributes, {props.children} }
508 }
509}
510
511#[derive(Props, Clone, PartialEq)]
513pub struct GradientDividerProps {
514 #[props(into)]
516 pub class: Option<String>,
517 #[props(extends = GlobalAttributes, extends = div)]
519 pub attributes: Vec<Attribute>,
520}
521
522const 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 ";
524
525#[component]
527pub fn GradientDivider(props: GradientDividerProps) -> Element {
528 let class = compose([
529 Some("ui-gradient-divider pointer-events-none flex justify-center"),
530 props.class.as_deref(),
531 ]);
532
533 rsx! {
534 div { aria_hidden: "true", class, ..props.attributes,
535 div { class: GRADIENT_DIVIDER_LINE }
536 }
537 }
538}
539
540#[derive(Props, Clone, PartialEq)]
542pub struct ScrollAreaProps {
543 #[props(default, into)]
545 pub glass: Glass,
546 #[props(into)]
548 pub class: Option<String>,
549 #[props(extends = GlobalAttributes, extends = div)]
551 pub attributes: Vec<Attribute>,
552 pub children: Element,
554}
555
556#[component]
558pub fn ScrollArea(props: ScrollAreaProps) -> Element {
559 let glass = props.glass.class("ui-scroll-area");
560 let class = compose([
561 Some("ui-scroll-area"),
562 glass.as_deref(),
563 props.class.as_deref(),
564 ]);
565
566 rsx! {
567 div { class, ..props.attributes, {props.children} }
568 }
569}
570
571#[derive(Props, Clone, PartialEq)]
573pub struct ToolbarProps {
574 #[props(default = "Toolbar".to_string(), into)]
576 pub aria_label: String,
577 #[props(default)]
579 pub orientation: Orientation,
580 #[props(into)]
582 pub class: Option<String>,
583 #[props(extends = GlobalAttributes, extends = div)]
585 pub attributes: Vec<Attribute>,
586 pub children: Element,
588}
589
590#[component]
592pub fn Toolbar(props: ToolbarProps) -> Element {
593 let orientation = props.orientation.as_str();
594 let class = compose([
595 Some("ui-toolbar"),
596 (props.orientation == Orientation::Vertical).then_some("ui-toolbar--vertical"),
597 props.class.as_deref(),
598 ]);
599
600 rsx! {
601 div {
602 aria_label: props.aria_label,
603 aria_orientation: orientation,
604 class,
605 "data-orientation": orientation,
606 "data-ui-toolbar": "true",
607 role: "toolbar",
608 ..props.attributes,
609 {props.children}
610 }
611 }
612}
613
614#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
616pub enum AffixPosition {
617 #[default]
619 Top,
620 Bottom,
622}
623
624impl AffixPosition {
625 pub fn as_str(self) -> &'static str {
627 match self {
628 Self::Top => "top",
629 Self::Bottom => "bottom",
630 }
631 }
632}
633
634#[derive(Props, Clone, PartialEq)]
636pub struct AffixProps {
637 #[props(default)]
639 pub offset: f64,
640 pub offset_top: Option<f64>,
642 pub offset_bottom: Option<f64>,
644 pub position: Option<AffixPosition>,
646 #[props(into)]
648 pub style: Option<String>,
649 #[props(into)]
651 pub class: Option<String>,
652 #[props(extends = GlobalAttributes, extends = div)]
654 pub attributes: Vec<Attribute>,
655 pub children: Element,
657}
658
659#[component]
661pub fn Affix(props: AffixProps) -> Element {
662 let position = props.position.unwrap_or(if props.offset_bottom.is_some() {
663 AffixPosition::Bottom
664 } else {
665 AffixPosition::Top
666 });
667 let offset = props
668 .offset_bottom
669 .or(props.offset_top)
670 .unwrap_or(props.offset);
671 let class = compose([
672 Some("ui-affix"),
673 (position == AffixPosition::Bottom).then_some("ui-affix--bottom"),
674 props.class.as_deref(),
675 ]);
676 let offset = format!("--ui-affix-offset: {}px", js_number(offset));
677 let style = join_style([Some(offset.as_str()), props.style.as_deref()]);
678
679 rsx! {
680 div {
681 class,
682 "data-position": position.as_str(),
683 style,
684 ..props.attributes,
685 {props.children}
686 }
687 }
688}
689
690#[derive(Props, Clone, PartialEq)]
692pub struct WatermarkProps {
693 #[props(into)]
695 pub content: Option<String>,
696 #[props(default = "Confidential".to_string(), into)]
698 pub text: String,
699 #[props(default = 120.0)]
701 pub gap: f64,
702 #[props(default = -22.0)]
704 pub rotate: f64,
705 #[props(into)]
707 pub style: Option<String>,
708 #[props(into)]
710 pub class: Option<String>,
711 #[props(extends = GlobalAttributes, extends = div)]
713 pub attributes: Vec<Attribute>,
714 pub children: Element,
716}
717
718pub(crate) fn watermark_image(text: &str, gap: f64, rotate: f64) -> (String, String) {
720 let tile = if gap.is_nan() { f64::NAN } else { gap.max(1.0) };
721 let size = js_number(tile);
722 let center = js_number(tile / 2.0);
723 let rotate = js_number(rotate);
724 let escaped = escape_xml(text);
725 let svg = format!(
726 "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{size}\" height=\"{size}\" viewBox=\"0 0 {size} {size}\">\
727 <text x=\"{center}\" y=\"{center}\" dominant-baseline=\"middle\" text-anchor=\"middle\" \
728 transform=\"rotate({rotate} {center} {center})\" fill=\"black\" \
729 font-family=\"Inter, ui-sans-serif, system-ui, -apple-system, Segoe UI, sans-serif\" \
730 font-size=\"16\" font-weight=\"500\" letter-spacing=\"1.6\">{escaped}</text></svg>"
731 );
732
733 (
734 format!("{size}px"),
735 format!("url(\"data:image/svg+xml,{}\")", encode_uri_component(&svg)),
736 )
737}
738
739#[component]
744pub fn Watermark(props: WatermarkProps) -> Element {
745 let text = props.content.clone().unwrap_or_else(|| props.text.clone());
746 let (gap, image) = watermark_image(&text, props.gap, props.rotate);
747 let variables = format!("--ui-watermark-gap: {gap}; --ui-watermark-image: {image}");
748 let style = join_style([Some(variables.as_str()), props.style.as_deref()]);
749 let class = compose([Some("ui-watermark"), props.class.as_deref()]);
750
751 rsx! {
752 div { class, "data-ui-watermark": "true", style, ..props.attributes,
753 {props.children}
754 div {
755 aria_hidden: "true",
756 class: "ui-watermark__overlay",
757 "data-text": text,
758 }
759 }
760 }
761}