ratatui_kit/components/
text.rs

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