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};
#[derive(Props, Clone, PartialEq)]
pub struct AspectRatioProps {
#[props(default = "16 / 9".to_string(), into)]
pub ratio: String,
#[props(into)]
pub style: Option<String>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[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} }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ContainerSize {
Sm,
Md,
#[default]
Lg,
Full,
}
impl ContainerSize {
pub fn as_str(self) -> &'static str {
match self {
Self::Sm => "sm",
Self::Md => "md",
Self::Lg => "lg",
Self::Full => "full",
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct ContainerProps {
#[props(default)]
pub size: ContainerSize,
#[props(default)]
pub r#as: ContainerElement,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[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)
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum DirectionValue {
#[default]
Ltr,
Rtl,
Auto,
}
impl DirectionValue {
pub fn as_str(self) -> &'static str {
match self {
Self::Ltr => "ltr",
Self::Rtl => "rtl",
Self::Auto => "auto",
}
}
pub fn is_rtl(self) -> bool {
self == Self::Rtl
}
}
#[derive(Clone, Copy)]
struct DirectionContext(Signal<DirectionValue>);
pub fn use_direction() -> DirectionValue {
try_use_context::<DirectionContext>().map_or(DirectionValue::Ltr, |context| (context.0)())
}
#[derive(Props, Clone, PartialEq)]
pub struct DirectionProps {
#[props(default)]
pub dir: DirectionValue,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[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} }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum GridColumns {
One,
Two,
Three,
Four,
Six,
Twelve,
#[default]
Auto,
}
impl GridColumns {
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",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum GridGap {
None,
Xs,
Sm,
Md,
Lg,
Xl,
Xl2,
Xl3,
Related,
#[default]
Group,
Section,
}
impl GridGap {
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",
}
}
}
pub type StackGap = GridGap;
#[derive(Props, Clone, PartialEq)]
pub struct GridProps {
#[props(default)]
pub columns: GridColumns,
#[props(default)]
pub gap: GridGap,
#[props(into)]
pub min_item_width: Option<String>,
#[props(default)]
pub r#as: GridElement,
#[props(into)]
pub style: Option<String>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[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)
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum StackAlign {
Start,
Center,
End,
#[default]
Stretch,
}
impl StackAlign {
pub fn as_str(self) -> &'static str {
match self {
Self::Start => "start",
Self::Center => "center",
Self::End => "end",
Self::Stretch => "stretch",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum StackJustify {
#[default]
Start,
Center,
End,
Between,
}
impl StackJustify {
pub fn as_str(self) -> &'static str {
match self {
Self::Start => "start",
Self::Center => "center",
Self::End => "end",
Self::Between => "between",
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct StackProps {
#[props(default = Orientation::Vertical)]
pub direction: Orientation,
#[props(default)]
pub gap: StackGap,
#[props(default)]
pub align: StackAlign,
#[props(default)]
pub justify: StackJustify,
#[props(default)]
pub wrap: bool,
#[props(default)]
pub r#as: StackElement,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[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)
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ProseAppearance {
#[default]
Default,
Editorial,
}
#[derive(Props, Clone, PartialEq)]
pub struct ProseProps {
#[props(default)]
pub appearance: ProseAppearance,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[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} }
}
}
#[derive(Props, Clone, PartialEq)]
pub struct VisuallyHiddenProps {
#[props(default)]
pub focusable: bool,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = span)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[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} }
}
}
#[derive(Props, Clone, PartialEq)]
pub struct GradientDividerProps {
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
}
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 ";
#[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 }
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct ScrollAreaProps {
#[props(default, into)]
pub glass: Glass,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[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} }
}
}
#[derive(Props, Clone, PartialEq)]
pub struct ToolbarProps {
#[props(default = "Toolbar".to_string(), into)]
pub aria_label: String,
#[props(default)]
pub orientation: Orientation,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[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}
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum AffixPosition {
#[default]
Top,
Bottom,
}
impl AffixPosition {
pub fn as_str(self) -> &'static str {
match self {
Self::Top => "top",
Self::Bottom => "bottom",
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct AffixProps {
#[props(default)]
pub offset: f64,
pub offset_top: Option<f64>,
pub offset_bottom: Option<f64>,
pub position: Option<AffixPosition>,
#[props(into)]
pub style: Option<String>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[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}
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct WatermarkProps {
#[props(into)]
pub content: Option<String>,
#[props(default = "Confidential".to_string(), into)]
pub text: String,
#[props(default = 120.0)]
pub gap: f64,
#[props(default = -22.0)]
pub rotate: f64,
#[props(into)]
pub style: Option<String>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
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)),
)
}
#[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,
}
}
}
}