Headings, paragraphs, quotes, lists and other text styles for prose.
use gpui::{div, prelude::*, px};
use crate::ui::typography::Typography;
pub fn typography_demo() -> impl IntoElement {
div()
.flex()
.gap(px(40.0))
.child(
div()
.w(px(330.0))
.flex()
.flex_col()
.gap_2()
.child(Typography::h1("The Joke Tax"))
.child(Typography::lead("A king with a very bad idea."))
.child(Typography::h2("The King's Plan"))
.child(Typography::p(
"The king thought long and hard, then came up with a plan: tax every joke.",
))
.child(Typography::blockquote("\"Everyone enjoys a good joke,\" he said.")),
)
.child(
div()
.w(px(270.0))
.flex()
.flex_col()
.gap_2()
.child(Typography::h3("The Joke Tax"))
.child(Typography::h4("Jokes went quiet"))
.child(Typography::list(["1st level: 5 gold", "2nd level: 10 gold"]))
.child(
div()
.flex()
.items_center()
.gap_2()
.child(Typography::small("Run"))
.child(Typography::inline_code("cargo hyuskui init")),
)
.child(Typography::large("Are you absolutely sure?"))
.child(Typography::muted("Enter your email address.")),
)
}Installation
Copies src/ui/typography.rs into your project, along with any components it depends on, and registers the module in src/ui/mod.rs.
Install the theme
Every component reads colors from
crate::theme. Follow the installation guide if you have not added it yet.Copy the source
Paste the code below into
src/ui/typography.rs.src/ui/typography.rs use gpui::{div, prelude::*, px, rems, App, FontWeight, SharedString, Window}; use crate::theme::ActiveTheme; /// The font used for inline code. Change it to a monospace family your app loads. const MONO: &str = "Geist Mono"; #[derive(Clone, Copy, PartialEq)] enum Style { H1, H2, H3, H4, P, Lead, Large, Small, Muted, Blockquote, InlineCode, } enum Content { Text(Style, SharedString), List(Vec<SharedString>), } /// Text styles for prose: `Typography::h1("Title")`, `Typography::p("Body")`, /// `Typography::list(["One", "Two"])`, and so on. #[derive(IntoElement)] pub struct Typography { content: Content, } impl Typography { fn text(style: Style, text: impl Into<SharedString>) -> Self { Self { content: Content::Text(style, text.into()), } } pub fn h1(text: impl Into<SharedString>) -> Self { Self::text(Style::H1, text) } /// A section heading with a rule under it. pub fn h2(text: impl Into<SharedString>) -> Self { Self::text(Style::H2, text) } pub fn h3(text: impl Into<SharedString>) -> Self { Self::text(Style::H3, text) } pub fn h4(text: impl Into<SharedString>) -> Self { Self::text(Style::H4, text) } pub fn p(text: impl Into<SharedString>) -> Self { Self::text(Style::P, text) } /// A larger, muted intro paragraph. pub fn lead(text: impl Into<SharedString>) -> Self { Self::text(Style::Lead, text) } pub fn large(text: impl Into<SharedString>) -> Self { Self::text(Style::Large, text) } pub fn small(text: impl Into<SharedString>) -> Self { Self::text(Style::Small, text) } pub fn muted(text: impl Into<SharedString>) -> Self { Self::text(Style::Muted, text) } pub fn blockquote(text: impl Into<SharedString>) -> Self { Self::text(Style::Blockquote, text) } /// A short code span. GPUI lays out text runs per element, so it sits beside other text /// in a flex row rather than inside a paragraph. pub fn inline_code(text: impl Into<SharedString>) -> Self { Self::text(Style::InlineCode, text) } /// A bulleted list. pub fn list<S: Into<SharedString>>(items: impl IntoIterator<Item = S>) -> Self { Self { content: Content::List(items.into_iter().map(Into::into).collect()), } } } impl RenderOnce for Typography { fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement { let theme = cx.theme(); let (fg, muted) = (theme.foreground, theme.muted_foreground); let (style, text) = match self.content { Content::List(items) => { return div() .flex() .flex_col() .gap_2() .pl(px(24.0)) .text_color(fg) .line_height(rems(1.75)) .children(items.into_iter().map(|item| { // The bullet is a dot rather than a glyph so it does not depend on the font. div() .flex() .items_start() .gap(px(10.0)) .child( div() .flex_none() .mt(px(12.0)) .size(px(4.0)) .rounded_full() .bg(fg), ) .child(item) })) .into_any_element(); } Content::Text(style, text) => (style, text), }; let base = div().text_color(fg); match style { Style::H1 => base .text_size(rems(2.25)) .line_height(rems(2.5)) .font_weight(FontWeight::EXTRA_BOLD), Style::H2 => base .pb_2() .border_b_1() .border_color(theme.border) .text_size(rems(1.875)) .line_height(rems(2.25)) .font_weight(FontWeight::SEMIBOLD), Style::H3 => base .text_size(rems(1.5)) .line_height(rems(2.0)) .font_weight(FontWeight::SEMIBOLD), Style::H4 => base .text_size(rems(1.25)) .line_height(rems(1.75)) .font_weight(FontWeight::SEMIBOLD), Style::P => base.line_height(rems(1.75)), Style::Lead => base .text_size(rems(1.25)) .line_height(rems(1.75)) .text_color(muted), Style::Large => base .text_size(rems(1.125)) .line_height(rems(1.75)) .font_weight(FontWeight::SEMIBOLD), Style::Small => base .text_sm() .line_height(rems(1.0)) .font_weight(FontWeight::MEDIUM), Style::Muted => base.text_sm().text_color(muted), Style::Blockquote => base.pl(px(24.0)).border_l_2().border_color(theme.border).italic(), Style::InlineCode => base .flex_none() .px(px(5.0)) .py(px(3.0)) .rounded(px(4.0)) .bg(theme.muted) .font_family(MONO) .text_sm() .font_weight(FontWeight::SEMIBOLD), } .child(text) .into_any_element() } }Register the module
// src/ui/mod.rs pub mod typography;
Usage
use crate::ui::typography::*;Examples
h1
Typography::h1 is the page title: extra bold at 36px.
use gpui::{div, prelude::*, px};
use crate::ui::typography::Typography;
pub fn typography_h1() -> impl IntoElement {
div().w(px(560.0)).child(Typography::h1("Taxing Laughter: The Joke Tax Chronicles"))
}h2
Typography::h2 starts a section and draws a rule under it.
use gpui::{div, prelude::*, px};
use crate::ui::typography::Typography;
pub fn typography_h2() -> impl IntoElement {
div().w(px(560.0)).child(Typography::h2("The People of the Kingdom"))
}h3
Typography::h3 heads a subsection.
use gpui::prelude::*;
use crate::ui::typography::Typography;
pub fn typography_h3() -> impl IntoElement {
Typography::h3("The Joke Tax")
}h4
Typography::h4 is the smallest heading.
use gpui::prelude::*;
use crate::ui::typography::Typography;
pub fn typography_h4() -> impl IntoElement {
Typography::h4("People stopped telling jokes")
}Paragraph
Typography::p is body text with relaxed line height.
use gpui::{div, prelude::*, px};
use crate::ui::typography::Typography;
pub fn typography_p() -> impl IntoElement {
div().w(px(480.0)).child(Typography::p("The king, seeing how much happier his subjects were, realized the error of his ways and repealed the joke tax."))
}Blockquote
Typography::blockquote sets a quotation in italics behind a left rule.
use gpui::{div, prelude::*, px};
use crate::ui::typography::Typography;
pub fn typography_blockquote() -> impl IntoElement {
div().w(px(480.0)).child(Typography::blockquote("\"After all,\" he said, \"everyone enjoys a good joke, so it's only fair that they should pay for the privilege.\""))
}Table
Prose tables use the Table component: a header row, body rows and dividers.
use gpui::{div, prelude::*, px};
use crate::ui::table::{Table, TableColumn};
const TREASURY: [[&str; 2]; 3] = [
["Empty", "Overflowing"],
["Modest", "Satisfied"],
["Full", "Ecstatic"],
];
pub fn typography_table() -> impl IntoElement {
let table = Table::new()
.column(TableColumn::new("King's Treasury"))
.column(TableColumn::new("People's happiness"));
div()
.w(px(400.0))
.child(TREASURY.into_iter().fold(table, |table, row| table.row(row)))
}List
Typography::list renders a bulleted list from any iterator of strings.
use gpui::prelude::*;
use crate::ui::typography::Typography;
pub fn typography_list() -> impl IntoElement {
Typography::list([
"1st level of puns: 5 gold coins",
"2nd level of jokes: 10 gold coins",
"3rd level of one-liners: 20 gold coins",
])
}Inline code
Typography::inline_code is a monospace span on a muted fill. Place it beside other text in a flex row.
use gpui::{div, prelude::*};
use crate::ui::typography::Typography;
pub fn typography_inline_code() -> impl IntoElement {
div()
.flex()
.items_center()
.gap_2()
.child(Typography::p("Install it with"))
.child(Typography::inline_code("cargo hyuskui add typography"))
}Lead
Typography::lead is a larger, muted intro paragraph.
use gpui::{div, prelude::*, px};
use crate::ui::typography::Typography;
pub fn typography_lead() -> impl IntoElement {
div().w(px(480.0)).child(Typography::lead("A modal dialog that interrupts the user with important content and expects a response."))
}Large
Typography::large is emphasized text a step above body size.
use gpui::prelude::*;
use crate::ui::typography::Typography;
pub fn typography_large() -> impl IntoElement {
Typography::large("Are you absolutely sure?")
}Small
Typography::small is compact medium-weight text for labels.
use gpui::prelude::*;
use crate::ui::typography::Typography;
pub fn typography_small() -> impl IntoElement {
Typography::small("Email address")
}Muted
Typography::muted is small secondary text, such as hints.
use gpui::prelude::*;
use crate::ui::typography::Typography;
pub fn typography_muted() -> impl IntoElement {
Typography::muted("Enter your email address.")
}API Reference
Typography
Text styles for prose: Typography::h1("Title"), Typography::p("Body"), Typography::list(["One", "Two"]), and so on.
Typography::h1(text: impl Into<SharedString>) -> SelfTypography::h2(text: impl Into<SharedString>) -> SelfA section heading with a rule under it.
Typography::h3(text: impl Into<SharedString>) -> SelfTypography::h4(text: impl Into<SharedString>) -> SelfTypography::p(text: impl Into<SharedString>) -> SelfTypography::lead(text: impl Into<SharedString>) -> SelfA larger, muted intro paragraph.
Typography::large(text: impl Into<SharedString>) -> SelfTypography::small(text: impl Into<SharedString>) -> SelfTypography::muted(text: impl Into<SharedString>) -> SelfTypography::blockquote(text: impl Into<SharedString>) -> SelfTypography::inline_code(text: impl Into<SharedString>) -> SelfA short code span. GPUI lays out text runs per element, so it sits beside other text in a flex row rather than inside a paragraph.
Typography::list<S: Into<SharedString>>(items: impl IntoIterator<Item = S>) -> SelfA bulleted list.