Link
Displays a link that looks like a button or underline link.
Usage
Primary
This is the primary variant of a Link
Link(href: "#", variant: :primary) { "Primary" }
Copied!
Copy failed!
Secondary
This is the secondary variant of a Link
Link(href: "#", variant: :secondary) { "Secondary" }
Copied!
Copy failed!
Destructive
This is the destructive variant of a Link
Link(href: "#", variant: :destructive) { "Destructive" }
Copied!
Copy failed!
With icon
This is the primary variant of a Link with an icon
Link(href: "#", variant: :primary) do email_icon span { "Login with Email" } end
Copied!
Copy failed!
Ghost
This is the ghost variant of a Link
Link(href: "#", variant: :ghost) { "Ghost" }
Copied!
Copy failed!
Installation
Using RubyUI CLI
Run the install command
rails g ruby_ui:component Link
Copied!
Copy failed!
Manual installation
1
Add RubyUI::Link to app/components/ruby_ui/link/link.rb
# frozen_string_literal: true module RubyUI class Link < Base BASE_CLASSES = [ "whitespace-nowrap inline-flex items-center justify-center rounded-md font-medium transition-colors", "disabled:pointer-events-none disabled:opacity-50", "focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring", "aria-disabled:pointer-events-none aria-disabled:opacity-50 aria-disabled:cursor-not-allowed" ].freeze def initialize(href: "#", variant: :link, size: :md, icon: false, **attrs) @href = href @variant = variant.to_sym @size = size.to_sym @icon = icon super(**attrs) end def view_template(&) a(href: @href, **attrs, &) end private def size_classes if @icon case @size when :sm then "h-6 w-6" when :md then "h-9 w-9" when :lg then "h-10 w-10" when :xl then "h-12 w-12" end else case @size when :sm then "px-3 py-1.5 h-8 text-xs" when :md then "px-4 py-2 h-9 text-sm" when :lg then "px-4 py-2 h-10 text-base" when :xl then "px-6 py-3 h-12 text-base" end end end def primary_classes [ BASE_CLASSES, size_classes, "bg-primary text-primary-foreground shadow", "hover:bg-primary/90" ] end def link_classes [ BASE_CLASSES, size_classes, "text-primary underline-offset-4", "hover:underline" ] end def secondary_classes [ BASE_CLASSES, size_classes, "bg-secondary text-secondary-foreground", "hover:bg-opacity-80" ] end def destructive_classes [ BASE_CLASSES, size_classes, "bg-destructive text-white shadow-sm", "[a&]:hover:bg-destructive/90 focus-visible:ring-destructive/20", "dark:focus-visible:ring-destructive/40 dark:bg-destructive/60" ] end def outline_classes [ BASE_CLASSES, size_classes, "border border-input bg-background shadow-sm", "hover:bg-accent hover:text-accent-foreground" ] end def ghost_classes [ BASE_CLASSES, size_classes, "hover:bg-accent hover:text-accent-foreground" ] end def default_classes case @variant when :primary then primary_classes when :link then link_classes when :secondary then secondary_classes when :destructive then destructive_classes when :outline then outline_classes when :ghost then ghost_classes end end def default_attrs {type: "button", class: default_classes} end end end
Copied!
Copy failed!
2
Add RubyUI::LinkDocs to app/components/ruby_ui/link/link_docs.rb
# frozen_string_literal: true class Views::Docs::Link < Views::Base def view_template component = "Link" div(class: "max-w-2xl mx-auto w-full py-10 space-y-10") do render Docs::Header.new(title: "Link", description: "Displays a link that looks like a button or underline link.") Heading(level: 2) { "Usage" } render Docs::VisualCodeExample.new(title: "Example", description: "This is the default appearance of a Link", context: self) do <<~RUBY Link(href: "#") { "Link" } RUBY end render Docs::VisualCodeExample.new(title: "Aria Disabled", context: self) do <<~RUBY Link(aria: {disabled: "true"}, href: "#") { "Link" } RUBY end render Docs::VisualCodeExample.new(title: "Primary", description: "This is the primary variant of a Link", context: self) do <<~RUBY Link(href: "#", variant: :primary) { "Primary" } RUBY end render Docs::VisualCodeExample.new(title: "Secondary", description: "This is the secondary variant of a Link", context: self) do <<~RUBY Link(href: "#", variant: :secondary) { "Secondary" } RUBY end render Docs::VisualCodeExample.new(title: "Destructive", description: "This is the destructive variant of a Link", context: self) do <<~RUBY Link(href: "#", variant: :destructive) { "Destructive" } RUBY end render Docs::VisualCodeExample.new(title: "Icon", description: "This is the icon variant of a Link", context: self) do <<~RUBY Link(href: "#", variant: :outline, icon: true) do chevron_icon end RUBY end render Docs::VisualCodeExample.new(title: "With Icon", description: "This is the primary variant of a Link with an icon", context: self) do <<~RUBY Link(href: "#", variant: :primary) do email_icon span { "Login with Email" } end RUBY end render Docs::VisualCodeExample.new(title: "Ghost", description: "This is the ghost variant of a Link", context: self) do <<~RUBY Link(href: "#", variant: :ghost) { "Ghost" } RUBY end render Components::ComponentSetup::Tabs.new(component_name: component) render Docs::ComponentsTable.new(component_files(component)) end end private def chevron_icon svg( xmlns: "http://www.w3.org/2000/svg", viewbox: "0 0 20 20", fill: "currentColor", class: "w-5 h-5" ) do |s| s.path( fill_rule: "evenodd", d: "M7.21 14.77a.75.75 0 01.02-1.06L11.168 10 7.23 6.29a.75.75 0 111.04-1.08l4.5 4.25a.75.75 0 010 1.08l-4.5 4.25a.75.75 0 01-1.06-.02z", clip_rule: "evenodd" ) end end def email_icon svg( xmlns: "http://www.w3.org/2000/svg", fill: "none", viewbox: "0 0 24 24", stroke_width: "1.5", stroke: "currentColor", class: "w-4 h-4 mr-2" ) do |s| s.path( stroke_linecap: "round", stroke_linejoin: "round", d: "M21.75 6.75v10.5a2.25 2.25 0 01-2.25 2.25h-15a2.25 2.25 0 01-2.25-2.25V6.75m19.5 0A2.25 2.25 0 0019.5 4.5h-15a2.25 2.25 0 00-2.25 2.25m19.5 0v.243a2.25 2.25 0 01-1.07 1.916l-7.5 4.615a2.25 2.25 0 01-2.36 0L3.32 8.91a2.25 2.25 0 01-1.07-1.916V6.75" ) end end end
Copied!
Copy failed!
Components
| Component | Built using | Source |
|---|---|---|
Link | Phlex | |
LinkDocs | Phlex |