Typography
Sensible defaults to use for text.
Usage
H1
This is an H1 title
Heading(level: 1) { "This is an H1 title" }
H2
This is an H2 title
Heading(level: 2) { "This is an H2 title" }
H3
This is an H3 title
Heading(level: 3) { "This is an H3 title" }
H4
This is an H4 title
Heading(level: 4) { "This is an H4 title" }
P
This is an P tag
Text { "This is an P tag" }
Inline link
Checkout our installation instructions to get started.
Text(class: 'text-center') do
plain "Checkout our "
InlineLink(href: docs_installation_path) { "installation instructions" }
plain " to get started."
end
List
- Phlex is fast
- Phlex is easy to use
- Phlex is awesome
Components.TypographyList(items: [
'Phlex is fast',
'Phlex is easy to use',
'Phlex is awesome',
])
Numbered list
- Copy
- Paste
- Customize
Components.TypographyList(items: [
'Copy',
'Paste',
'Customize',
], numbered: true)
Inline code
This is an inline code blockInlineCode { "This is an inline code block" }
Lead
A modal dialog that interrupts the user with important content and expects a response.
Text(as: "p", size: "5", weight: "muted") { "A modal dialog that interrupts the user with important content and expects a response." }
Large
Are you sure absolutely sure?
Text(size: "4", weight: "semibold") { "Are you sure absolutely sure?" }
Small
Email address
Text(size: "sm") { "Email address" }
Muted
Enter your email address.
Text(size: "2", class: "text-muted-foreground") { "Enter your email address." }
Installation
Using RubyUI CLI
Run the install command
rails g ruby_ui:component Typography
Manual installation
1
Add RubyUI::Heading to app/components/ruby_ui/typography/heading.rb
# frozen_string_literal: true
module RubyUI
class Heading < Base
def initialize(level: nil, as: nil, size: nil, **attrs)
@level = level
@as = as
@size = size
super(**attrs)
end
def view_template(&)
tag = determine_tag
public_send(tag, **attrs, &)
end
private
def determine_tag
return @as if @as
return "h#{@level}" if @level
"h1"
end
def default_attrs
{
class: class_names
}
end
def class_names
base_classes = "scroll-m-20 font-bold tracking-tight"
size_class = size_to_class[(@size || level_to_size[@level&.to_s] || "6").to_s]
"#{base_classes} #{size_class}"
end
def size_to_class
{
"1" => "text-xs",
"2" => "text-sm",
"3" => "text-base",
"4" => "text-lg",
"5" => "text-xl",
"6" => "text-2xl",
"7" => "text-3xl lg:text-4xl",
"8" => "text-4xl",
"9" => "text-5xl"
}
end
def level_to_size
{
"1" => "7",
"2" => "6",
"3" => "5",
"4" => "4"
}
end
end
end
2
Add RubyUI::InlineCode to app/components/ruby_ui/typography/inline_code.rb
# frozen_string_literal: true
module RubyUI
class InlineCode < Base
def view_template(&)
code(**attrs, &)
end
private
def default_attrs
{
class: "relative rounded bg-muted px-[0.3rem] py-[0.2rem] font-mono text-sm font-semibold"
}
end
end
end
3
Add RubyUI::InlineLink to app/components/ruby_ui/typography/inline_link.rb
# frozen_string_literal: true
module RubyUI
class InlineLink < Base
def initialize(href:, **attrs)
super(**attrs)
@href = href
end
def view_template(&)
a(href: @href, **attrs, &)
end
private
def default_attrs
{
class: "text-primary font-medium hover:underline underline-offset-4 cursor-pointer"
}
end
end
end
4
Add RubyUI::Text to app/components/ruby_ui/typography/text.rb
# frozen_string_literal: true
module RubyUI
class Text < Base
def initialize(as: "p", size: "3", weight: "regular", **attrs)
@as = as
@size = size
@weight = weight
super(**attrs)
end
def view_template(&)
public_send(@as, **attrs, &)
end
private
def default_attrs
{
class: class_names
}
end
def class_names
"#{size_to_class[@size]} #{weight_to_class[@weight]}"
end
def size_to_class
{
"1" => "text-xs", "xs" => "text-xs",
"2" => "text-sm", "sm" => "text-sm",
"3" => "text-base", "base" => "text-base",
"4" => "text-lg", "lg" => "text-lg",
"5" => "text-xl", "xl" => "text-xl",
"6" => "text-2xl", "2xl" => "text-2xl",
"7" => "text-3xl", "3xl" => "text-3xl",
"8" => "text-4xl", "4xl" => "text-4xl",
"9" => "text-5xl", "5xl" => "text-5xl"
}
end
def weight_to_class
{
"muted" => "text-muted-foreground",
"light" => "font-light",
"regular" => "font-normal",
"medium" => "font-medium",
"semibold" => "font-semibold",
"bold" => "font-bold"
}
end
end
end
5
Add RubyUI::TypographyBlockquote to app/components/ruby_ui/typography/typography_blockquote.rb
# frozen_string_literal: true
module RubyUI
class TypographyBlockquote < Base
def view_template(&)
blockquote(**attrs, &)
end
private
def default_attrs
{
class: "mt-6 border-l-2 pl-6 italic"
}
end
end
end
6
Add RubyUI::TypographyDocs to app/components/ruby_ui/typography/typography_docs.rb
# frozen_string_literal: true
class Views::Docs::Typography < Views::Base
def view_template
component = "Typography"
div(class: "max-w-2xl mx-auto w-full py-10 space-y-10") do
render Docs::Header.new(title: "Typography", description: "Sensible defaults to use for text.")
Components.Heading(level: 2) { "Usage" }
render Docs::VisualCodeExample.new(title: "h1", context: self) do
<<~RUBY
Heading(level: 1) { "This is an H1 title" }
RUBY
end
render Docs::VisualCodeExample.new(title: "h2", context: self) do
<<~RUBY
Heading(level: 2) { "This is an H2 title" }
RUBY
end
render Docs::VisualCodeExample.new(title: "h3", context: self) do
<<~RUBY
Heading(level: 3) { "This is an H3 title" }
RUBY
end
render Docs::VisualCodeExample.new(title: "h4", context: self) do
<<~RUBY
Heading(level: 4) { "This is an H4 title" }
RUBY
end
render Docs::VisualCodeExample.new(title: "p", context: self) do
<<~RUBY
Text { "This is an P tag" }
RUBY
end
render Docs::VisualCodeExample.new(title: "Inline Link", context: self) do
<<~RUBY
Text(class: 'text-center') do
plain "Checkout our "
InlineLink(href: docs_installation_path) { "installation instructions" }
plain " to get started."
end
RUBY
end
render Docs::VisualCodeExample.new(title: "List", context: self) do
<<~RUBY
Components.TypographyList(items: [
'Phlex is fast',
'Phlex is easy to use',
'Phlex is awesome',
])
RUBY
end
render Docs::VisualCodeExample.new(title: "Numbered List", context: self) do
<<~RUBY
Components.TypographyList(items: [
'Copy',
'Paste',
'Customize',
], numbered: true)
RUBY
end
render Docs::VisualCodeExample.new(title: "Inline Code", context: self) do
<<~RUBY
InlineCode { "This is an inline code block" }
RUBY
end
render Docs::VisualCodeExample.new(title: "Lead", context: self) do
<<~RUBY
Text(as: "p", size: "5", weight: "muted") { "A modal dialog that interrupts the user with important content and expects a response." }
RUBY
end
render Docs::VisualCodeExample.new(title: "Large", context: self) do
<<~RUBY
Text(size: "4", weight: "semibold") { "Are you sure absolutely sure?" }
RUBY
end
render Docs::VisualCodeExample.new(title: "Small", context: self) do
<<~RUBY
Text(size: "sm") { "Email address" }
RUBY
end
render Docs::VisualCodeExample.new(title: "Muted", context: self) do
<<~RUBY
Text(size: "2", class: "text-muted-foreground") { "Enter your email address." }
RUBY
end
render Components::ComponentSetup::Tabs.new(component_name: component)
render Docs::ComponentsTable.new(component_files(component))
end
end
end
Components
| Component | Built using | Source |
|---|---|---|
Heading | Phlex | |
InlineCode | Phlex | |
InlineLink | Phlex | |
Text | Phlex | |
TypographyBlockquote | Phlex | |
TypographyDocs | Phlex |