velvetkit 0.1.0 → 0.2.1
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/README.md +1 -1
- data/lib/velvetkit/version.rb +1 -1
- data/registry/app/components/ui/file_field.html.erb +4 -0
- data/registry/app/components/ui/file_field.rb +73 -0
- data/registry/app/components/ui/navigation_menu/item.html.erb +16 -0
- data/registry/app/components/ui/navigation_menu/item.rb +102 -0
- data/registry/app/components/ui/navigation_menu/link.html.erb +9 -0
- data/registry/app/components/ui/navigation_menu/link.rb +41 -0
- data/registry/app/components/ui/navigation_menu.html.erb +3 -0
- data/registry/app/components/ui/navigation_menu.rb +64 -0
- data/registry/app/components/ui/session_tabs/add.html.erb +1 -0
- data/registry/app/components/ui/session_tabs/add.rb +52 -0
- data/registry/app/components/ui/session_tabs/tab.html.erb +37 -0
- data/registry/app/components/ui/session_tabs/tab.rb +243 -0
- data/registry/app/components/ui/session_tabs.html.erb +15 -0
- data/registry/app/components/ui/session_tabs.rb +101 -0
- data/registry/app/components/ui/skeleton.html.erb +1 -0
- data/registry/app/components/ui/skeleton.rb +30 -0
- data/registry/app/components/ui/toast/viewport.html.erb +4 -0
- data/registry/app/components/ui/toast/viewport.rb +41 -0
- data/registry/app/components/ui/toast.html.erb +11 -0
- data/registry/app/components/ui/toast.rb +89 -0
- data/registry/app/javascript/controllers/ui/file_field_controller.ts +49 -0
- data/registry/app/javascript/controllers/ui/menu_controller.ts +3 -0
- data/registry/app/javascript/controllers/ui/navigation_menu_controller.ts +265 -0
- data/registry/app/javascript/controllers/ui/session_tabs_controller.ts +732 -0
- data/registry/app/javascript/controllers/ui/toast_controller.ts +62 -0
- data/registry/app/javascript/controllers/ui/toast_viewport_controller.ts +82 -0
- data/registry/app/javascript/velvetkit_icons.ts +21 -9
- data/registry/components.yml +10 -0
- data/registry/config/locales/velvetkit.en.yml +21 -0
- data/registry/test/components/previews/ui/file_field_preview/multiple_files.html.erb +3 -0
- data/registry/test/components/previews/ui/file_field_preview/with_error.html.erb +3 -0
- data/registry/test/components/previews/ui/file_field_preview.rb +18 -0
- data/registry/test/components/previews/ui/navigation_menu_preview/custom_panel.html.erb +20 -0
- data/registry/test/components/previews/ui/navigation_menu_preview/playground.html.erb +73 -0
- data/registry/test/components/previews/ui/navigation_menu_preview/simple.html.erb +23 -0
- data/registry/test/components/previews/ui/navigation_menu_preview.rb +23 -0
- data/registry/test/components/previews/ui/session_tabs_preview/handling_events.html.erb +46 -0
- data/registry/test/components/previews/ui/session_tabs_preview/saved_views.html.erb +16 -0
- data/registry/test/components/previews/ui/session_tabs_preview/touch_with_confirmation.html.erb +20 -0
- data/registry/test/components/previews/ui/session_tabs_preview.rb +35 -0
- data/registry/test/components/previews/ui/skeleton_preview/card_placeholder.html.erb +10 -0
- data/registry/test/components/previews/ui/skeleton_preview/shapes.html.erb +9 -0
- data/registry/test/components/previews/ui/skeleton_preview.rb +15 -0
- data/registry/test/components/previews/ui/toast_preview/live_viewport.html.erb +21 -0
- data/registry/test/components/previews/ui/toast_preview/variants.html.erb +7 -0
- data/registry/test/components/previews/ui/toast_preview/with_action.html.erb +7 -0
- data/registry/test/components/previews/ui/toast_preview.rb +29 -0
- data/registry/test/components/ui/file_field_test.rb +101 -0
- data/registry/test/components/ui/navigation_menu_test.rb +130 -0
- data/registry/test/components/ui/session_tabs/add_test.rb +52 -0
- data/registry/test/components/ui/session_tabs_test.rb +281 -0
- data/registry/test/components/ui/skeleton_test.rb +65 -0
- data/registry/test/components/ui/toast/viewport_test.rb +72 -0
- data/registry/test/components/ui/toast_test.rb +183 -0
- metadata +51 -1
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class UI::ToastPreview < ViewComponent::Preview
|
|
4
|
+
# @label Playground
|
|
5
|
+
# @param variant select { choices: [default, success, destructive, warning, info] }
|
|
6
|
+
# @param title text
|
|
7
|
+
# @param description text
|
|
8
|
+
# @param duration number Seconds before it auto-dismisses; 0 stays until dismissed.
|
|
9
|
+
# @param dismissible toggle
|
|
10
|
+
def playground(variant: :success, title: "Saved", description: "Your changes have been saved.",
|
|
11
|
+
duration: 5, dismissible: true)
|
|
12
|
+
render(UI::Toast.new(variant: variant.to_sym, title: title, description: description,
|
|
13
|
+
duration: duration, dismissible: dismissible))
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# @label All Variants
|
|
17
|
+
def variants; render_with_template; end
|
|
18
|
+
|
|
19
|
+
# @label With Action
|
|
20
|
+
def with_action; render_with_template; end
|
|
21
|
+
|
|
22
|
+
# @label Live Viewport
|
|
23
|
+
#
|
|
24
|
+
# Each button fires the same `toast:show` event the `toast()` helper
|
|
25
|
+
# dispatches, so this is exactly what a client-triggered toast looks like:
|
|
26
|
+
# stacking, auto-dismiss and pause-on-hover all come from the real
|
|
27
|
+
# UI::Toast::Viewport and ui--toast controllers, not a preview-only stand-in.
|
|
28
|
+
def live_viewport; render_with_template; end
|
|
29
|
+
end
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class UI::FileFieldTest < ViewComponent::TestCase
|
|
6
|
+
def setup_fixtures(*); end
|
|
7
|
+
def teardown_fixtures(*); end
|
|
8
|
+
|
|
9
|
+
def test_renders_a_file_input
|
|
10
|
+
render_inline(UI::FileField.new)
|
|
11
|
+
assert_selector "input[type='file']"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def test_input_is_screen_reader_only_not_hidden
|
|
15
|
+
render_inline(UI::FileField.new)
|
|
16
|
+
input = page.find("input", visible: :all)
|
|
17
|
+
assert_includes input[:class].split, "sr-only"
|
|
18
|
+
refute input[:hidden]
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def test_dropzone_is_a_label_pointing_at_the_input
|
|
22
|
+
render_inline(UI::FileField.new)
|
|
23
|
+
label = page.find("label")
|
|
24
|
+
input = page.find("input", visible: :all)
|
|
25
|
+
assert_equal input[:id], label[:for]
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def test_dropzone_wired_to_controller
|
|
29
|
+
render_inline(UI::FileField.new)
|
|
30
|
+
label = page.find("label")
|
|
31
|
+
assert_equal "ui--file-field", label["data-controller"]
|
|
32
|
+
assert_includes label["data-action"], "dragover->ui--file-field#dragOver"
|
|
33
|
+
assert_includes label["data-action"], "dragleave->ui--file-field#dragLeave"
|
|
34
|
+
assert_includes label["data-action"], "drop->ui--file-field#drop"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def test_input_wired_to_update_names_on_change
|
|
38
|
+
render_inline(UI::FileField.new)
|
|
39
|
+
input = page.find("input", visible: :all)
|
|
40
|
+
assert_includes input["data-action"], "change->ui--file-field#updateNames"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_not_multiple_by_default
|
|
44
|
+
render_inline(UI::FileField.new)
|
|
45
|
+
refute page.find("input", visible: :all)[:multiple]
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def test_multiple
|
|
49
|
+
render_inline(UI::FileField.new(multiple: true))
|
|
50
|
+
assert page.find("input", visible: :all)[:multiple]
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def test_accept
|
|
54
|
+
render_inline(UI::FileField.new(accept: "image/*"))
|
|
55
|
+
assert_equal "image/*", page.find("input", visible: :all)[:accept]
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def test_disabled
|
|
59
|
+
render_inline(UI::FileField.new(disabled: true))
|
|
60
|
+
assert page.find("input", visible: :all)[:disabled]
|
|
61
|
+
assert_includes page.find("label")[:class], "opacity-50"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def test_error_true_marks_the_input_invalid
|
|
65
|
+
render_inline(UI::FileField.new(error: true))
|
|
66
|
+
input = page.find("input", visible: :all)
|
|
67
|
+
assert_equal "true", input["aria-invalid"]
|
|
68
|
+
assert_includes page.find("label")[:class], "border-destructive"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# --- form-builder binding (Rails convention) ---
|
|
72
|
+
|
|
73
|
+
class User
|
|
74
|
+
include ActiveModel::Model
|
|
75
|
+
attr_accessor :avatar
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def form_builder_for(record)
|
|
79
|
+
ActionView::Helpers::FormBuilder.new(:user, record, vc_test_controller.view_context, {})
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def test_form_binding_scopes_the_field_name
|
|
83
|
+
render_inline(UI::FileField.new(form: form_builder_for(User.new), field: :avatar))
|
|
84
|
+
assert_selector "input[type='file'][name='user[avatar]']"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def test_label_is_derived_from_the_attribute_name
|
|
88
|
+
render_inline(UI::FileField.new(form: form_builder_for(User.new), field: :avatar))
|
|
89
|
+
assert_selector "label", text: "Avatar"
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def test_hint_renders
|
|
93
|
+
render_inline(UI::FileField.new(hint: "PNG or JPG, up to 5MB"))
|
|
94
|
+
assert_text "PNG or JPG, up to 5MB"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def test_html_attrs_passthrough
|
|
98
|
+
render_inline(UI::FileField.new(id: "my-id", data: { test_id: "f" }))
|
|
99
|
+
assert_selector "input#my-id[data-test-id='f']", visible: :all
|
|
100
|
+
end
|
|
101
|
+
end
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class UI::NavigationMenuTest < ViewComponent::TestCase
|
|
6
|
+
def setup_fixtures(*); end
|
|
7
|
+
def teardown_fixtures(*); end
|
|
8
|
+
|
|
9
|
+
def render_nav(**opts)
|
|
10
|
+
render_inline(UI::NavigationMenu.new(**opts)) do |nav|
|
|
11
|
+
nav.with_item(label: "Getting started") do |item|
|
|
12
|
+
item.with_link(title: "Introduction", href: "/docs", description: "Re-usable components.")
|
|
13
|
+
item.with_link(title: "Installation", href: "/docs/install")
|
|
14
|
+
end
|
|
15
|
+
nav.with_item(label: "Docs", href: "/docs")
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def test_is_a_labelled_nav_with_the_controller
|
|
20
|
+
render_nav
|
|
21
|
+
nav = page.find("nav[data-controller~='ui--navigation-menu']")
|
|
22
|
+
assert_equal "Main", nav["aria-label"]
|
|
23
|
+
assert nav[:id].start_with?("ui-navigation-menu-")
|
|
24
|
+
assert_includes nav["data-action"], "focusout->ui--navigation-menu#focusOut"
|
|
25
|
+
assert_includes nav["data-action"], "focusin->ui--navigation-menu#focusIn"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def test_custom_label_and_delays
|
|
29
|
+
render_nav(label: "Product", open_delay: 0, close_delay: 500)
|
|
30
|
+
nav = page.find("nav")
|
|
31
|
+
assert_equal "Product", nav["aria-label"]
|
|
32
|
+
assert_equal "0", nav["data-ui--navigation-menu-open-delay-value"]
|
|
33
|
+
assert_equal "500", nav["data-ui--navigation-menu-close-delay-value"]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def test_item_with_links_is_a_disclosure_trigger
|
|
37
|
+
render_nav
|
|
38
|
+
trigger = page.find("button", text: "Getting started")
|
|
39
|
+
assert_equal "false", trigger["aria-expanded"]
|
|
40
|
+
panel = page.find("##{trigger['aria-controls']}", visible: :all)
|
|
41
|
+
assert panel[:hidden]
|
|
42
|
+
assert_equal "closed", panel["data-state"]
|
|
43
|
+
assert_includes trigger["data-action"], "click->ui--navigation-menu#toggle"
|
|
44
|
+
assert_includes trigger["data-action"], "pointerenter->ui--navigation-menu#hoverOpen"
|
|
45
|
+
assert_includes trigger["data-ui--navigation-menu-target"].split, "trigger"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def test_it_is_not_an_aria_menu
|
|
49
|
+
render_nav
|
|
50
|
+
assert_no_selector "[role='menu'], [role='menuitem']", visible: :all
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def test_trigger_has_a_rotating_chevron
|
|
54
|
+
render_nav
|
|
55
|
+
icon = page.find("button", text: "Getting started").find("[data-lucide='chevron-down']", visible: :all)
|
|
56
|
+
assert_includes icon[:class], "group-aria-expanded/nav-item:rotate-180"
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def test_item_without_panel_is_a_plain_link
|
|
60
|
+
render_nav
|
|
61
|
+
link = page.find("a", text: "Docs")
|
|
62
|
+
assert_equal "/docs", link[:href]
|
|
63
|
+
assert_nil link["aria-expanded"]
|
|
64
|
+
assert_includes link["data-ui--navigation-menu-target"], "top"
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def test_active_plain_link_is_current_page
|
|
68
|
+
render_inline(UI::NavigationMenu.new) { |nav| nav.with_item(label: "Docs", href: "/docs", active: true) }
|
|
69
|
+
assert_selector "a[aria-current='page']", text: "Docs"
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def test_panel_links_have_title_and_description
|
|
73
|
+
render_nav
|
|
74
|
+
link = page.find("a[href='/docs']", text: "Introduction", visible: :all)
|
|
75
|
+
assert link.has_text?(:all, "Re-usable components.")
|
|
76
|
+
assert_equal "link", link["data-ui--navigation-menu-target"]
|
|
77
|
+
assert_selector "a[href='/docs/install']", text: "Installation", visible: :all
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def test_panel_link_icon_and_active
|
|
81
|
+
render_inline(UI::NavigationMenu.new) do |nav|
|
|
82
|
+
nav.with_item(label: "More") { |i| i.with_link(title: "Status", href: "/s", icon: "circle-check", active: true) }
|
|
83
|
+
end
|
|
84
|
+
link = page.find("a[href='/s']", visible: :all)
|
|
85
|
+
assert link.has_selector?("[data-lucide='circle-check']", visible: :all)
|
|
86
|
+
assert_includes link[:class], "items-center"
|
|
87
|
+
assert_equal "page", link["aria-current"]
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def test_two_columns
|
|
91
|
+
render_inline(UI::NavigationMenu.new) do |nav|
|
|
92
|
+
nav.with_item(label: "Components", columns: 2) { |i| i.with_link(title: "A", href: "/a") }
|
|
93
|
+
end
|
|
94
|
+
assert_selector "ul.sm\\:grid-cols-2", visible: :all
|
|
95
|
+
assert_includes page.find("[data-ui--navigation-menu-target='panel']", visible: :all)[:class], "w-[36rem]"
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def test_custom_panel_content
|
|
99
|
+
render_inline(UI::NavigationMenu.new) do |nav|
|
|
100
|
+
nav.with_item(label: "Company", panel_class: "w-96") { "<p class='promo'>We're hiring</p>".html_safe }
|
|
101
|
+
end
|
|
102
|
+
assert_selector "button", text: "Company"
|
|
103
|
+
panel = page.find("[data-ui--navigation-menu-target='panel']", visible: :all)
|
|
104
|
+
assert panel.has_selector?("p.promo", visible: :all)
|
|
105
|
+
assert_includes panel[:class], "w-96"
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def test_ids_are_unique_per_item
|
|
109
|
+
render_inline(UI::NavigationMenu.new(id: "nav")) do |nav|
|
|
110
|
+
nav.with_item(label: "One") { |i| i.with_link(title: "A", href: "/a") }
|
|
111
|
+
nav.with_item(label: "Two") { |i| i.with_link(title: "B", href: "/b") }
|
|
112
|
+
end
|
|
113
|
+
assert_equal %w[nav-panel-1 nav-panel-2], page.all("button", visible: :all).map { _1["aria-controls"] }
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def test_active_trigger_is_marked
|
|
117
|
+
render_inline(UI::NavigationMenu.new) do |nav|
|
|
118
|
+
nav.with_item(label: "Docs", active: true) { |i| i.with_link(title: "A", href: "/a") }
|
|
119
|
+
end
|
|
120
|
+
assert_selector "button[data-active]", text: "Docs"
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def test_class_and_attrs_on_nav
|
|
124
|
+
render_nav(class: "my-nav", data: { turbo_permanent: true })
|
|
125
|
+
nav = page.find("nav")
|
|
126
|
+
assert_includes nav[:class], "my-nav"
|
|
127
|
+
assert_equal "true", nav["data-turbo-permanent"]
|
|
128
|
+
assert_equal "ui--navigation-menu", nav["data-controller"]
|
|
129
|
+
end
|
|
130
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class UI::SessionTabs::AddTest < ViewComponent::TestCase
|
|
6
|
+
def setup_fixtures(*); end
|
|
7
|
+
def teardown_fixtures(*); end
|
|
8
|
+
|
|
9
|
+
def render_with_add(size: :default, **opts)
|
|
10
|
+
render_inline(UI::SessionTabs.new(size: size)) do |tabs|
|
|
11
|
+
tabs.with_tab(value: "a", active: true) { "Alpha" }
|
|
12
|
+
tabs.with_add(**opts)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def test_without_url_is_an_event_button
|
|
17
|
+
render_with_add
|
|
18
|
+
button = page.find("button[aria-label='New tab']")
|
|
19
|
+
assert_equal "button", button[:type]
|
|
20
|
+
assert_includes button["data-action"], "click->ui--session-tabs#add"
|
|
21
|
+
assert_no_selector "form"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def test_with_url_posts_a_form
|
|
25
|
+
render_with_add(url: "/tabs")
|
|
26
|
+
form = page.find("form[action='/tabs']")
|
|
27
|
+
assert_equal "post", form[:method]
|
|
28
|
+
assert form.has_selector?("button[aria-label='New tab'][data-action~='click->ui--session-tabs#add']")
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_custom_label
|
|
32
|
+
render_with_add(label: "New order")
|
|
33
|
+
assert_selector "button[aria-label='New order'][title='New order']"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def test_touch_size
|
|
37
|
+
render_with_add(size: :touch)
|
|
38
|
+
assert_includes page.find("button[aria-label='New tab']")[:class], "size-8"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def test_default_size_is_compact
|
|
42
|
+
render_with_add
|
|
43
|
+
assert_includes page.find("button[aria-label='New tab']")[:class], "size-7"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def test_keeps_user_action
|
|
47
|
+
render_with_add(data: { action: "click->analytics#track" })
|
|
48
|
+
action = page.find("button[aria-label='New tab']")["data-action"]
|
|
49
|
+
assert_includes action, "click->analytics#track"
|
|
50
|
+
assert_includes action, "click->ui--session-tabs#add"
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class UI::SessionTabsTest < ViewComponent::TestCase
|
|
6
|
+
def setup_fixtures(*); end
|
|
7
|
+
def teardown_fixtures(*); end
|
|
8
|
+
|
|
9
|
+
def render_tabs(**root_opts, &block)
|
|
10
|
+
render_inline(UI::SessionTabs.new(**root_opts)) do |tabs|
|
|
11
|
+
if block
|
|
12
|
+
block.call(tabs)
|
|
13
|
+
else
|
|
14
|
+
tabs.with_tab(value: "a", active: true, rename: true, close: true) { "Alpha" }
|
|
15
|
+
tabs.with_tab(value: "b", rename: true, close: true) { "Beta" }
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def test_root_carries_controller_and_values
|
|
21
|
+
render_tabs(min_tabs: 2, return_focus: "#search", rename_param: "view[name]")
|
|
22
|
+
root = page.find("[data-controller~='ui--session-tabs']")
|
|
23
|
+
assert_equal "2", root["data-ui--session-tabs-min-tabs-value"]
|
|
24
|
+
assert_equal "#search", root["data-ui--session-tabs-return-focus-value"]
|
|
25
|
+
assert_equal "view[name]", root["data-ui--session-tabs-rename-param-value"]
|
|
26
|
+
assert_equal "Tab name", root["data-ui--session-tabs-rename-label-value"]
|
|
27
|
+
assert root[:id].start_with?("ui-session-tabs-")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def test_tablist_has_accessible_name
|
|
31
|
+
render_tabs(label: "Documents")
|
|
32
|
+
assert_selector "[role='tablist'][aria-label='Documents']"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def test_tablist_label_defaults_to_translation
|
|
36
|
+
render_tabs
|
|
37
|
+
assert_selector "[role='tablist'][aria-label='Open tabs']"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def test_tab_without_href_is_a_button
|
|
41
|
+
render_tabs
|
|
42
|
+
assert_selector "button[role='tab'][type='button']", text: "Alpha"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def test_tab_with_href_is_a_link
|
|
46
|
+
render_tabs do |t|
|
|
47
|
+
t.with_tab(value: "a", href: "/docs/a", active: true) { "Alpha" }
|
|
48
|
+
end
|
|
49
|
+
link = page.find("a[role='tab']")
|
|
50
|
+
assert_equal "/docs/a", link[:href]
|
|
51
|
+
assert_equal "page", link["aria-current"]
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def test_active_state_and_roving_tabindex
|
|
55
|
+
render_tabs
|
|
56
|
+
active = page.find("[role='tab']", text: "Alpha")
|
|
57
|
+
inactive = page.find("[role='tab']", text: "Beta")
|
|
58
|
+
assert_equal "true", active["aria-selected"]
|
|
59
|
+
assert_equal "0", active[:tabindex]
|
|
60
|
+
assert_equal "false", inactive["aria-selected"]
|
|
61
|
+
assert_equal "-1", inactive[:tabindex]
|
|
62
|
+
assert_selector "[data-ui--session-tabs-target='tab'][data-value='a'][data-state='active']"
|
|
63
|
+
assert_selector "[data-ui--session-tabs-target='tab'][data-value='b'][data-state='inactive']"
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def test_wrapper_is_presentation
|
|
67
|
+
render_tabs
|
|
68
|
+
assert_selector "[role='presentation'][data-ui--session-tabs-target='tab']", count: 2
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def test_tab_html_attrs_go_on_the_tab_element
|
|
72
|
+
render_tabs do |t|
|
|
73
|
+
t.with_tab(value: "a", href: "/a", data: { turbo_frame: "pane" }) { "Alpha" }
|
|
74
|
+
end
|
|
75
|
+
tab = page.find("a[role='tab']")
|
|
76
|
+
assert_equal "pane", tab["data-turbo-frame"]
|
|
77
|
+
assert_includes tab["data-action"], "click->ui--session-tabs#select"
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def test_meta_slot
|
|
81
|
+
render_tabs do |t|
|
|
82
|
+
t.with_tab(value: "a") do |tab|
|
|
83
|
+
tab.with_meta { "3 items" }
|
|
84
|
+
"Alpha"
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
assert_selector "[role='tab'] span", text: "3 items"
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def test_no_menu_without_actions
|
|
91
|
+
render_tabs do |t|
|
|
92
|
+
t.with_tab(value: "a") { "Alpha" }
|
|
93
|
+
end
|
|
94
|
+
assert_no_selector "[data-controller~='ui--menu']"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def test_menu_items_follow_enabled_actions
|
|
98
|
+
render_tabs do |t|
|
|
99
|
+
t.with_tab(value: "a", rename: true) { "Alpha" }
|
|
100
|
+
t.with_tab(value: "b") { "Beta" }
|
|
101
|
+
end
|
|
102
|
+
assert_selector "[role='menuitem']", text: "Rename", visible: :all
|
|
103
|
+
assert_no_selector "[role='menuitem']", text: "Duplicate", visible: :all
|
|
104
|
+
assert_no_selector "[role='menuitem']", text: "Close", visible: :all
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def test_menu_trigger_is_labelled_by_tab
|
|
108
|
+
render_tabs
|
|
109
|
+
trigger = page.find("[data-ui--session-tabs-target='menuTrigger']", match: :first)
|
|
110
|
+
assert_equal "Tab options", trigger["aria-label"]
|
|
111
|
+
label_id = page.find("[data-ui--session-tabs-target='label']", text: "Alpha")[:id]
|
|
112
|
+
assert_equal label_id, trigger["aria-describedby"]
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def test_action_urls_and_flags
|
|
116
|
+
render_tabs do |t|
|
|
117
|
+
t.with_tab(value: "a", rename: "/a/rename", duplicate: "/a/dup", move: true, close: "/a",
|
|
118
|
+
close_confirm: "Discard?") { "Alpha" }
|
|
119
|
+
t.with_tab(value: "b") { "Beta" }
|
|
120
|
+
end
|
|
121
|
+
tab = page.find("[data-value='a']")
|
|
122
|
+
assert_equal "/a/rename", tab["data-rename-url"]
|
|
123
|
+
assert_equal "/a/dup", tab["data-duplicate-url"]
|
|
124
|
+
assert_equal "/a", tab["data-close-url"]
|
|
125
|
+
assert_nil tab["data-move-url"]
|
|
126
|
+
assert_equal "true", tab["data-move-enabled"]
|
|
127
|
+
assert_equal "Discard?", tab["data-close-confirm"]
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def test_menu_actions_are_wired
|
|
131
|
+
render_tabs do |t|
|
|
132
|
+
t.with_tab(value: "a", rename: true, duplicate: true, move: true, close: true) { "Alpha" }
|
|
133
|
+
t.with_tab(value: "b") { "Beta" }
|
|
134
|
+
end
|
|
135
|
+
%w[rename duplicate moveLeft moveRight close].each do |action|
|
|
136
|
+
assert_selector "[role='menuitem'][data-action='click->ui--session-tabs##{action}']", visible: :all
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def test_move_left_hidden_on_first_and_move_right_on_last
|
|
141
|
+
render_tabs do |t|
|
|
142
|
+
t.with_tab(value: "a", move: true) { "Alpha" }
|
|
143
|
+
t.with_tab(value: "b", move: true) { "Beta" }
|
|
144
|
+
end
|
|
145
|
+
first = page.find("[data-value='a']")
|
|
146
|
+
last = page.find("[data-value='b']")
|
|
147
|
+
assert first.has_selector?("[data-move='left'][hidden][data-disabled]", visible: :all)
|
|
148
|
+
assert first.has_selector?("[data-move='right']:not([hidden])", visible: :all)
|
|
149
|
+
assert last.has_selector?("[data-move='right'][hidden][data-disabled]", visible: :all)
|
|
150
|
+
assert last.has_selector?("[data-move='left']:not([hidden])", visible: :all)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def test_fixed_tab_is_never_passed
|
|
154
|
+
render_tabs do |t|
|
|
155
|
+
t.with_tab(value: "default") { "Default" }
|
|
156
|
+
t.with_tab(value: "a", move: true) { "Alpha" }
|
|
157
|
+
t.with_tab(value: "b", move: true) { "Beta" }
|
|
158
|
+
end
|
|
159
|
+
alpha = page.find("[data-value='a']")
|
|
160
|
+
assert alpha.has_selector?("[data-move='left'][hidden]", visible: :all)
|
|
161
|
+
assert_nil page.find("[data-value='default']")["data-move-enabled"]
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def test_movable_tab_gets_a_drag_handle
|
|
165
|
+
render_tabs do |t|
|
|
166
|
+
t.with_tab(value: "a", move: true) { "Alpha" }
|
|
167
|
+
end
|
|
168
|
+
handle = page.find("[data-ui--session-tabs-target='handle']", visible: :all)
|
|
169
|
+
assert_equal "pointerdown->ui--session-tabs#dragStart", handle["data-action"]
|
|
170
|
+
assert_equal "true", handle["aria-hidden"]
|
|
171
|
+
assert_includes handle[:class], "touch-none"
|
|
172
|
+
assert handle.has_selector?("[data-lucide='grip-vertical']", visible: :all)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def test_controls_reveal_on_hover_and_slide_in
|
|
176
|
+
render_tabs do |t|
|
|
177
|
+
t.with_tab(value: "a", move: true, rename: true) { "Alpha" }
|
|
178
|
+
end
|
|
179
|
+
grip = page.find("[data-ui--session-tabs-target='handle'] [data-lucide]", visible: :all)[:class]
|
|
180
|
+
menu = page.find("[data-ui--session-tabs-target='menuTrigger']")[:class]
|
|
181
|
+
dots = page.find("[data-ui--session-tabs-target='menuTrigger'] [data-lucide]", visible: :all)[:class]
|
|
182
|
+
[ grip, menu ].each do |classes|
|
|
183
|
+
assert_includes classes, "opacity-0"
|
|
184
|
+
assert_includes classes, "group-hover/tab:opacity-100"
|
|
185
|
+
assert_includes classes, "group-focus-within/tab:opacity-100"
|
|
186
|
+
assert_includes classes, "[@media(hover:none)]:opacity-100"
|
|
187
|
+
end
|
|
188
|
+
assert_includes grip, "-translate-x-2"
|
|
189
|
+
assert_includes dots, "translate-x-2"
|
|
190
|
+
assert_includes dots, "group-hover/tab:translate-x-0"
|
|
191
|
+
assert_includes page.find("[data-value='a']")[:class], "hover:bg-primary/5"
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def test_fixed_tab_has_no_handle
|
|
195
|
+
render_tabs do |t|
|
|
196
|
+
t.with_tab(value: "a", rename: true) { "Alpha" }
|
|
197
|
+
end
|
|
198
|
+
assert_no_selector "[data-ui--session-tabs-target='handle']", visible: :all
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def test_tab_body_is_draggable_but_not_natively
|
|
202
|
+
render_tabs do |t|
|
|
203
|
+
t.with_tab(value: "a", href: "/a", move: true) { "Alpha" }
|
|
204
|
+
end
|
|
205
|
+
tab = page.find("a[role='tab']")
|
|
206
|
+
assert_includes tab["data-action"], "pointerdown->ui--session-tabs#pressStart"
|
|
207
|
+
assert_equal "false", tab[:draggable]
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def test_menu_trigger_is_compact
|
|
211
|
+
render_tabs
|
|
212
|
+
assert_includes page.find("[data-ui--session-tabs-target='menuTrigger']", match: :first)[:class], "size-6"
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def test_close_hidden_at_min_tabs
|
|
216
|
+
render_tabs do |t|
|
|
217
|
+
t.with_tab(value: "a", close: true) { "Alpha" }
|
|
218
|
+
end
|
|
219
|
+
assert_selector "[role='menuitem'][data-close-part][hidden][data-disabled]", visible: :all
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def test_close_available_above_min_tabs
|
|
223
|
+
render_tabs
|
|
224
|
+
assert_selector "[role='menuitem'][data-close-part]:not([hidden])", visible: :all, count: 2
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def test_close_is_destructive_and_divided
|
|
228
|
+
render_tabs
|
|
229
|
+
item = page.find("[role='menuitem'][data-close-part]", match: :first, visible: :all)
|
|
230
|
+
assert_includes item[:class], "text-destructive"
|
|
231
|
+
assert_selector "[role='separator'][data-close-part]", visible: :all, count: 2
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
def test_extra_menu_items
|
|
235
|
+
render_tabs do |t|
|
|
236
|
+
t.with_tab(value: "a", close: true) do |tab|
|
|
237
|
+
tab.with_menu_item("Pin", data: { action: "click->pins#toggle" })
|
|
238
|
+
"Alpha"
|
|
239
|
+
end
|
|
240
|
+
t.with_tab(value: "b") { "Beta" }
|
|
241
|
+
end
|
|
242
|
+
assert_selector "[role='menuitem'][data-action='click->pins#toggle']", text: "Pin", visible: :all
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
def test_confirm_dialog_is_rendered_once
|
|
246
|
+
render_tabs
|
|
247
|
+
assert_selector "[data-ui--session-tabs-target='dialog'] dialog", count: 1, visible: :all
|
|
248
|
+
assert_selector "[data-ui--session-tabs-target='confirmMessage']", visible: :all
|
|
249
|
+
assert_selector "button[data-action='click->ui--session-tabs#confirmClose']", text: "Close tab", visible: :all
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def test_touch_size
|
|
253
|
+
render_tabs(size: :touch)
|
|
254
|
+
assert_includes page.find("[data-value='a']")[:class], "h-11"
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
def test_default_size
|
|
258
|
+
render_tabs
|
|
259
|
+
assert_includes page.find("[data-value='a']")[:class], "h-9"
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def test_unknown_size_falls_back
|
|
263
|
+
render_tabs(size: :huge)
|
|
264
|
+
assert_includes page.find("[data-value='a']")[:class], "h-9"
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def test_user_class_and_attrs_on_root
|
|
268
|
+
render_tabs(class: "my-tabs", id: "doc-tabs", data: { turbo_permanent: true })
|
|
269
|
+
root = page.find("#doc-tabs")
|
|
270
|
+
assert_includes root[:class], "my-tabs"
|
|
271
|
+
assert_equal "ui--session-tabs", root["data-controller"]
|
|
272
|
+
assert_equal "true", root["data-turbo-permanent"]
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
def test_values_with_spaces_get_safe_ids
|
|
276
|
+
render_tabs do |t|
|
|
277
|
+
t.with_tab(value: "Saved view 1") { "One" }
|
|
278
|
+
end
|
|
279
|
+
assert page.find("[role='tab']")[:id].end_with?("-tab-saved-view-1")
|
|
280
|
+
end
|
|
281
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class UI::SkeletonTest < ViewComponent::TestCase
|
|
6
|
+
def setup_fixtures(*); end
|
|
7
|
+
def teardown_fixtures(*); end
|
|
8
|
+
|
|
9
|
+
def skeleton_el
|
|
10
|
+
page.find("div", visible: :all)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def test_renders_a_div
|
|
14
|
+
render_inline(UI::Skeleton.new)
|
|
15
|
+
assert skeleton_el.present?
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def test_pulses_regardless_of_shape
|
|
19
|
+
render_inline(UI::Skeleton.new)
|
|
20
|
+
assert_includes skeleton_el[:class].split, "animate-pulse"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def test_hidden_from_screen_readers
|
|
24
|
+
render_inline(UI::Skeleton.new)
|
|
25
|
+
assert_equal "true", skeleton_el["aria-hidden"]
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def test_default_shape_is_text
|
|
29
|
+
render_inline(UI::Skeleton.new)
|
|
30
|
+
classes = skeleton_el[:class].split
|
|
31
|
+
assert_includes classes, "h-4"
|
|
32
|
+
assert_includes classes, "w-full"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def test_circle_shape
|
|
36
|
+
render_inline(UI::Skeleton.new(shape: :circle))
|
|
37
|
+
classes = skeleton_el[:class].split
|
|
38
|
+
assert_includes classes, "size-10"
|
|
39
|
+
assert_includes classes, "rounded-full"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def test_rectangle_shape
|
|
43
|
+
render_inline(UI::Skeleton.new(shape: :rectangle))
|
|
44
|
+
classes = skeleton_el[:class].split
|
|
45
|
+
assert_includes classes, "h-24"
|
|
46
|
+
assert_includes classes, "w-full"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def test_user_class_merge
|
|
50
|
+
render_inline(UI::Skeleton.new(class: "w-32"))
|
|
51
|
+
assert_includes skeleton_el[:class].split, "w-32"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def test_user_class_overrides_shape_sizing_via_tailwind_merge
|
|
55
|
+
render_inline(UI::Skeleton.new(shape: :text, class: "h-10"))
|
|
56
|
+
classes = skeleton_el[:class].split
|
|
57
|
+
assert_includes classes, "h-10"
|
|
58
|
+
refute_includes classes, "h-4"
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def test_html_attrs_passthrough
|
|
62
|
+
render_inline(UI::Skeleton.new(id: "my-id", data: { test_id: "s" }))
|
|
63
|
+
assert_selector "div#my-id[data-test-id='s']"
|
|
64
|
+
end
|
|
65
|
+
end
|