Skip to content

Commit 6734331

Browse files
authored
Primer Alpha::ToggleSwitch custom status label support (#3659)
1 parent db729fb commit 6734331

4 files changed

Lines changed: 27 additions & 2 deletions

File tree

.changeset/spotty-birds-cough.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+
Add custom label support for ToggleSwitch on/off states

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<%= render(Primer::Beta::Spinner.new(size: :small, hidden: "true", data: { target: "toggle-switch.loadingSpinner" })) %>
55
</span>
66
<%= render(Primer::Beta::Text.new(aria: { hidden: true }, classes: "ToggleSwitch-status")) do %>
7-
<%= render(Primer::Box.new(classes: "ToggleSwitch-statusOn").with_content("On")) %>
8-
<%= render(Primer::Box.new(classes: "ToggleSwitch-statusOff").with_content("Off")) %>
7+
<%= render(Primer::Box.new(classes: "ToggleSwitch-statusOn").with_content(@on_label)) %>
8+
<%= render(Primer::Box.new(classes: "ToggleSwitch-statusOff").with_content(@off_label)) %>
99
<% end %>
1010

1111
<%= render(Primer::BaseComponent.new(tag: :button, classes: "ToggleSwitch-track", disabled: disabled?, data: { target: "toggle-switch.switch", action: "click:toggle-switch#toggle" }, **@button_arguments)) do %>

app/components/primer/alpha/toggle_switch.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ class ToggleSwitch < Primer::Component
2929
# @param turbo [Boolean] Whether or not to request a turbo stream and render the response as such.
3030
# @param autofocus [Boolean] Whether switch should be autofocused when rendered.
3131
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
32+
# @param on_label [String] Custom label to show when the switch is ON.
33+
# Defaults to On.
34+
# Only customize this label if it makes the toggle’s state more meaningful
35+
# in its specific context. For example, for a "Show images" setting,
36+
# you might use "Hide" when the switch is ON.
37+
# @param off_label [String] Custom label to show when the switch is OFF.
38+
# Defaults to ("Off").
39+
# Only customize this label if it makes the toggle’s state more meaningful
40+
# in its specific context. For example, for a "Show images" setting,
41+
# you might use "Show" when the switch is OFF.
42+
3243
def initialize(
3344
src: nil,
3445
csrf_token: nil,
@@ -38,6 +49,8 @@ def initialize(
3849
status_label_position: STATUS_LABEL_POSITION_DEFAULT,
3950
turbo: false,
4051
autofocus: nil,
52+
on_label: nil,
53+
off_label: nil,
4154
**system_arguments
4255
)
4356
@src = src
@@ -70,6 +83,9 @@ def initialize(
7083
@button_arguments[:autofocus] = true if autofocus
7184

7285
@system_arguments[:src] = @src if @src
86+
87+
@on_label = on_label || "On"
88+
@off_label = off_label || "Off"
7389
end
7490

7591
def on?

previews/primer/alpha/toggle_switch_preview.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,9 @@ def with_autofocus
6767
render(Primer::Alpha::ToggleSwitch.new(src: UrlHelpers.primer_view_components.toggle_switch_index_path, autofocus: true))
6868
end
6969
end
70+
71+
def with_custom_status_label
72+
render Primer::Alpha::ToggleSwitch.new(src: UrlHelpers.primer_view_components.toggle_switch_index_path, on_label: "Enabled", off_label: "Disabled")
73+
end
7074
end
7175
end

0 commit comments

Comments
 (0)