Class Name Resolution Helper Functions

flor::view::resolver::shared provides helper functions for class name resolution, used to parse state prefixes, colors, rounded corners, font weights and other common style class names.

use flor::view::resolver::shared::*;

parse_state_prefix

Parse state prefix in class name, return corresponding ControlState and remaining class name.

pub fn parse_state_prefix(class: &str) -> (ControlState, &str)

Supported Prefixes

PrefixReturned State
hover:ControlState::Hover
focus:ControlState::Focus
active:ControlState::Active
disabled:ControlState::Disabled
No prefixControlState::Normal

Example

let (state, rest) = parse_state_prefix("hover:bg-red-500");
// state = ControlState::Hover, rest = "bg-red-500"

let (state, rest) = parse_state_prefix("bg-blue-200");
// state = ControlState::Normal, rest = "bg-blue-200"

extract_bracket_value

Extract value inside brackets, used to parse [value] form arbitrary value class names.

pub fn extract_bracket_value(s: &str) -> Option<&str>

Parameters

ParameterTypeDescription
s&strString to parse

Return Value

  • If string starts with [ and ends with ], return content inside brackets
  • Otherwise return None

Example

let val = extract_bracket_value("[#fff]");
// val = Some("#fff")

let val = extract_bracket_value("#fff");
// val = None

parse_color

Parse color value, supports keywords, Hex and Tailwind color names.

pub fn parse_color(value: &str) -> Option<Color>

Supported Formats

FormatExampleDescription
Keywordtransparent, black, whitePredefined color names
Hex#fff, #ffffff3-digit or 6-digit hexadecimal
Bracket Hex[#fff], [#ffffff]Hex wrapped in brackets
Tailwindred-500, blue-100TW palette names

Tailwind Palette

Supports following palettes, each palette contains 50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950 total 11 shades:

  • Gray series: slate, gray, zinc, neutral
  • Red series: red, orange, amber, yellow
  • Green series: lime, green, emerald, teal, cyan
  • Blue series: sky, blue, indigo
  • Purple series: violet, purple, fuchsia, pink, rose

Example

let color = parse_color("transparent");
// color = Some(Color::rgba(0, 0, 0, 0))

let color = parse_color("#fff");
// color = Some(Color::from_hex_str("#fff"))

let color = parse_color("red-500");
// color = Some(Color::RED_500)

let color = parse_color("slate-700");
// color = Some(Color::SLATE_700)

parse_tw_color

Parse Tailwind color name, return corresponding Color.

pub fn parse_tw_color(color_name: &str, shade: &str) -> Option<Color>

Parameters

ParameterTypeDescription
color_name&strPalette name, like red, blue
shade&strShade number, like 500, 100

Return Value

  • If both palette name and shade are valid, return corresponding Color
  • Otherwise return None

Example

let color = parse_tw_color("red", "500");
// color = Some(Color::RED_500)

let color = parse_tw_color("emerald", "700");
// color = Some(Color::EMERALD_700)

let color = parse_tw_color("unknown", "500");
// color = None

parse_rounded

Parse rounded-* class name, return rounded corner value.

pub fn parse_rounded(class: &str) -> Option<f32>

Supported Class Names

Class NameReturn ValueDescription
rounded-none0.0No rounded corner
rounded-sm2.0Small rounded corner
rounded4.0Default rounded corner
rounded-md6.0Medium rounded corner
rounded-lg8.0Large rounded corner
rounded-xl12.0Larger rounded corner
rounded-2xl16.0Extra large rounded corner
rounded-3xl24.0Huge rounded corner
rounded-full9999.0Fully circular
rounded-[Npx]N.0Custom pixel value
rounded-[N]N.0Custom numeric value

Example

let radius = parse_rounded("rounded-lg");
// radius = Some(8.0)

let radius = parse_rounded("rounded-[12px]");
// radius = Some(12.0)

let radius = parse_rounded("rounded-unknown");
// radius = None

parse_font_weight

Parse font weight in font-* class name, return FontWeight.

pub fn parse_font_weight(name: &str) -> Option<FontWeight>

Supported Weight Names

Class NameReturn ValueCSS Weight
font-thinFontWeight::Thin100
font-extralightFontWeight::ExtraLight200
font-lightFontWeight::Light300
font-normalFontWeight::Normal400
font-mediumFontWeight::Medium500
font-semiboldFontWeight::SemiBold600
font-boldFontWeight::Bold700
font-extraboldFontWeight::ExtraBold800
font-blackFontWeight::Black900

Parameters

ParameterTypeDescription
name&strWeight name (without font- prefix), like bold, medium

Example

let weight = parse_font_weight("bold");
// weight = Some(FontWeight::Bold)

let weight = parse_font_weight("semibold");
// weight = Some(FontWeight::SemiBold)

let weight = parse_font_weight("unknown");
// weight = None