ratatui_kit/components/
text.rs

1use crate::{AnyElement, element, prelude::Fragment};
2use ratatui::{layout::Position, style::Style, widgets::Paragraph};
3use ratatui_kit_macros::{Props, component};
4use std::ops::{Deref, DerefMut};
5
6#[derive(Clone, Default)]
7pub struct TextParagraph<'a> {
8    inner: Paragraph<'a>,
9}
10
11impl<'a> Deref for TextParagraph<'a> {
12    type Target = Paragraph<'a>;
13
14    fn deref(&self) -> &Self::Target {
15        &self.inner
16    }
17}
18
19impl DerefMut for TextParagraph<'_> {
20    fn deref_mut(&mut self) -> &mut Self::Target {
21        &mut self.inner
22    }
23}
24
25impl From<String> for TextParagraph<'_> {
26    fn from(value: String) -> Self {
27        Self {
28            inner: Paragraph::new(value),
29        }
30    }
31}
32
33impl<'a> From<Paragraph<'a>> for TextParagraph<'a> {
34    fn from(value: Paragraph<'a>) -> Self {
35        Self { inner: value }
36    }
37}
38
39#[derive(Default, Props)]
40pub struct TextProps {
41    pub text: TextParagraph<'static>,
42    pub style: Style,
43    pub alignment: ratatui::layout::Alignment,
44    pub scroll: Position,
45    pub wrap: Option<bool>,
46}
47
48#[component]
49pub fn Text(props: &TextProps) -> impl Into<AnyElement<'static>> {
50    let paragraph = props
51        .text
52        .inner
53        .clone()
54        .style(props.style)
55        .scroll(props.scroll.into())
56        .alignment(props.alignment);
57
58    let paragraph = if let Some(wrap) = props.wrap {
59        paragraph.wrap(ratatui::widgets::Wrap { trim: wrap })
60    } else {
61        paragraph
62    };
63
64    element! {
65        Fragment{
66            $paragraph
67        }
68    }
69}