Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When hiding a Spinner, also hide its screen reader text #3021

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/neat-pumpkins-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

When hiding a `Spinner`, also hide its screen reader text
14 changes: 8 additions & 6 deletions app/components/primer/beta/spinner.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<%= render Primer::BaseComponent.new(**@system_arguments) do %>
<circle cx="8" cy="8" r="7" stroke="currentColor" stroke-opacity="0.25" stroke-width="2" vector-effect="non-scaling-stroke" fill="none" />
<path d="M15 8a7.002 7.002 0 00-7-7" stroke="currentColor" stroke-width="2" stroke-linecap="round" vector-effect="non-scaling-stroke" />
<% end %>
<% if no_aria_label? %>
<span class="spinner-screenreader-text sr-only"><%= @sr_text %></span>
<%= render(Primer::BaseComponent.new(tag: :span, hidden: @hidden, data: { target: @target })) do %>
<%= render Primer::BaseComponent.new(**@system_arguments) do %>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aaah, I'm guessing the Primer::BaseComponent.new() is the magic bit I needed to be able to access the screenreader-only element from other spinner files?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort of, we just needed to wrap both the <svg> and the <span class="sr-only"> in another <span> so we could hide them both together. It was a little tricky because usually we pass **system_arguments to the top-level element (eg. the new <span>), but that would break the existing API which puts **system_arguments on the <svg> element. I had to extract only certain attributes, eg. data-target and hidden, to be able to put them on the new <span> and pass everything else down to the <svg>.

<circle cx="8" cy="8" r="7" stroke="currentColor" stroke-opacity="0.25" stroke-width="2" vector-effect="non-scaling-stroke" fill="none" />
<path d="M15 8a7.002 7.002 0 00-7-7" stroke="currentColor" stroke-width="2" stroke-linecap="round" vector-effect="non-scaling-stroke" />
<% end %>
<% if no_aria_label? %>
<span class="sr-only"><%= @sr_text %></span>
<% end %>
<% end %>
3 changes: 3 additions & 0 deletions app/components/primer/beta/spinner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def initialize(size: DEFAULT_SIZE, style: DEFAULT_STYLE, sr_text: DEFAULT_SR_TEX
else
@system_arguments[:role] = "img"
end

@target = extract_data(:target, @system_arguments)
@hidden = @system_arguments.delete(:hidden)
end

def no_aria_label?
Expand Down
10 changes: 10 additions & 0 deletions app/lib/primer/attributes_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ def data(val, system_arguments)
system_arguments[:"data-#{val}"] || system_arguments.dig(:data, val.to_sym)
end

def extract_data(val, system_arguments)
if system_arguments.include?(:"data-#{val}")
system_arguments.delete(:"data-#{val}")
else
if system_arguments.include?(:data) && system_arguments[:data][val.to_sym]
system_arguments[:data].delete(val.to_sym)
end
end
end

# Merges hashes that contain "aria-*" keys and nested aria: hashes. Removes keys from
# each hash and returns them in the new hash.
#
Expand Down
3 changes: 0 additions & 3 deletions lib/primer/forms/primer_text_field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ declare global {
}
}

const SCREENREADER_TEXT_CLASSNAME = 'spinner-screenreader-text'
@controller
export class PrimerTextFieldElement extends HTMLElement {
@target inputElement: HTMLInputElement
Expand Down Expand Up @@ -99,13 +98,11 @@ export class PrimerTextFieldElement extends HTMLElement {

showLeadingSpinner(): void {
this.leadingSpinner?.removeAttribute('hidden')
this.leadingSpinner?.querySelector(SCREENREADER_TEXT_CLASSNAME)?.removeAttribute('hidden')
this.leadingVisual?.setAttribute('hidden', '')
}

hideLeadingSpinner(): void {
this.leadingSpinner?.setAttribute('hidden', '')
this.leadingSpinner?.querySelector(SCREENREADER_TEXT_CLASSNAME)?.setAttribute('hidden', '')
this.leadingVisual?.removeAttribute('hidden')
}
}
2 changes: 1 addition & 1 deletion test/components/alpha/text_field_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_renders_a_spinner
Primer::Alpha::TextField.new(**@default_params, leading_visual: { icon: :search }, leading_spinner: true)
)

assert_selector "svg[data-target='primer-text-field.leadingSpinner']", visible: :hidden
assert_selector "[data-target='primer-text-field.leadingSpinner']", visible: :hidden
end

def test_enforces_leading_visual_when_spinner_requested
Expand Down
18 changes: 18 additions & 0 deletions test/system/alpha/text_field_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,23 @@ def test_show_and_hide_leading_spinner
assert_selector "[data-target='primer-text-field.leadingVisual']"
refute_selector "[data-target='primer-text-field.leadingSpinner']"
end

def test_shows_and_hides_screenreader_text
visit_preview(:playground, leading_spinner: true)

evaluate_multiline_script(<<~JS)
const textField = document.querySelector('primer-text-field')
textField.showLeadingSpinner()
JS

assert_selector "[data-target='primer-text-field.leadingSpinner'] .sr-only", text: "Loading"

evaluate_multiline_script(<<~JS)
const textField = document.querySelector('primer-text-field')
textField.hideLeadingSpinner()
JS

refute_selector "[data-target='primer-text-field.leadingSpinner'] .sr-only"
end
end
end
Loading