Skip to content

Commit

Permalink
support autofocus on button
Browse files Browse the repository at this point in the history
  • Loading branch information
ktravers authored Feb 7, 2025
1 parent 1f1a95f commit bb05db7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/components/primer/alpha/toggle_switch.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<%= render(Primer::Box.new(classes: "ToggleSwitch-statusOff").with_content("Off")) %>
<% end %>

<%= render(Primer::BaseComponent.new(tag: :button, classes: "ToggleSwitch-track", disabled: disabled?, data: { target: "toggle-switch.switch", action: "click:toggle-switch#toggle" }, **@aria_arguments)) do %>
<%= render(Primer::BaseComponent.new(tag: :button, classes: "ToggleSwitch-track", disabled: disabled?, data: { target: "toggle-switch.switch", action: "click:toggle-switch#toggle" }, **@button_arguments)) do %>
<%= render(Primer::Box.new(classes: "ToggleSwitch-icons", aria: { hidden: true })) do %>
<%= render(Primer::Box.new(classes: "ToggleSwitch-lineIcon")) do %>
<%= render(Primer::BaseComponent.new(
Expand Down
16 changes: 14 additions & 2 deletions app/components/primer/alpha/toggle_switch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,19 @@ class ToggleSwitch < Primer::Component
# @param size [Symbol] What size toggle switch to render. <%= one_of(Primer::Alpha::ToggleSwitch::SIZE_OPTIONS) %>
# @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) %>
# @param turbo [Boolean] Whether or not to request a turbo stream and render the response as such.
# @param autofocus [Boolean] Whether switch should be autofocused when rendered.
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
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)
def initialize(
src: nil,
csrf_token: nil,
checked: false,
enabled: true,
size: SIZE_DEFAULT,
status_label_position: STATUS_LABEL_POSITION_DEFAULT,
turbo: false,
autofocus: nil,
**system_arguments
)
@src = src
@csrf_token = csrf_token
@checked = checked
Expand All @@ -50,12 +61,13 @@ def initialize(src: nil, csrf_token: nil, checked: false, enabled: true, size: S
SIZE_MAPPINGS[@size]
)

@aria_arguments = {
@button_arguments = {
aria: merge_aria(
@system_arguments,
aria: { pressed: on? }
)
}
@button_arguments[:autofocus] = true if autofocus

@system_arguments[:src] = @src if @src
end
Expand Down
4 changes: 4 additions & 0 deletions previews/primer/alpha/toggle_switch_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ def with_bad_csrf_token
def with_turbo
render(Primer::Alpha::ToggleSwitch.new(src: UrlHelpers.toggle_switch_index_path, turbo: true))
end

def with_autofocus
render(Primer::Alpha::ToggleSwitch.new(src: UrlHelpers.toggle_switch_index_path, autofocus: true))
end
end
end
end
6 changes: 6 additions & 0 deletions test/components/alpha/toggle_switch_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ def test_turbo

assert_selector("[data-turbo]")
end

def test_autofocus
render_inline(Primer::Alpha::ToggleSwitch.new(src: "/foo", autofocus: true))

assert_selector(".ToggleSwitch-track[autofocus]")
end
end
end
end

0 comments on commit bb05db7

Please sign in to comment.