var

Function var 

Source
pub fn var(name: &str) -> CssVar
Expand description

Creates a new CSS variable reference.

This is a convenience function that creates a new CssVar instance. It’s a shorter alternative to calling CssVar::new().

§Arguments

  • name - The variable name (with or without the -- prefix)

§Returns

A new CssVar instance

§Examples

use mew_css::variable::var;
use mew_css::style;
use mew_css::values::Color;

// Create a CSS variable reference
let primary_color = var("primary-color");

// Use it in a style
let css = style()
    .color(primary_color.into())
    .apply();

assert_eq!(css, "color: var(--primary-color);");
Examples found in repository?
examples/css_variables.rs (line 7)
5fn main() {
6    // Create CSS variables
7    let primary_color = var("primary-color");
8    let spacing = var("spacing");
9    let display_mode = var("display-mode");
10    let font_weight = var("font-weight");
11
12    // Use CSS variables in style properties
13    // Method 1: Using explicit enum variants
14    let css1 = style()
15        .color(Color::Var(primary_color.clone()))
16        .background_color(Color::Rgba(240, 240, 240, 0.5))
17        .margin(Size::Var(spacing.clone()))
18        .padding(Size::Px(10))
19        .display(Display::Var(display_mode.clone()))
20        .font_weight(FontWeight::Var(font_weight.clone()))
21        .apply();
22
23    // Method 2: Using Into trait
24    let css2 = style()
25        .color(primary_color.clone().into())
26        .background_color(Color::Rgba(240, 240, 240, 0.5))
27        .margin(spacing.clone().into())
28        .padding(Size::Px(10))
29        .display(display_mode.into())
30        .font_weight(font_weight.into())
31        .apply();
32
33    println!("Generated CSS with variables (Method 1): {}", css1);
34    println!("Generated CSS with variables (Method 2): {}", css2);
35}