Skip to main content

takumi_css/style/properties/
overflow.rs

1use cssparser::match_ignore_ascii_case;
2use taffy::Overflow as TaffyOverflow;
3
4use crate::style::{declare_enum_from_css_impl, tw::TailwindPropertyParser};
5
6/// How children overflowing their container should affect layout
7#[derive(Debug, Clone, Copy, PartialEq, Default)]
8#[non_exhaustive]
9pub enum Overflow {
10  /// The automatic minimum size of this node as a flexbox/grid item should be based on the size of its content.
11  /// Content that overflows this node *should* contribute to the scroll region of its parent.
12  #[default]
13  Visible,
14  /// The automatic minimum size of this node as a flexbox/grid item should be based on the size of its content.
15  /// Content that overflows this node should *not* contribute to the scroll region of its parent.
16  Clip,
17  /// The automatic minimum size of this node as a flexbox/grid item should be `0`.
18  /// Content that overflows this node should *not* contribute to the scroll region of its parent.
19  Hidden,
20}
21
22declare_enum_from_css_impl!(
23  Overflow,
24  "visible" => Overflow::Visible,
25  "clip" => Overflow::Clip,
26  "hidden" => Overflow::Hidden,
27);
28
29impl TailwindPropertyParser for Overflow {
30  fn parse_tw(token: &str) -> Option<Self> {
31    match_ignore_ascii_case! {token,
32      "visible" => Some(Overflow::Visible),
33      "clip" => Some(Overflow::Clip),
34      "hidden" => Some(Overflow::Hidden),
35      _ => None,
36    }
37  }
38}
39
40impl From<Overflow> for TaffyOverflow {
41  fn from(val: Overflow) -> Self {
42    match val {
43      Overflow::Visible => TaffyOverflow::Visible,
44      Overflow::Clip => TaffyOverflow::Clip,
45      Overflow::Hidden => TaffyOverflow::Hidden,
46    }
47  }
48}
49
50#[cfg(test)]
51mod tests {
52  use super::*;
53  use crate::style::FromCss;
54
55  #[test]
56  fn parses_css_clip() {
57    assert_eq!(Overflow::from_str("clip"), Ok(Overflow::Clip));
58  }
59
60  #[test]
61  fn parses_tailwind_clip() {
62    assert_eq!(Overflow::parse_tw("clip"), Some(Overflow::Clip));
63  }
64}