Skip to content

Commit 42db9dd

Browse files
authored
update ToggleSwitch to support autofocus (#3319)
1 parent 6afe922 commit 42db9dd

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

.changeset/late-jobs-think.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/view-components': patch
3+
---
4+
5+
Adds optional autofocus to ToggleSwitch

app/components/primer/alpha/toggle_switch.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<%= render(Primer::Box.new(classes: "ToggleSwitch-statusOff").with_content("Off")) %>
99
<% end %>
1010

11-
<%= render(Primer::BaseComponent.new(tag: :button, classes: "ToggleSwitch-track", disabled: disabled?, data: { target: "toggle-switch.switch", action: "click:toggle-switch#toggle" }, **@aria_arguments)) do %>
11+
<%= render(Primer::BaseComponent.new(tag: :button, classes: "ToggleSwitch-track", disabled: disabled?, data: { target: "toggle-switch.switch", action: "click:toggle-switch#toggle" }, **@button_arguments)) do %>
1212
<%= render(Primer::Box.new(classes: "ToggleSwitch-icons", aria: { hidden: true })) do %>
1313
<%= render(Primer::Box.new(classes: "ToggleSwitch-lineIcon")) do %>
1414
<%= render(Primer::BaseComponent.new(

app/components/primer/alpha/toggle_switch.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,19 @@ class ToggleSwitch < Primer::Component
2727
# @param size [Symbol] What size toggle switch to render. <%= one_of(Primer::Alpha::ToggleSwitch::SIZE_OPTIONS) %>
2828
# @param status_label_position [Symbol] Which side of the toggle switch to render the status label. <%= one_of(Primer::Alpha::ToggleSwitch::STATUS_LABEL_POSITION_OPTIONS) %>
2929
# @param turbo [Boolean] Whether or not to request a turbo stream and render the response as such.
30+
# @param autofocus [Boolean] Whether switch should be autofocused when rendered.
3031
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
31-
def initialize(src: nil, csrf_token: nil, checked: false, enabled: true, size: SIZE_DEFAULT, status_label_position: STATUS_LABEL_POSITION_DEFAULT, turbo: false, **system_arguments)
32+
def initialize(
33+
src: nil,
34+
csrf_token: nil,
35+
checked: false,
36+
enabled: true,
37+
size: SIZE_DEFAULT,
38+
status_label_position: STATUS_LABEL_POSITION_DEFAULT,
39+
turbo: false,
40+
autofocus: nil,
41+
**system_arguments
42+
)
3243
@src = src
3344
@csrf_token = csrf_token
3445
@checked = checked
@@ -50,12 +61,13 @@ def initialize(src: nil, csrf_token: nil, checked: false, enabled: true, size: S
5061
SIZE_MAPPINGS[@size]
5162
)
5263

53-
@aria_arguments = {
64+
@button_arguments = {
5465
aria: merge_aria(
5566
@system_arguments,
5667
aria: { pressed: on? }
5768
)
5869
}
70+
@button_arguments[:autofocus] = true if autofocus
5971

6072
@system_arguments[:src] = @src if @src
6173
end

previews/primer/alpha/toggle_switch_preview.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ def with_bad_csrf_token
6262
def with_turbo
6363
render(Primer::Alpha::ToggleSwitch.new(src: UrlHelpers.toggle_switch_index_path, turbo: true))
6464
end
65+
66+
def with_autofocus
67+
render(Primer::Alpha::ToggleSwitch.new(src: UrlHelpers.toggle_switch_index_path, autofocus: true))
68+
end
6569
end
6670
end
6771
end

test/components/alpha/toggle_switch_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ def test_turbo
8080

8181
assert_selector("[data-turbo]")
8282
end
83+
84+
def test_autofocus
85+
render_inline(Primer::Alpha::ToggleSwitch.new(src: "/foo", autofocus: true))
86+
87+
assert_selector(".ToggleSwitch-track[autofocus]")
88+
end
8389
end
8490
end
8591
end

0 commit comments

Comments
 (0)