mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-10 07:04:17 +00:00
docs(block): add docs about style inheritance (#1190)
Fixes: https://github.com/ratatui-org/ratatui/issues/1129
This commit is contained in:
parent
272d0591a7
commit
6ce447c4f3
1 changed files with 49 additions and 10 deletions
|
@ -27,8 +27,8 @@ pub use title::{Position, Title};
|
|||
/// both centered and non-centered titles are rendered, the centered space is calculated based on
|
||||
/// the full width of the block, rather than the leftover width.
|
||||
///
|
||||
/// Titles are not rendered in the corners of the block unless there is no border on that edge.
|
||||
/// If the block is too small and multiple titles overlap, the border may get cut off at a corner.
|
||||
/// Titles are not rendered in the corners of the block unless there is no border on that edge. If
|
||||
/// the block is too small and multiple titles overlap, the border may get cut off at a corner.
|
||||
///
|
||||
/// ```plain
|
||||
/// ┌With at least a left border───
|
||||
|
@ -60,6 +60,10 @@ pub use title::{Position, Title};
|
|||
/// # Other Methods
|
||||
/// - [`Block::inner`] Compute the inner area of a block based on its border visibility rules.
|
||||
///
|
||||
/// [`Style`]s are applied first to the entire block, then to the borders, and finally to the
|
||||
/// titles. If the block is used as a container for another widget, the inner widget can also be
|
||||
/// styled. See [`Style`] for more information on how merging styles works.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
|
@ -252,7 +256,10 @@ impl<'a> Block<'a> {
|
|||
/// Note: If the block is too small and multiple titles overlap, the border might get cut off at
|
||||
/// a corner.
|
||||
///
|
||||
/// # Example
|
||||
/// # Examples
|
||||
///
|
||||
/// See the [Block example] for a visual representation of how the various borders and styles
|
||||
/// look when rendered.
|
||||
///
|
||||
/// The following example demonstrates:
|
||||
/// - Default title alignment
|
||||
|
@ -280,6 +287,8 @@ impl<'a> Block<'a> {
|
|||
/// - [`Block::title_style`]
|
||||
/// - [`Block::title_alignment`]
|
||||
/// - [`Block::title_position`]
|
||||
///
|
||||
/// [Block example]: https://github.com/ratatui-org/ratatui/blob/main/examples/README.md#block
|
||||
#[must_use = "method moves the value of self and returns the modified value"]
|
||||
pub fn title<T>(mut self, title: T) -> Self
|
||||
where
|
||||
|
@ -347,10 +356,14 @@ impl<'a> Block<'a> {
|
|||
|
||||
/// Applies the style to all titles.
|
||||
///
|
||||
/// This style will be applied to all titles of the block. If a title has a style set, it will
|
||||
/// be applied after this style. This style will be applied after any [`Block::style`] or
|
||||
/// [`Block::border_style`] is applied.
|
||||
///
|
||||
/// See [`Style`] for more information on how merging styles works.
|
||||
///
|
||||
/// `style` accepts any type that is convertible to [`Style`] (e.g. [`Style`], [`Color`], or
|
||||
/// your own type that implements [`Into<Style>`]).
|
||||
///
|
||||
/// If a [`Title`] already has a style, the title's style will add on top of this one.
|
||||
#[must_use = "method moves the value of self and returns the modified value"]
|
||||
pub fn title_style<S: Into<Style>>(mut self, style: S) -> Self {
|
||||
self.titles_style = style.into();
|
||||
|
@ -416,7 +429,10 @@ impl<'a> Block<'a> {
|
|||
|
||||
/// Defines the style of the borders.
|
||||
///
|
||||
/// If a [`Block::style`] is defined, `border_style` will be applied on top of it.
|
||||
/// This style is applied only to the areas covered by borders, and is applied to the block
|
||||
/// after any [`Block::style`] is applied.
|
||||
///
|
||||
/// See [`Style`] for more information on how merging styles works.
|
||||
///
|
||||
/// `style` accepts any type that is convertible to [`Style`] (e.g. [`Style`], [`Color`], or
|
||||
/// your own type that implements [`Into<Style>`]).
|
||||
|
@ -434,16 +450,39 @@ impl<'a> Block<'a> {
|
|||
self
|
||||
}
|
||||
|
||||
/// Defines the block style.
|
||||
/// Defines the style of the entire block.
|
||||
///
|
||||
/// This is the most generic [`Style`] a block can receive, it will be merged with any other
|
||||
/// more specific style. Elements can be styled further with [`Block::title_style`] and
|
||||
/// [`Block::border_style`].
|
||||
/// more specific styles. Elements can be styled further with [`Block::title_style`] and
|
||||
/// [`Block::border_style`], which will be applied on top of this style. If the block is used as
|
||||
/// a container for another widget (e.g. a [`Paragraph`]), then the style of the widget is
|
||||
/// generally applied before this style.
|
||||
///
|
||||
/// See [`Style`] for more information on how merging styles works.
|
||||
///
|
||||
/// `style` accepts any type that is convertible to [`Style`] (e.g. [`Style`], [`Color`], or
|
||||
/// your own type that implements [`Into<Style>`]).
|
||||
///
|
||||
/// This will also apply to the widget inside that block, unless the inner widget is styled.
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use ratatui::{prelude::*, widgets::*};
|
||||
/// let block = Block::new().style(Style::new().red().on_black());
|
||||
///
|
||||
/// // For border and title you can additionally apply styles on top of the block level style.
|
||||
/// let block = Block::new()
|
||||
/// .style(Style::new().red().bold().italic())
|
||||
/// .border_style(Style::new().not_italic()) // will be red and bold
|
||||
/// .title_style(Style::new().not_bold()) // will be red and italic
|
||||
/// .title("Title");
|
||||
///
|
||||
/// // To style the inner widget, you can style the widget itself.
|
||||
/// let paragraph = Paragraph::new("Content")
|
||||
/// .block(block)
|
||||
/// .style(Style::new().white().not_bold()); // will be white, and italic
|
||||
/// ```
|
||||
///
|
||||
/// [`Paragraph`]: crate::widgets::Paragraph
|
||||
#[must_use = "method moves the value of self and returns the modified value"]
|
||||
pub fn style<S: Into<Style>>(mut self, style: S) -> Self {
|
||||
self.style = style.into();
|
||||
|
|
Loading…
Reference in a new issue