Skip to content

Commit 0413197

Browse files
committed
set aria message message as a parameter
1 parent cfd6ddd commit 0413197

3 files changed

Lines changed: 43 additions & 5 deletions

File tree

app/components/primer/open_project/danger_dialog.html.erb

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
<% dialog.with_header(close_label: I18n.t("button_close")) %>
44
<% end %>
55

6-
<danger-dialog-form-helper>
6+
<danger-dialog-form-helper
7+
data-confirmation-live-message-checked="<%= @confirmation_live_message_checked %>"
8+
data-confirmation-live-message-unchecked="<%= @confirmation_live_message_unchecked %>">
9+
710
<%= render(@form_wrapper) do %>
811
<scrollable-region data-labelled-by="<%= labelledby %>">
912
<%= render(Primer::Alpha::Dialog::Body.new) do %>
@@ -15,9 +18,26 @@
1518
<% end %>
1619
</scrollable-region>
1720
<%= render(Primer::Alpha::Dialog::Footer.new(show_divider: false)) do %>
18-
<%= render(Primer::Beta::Button.new(data: { "close-dialog-id": dialog_id })) { @cancel_button_text } %>
19-
<%= render(Primer::Beta::Button.new(type: (@form_wrapper.shows_form? ? :submit : :button), scheme: :danger, aria: { describedby: "#{dialog_id}-check_box" }, data: { "submit-dialog-id": dialog_id })) { @confirm_button_text } %>
21+
<%= render(Primer::Beta::Button.new(
22+
data: { "close-dialog-id": dialog_id }
23+
)) { @cancel_button_text } %>
24+
25+
<%= render(Primer::Beta::Button.new(
26+
type: (@form_wrapper.shows_form? ? :submit : :button),
27+
scheme: :danger,
28+
disabled: true,
29+
aria: { describedby: "#{dialog_id}-check_box" },
30+
data: {
31+
"submit-dialog-id": dialog_id,
32+
target: "danger-dialog-form-helper.submitButton"
33+
}
34+
)) { @confirm_button_text } %>
2035
<% end %>
36+
37+
<div class="sr-only"
38+
aria-live="assertive"
39+
aria-atomic="true"
40+
data-target="danger-dialog-form-helper.liveRegion"></div>
2141
<% end %>
2242
</danger-dialog-form-helper>
2343
<% end %>

app/components/primer/open_project/danger_dialog.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class DangerDialog < Primer::Component
6565
# @param form_arguments [Hash] Allows the dialog to submit a form. Pass EITHER the `builder:` option to this hash to reuse an existing Rails form, or `action:` if you prefer the component to render the form tag itself. `builder:` should be an instance of `ActionView::Helpers::FormBuilder`, which is created by the standard Rails `#form_with` and `#form_for` helpers. The `name:` option is the desired name of the field that will be included in the params sent to the server on form submission.
6666
# @param id [String] The id of the dialog.
6767
# @param title [String] The title of the dialog. Although visually hidden, a label is rendered for assistive technologies.
68+
# @param confirmation_live_message_checked [String] The message announced to assistive technologies when the confirmation checkbox is checked. Defaults to "Confirmation checkbox checked. You can now proceed."
69+
# @param confirmation_live_message_unchecked [String] The message announced to assistive technologies when the confirmation checkbox is unchecked. Defaults to "Please check the confirmation box to proceed."
6870
# @param confirm_button_text [String] The text of the confirm button. Will default to `I18n.t("button_delete")`, or `I18n.t("button_delete_permanently")` if `confirmation_check_box` slot has been passed to the component.
6971
# @param cancel_button_text [String] The text of the cancel button. Will default to `I18n.t("button_cancel")`.
7072
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
@@ -74,6 +76,8 @@ def initialize(
7476
title:,
7577
confirm_button_text: nil,
7678
cancel_button_text: nil,
79+
confirmation_live_message_checked: "Confirmation checkbox checked. You can now proceed.",
80+
confirmation_live_message_unchecked: "Please check the confirmation box to proceed.",
7781
**system_arguments
7882
)
7983
@check_box_name = form_arguments.delete(:name) || "confirm_dangerous_action"
@@ -83,6 +87,9 @@ def initialize(
8387
@confirm_button_text = confirm_button_text
8488
@cancel_button_text = cancel_button_text
8589

90+
@confirmation_live_message_checked = confirmation_live_message_checked
91+
@confirmation_live_message_unchecked = confirmation_live_message_unchecked
92+
8693
deny_single_argument(:role, "`role` will always be set to `alertdialog`.", **system_arguments)
8794

8895
@system_arguments = system_arguments

app/components/primer/open_project/danger_dialog_form_helper.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const SUBMIT_BUTTON_SELECTOR = 'input[type=submit],button[type=submit],button[da
55
@controller
66
class DangerDialogFormHelperElement extends HTMLElement {
77
@target checkbox: HTMLInputElement | undefined
8+
@target liveRegion: HTMLElement | undefined
89

910
get form() {
1011
return this.querySelector('form')
@@ -25,8 +26,18 @@ class DangerDialogFormHelperElement extends HTMLElement {
2526
}
2627

2728
toggle(): void {
28-
if (this.checkbox) {
29-
this.submitButton.disabled = !this.checkbox.checked
29+
if (!this.checkbox || !this.submitButton) return
30+
31+
const enabled = this.checkbox.checked
32+
this.submitButton.disabled = !enabled
33+
34+
if (this.liveRegion) {
35+
const message = enabled
36+
? this.dataset.confirmationLiveMessageChecked
37+
: this.dataset.confirmationLiveMessageUnchecked
38+
39+
this.liveRegion.textContent = ''
40+
this.liveRegion!.textContent = message || ''
3041
}
3142
}
3243

0 commit comments

Comments
 (0)