velvetkit 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +143 -2
- data/lib/velvetkit/version.rb +1 -1
- data/registry/app/components/ui/accordion/trigger.rb +1 -1
- data/registry/app/components/ui/badge.rb +1 -1
- data/registry/app/components/ui/base.rb +2 -2
- data/registry/app/components/ui/button.rb +1 -1
- data/registry/app/components/ui/card/action.html.erb +1 -0
- data/registry/app/components/ui/card/action.rb +14 -0
- data/registry/app/components/ui/card/content.html.erb +1 -1
- data/registry/app/components/ui/card/content.rb +4 -2
- data/registry/app/components/ui/card/description.html.erb +1 -1
- data/registry/app/components/ui/card/description.rb +4 -2
- data/registry/app/components/ui/card/footer.html.erb +1 -1
- data/registry/app/components/ui/card/footer.rb +4 -2
- data/registry/app/components/ui/card/header.html.erb +1 -1
- data/registry/app/components/ui/card/header.rb +9 -2
- data/registry/app/components/ui/card/title.html.erb +1 -1
- data/registry/app/components/ui/card/title.rb +4 -2
- data/registry/app/components/ui/card.html.erb +1 -1
- data/registry/app/components/ui/card.rb +49 -3
- data/registry/app/components/ui/carousel.html.erb +7 -0
- data/registry/app/components/ui/carousel.rb +60 -0
- data/registry/app/components/ui/chart.html.erb +3 -3
- data/registry/app/components/ui/chart.rb +18 -10
- data/registry/app/components/ui/checkbox.html.erb +6 -0
- data/registry/app/components/ui/checkbox.rb +11 -4
- data/registry/app/components/ui/fieldset.html.erb +6 -0
- data/registry/app/components/ui/fieldset.rb +76 -0
- data/registry/app/components/ui/gallery/image.rb +14 -1
- data/registry/app/components/ui/gallery.html.erb +32 -3
- data/registry/app/components/ui/gallery.rb +46 -7
- data/registry/app/components/ui/link.rb +1 -1
- data/registry/app/components/ui/menu/item.rb +1 -1
- data/registry/app/components/ui/radio_group/item.html.erb +1 -1
- data/registry/app/components/ui/radio_group/item.rb +5 -1
- data/registry/app/components/ui/radio_group.html.erb +1 -1
- data/registry/app/components/ui/radio_group.rb +42 -2
- data/registry/app/components/ui/scrubber.html.erb +18 -0
- data/registry/app/components/ui/scrubber.rb +62 -0
- data/registry/app/components/ui/select.html.erb +1 -1
- data/registry/app/components/ui/select.rb +16 -1
- data/registry/app/components/ui/selection_bar.html.erb +7 -0
- data/registry/app/components/ui/selection_bar.rb +75 -0
- data/registry/app/components/ui/sidebar/group_label.rb +1 -1
- data/registry/app/components/ui/sidebar/inset.rb +5 -2
- data/registry/app/components/ui/sidebar/menu_button.rb +24 -9
- data/registry/app/components/ui/sidebar/menu_item.html.erb +1 -1
- data/registry/app/components/ui/sidebar/menu_item.rb +31 -5
- data/registry/app/components/ui/sidebar/menu_sub.rb +59 -0
- data/registry/app/components/ui/sidebar.html.erb +3 -3
- data/registry/app/components/ui/sidebar.rb +91 -13
- data/registry/app/components/ui/switch.rb +2 -2
- data/registry/app/components/ui/table/column.html.erb +1 -1
- data/registry/app/components/ui/table/column.rb +26 -2
- data/registry/app/components/ui/table/head.html.erb +1 -1
- data/registry/app/components/ui/table/head.rb +15 -2
- data/registry/app/components/ui/table/row.html.erb +1 -1
- data/registry/app/components/ui/table/row.rb +19 -5
- data/registry/app/components/ui/table.html.erb +2 -2
- data/registry/app/components/ui/table.rb +29 -4
- data/registry/app/components/ui/tabs/trigger.rb +1 -1
- data/registry/app/components/ui/toggle_group.html.erb +1 -1
- data/registry/app/components/ui/toggle_group.rb +26 -3
- data/registry/app/javascript/controllers/ui/carousel_controller.ts +63 -0
- data/registry/app/javascript/controllers/ui/file_field_controller.ts +1 -1
- data/registry/app/javascript/controllers/ui/gallery_controller.ts +52 -14
- data/registry/app/javascript/controllers/ui/scrubber_controller.ts +188 -0
- data/registry/app/javascript/controllers/ui/selection_bar_controller.ts +122 -0
- data/registry/app/javascript/controllers/ui/sidebar_controller.ts +102 -32
- data/registry/components.yml +12 -4
- data/registry/config/locales/velvetkit.en.yml +16 -0
- data/registry/icons/lucide/arrow-down.svg +16 -0
- data/registry/icons/lucide/arrow-up.svg +16 -0
- data/registry/icons/lucide/image.svg +17 -0
- data/registry/icons/lucide/star.svg +15 -0
- data/registry/icons/phosphor/arrow-down.svg +2 -0
- data/registry/icons/phosphor/arrow-up.svg +2 -0
- data/registry/icons/phosphor/image.svg +2 -0
- data/registry/icons/phosphor/star.svg +2 -0
- data/registry/icons/tabler/arrow-down.svg +18 -0
- data/registry/icons/tabler/arrow-up.svg +18 -0
- data/registry/icons/tabler/photo.svg +19 -0
- data/registry/icons/tabler/star.svg +16 -0
- data/registry/icons.yml +4 -0
- data/registry/test/components/previews/ui/card_preview/edge_to_edge.html.erb +19 -0
- data/registry/test/components/previews/ui/card_preview/image.html.erb +12 -0
- data/registry/test/components/previews/ui/card_preview/just_content.html.erb +10 -0
- data/registry/test/components/previews/ui/card_preview/open_bill.html.erb +18 -0
- data/registry/test/components/previews/ui/card_preview/playground.html.erb +17 -0
- data/registry/test/components/previews/ui/card_preview/settings_form.html.erb +15 -24
- data/registry/test/components/previews/ui/card_preview/sign_in.html.erb +23 -0
- data/registry/test/components/previews/ui/card_preview/small.html.erb +17 -0
- data/registry/test/components/previews/ui/card_preview/spacing.html.erb +13 -0
- data/registry/test/components/previews/ui/card_preview/stats.html.erb +11 -0
- data/registry/test/components/previews/ui/card_preview/team.html.erb +21 -0
- data/registry/test/components/previews/ui/card_preview.rb +42 -28
- data/registry/test/components/previews/ui/carousel_preview/highlights.html.erb +17 -0
- data/registry/test/components/previews/ui/carousel_preview.rb +22 -0
- data/registry/test/components/previews/ui/chart_preview/whole_dollars.html.erb +19 -0
- data/registry/test/components/previews/ui/chart_preview.rb +7 -1
- data/registry/test/components/previews/ui/checkbox_preview/in_form.html.erb +2 -3
- data/registry/test/components/previews/ui/fieldset_preview/block_of_fields.html.erb +18 -0
- data/registry/test/components/previews/ui/fieldset_preview/groups.html.erb +14 -0
- data/registry/test/components/previews/ui/fieldset_preview/model_errors.html.erb +22 -0
- data/registry/test/components/previews/ui/fieldset_preview/playground.html.erb +5 -0
- data/registry/test/components/previews/ui/fieldset_preview.rb +24 -0
- data/registry/test/components/previews/ui/gallery_preview/grid.html.erb +25 -0
- data/registry/test/components/previews/ui/gallery_preview.rb +3 -0
- data/registry/test/components/previews/ui/scrubber_preview/default.html.erb +24 -0
- data/registry/test/components/previews/ui/scrubber_preview.rb +6 -0
- data/registry/test/components/previews/ui/selection_bar_preview/floating.html.erb +16 -0
- data/registry/test/components/previews/ui/selection_bar_preview/table.html.erb +26 -0
- data/registry/test/components/previews/ui/selection_bar_preview.rb +9 -0
- data/registry/test/components/previews/ui/sidebar_preview/_shell.html.erb +5 -4
- data/registry/test/components/previews/ui/sidebar_preview/pill_links.html.erb +52 -0
- data/registry/test/components/previews/ui/sidebar_preview.rb +3 -0
- data/registry/test/components/previews/ui/table_preview/selectable.html.erb +26 -0
- data/registry/test/components/previews/ui/table_preview/sortable.html.erb +20 -0
- data/registry/test/components/previews/ui/table_preview/sticky_header.html.erb +13 -0
- data/registry/test/components/previews/ui/table_preview.rb +9 -0
- data/registry/test/components/ui/button_test.rb +2 -2
- data/registry/test/components/ui/card/action_test.rb +19 -0
- data/registry/test/components/ui/card/content_test.rb +1 -11
- data/registry/test/components/ui/card/footer_test.rb +3 -2
- data/registry/test/components/ui/card/header_test.rb +3 -4
- data/registry/test/components/ui/card/title_test.rb +3 -4
- data/registry/test/components/ui/card_test.rb +63 -2
- data/registry/test/components/ui/carousel_test.rb +38 -0
- data/registry/test/components/ui/chart_test.rb +20 -0
- data/registry/test/components/ui/checkbox_test.rb +11 -2
- data/registry/test/components/ui/fieldset_test.rb +119 -0
- data/registry/test/components/ui/gallery_test.rb +40 -5
- data/registry/test/components/ui/link_test.rb +1 -1
- data/registry/test/components/ui/number_input_test.rb +1 -1
- data/registry/test/components/ui/radio_group_test.rb +106 -0
- data/registry/test/components/ui/scrubber_test.rb +43 -0
- data/registry/test/components/ui/select_test.rb +21 -0
- data/registry/test/components/ui/selection_bar_test.rb +46 -0
- data/registry/test/components/ui/sidebar/menu_button_test.rb +9 -0
- data/registry/test/components/ui/sidebar_test.rb +79 -1
- data/registry/test/components/ui/switch_test.rb +4 -4
- data/registry/test/components/ui/toggle_group_test.rb +45 -0
- metadata +60 -5
- data/registry/test/components/previews/ui/card_preview/complete.html.erb +0 -31
- data/registry/test/components/previews/ui/card_preview/default.html.erb +0 -7
- data/registry/test/components/previews/ui/card_preview/with_footer.html.erb +0 -13
- data/registry/test/components/previews/ui/card_preview/with_header.html.erb +0 -11
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# What to do with the things chosen on a page: "3 selected", a button to clear
|
|
4
|
+
# them, and the actions that work on all of them at once.
|
|
5
|
+
#
|
|
6
|
+
# <%= render(UI::SelectionBar.new(for: "customers")) do |bar| %>
|
|
7
|
+
# <% bar.with_action do %>
|
|
8
|
+
# <%= render(UI::Button.new(variant: :ghost, type: :submit, form: "bulk", formaction: export_customers_path)) { "Export" } %>
|
|
9
|
+
# <% end %>
|
|
10
|
+
# <% end %>
|
|
11
|
+
#
|
|
12
|
+
# `for:` is the id of what holds the checkboxes: a `UI::Table` with
|
|
13
|
+
# `selection:`, or any list whose boxes carry `data-selection="item"` (and one
|
|
14
|
+
# with `data-selection="all"` to choose them all). The bar shows once one is
|
|
15
|
+
# checked and hides when none are, keeps "select all" checked, unchecked or
|
|
16
|
+
# partly checked to match, and lets Shift-click check a run of them. Escape
|
|
17
|
+
# or the clear button unchecks them all.
|
|
18
|
+
#
|
|
19
|
+
# An "all" box inside an element with `data-selection-group` chooses only the
|
|
20
|
+
# items in it: a day's photos under that day's heading.
|
|
21
|
+
#
|
|
22
|
+
# The boxes post like any checkbox, so an action is a submit button for the
|
|
23
|
+
# form they belong to (`form:` and `formaction:` on the button, `form:` on the
|
|
24
|
+
# table). A `selection-bar:change` event carries the count and the values, for
|
|
25
|
+
# a page that does more.
|
|
26
|
+
#
|
|
27
|
+
# `placement:` is :top, a bar that sticks to the top of its scrolling area, or
|
|
28
|
+
# :floating, a rounded bar near the bottom of the window.
|
|
29
|
+
class UI::SelectionBar < UI::Base
|
|
30
|
+
PLACEMENTS = {
|
|
31
|
+
top: "sticky top-0 z-30 flex h-14 w-full items-center gap-2 border-b bg-background/95 px-2 backdrop-blur " \
|
|
32
|
+
"supports-[backdrop-filter]:bg-background/80 sm:px-4",
|
|
33
|
+
floating: "fixed inset-x-4 bottom-4 z-40 mx-auto flex w-auto max-w-xl items-center gap-2 rounded-full border " \
|
|
34
|
+
"bg-popover py-1.5 ps-1.5 pe-2 text-popover-foreground shadow-lg sm:inset-x-0 sm:w-fit"
|
|
35
|
+
}.freeze
|
|
36
|
+
|
|
37
|
+
renders_many :actions
|
|
38
|
+
|
|
39
|
+
def initialize(for:, placement: :top, label: nil, class: nil, **html_attrs)
|
|
40
|
+
@for = binding.local_variable_get(:for)
|
|
41
|
+
@placement = PLACEMENTS.key?(placement.to_sym) ? placement.to_sym : :top
|
|
42
|
+
@label = label
|
|
43
|
+
@user_class = binding.local_variable_get(:class)
|
|
44
|
+
@html_attrs = html_attrs
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def root_attrs
|
|
48
|
+
attrs = @html_attrs.except(:data, :aria)
|
|
49
|
+
attrs[:role] = "region"
|
|
50
|
+
attrs[:hidden] = true
|
|
51
|
+
attrs[:class] = cn(PLACEMENTS[@placement], "[&[hidden]]:hidden", @user_class)
|
|
52
|
+
attrs[:aria] = { label: @label || t("ui.selection_bar.label", default: "Selection") }.merge(@html_attrs[:aria] || {})
|
|
53
|
+
attrs[:data] = { slot: "selection-bar", placement: @placement, controller: "ui--selection-bar",
|
|
54
|
+
"ui--selection-bar-for-value": @for,
|
|
55
|
+
"ui--selection-bar-one-value": t("ui.selection_bar.count.one", default: "%{count} selected"),
|
|
56
|
+
"ui--selection-bar-other-value": t("ui.selection_bar.count.other", default: "%{count} selected") }
|
|
57
|
+
.merge(@html_attrs[:data] || {})
|
|
58
|
+
attrs
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# A checkbox the bar counts: `:item` for each thing to choose, `:all` for
|
|
62
|
+
# one that chooses them all. `form:` is the id of the form it posts with;
|
|
63
|
+
# `class:` restyles it, such as a round box over a photo.
|
|
64
|
+
def self.checkbox(kind, label:, name: nil, value: nil, form: nil, checked: false, **options)
|
|
65
|
+
attrs = { "aria-label": label, data: { selection: kind }, checked: checked, **options }
|
|
66
|
+
attrs["form"] = form if form # the HTML attribute; `form:` is a form builder to Checkbox
|
|
67
|
+
UI::Checkbox.new(name: name, value: value, unchecked_value: nil, **attrs)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def clear_attrs
|
|
71
|
+
{ variant: :ghost, size: :icon, class: "shrink-0 rounded-full",
|
|
72
|
+
aria: { label: t("ui.selection_bar.clear", default: "Clear selection") },
|
|
73
|
+
data: { action: "ui--selection-bar#clear" } }
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -10,7 +10,7 @@ class UI::Sidebar::GroupLabel < ViewComponent::Base
|
|
|
10
10
|
@system_args[:data][:sidebar] = "group-label"
|
|
11
11
|
|
|
12
12
|
@system_args[:class] = class_names(
|
|
13
|
-
"text-sidebar-foreground/70 ring-sidebar-ring flex h-8 shrink-0 items-center rounded-md px-2 text-xs font-medium outline-hidden transition-[margin,opacity] duration-200 ease-linear focus:ring-2 [&>svg]:size-4 [&>svg]:shrink-0",
|
|
13
|
+
"text-sidebar-foreground/70 ring-sidebar-ring flex h-8 shrink-0 items-center rounded-md px-2 text-xs font-medium outline-hidden transition-[margin,opacity] duration-200 ease-linear focus-visible:ring-2 [&>svg]:size-4 [&>svg]:shrink-0",
|
|
14
14
|
"group-data-[collapsible=icon]:-mt-8 group-data-[collapsible=icon]:opacity-0",
|
|
15
15
|
@system_args[:class]
|
|
16
16
|
)
|
|
@@ -7,9 +7,12 @@ class UI::Sidebar::Inset < UI::Base
|
|
|
7
7
|
@system_args[:data][:slot] = "sidebar-inset"
|
|
8
8
|
end
|
|
9
9
|
|
|
10
|
+
# cn, not class_names: a class passed in replaces the one it clashes with
|
|
11
|
+
# (bg-transparent for bg-background, shadow-none for shadow-sm), rather than
|
|
12
|
+
# both landing on the element and CSS order picking one.
|
|
10
13
|
def before_render
|
|
11
|
-
@system_args[:class] =
|
|
12
|
-
"bg-background relative flex w-full flex-1 flex-col",
|
|
14
|
+
@system_args[:class] = cn(
|
|
15
|
+
"bg-background relative flex w-full min-w-0 flex-1 flex-col",
|
|
13
16
|
"md:peer-data-[variant=inset]:m-2 md:peer-data-[variant=inset]:ml-0 md:peer-data-[variant=inset]:rounded-xl md:peer-data-[variant=inset]:shadow-sm md:peer-data-[variant=inset]:peer-data-[state=collapsed]:ml-2",
|
|
14
17
|
@system_args[:class]
|
|
15
18
|
)
|
|
@@ -1,16 +1,28 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
# A link in the sidebar. `active: true` marks the page being shown, for screen
|
|
4
|
+
# readers too (aria-current="page").
|
|
5
|
+
#
|
|
6
|
+
# `shape:` is :default, a compact row with a bar at the sidebar's edge for the
|
|
7
|
+
# current page, or :pill, a taller row whose current page is filled in edge to
|
|
8
|
+
# edge, rounded at both ends.
|
|
9
|
+
class UI::Sidebar::MenuButton < UI::Base
|
|
10
|
+
SHAPES = {
|
|
11
|
+
default: "h-8 rounded-md p-2 data-[active=true]:bg-sidebar-primary/10",
|
|
12
|
+
pill: "h-10 gap-3 rounded-full px-4 data-[active=true]:bg-sidebar-primary/15 group-data-[collapsible=icon]:rounded-full!"
|
|
13
|
+
}.freeze
|
|
14
|
+
|
|
15
|
+
def initialize(path, active: false, current: nil, shape: :default, icon: nil, **system_args) # rubocop:disable Lint/UnusedMethodArgument
|
|
5
16
|
@path = path
|
|
6
17
|
@is_active = active
|
|
7
|
-
|
|
18
|
+
@shape = SHAPES.key?(shape.to_sym) ? shape.to_sym : :default
|
|
8
19
|
@system_args = system_args
|
|
9
20
|
|
|
10
21
|
@system_args[:data] ||= {}
|
|
11
22
|
@system_args[:data][:slot] = "sidebar-menu-button"
|
|
12
23
|
@system_args[:data][:sidebar] = "menu-button"
|
|
13
24
|
@system_args[:data][:active] = @is_active.to_s
|
|
25
|
+
@system_args[:data][:shape] = @shape
|
|
14
26
|
|
|
15
27
|
# An explicit `current:` always wins; an active button falls back to "page"
|
|
16
28
|
# so assistive tech announces it without the caller repeating themselves.
|
|
@@ -19,12 +31,15 @@ class UI::Sidebar::MenuButton < ViewComponent::Base
|
|
|
19
31
|
@system_args[:aria] = (@system_args[:aria] || {}).merge(current: aria_current)
|
|
20
32
|
end
|
|
21
33
|
|
|
22
|
-
@system_args[:class] =
|
|
23
|
-
"peer/menu-button flex w-full items-center gap-2 overflow-hidden
|
|
24
|
-
"hover:bg-sidebar-accent hover:text-sidebar-accent-foreground"
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
"
|
|
34
|
+
@system_args[:class] = cn(
|
|
35
|
+
"peer/menu-button flex w-full items-center gap-2 overflow-hidden text-left text-sm outline-hidden ring-sidebar-ring " \
|
|
36
|
+
"transition-[width,height,padding] hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 " \
|
|
37
|
+
"active:bg-sidebar-accent active:text-sidebar-accent-foreground disabled:pointer-events-none disabled:opacity-50 " \
|
|
38
|
+
"group-has-data-[sidebar=menu-action]/menu-item:pe-8 aria-disabled:pointer-events-none aria-disabled:opacity-50 " \
|
|
39
|
+
"data-[active=true]:font-semibold data-[active=true]:text-sidebar-primary data-[active=true]:[&>svg]:stroke-current " \
|
|
40
|
+
"group-data-[collapsible=icon]:size-8! group-data-[collapsible=icon]:p-2! [&>span:last-child]:truncate [&>svg]:size-4 [&>svg]:shrink-0",
|
|
41
|
+
SHAPES[@shape],
|
|
42
|
+
@system_args[:class]
|
|
28
43
|
)
|
|
29
44
|
end
|
|
30
45
|
end
|
|
@@ -1 +1 @@
|
|
|
1
|
-
<%= tag.li(**@system_args) do %><%= content %><%= menu_button if menu_button %><% end %>
|
|
1
|
+
<%= tag.li(**@system_args) do %><%= content %><%= menu_button if menu_button %><% if sub_menu? %><% links = sub_menu_html %><%= tag.button(**toggle_attrs) do %><%= render(UI::Icon.new(name: "chevron-right", size: :sm, class: "rtl:rotate-180")) %><% end %><%= links %><% end %><% end %>
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
# One entry in a sidebar menu: a `menu_button`, and optionally a `sub_menu` of
|
|
4
|
+
# links under it, such as a list of albums. The sub-menu has a button of its
|
|
5
|
+
# own to open and close it, beside the link, so the link still goes to its
|
|
6
|
+
# page. It starts open when `open:` says so, or when one of its links is the
|
|
7
|
+
# current page.
|
|
3
8
|
class UI::Sidebar::MenuItem < UI::Base
|
|
4
9
|
renders_one :menu_button, UI::Sidebar::MenuButton
|
|
10
|
+
renders_one :sub_menu, UI::Sidebar::MenuSub
|
|
5
11
|
|
|
6
|
-
def initialize(**system_args)
|
|
12
|
+
def initialize(open: nil, **system_args)
|
|
13
|
+
@open = open
|
|
7
14
|
@system_args = system_args
|
|
8
15
|
|
|
9
16
|
@system_args[:data] ||= {}
|
|
@@ -12,12 +19,31 @@ class UI::Sidebar::MenuItem < UI::Base
|
|
|
12
19
|
|
|
13
20
|
@system_args[:class] = class_names(
|
|
14
21
|
"group/menu-item relative",
|
|
15
|
-
# Active indicator: a primary bar flush with the sidebar edge
|
|
16
|
-
|
|
17
|
-
"before:-
|
|
22
|
+
# Active indicator: a primary bar flush with the sidebar edge, for the
|
|
23
|
+
# default shape only; a pill is filled in instead.
|
|
24
|
+
"before:absolute before:-start-2 before:top-4 before:h-6 before:w-1 " \
|
|
25
|
+
"before:-translate-y-1/2 before:rounded-e-full before:bg-sidebar-primary " \
|
|
18
26
|
"before:opacity-0 before:transition-opacity " \
|
|
19
|
-
"has-[[data-active=true]]:before:opacity-100",
|
|
27
|
+
"has-[>[data-active=true][data-shape=default]]:before:opacity-100",
|
|
20
28
|
@system_args[:class]
|
|
21
29
|
)
|
|
22
30
|
end
|
|
31
|
+
|
|
32
|
+
def sub_menu_id = @sub_menu_id ||= "sidebar-sub-#{SecureRandom.hex(4)}"
|
|
33
|
+
|
|
34
|
+
# Drawn first, so it knows whether one of its links is the current page.
|
|
35
|
+
def sub_menu_html
|
|
36
|
+
sub_menu.configure(id: sub_menu_id, open: @open)
|
|
37
|
+
sub_menu.to_s
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def toggle_attrs
|
|
41
|
+
{ type: "button", data: { sidebar: "menu-action", action: "ui--sidebar#toggleSub" },
|
|
42
|
+
aria: { expanded: sub_menu.open?.to_s, controls: sub_menu_id,
|
|
43
|
+
label: t("ui.sidebar.toggle_sub", name: sub_menu.name, default: "Show the links under %{name}") },
|
|
44
|
+
class: "absolute end-1 top-1 flex size-6 items-center justify-center rounded-md text-sidebar-foreground/70 outline-hidden " \
|
|
45
|
+
"ring-sidebar-ring hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 " \
|
|
46
|
+
"group-data-[collapsible=icon]:hidden group-has-[>[data-shape=pill]]/menu-item:top-2 " \
|
|
47
|
+
"[&[aria-expanded=true]>svg]:rotate-90 [&>svg]:transition-transform motion-reduce:[&>svg]:transition-none" }
|
|
48
|
+
end
|
|
23
49
|
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Links under a sidebar menu item, indented beneath it, such as the albums
|
|
4
|
+
# under Albums. `name:` is the item's name, for the button that opens them.
|
|
5
|
+
#
|
|
6
|
+
# <% item.with_sub_menu(name: "Albums") do |sub| %>
|
|
7
|
+
# <% sub.with_item(album_path(album), active: current_page?(album_path(album))) { album.name } %>
|
|
8
|
+
# <% end %>
|
|
9
|
+
class UI::Sidebar::MenuSub < UI::Base
|
|
10
|
+
Item = Struct.new(:path, :active, :content, keyword_init: true)
|
|
11
|
+
|
|
12
|
+
attr_reader :name
|
|
13
|
+
|
|
14
|
+
def initialize(name: nil, class: nil, **html_attrs)
|
|
15
|
+
@name = name
|
|
16
|
+
@user_class = binding.local_variable_get(:class)
|
|
17
|
+
@html_attrs = html_attrs
|
|
18
|
+
@items = []
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def with_item(path, active: false, &block)
|
|
22
|
+
@items << Item.new(path: path, active: active, content: block)
|
|
23
|
+
nil
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def active? = @items.any?(&:active)
|
|
27
|
+
|
|
28
|
+
# Set by the menu item before it draws this: the id its button points at,
|
|
29
|
+
# and `open:` as given to the item (nil to follow the current page).
|
|
30
|
+
def configure(id:, open:)
|
|
31
|
+
@id = id
|
|
32
|
+
@open = open
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Known once drawn, since the items are added as the block runs.
|
|
36
|
+
def open? = @open.nil? ? active? : @open
|
|
37
|
+
|
|
38
|
+
def call
|
|
39
|
+
content # runs the block, which adds the items
|
|
40
|
+
tag.ul(id: @id, hidden: !open?, data: { slot: "sidebar-menu-sub", sidebar: "menu-sub" },
|
|
41
|
+
class: cn("ms-5 flex min-w-0 flex-col gap-0.5 border-s border-sidebar-border ps-2.5 py-0.5 group-data-[collapsible=icon]:hidden",
|
|
42
|
+
@user_class), **@html_attrs) do
|
|
43
|
+
safe_join(@items.map { |item| tag.li(item_link(item)) })
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
def item_link(item)
|
|
50
|
+
link_to(item.path, aria: { current: ("page" if item.active) },
|
|
51
|
+
data: { active: item.active.to_s },
|
|
52
|
+
class: "flex h-7 min-w-0 items-center gap-2 rounded-md px-2 text-sm text-sidebar-foreground/80 outline-hidden " \
|
|
53
|
+
"ring-sidebar-ring hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 " \
|
|
54
|
+
"data-[active=true]:bg-sidebar-accent data-[active=true]:font-medium data-[active=true]:text-sidebar-accent-foreground " \
|
|
55
|
+
"[&>span:last-child]:truncate [&>svg]:size-4 [&>svg]:shrink-0") do
|
|
56
|
+
capture(&item.content)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<%= tag.aside(**@system_args) do %>
|
|
2
|
-
<div data-slot="sidebar-
|
|
3
|
-
<div data-slot="sidebar-container" class="
|
|
4
|
-
<div data-sidebar="sidebar" data-slot="sidebar-inner" class="bg-sidebar group-data-[variant=floating]:border-sidebar-border
|
|
2
|
+
<div data-slot="sidebar-backdrop" aria-hidden="true" class="<%= backdrop_classes %>" data-action="click->ui--sidebar#close"></div>
|
|
3
|
+
<div data-slot="sidebar-container" class="<%= container_classes %>" data-ui--sidebar-target="container" data-action="click->ui--sidebar#navigate">
|
|
4
|
+
<div data-sidebar="sidebar" data-slot="sidebar-inner" class="bg-sidebar flex h-full w-full flex-col overflow-hidden shadow-xl md:shadow-none group-data-[variant=floating]:border-sidebar-border group-data-[variant=floating]:rounded-lg group-data-[variant=floating]:border md:group-data-[variant=floating]:shadow-sm">
|
|
5
5
|
<%= sidebar_header if sidebar_header %>
|
|
6
6
|
<%= sidebar_content if sidebar_content %>
|
|
7
7
|
<%= sidebar_footer if sidebar_footer %>
|
|
@@ -1,29 +1,107 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
# An app's sidebar: a header, groups of links, and a footer, beside the page's
|
|
4
|
+
# content (`UI::Sidebar::Inset`), both inside a `UI::Sidebar::Wrapper`.
|
|
5
|
+
#
|
|
6
|
+
# <%= render(UI::Sidebar::Wrapper.new) do %>
|
|
7
|
+
# <%= render(UI::Sidebar.new(id: "app-sidebar", collapsible: :icon)) do |sidebar| %>
|
|
8
|
+
# <% sidebar.with_sidebar_content do |content| %>
|
|
9
|
+
# <% content.with_sidebar_group do |group| %>
|
|
10
|
+
# <% group.with_menu do |menu| %>
|
|
11
|
+
# <% menu.with_menu_item do |item| %>
|
|
12
|
+
# <% item.with_menu_button(photos_path, active: true, shape: :pill) do %>…<% end %>
|
|
13
|
+
# <% end %>
|
|
14
|
+
# <% end %>
|
|
15
|
+
# <% end %>
|
|
16
|
+
# <% end %>
|
|
17
|
+
# <% end %>
|
|
18
|
+
# <%= render(UI::Sidebar::Inset.new) do %>
|
|
19
|
+
# <%= render(UI::Sidebar::Trigger.new(sidebar_id: "app-sidebar")) %> …
|
|
20
|
+
# <% end %>
|
|
21
|
+
# <% end %>
|
|
22
|
+
#
|
|
23
|
+
# `collapsible:` says what the trigger does on a wide screen: :icon shrinks the
|
|
24
|
+
# sidebar to its icons, :offcanvas slides it away, and :none keeps it open.
|
|
25
|
+
# `state:` is whether it starts :expanded or :collapsed. Left out, it's what the
|
|
26
|
+
# reader last chose, kept in a cookie so the page is drawn that way from the
|
|
27
|
+
# start rather than changing once it loads.
|
|
28
|
+
#
|
|
29
|
+
# On a phone the sidebar is a drawer over the page, closed until the trigger
|
|
30
|
+
# opens it, whatever `collapsible:` says. Escape, the backdrop, or following a
|
|
31
|
+
# link closes it.
|
|
32
|
+
#
|
|
33
|
+
# The sidebar sticks to the top of the window and is as tall as it, or as the
|
|
34
|
+
# wrapper when that's shorter, so a bar above the app doesn't push its footer
|
|
35
|
+
# out of sight: give the wrapper a height (`class: "h-[calc(100svh-3rem)]"`).
|
|
3
36
|
class UI::Sidebar < UI::Base
|
|
37
|
+
COLLAPSIBLE = %i[icon offcanvas none].freeze
|
|
38
|
+
STATES = %i[expanded collapsed].freeze
|
|
39
|
+
|
|
4
40
|
renders_one :sidebar_header, UI::Sidebar::Header
|
|
5
41
|
renders_one :sidebar_content, UI::Sidebar::Content
|
|
6
42
|
renders_one :sidebar_footer, UI::Sidebar::Footer
|
|
7
43
|
|
|
8
|
-
def initialize(collapsible: :
|
|
9
|
-
@collapsible = collapsible
|
|
44
|
+
def initialize(collapsible: :icon, variant: :sidebar, side: :left, state: nil, **system_args)
|
|
45
|
+
@collapsible = COLLAPSIBLE.include?(collapsible.to_sym) ? collapsible.to_sym : :icon
|
|
10
46
|
@variant = variant
|
|
11
47
|
@side = side
|
|
12
|
-
@state = state
|
|
48
|
+
@state = state&.to_sym
|
|
13
49
|
@system_args = system_args
|
|
50
|
+
end
|
|
14
51
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
@system_args[:data][:variant] = @variant
|
|
18
|
-
@system_args[:data][:collapsible] = @collapsible
|
|
19
|
-
@system_args[:data][:side] = @side
|
|
20
|
-
@system_args[:data][:state] = @state
|
|
21
|
-
@system_args[:data]["ui--sidebar-target"] = "sidebar"
|
|
22
|
-
|
|
52
|
+
# The cookie the controller writes the reader's choice to, one per sidebar.
|
|
53
|
+
def self.cookie_name(id) = "ui_sidebar_#{id.presence || 'state'}".tr("-", "_")
|
|
23
54
|
|
|
24
|
-
|
|
25
|
-
|
|
55
|
+
def before_render
|
|
56
|
+
@system_args[:data] = (@system_args[:data] || {}).merge(
|
|
57
|
+
slot: "sidebar", variant: @variant, side: @side, state: state,
|
|
58
|
+
# What the sidebar is doing now: "icon" or "offcanvas" while collapsed,
|
|
59
|
+
# empty while open. The styles hang off this.
|
|
60
|
+
collapsible: (state == :collapsed ? @collapsible : ""),
|
|
61
|
+
collapsible_mode: @collapsible,
|
|
62
|
+
"ui--sidebar-target": "sidebar",
|
|
63
|
+
cookie: self.class.cookie_name(@system_args[:id])
|
|
64
|
+
)
|
|
65
|
+
@system_args[:class] = cn(
|
|
66
|
+
"group peer shrink-0 text-sidebar-foreground w-0 md:w-(--sidebar-width) md:transition-[width] md:duration-200 md:ease-linear",
|
|
67
|
+
"md:data-[collapsible=icon]:w-(--sidebar-width-icon) md:data-[collapsible=offcanvas]:w-0 data-[side=right]:order-last",
|
|
68
|
+
# The floating and inset sidebars pad their panel, so their icons need that much more room.
|
|
69
|
+
"md:data-[variant=floating]:data-[collapsible=icon]:w-[calc(var(--sidebar-width-icon)+1rem)]",
|
|
70
|
+
"md:data-[variant=inset]:data-[collapsible=icon]:w-[calc(var(--sidebar-width-icon)+1rem)]",
|
|
26
71
|
@system_args[:class]
|
|
27
72
|
)
|
|
28
73
|
end
|
|
74
|
+
|
|
75
|
+
def state
|
|
76
|
+
return :collapsed if @collapsible != :none && @state == :collapsed
|
|
77
|
+
return :expanded if @state || @collapsible == :none
|
|
78
|
+
|
|
79
|
+
saved = helpers.request&.cookies&.[](self.class.cookie_name(@system_args[:id]))
|
|
80
|
+
saved == "collapsed" ? :collapsed : :expanded
|
|
81
|
+
rescue NoMethodError
|
|
82
|
+
:expanded
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# A drawer on a phone, beside the content from `md` up.
|
|
86
|
+
def container_classes
|
|
87
|
+
cn(
|
|
88
|
+
"fixed inset-y-0 start-0 z-50 flex w-(--sidebar-width-mobile) max-w-[85vw] invisible",
|
|
89
|
+
"-translate-x-full rtl:translate-x-full group-data-[side=right]:start-auto group-data-[side=right]:end-0",
|
|
90
|
+
"group-data-[side=right]:translate-x-full transition-[translate,visibility] duration-200 ease-out",
|
|
91
|
+
"group-data-[mobile=open]:visible group-data-[mobile=open]:translate-x-0 motion-reduce:transition-none",
|
|
92
|
+
"md:visible md:sticky md:top-0 md:z-10 md:h-svh md:max-h-full md:w-(--sidebar-width) md:translate-x-0",
|
|
93
|
+
"md:transition-[width,translate] md:group-data-[collapsible=icon]:w-(--sidebar-width-icon)",
|
|
94
|
+
"md:group-data-[variant=floating]:group-data-[collapsible=icon]:w-[calc(var(--sidebar-width-icon)+1rem)]",
|
|
95
|
+
"md:group-data-[variant=inset]:group-data-[collapsible=icon]:w-[calc(var(--sidebar-width-icon)+1rem)]",
|
|
96
|
+
"md:group-data-[collapsible=offcanvas]:-translate-x-full md:group-data-[collapsible=offcanvas]:invisible!",
|
|
97
|
+
"md:group-data-[side=left]:border-e md:group-data-[side=right]:border-s",
|
|
98
|
+
"md:group-data-[variant=floating]:border-0 md:group-data-[variant=inset]:border-0",
|
|
99
|
+
"md:group-data-[variant=floating]:p-2 md:group-data-[variant=inset]:p-2"
|
|
100
|
+
)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def backdrop_classes
|
|
104
|
+
"fixed inset-0 z-40 bg-black/50 invisible opacity-0 transition-[opacity,visibility] duration-200 " \
|
|
105
|
+
"group-data-[mobile=open]:visible group-data-[mobile=open]:opacity-100 motion-reduce:transition-none md:hidden"
|
|
106
|
+
end
|
|
29
107
|
end
|
|
@@ -11,8 +11,8 @@ class UI::Switch < UI::Base
|
|
|
11
11
|
base: "absolute inset-0 rounded-full " \
|
|
12
12
|
"bg-input peer-checked:bg-primary " \
|
|
13
13
|
"transition-all duration-200 " \
|
|
14
|
-
"peer-focus:outline-none peer-focus:ring-2 peer-focus:ring-primary " \
|
|
15
|
-
"peer-focus:ring-offset-1 ring-offset-background " \
|
|
14
|
+
"peer-focus:outline-none peer-focus-visible:ring-2 peer-focus-visible:ring-primary " \
|
|
15
|
+
"peer-focus-visible:ring-offset-1 ring-offset-background " \
|
|
16
16
|
"peer-disabled:cursor-not-allowed",
|
|
17
17
|
variants: {
|
|
18
18
|
size: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
<%= tag.th
|
|
1
|
+
<%= tag.th(**th_attrs) do %><% if sortable? %><%= link_to @sort_href, class: link_classes do %><span><%= content %></span><%= render(UI::Icon.new(name: sort_icon, class: icon_classes)) %><% end %><% else %><%= content %><% end %><% end %>
|
|
@@ -2,17 +2,41 @@
|
|
|
2
2
|
|
|
3
3
|
# A header cell. Renders <th scope="col"> so assistive technology announces it
|
|
4
4
|
# as a header rather than as data.
|
|
5
|
+
#
|
|
6
|
+
# With `sort_href:` the header is a link that sorts by the column, and
|
|
7
|
+
# `sorted:` (:asc or :desc) says it's sorted that way now.
|
|
5
8
|
class UI::Table::Column < UI::Base
|
|
6
9
|
ALIGNMENTS = { start: "text-left", center: "text-center", end: "text-right" }.freeze
|
|
10
|
+
SORT_ICONS = { asc: "arrow-up", desc: "arrow-down", none: "chevrons-up-down" }.freeze
|
|
7
11
|
|
|
8
|
-
def initialize(align: :start, class: nil, **html_attrs)
|
|
12
|
+
def initialize(align: :start, sort_href: nil, sorted: nil, sticky: false, class: nil, **html_attrs)
|
|
9
13
|
@align = align
|
|
14
|
+
@sort_href = sort_href
|
|
15
|
+
@sorted = sorted&.to_sym
|
|
16
|
+
@sticky = sticky
|
|
10
17
|
@user_class = binding.local_variable_get(:class)
|
|
11
18
|
@html_attrs = html_attrs
|
|
12
19
|
end
|
|
13
20
|
|
|
21
|
+
def sortable? = @sort_href.present?
|
|
22
|
+
|
|
23
|
+
def sort_icon = SORT_ICONS.fetch(@sorted || :none, SORT_ICONS[:none])
|
|
24
|
+
|
|
25
|
+
def th_attrs
|
|
26
|
+
attrs = { scope: "col", class: classes, **@html_attrs }
|
|
27
|
+
attrs[:"aria-sort"] = { asc: "ascending", desc: "descending" }[@sorted] if sortable? && @sorted
|
|
28
|
+
attrs
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def link_classes
|
|
32
|
+
cn("-mx-2 inline-flex h-8 items-center gap-1.5 rounded-md px-2 hover:bg-accent hover:text-accent-foreground",
|
|
33
|
+
{ "flex-row-reverse" => @align == :end }, UI::Base::FOCUS_RING)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def icon_classes = cn("size-3.5", { "text-muted-foreground/60" => @sorted.nil? })
|
|
37
|
+
|
|
14
38
|
def classes
|
|
15
39
|
cn("h-10 px-2 align-middle font-medium text-foreground whitespace-nowrap",
|
|
16
|
-
ALIGNMENTS.fetch(@align, ALIGNMENTS[:start]), @user_class)
|
|
40
|
+
ALIGNMENTS.fetch(@align, ALIGNMENTS[:start]), { "sticky top-0 z-10 bg-muted" => @sticky }, @user_class)
|
|
17
41
|
end
|
|
18
42
|
end
|
|
@@ -1 +1 @@
|
|
|
1
|
-
<%= tag.thead class: classes, **@html_attrs do %><tr
|
|
1
|
+
<%= tag.thead class: classes, **@html_attrs do %><tr><% if selection? %><%= tag.th class: selection_cell_classes do %><%= render(select_all_box) %><% end %><% end %><%= columns.map(&:to_s).join.html_safe %></tr><% end %>
|
|
@@ -2,12 +2,25 @@
|
|
|
2
2
|
|
|
3
3
|
# The header row of a UI::Table.
|
|
4
4
|
class UI::Table::Head < UI::Base
|
|
5
|
-
renders_many :columns, UI::Table::Column
|
|
5
|
+
renders_many :columns, ->(**args) { UI::Table::Column.new(sticky: @sticky, **args) }
|
|
6
6
|
|
|
7
|
-
def initialize(class: nil, **html_attrs)
|
|
7
|
+
def initialize(selection: nil, sticky: false, form: nil, class: nil, **html_attrs)
|
|
8
|
+
@selection = selection
|
|
9
|
+
@sticky = sticky
|
|
10
|
+
@form = form
|
|
8
11
|
@user_class = binding.local_variable_get(:class)
|
|
9
12
|
@html_attrs = html_attrs
|
|
10
13
|
end
|
|
11
14
|
|
|
15
|
+
def selection? = @selection.present?
|
|
16
|
+
|
|
17
|
+
def select_all_box
|
|
18
|
+
UI::SelectionBar.checkbox(:all, label: t("ui.table.select_all", default: "Select all rows"), form: nil)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def selection_cell_classes
|
|
22
|
+
cn("h-10 w-px ps-3 pe-1 align-middle", { "sticky top-0 z-10 bg-muted" => @sticky })
|
|
23
|
+
end
|
|
24
|
+
|
|
12
25
|
def classes = cn("border-b border-border bg-muted/50", @user_class)
|
|
13
26
|
end
|
|
@@ -1 +1 @@
|
|
|
1
|
-
<%= tag.tr(**html_options) do %><%= cells.map(&:to_s).join.html_safe %><% end %>
|
|
1
|
+
<%= tag.tr(**html_options) do %><% if selection_column? %><td class="w-px ps-3 pe-1 align-middle"><% if selectable? %><%= render(select_box) %><% end %></td><% end %><%= cells.map(&:to_s).join.html_safe %><% end %>
|
|
@@ -1,26 +1,40 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
# A body row.
|
|
3
|
+
# A body row. In a table with `selection:`, `value:` is what its checkbox
|
|
4
|
+
# posts and `label:` names the row for it ("Select Acme").
|
|
4
5
|
class UI::Table::Row < UI::Base
|
|
5
6
|
renders_many :cells, UI::Table::Cell
|
|
6
7
|
|
|
7
|
-
def initialize(selected: false, class: nil, **html_attrs)
|
|
8
|
+
def initialize(selected: false, value: nil, label: nil, selection: nil, form: nil, class: nil, **html_attrs)
|
|
8
9
|
@selected = selected
|
|
10
|
+
@value = value
|
|
11
|
+
@label = label
|
|
12
|
+
@selection = selection
|
|
13
|
+
@form = form
|
|
9
14
|
@user_class = binding.local_variable_get(:class)
|
|
10
15
|
@html_attrs = html_attrs
|
|
11
16
|
end
|
|
12
17
|
|
|
18
|
+
def selectable? = @selection.present? && !@value.nil?
|
|
19
|
+
def selection_column? = @selection.present?
|
|
20
|
+
|
|
21
|
+
def select_box
|
|
22
|
+
UI::SelectionBar.checkbox(:item, name: @selection, value: @value, form: @form, checked: @selected,
|
|
23
|
+
label: t("ui.table.select_row", label: @label || @value, default: "Select %{label}"))
|
|
24
|
+
end
|
|
25
|
+
|
|
13
26
|
def classes
|
|
14
27
|
cn("border-b border-border transition-colors hover:bg-muted/50",
|
|
15
|
-
|
|
28
|
+
"has-[[data-selection=item]:checked]:bg-primary/5 has-[[data-selection=item]:checked]:hover:bg-primary/10",
|
|
29
|
+
{ "bg-muted" => @selected && !selection_column? }, @user_class)
|
|
16
30
|
end
|
|
17
31
|
|
|
18
32
|
def html_options
|
|
19
33
|
opts = @html_attrs.dup
|
|
20
34
|
opts[:class] = classes
|
|
21
35
|
# aria-selected rather than colour alone, so the state is not conveyed by
|
|
22
|
-
# appearance only.
|
|
23
|
-
opts[:"aria-selected"] = "true" if @selected
|
|
36
|
+
# appearance only. A row with a checkbox has that to say it instead.
|
|
37
|
+
opts[:"aria-selected"] = "true" if @selected && !selection_column?
|
|
24
38
|
opts
|
|
25
39
|
end
|
|
26
40
|
end
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
<%# The wrapper scrolls sideways so a wide table never breaks the page. %>
|
|
2
|
-
|
|
2
|
+
<%= tag.div(**wrapper_attrs) do %>
|
|
3
3
|
<%= tag.table class: classes, **@html_attrs do %><% if caption.present? %><caption class="mt-4 px-2 text-sm text-muted-foreground text-left"><%= caption %></caption><% end %><%= head %><tbody class="[&_tr:last-child]:border-0"><% if show_empty_state? %><tr><td colspan="<%= column_count %>" class="p-8 text-center text-sm text-muted-foreground"><%= empty_state %></td></tr><% else %><%= rows.map(&:to_s).join.html_safe %><% end %></tbody><% end %>
|
|
4
|
-
|
|
4
|
+
<% end %>
|
|
@@ -19,15 +19,35 @@
|
|
|
19
19
|
# <% end %>
|
|
20
20
|
# <% table.with_empty_state { "No invoices yet" } %>
|
|
21
21
|
# <% end %>
|
|
22
|
+
#
|
|
23
|
+
# A column sorts on the server: give it the link that sorts by it, and which
|
|
24
|
+
# way it's sorted now, and the header becomes that link with an arrow, read
|
|
25
|
+
# out by screen readers as sorted (aria-sort).
|
|
26
|
+
#
|
|
27
|
+
# <% head.with_column(sort_href: invoices_path(sort: "amount", dir: "desc"), sorted: :asc) { "Amount" } %>
|
|
28
|
+
#
|
|
29
|
+
# `selection: "ids[]"` adds a checkbox to each row, named that, with the
|
|
30
|
+
# value each row gives (`with_row(value: invoice.id, label: invoice.number)`),
|
|
31
|
+
# and a box in the header for all of them (made with UI::SelectionBar.checkbox). `form:` is the id of the form the
|
|
32
|
+
# boxes post with, when the table isn't inside it. A `UI::SelectionBar` for
|
|
33
|
+
# the table counts what's chosen, shows the actions for it and handles
|
|
34
|
+
# select-all and Shift-click.
|
|
35
|
+
#
|
|
36
|
+
# `sticky_header: true` keeps the header in sight while the rows scroll under
|
|
37
|
+
# it, inside `max_height:` ("32rem"); the table scrolls in its own box then.
|
|
22
38
|
class UI::Table < UI::Base
|
|
23
|
-
renders_one :head, UI::Table::Head
|
|
24
|
-
renders_many :rows, UI::Table::Row
|
|
39
|
+
renders_one :head, ->(**args) { UI::Table::Head.new(selection: @selection, sticky: @sticky_header, form: @form, **args) }
|
|
40
|
+
renders_many :rows, ->(**args) { UI::Table::Row.new(selection: @selection, form: @form, **args) }
|
|
25
41
|
# Shown only when there are no rows, so an empty table reads as empty
|
|
26
42
|
# rather than as a header with nothing under it.
|
|
27
43
|
renders_one :empty_state
|
|
28
44
|
|
|
29
|
-
def initialize(caption: nil, class: nil, **html_attrs)
|
|
45
|
+
def initialize(caption: nil, selection: nil, form: nil, sticky_header: false, max_height: nil, class: nil, **html_attrs)
|
|
30
46
|
@caption = caption
|
|
47
|
+
@selection = selection
|
|
48
|
+
@form = form
|
|
49
|
+
@sticky_header = sticky_header
|
|
50
|
+
@max_height = max_height
|
|
31
51
|
@user_class = binding.local_variable_get(:class)
|
|
32
52
|
@html_attrs = html_attrs
|
|
33
53
|
end
|
|
@@ -41,5 +61,10 @@ class UI::Table < UI::Base
|
|
|
41
61
|
def show_empty_state? = empty? && empty_state?
|
|
42
62
|
|
|
43
63
|
# Spans the full width so the message centres under the header.
|
|
44
|
-
def column_count = head? ? head.columns.size : 1
|
|
64
|
+
def column_count = (head? ? head.columns.size : 1) + (@selection ? 1 : 0)
|
|
65
|
+
|
|
66
|
+
def wrapper_attrs
|
|
67
|
+
{ class: cn("relative w-full overflow-x-auto rounded-md border border-border", { "overflow-y-auto" => @max_height }),
|
|
68
|
+
style: ("max-height: #{@max_height}" if @max_height), data: { slot: "table" } }
|
|
69
|
+
end
|
|
45
70
|
end
|
|
@@ -4,7 +4,7 @@ class UI::Tabs::Trigger < UI::Base
|
|
|
4
4
|
variants(
|
|
5
5
|
base: "inline-flex items-center justify-center gap-2 whitespace-nowrap " \
|
|
6
6
|
"font-medium transition-all " \
|
|
7
|
-
"focus:outline-none focus:ring-2 focus:ring-primary focus:ring-offset-1 " \
|
|
7
|
+
"focus:outline-none focus-visible:ring-2 focus-visible:ring-primary focus-visible:ring-offset-1 " \
|
|
8
8
|
"disabled:pointer-events-none disabled:bg-muted! disabled:text-muted-foreground!",
|
|
9
9
|
variants: {
|
|
10
10
|
variant: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
<% group = capture do %><%= tag.div(**root_attrs) do %><% if multiple? && input_name %><%= tag.input type: "hidden", name: input_name, value: "", autocomplete: "off" %><% end %><% items.each do |item| %><%= item %><% end %><% end %><% end %><% if fieldset.wrap? %><%= render(fieldset) { group } %><% else %><%= group %><% end %>
|