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

Localization breaks accessibility of some elements #842

Merged
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
16 changes: 8 additions & 8 deletions Sources/Orbit/Components/InputField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public struct InputField<Label: View, Prompt: View, Prefix: View, Suffix: View>:
@Environment(\.inputFieldBeginEditingAction) private var inputFieldBeginEditingAction
@Environment(\.inputFieldEndEditingAction) private var inputFieldEndEditingAction
@Environment(\.isEnabled) private var isEnabled
@Environment(\.locale) private var locale
@Environment(\.sizeCategory) private var sizeCategory
@Environment(\.textColor) private var textColor

Expand Down Expand Up @@ -136,19 +135,19 @@ public struct InputField<Label: View, Prompt: View, Prefix: View, Suffix: View>:
isFocused = false
inputFieldEndEditingAction()
}
.overlay(
resolvedPrompt,
alignment: .leadingFirstTextBaseline
)
.accessibility {
.accessibility(children: nil) {
label
} value: {
Text(value)
} hint: {
Text(message?.description ?? "")
prompt
}
.overlay(
resolvedPrompt,
alignment: .leadingFirstTextBaseline
)
}

@ViewBuilder private var secureTextRedactedButton: some View {
if showSecureTextRedactedButton {
IconButton(isSecureTextRedacted ? .visibility : .visibilityOff) {
Expand Down Expand Up @@ -178,6 +177,7 @@ public struct InputField<Label: View, Prompt: View, Prefix: View, Suffix: View>:
.lineLimit(1)
.padding(.horizontal, .small)
.allowsHitTesting(false)
.accessibility(hidden: true)
}
}

Expand Down
8 changes: 4 additions & 4 deletions Sources/Orbit/Components/ListChoice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public struct ListChoice<Icon: View, Title: View, Description: View, Header: Vie
} hint: {
description
}
.accessibility(addTraits: accessibilityTraitsToAdd)
.accessibility(addTraits: accessibilityTraits)
.accessibility(.listChoice)
}
}
Expand Down Expand Up @@ -240,12 +240,12 @@ public struct ListChoice<Icon: View, Title: View, Description: View, Header: Vie
title.isEmpty && description.isEmpty
}

private var accessibilityTraitsToAdd: AccessibilityTraits {
private var accessibilityTraits: AccessibilityTraits {
switch disclosure {
case .none, .disclosure, .button(.add), .buttonLink, .checkbox(false, _), .radio(false, _), .icon:
return []
.isButton
case .button(.remove), .checkbox(true, _), .radio(true, _):
return .isSelected
[.isButton, .isSelected]
}
}

Expand Down
8 changes: 8 additions & 0 deletions Sources/Orbit/Components/Textarea.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ public struct Textarea<Label: View, Prompt: View>: View, TextFieldBuildable {
isFocused = false
inputFieldEndEditingAction()
}
.accessibility(children: nil) {
label
} value: {
Text(value)
} hint: {
prompt
}
.overlay(resolvedPrompt, alignment: .topLeading)
}

Expand All @@ -94,6 +101,7 @@ public struct Textarea<Label: View, Prompt: View>: View, TextFieldBuildable {
.textColor(isEnabled ? state.placeholderColor : .cloudDarkActive)
.padding(.small)
.allowsHitTesting(false)
.accessibility(hidden: true)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,34 @@ struct AccessibilityLabelValueModifier<Label: View, Value: View, Hint: View>: Vi
@Environment(\.localizationBundle) private var localizationBundle
@Environment(\.locale) private var locale

let childBehavior: AccessibilityChildBehavior?
@ViewBuilder let label: Label
@ViewBuilder let value: Value
@ViewBuilder let hint: Hint

func body(content: Content) -> some View {
if isLabelAndValueTextual {
content
.accessibilityElement(children: .ignore)
.accessibility(label: textualLabel ?? SwiftUI.Text(""))
.accessibility(value: textualValue ?? SwiftUI.Text(""))
.accessibility(hint: textualHint ?? SwiftUI.Text(""))
if isLabelTextual {
if let childBehavior {
content
.accessibilityElement(children: childBehavior)
.accessibility(label: textualLabel ?? SwiftUI.Text(""))
.accessibility(value: textualValue ?? SwiftUI.Text(""))
.accessibility(hint: textualHint ?? SwiftUI.Text(""))

} else {
content
.accessibility(label: textualLabel ?? SwiftUI.Text(""))
.accessibility(value: textualValue ?? SwiftUI.Text(""))
.accessibility(hint: textualHint ?? SwiftUI.Text(""))
}
} else {
content
.accessibilityElement(children: .contain)
}
}

private var isLabelAndValueTextual: Bool {
textualLabel != nil
&& (textualValue != nil || value is EmptyView)
&& (textualHint != nil || hint is EmptyView)
private var isLabelTextual: Bool {
textualLabel != nil
}

private var textualLabel: SwiftUI.Text? {
Expand All @@ -44,10 +51,11 @@ struct AccessibilityLabelValueModifier<Label: View, Value: View, Hint: View>: Vi
extension View {

func accessibility<Label: View, Value: View, Hint: View>(
children: AccessibilityChildBehavior? = .ignore,
@ViewBuilder label: () -> Label,
@ViewBuilder value: () -> Value = { EmptyView() },
@ViewBuilder hint: () -> Hint = { EmptyView() }
) -> some View {
modifier(AccessibilityLabelValueModifier(label: label, value: value, hint: hint))
modifier(AccessibilityLabelValueModifier(childBehavior: children, label: label, value: value, hint: hint))
}
}
Loading