Pagination
Pagination with page navigation, next and previous links.
Usage
Example
This is the default appearance of a Pagination
Pagination do PaginationContent do PaginationItem(href: "#") do chevrons_left_icon plain "First" end PaginationItem(href: "#") do chevron_left_icon plain "Prev" end PaginationEllipsis PaginationItem(href: "#") { "4" } PaginationItem(href: "#", active: true) { "5" } PaginationItem(href: "#") { "6" } PaginationEllipsis PaginationItem(href: "#") do plain "Next" chevron_right_icon end PaginationItem(href: "#") do plain "Last" chevrons_right_icon end end end
Copied!
Copy failed!
Installation
Using RubyUI CLI
Run the install command
rails g ruby_ui:component Pagination
Copied!
Copy failed!
Manual installation
1
Add RubyUI::Pagination to app/components/ruby_ui/pagination/pagination.rb
# frozen_string_literal: true module RubyUI class Pagination < Base def view_template(&) nav(**attrs, &) end private def default_attrs { aria: {label: "pagination"}, class: "mx-auto flex w-full justify-center", role: "navigation" } end end end
Copied!
Copy failed!
2
Add RubyUI::PaginationContent to app/components/ruby_ui/pagination/pagination_content.rb
# frozen_string_literal: true module RubyUI class PaginationContent < Base def view_template(&) ul(**attrs, &) end private def default_attrs { class: "flex flex-row items-center gap-1" } end end end
Copied!
Copy failed!
3
Add RubyUI::PaginationDocs to app/components/ruby_ui/pagination/pagination_docs.rb
# frozen_string_literal: true class Views::Docs::Pagination < Views::Base def view_template component = "Pagination" div(class: "max-w-2xl mx-auto w-full py-10 space-y-10") do render Docs::Header.new(title: "Pagination", description: "Pagination with page navigation, next and previous links.") Heading(level: 2) { "Usage" } render Docs::VisualCodeExample.new(title: "Example", description: "This is the default appearance of a Pagination", context: self) do <<~RUBY Pagination do PaginationContent do PaginationItem(href: "#") do chevrons_left_icon plain "First" end PaginationItem(href: "#") do chevron_left_icon plain "Prev" end PaginationEllipsis PaginationItem(href: "#") { "4" } PaginationItem(href: "#", active: true) { "5" } PaginationItem(href: "#") { "6" } PaginationEllipsis PaginationItem(href: "#") do plain "Next" chevron_right_icon end PaginationItem(href: "#") do plain "Last" chevrons_right_icon end end end RUBY end render Components::ComponentSetup::Tabs.new(component_name: component) render Docs::ComponentsTable.new(component_files(component)) end end private def chevrons_left_icon svg( xmlns: "http://www.w3.org/2000/svg", width: "24", height: "24", viewbox: "0 0 24 24", stroke_width: "1.5", stroke: "currentColor", fill: "none", stroke_linecap: "round", stroke_linejoin: "round", class: "h-4 w-4" ) do |s| s.path(stroke: "none", d: "M0 0h24v24H0z", fill: "none") s.path(d: "M11 7l-5 5l5 5") s.path(d: "M17 7l-5 5l5 5") end end def chevron_left_icon svg( xmlns: "http://www.w3.org/2000/svg", width: "24", height: "24", viewbox: "0 0 24 24", stroke_width: "1.5", stroke: "currentColor", fill: "none", stroke_linecap: "round", stroke_linejoin: "round", class: "h-4 w-4" ) do |s| s.path(stroke: "none", d: "M0 0h24v24H0z", fill: "none") s.path(d: "M15 6l-6 6l6 6") end end def chevrons_right_icon svg( xmlns: "http://www.w3.org/2000/svg", width: "24", height: "24", viewbox: "0 0 24 24", stroke_width: "1.5", stroke: "currentColor", fill: "none", stroke_linecap: "round", stroke_linejoin: "round", class: "h-4 w-4" ) do |s| s.path(stroke: "none", d: "M0 0h24v24H0z", fill: "none") s.path(d: "M7 7l5 5l-5 5") s.path(d: "M13 7l5 5l-5 5") end end def chevron_right_icon svg( xmlns: "http://www.w3.org/2000/svg", width: "24", height: "24", viewbox: "0 0 24 24", stroke_width: "1.5", stroke: "currentColor", fill: "none", stroke_linecap: "round", stroke_linejoin: "round", class: "h-4 w-4" ) do |s| s.path(stroke: "none", d: "M0 0h24v24H0z", fill: "none") s.path(d: "M9 6l6 6l-6 6") end end end
Copied!
Copy failed!
4
Add RubyUI::PaginationEllipsis to app/components/ruby_ui/pagination/pagination_ellipsis.rb
# frozen_string_literal: true module RubyUI class PaginationEllipsis < Base def view_template(&block) li do span(**attrs) do icon span(class: "sr-only") { "More pages" } end end end private def icon svg( xmlns: "http://www.w3.org/2000/svg", width: "24", height: "24", viewbox: "0 0 24 24", fill: "none", stroke: "currentColor", stroke_width: "2", stroke_linecap: "round", stroke_linejoin: "round", class: "h-4 w-4" ) do |s| s.circle(cx: "12", cy: "12", r: "1") s.circle(cx: "19", cy: "12", r: "1") s.circle(cx: "5", cy: "12", r: "1") end end def default_attrs { aria: {hidden: true}, class: "flex h-9 w-9 items-center justify-center" } end end end
Copied!
Copy failed!
5
Add RubyUI::PaginationItem to app/components/ruby_ui/pagination/pagination_item.rb
# frozen_string_literal: true module RubyUI class PaginationItem < Base def initialize(href: "#", active: false, **attrs) @href = href @active = active super(**attrs) end def view_template(&block) li do a(href: @href, **attrs, &block) end end private def default_attrs { aria: {current: @active ? "page" : nil}, class: [ RubyUI::Button.new(variant: @active ? :outline : :ghost).attrs[:class] ] } end end end
Copied!
Copy failed!
6
Install required components
Component Pagination relies on the following RubyUI components. Refer to their individual installation guides for setup instructions:
Components
| Component | Built using | Source |
|---|---|---|
Pagination | Phlex | |
PaginationContent | Phlex | |
PaginationDocs | Phlex | |
PaginationEllipsis | Phlex | |
PaginationItem | Phlex |