Skip to main content

typst_library/model/
divider.rs

1use crate::foundations::{Packed, ShowSet, Smart, StyleChain, Styles, elem};
2use crate::layout::{BlockElem, Em, Ratio};
3use crate::visualize::{LineElem, Stroke};
4
5/// A thematic break that separates sections of content.
6///
7/// By default, it renders as a horizontal line, but it can be customized
8/// through show rules.
9///
10/// = Example <example>
11/// ```example
12/// She left without a word.
13/// #divider()
14/// Three days later, she returned.
15/// ```
16///
17/// = Styling <styling>
18/// The divider can be styled through show rules.
19///
20/// Since the divider shows as a line by default, you can use a set rule to
21/// adjust the line's stroke:
22///
23/// ```example
24/// #show divider: set line(stroke: 2pt + red)
25/// First part
26/// #divider()
27/// Second part
28/// ```
29///
30/// You can also fully replace the divider with custom content like a floral or
31/// asterisks, but then you should wrap it in a block to preserve spacing:
32///
33/// ```example
34/// #show divider: set align(center)
35/// #show divider: block[∗ ∗ ∗]
36/// Chapter 1
37/// #divider()
38/// Chapter 2
39/// ```
40#[elem(ShowSet)]
41pub struct DividerElem {}
42
43impl ShowSet for Packed<DividerElem> {
44    fn show_set(&self, _: StyleChain) -> Styles {
45        let mut out = Styles::new();
46        out.set(BlockElem::above, Smart::Custom(Em::new(2.0).into()));
47        out.set(BlockElem::below, Smart::Custom(Em::new(2.0).into()));
48        out.set(LineElem::length, Ratio::one().into());
49        out.set(
50            LineElem::stroke,
51            Stroke {
52                thickness: Smart::Custom(Em::new(0.05).into()),
53                ..Default::default()
54            },
55        );
56        out
57    }
58}