Class: Plutonium::UI::Table::Components::ViewSwitcher

Inherits:
Component::Base show all
Defined in:
lib/plutonium/ui/table/components/view_switcher.rb

Overview

Segmented control for switching between index views (Table / Grid). Renders nothing unless at least two views are enabled. Selection is persisted in a per-resource cookie that the server reads on the next request — no ‘?view=` URL pollution so filters / search / clear-x links don’t have to thread it through. The Stimulus controller sets the cookie on click, then reloads.

Constant Summary collapse

SEGMENT_LABELS =
{
  table: {label: "Table", icon: Phlex::TablerIcons::Table},
  grid: {label: "Grid", icon: Phlex::TablerIcons::LayoutGrid}
}.freeze

Instance Method Summary collapse

Methods included from Component::Behaviour

#around_template

Methods included from Component::Tokens

#classes, #tokens

Methods included from Component::Kit

#BuildActionButton, #BuildActionsDropdown, #BuildBlock, #BuildBreadcrumbs, #BuildBulkActionsToolbar, #BuildColorModeSelector, #BuildDynaFrameContent, #BuildDynaFrameHost, #BuildEmptyCard, #BuildFrameNavigatorPanel, #BuildModalCentered, #BuildModalSlideover, #BuildPageHeader, #BuildPanel, #BuildRowActionsDropdown, #BuildSkeletonTable, #BuildTabList, #BuildTableFilterPills, #BuildTableInfo, #BuildTablePagination, #BuildTableScopesBar, #BuildTableScopesPills, #BuildTableSearchBar, #BuildTableToolbar, #BuildTableViewSwitcher, #method_missing, #respond_to_missing?

Constructor Details

#initialize(views:, current:, cookie_name:, cookie_path: "/") ⇒ ViewSwitcher

Returns a new instance of ViewSwitcher.



20
21
22
23
24
25
# File 'lib/plutonium/ui/table/components/view_switcher.rb', line 20

def initialize(views:, current:, cookie_name:, cookie_path: "/")
  @views = views
  @current = current
  @cookie_name = cookie_name
  @cookie_path = cookie_path
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Plutonium::UI::Component::Kit

Instance Method Details

#render?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/plutonium/ui/table/components/view_switcher.rb', line 27

def render?
  @views.size > 1
end

#view_templateObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/plutonium/ui/table/components/view_switcher.rb', line 31

def view_template
  div(
    role: "tablist",
    aria: {label: "View"},
    class: "inline-flex h-8 rounded-md border border-[var(--pu-border)] bg-[var(--pu-surface)] overflow-hidden",
    data: {
      controller: "view-switcher",
      view_switcher_cookie_name_value: @cookie_name,
      view_switcher_cookie_path_value: @cookie_path
    }
  ) do
    @views.each_with_index do |key, i|
      render_segment(key, last: i == @views.length - 1)
    end
  end
end