Skip to content

Commit c030b7c

Browse files
authored
Fabric Text components use their text value as the accessibility name by default (#12039)
Fabric Text components use their text value as the accessibility name by default * Components can provide a default accessible name * Text component falls back to the content if accessibleLabel isn't set * Update accessibilityRole logic to match style
1 parent 0cff913 commit c030b7c

File tree

6 files changed

+26
-4
lines changed

6 files changed

+26
-4
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Components can provide a default accessible name Text component falls back to the content if accessibleLabel isn't set",
4+
"packageName": "react-native-windows",
5+
"email": "26607885+chrisglein@users.noreply.github.com",
6+
"dependentChangeType": "patch"
7+
}

vnext/Microsoft.ReactNative/Fabric/Composition/CompositionDynamicAutomationProvider.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ HRESULT __stdcall CompositionDynamicAutomationProvider::GetPropertyValue(PROPERT
234234
switch (propertyId) {
235235
case UIA_ControlTypePropertyId: {
236236
pRetVal->vt = VT_I4;
237-
auto role = props->accessibilityRole == "" ? baseView.get()->DefaultControlType() : props->accessibilityRole;
237+
auto role = props->accessibilityRole.empty() ? baseView->DefaultControlType() : props->accessibilityRole;
238238
pRetVal->lVal = GetControlType(role);
239239
break;
240240
}
@@ -247,7 +247,8 @@ HRESULT __stdcall CompositionDynamicAutomationProvider::GetPropertyValue(PROPERT
247247
}
248248
case UIA_NamePropertyId: {
249249
pRetVal->vt = VT_BSTR;
250-
auto wideName = ::Microsoft::Common::Unicode::Utf8ToUtf16(props->accessibilityLabel);
250+
auto wideName = ::Microsoft::Common::Unicode::Utf8ToUtf16(
251+
props->accessibilityLabel.empty() ? baseView->DefaultAccessibleName() : props->accessibilityLabel);
251252
pRetVal->bstrVal = SysAllocString(wideName.c_str());
252253
hr = pRetVal->bstrVal != nullptr ? S_OK : E_OUTOFMEMORY;
253254
break;

vnext/Microsoft.ReactNative/Fabric/Composition/CompositionViewComponentView.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,10 @@ void CompositionBaseComponentView::updateAccessibilityProps(
10721072
provider, UIA_IsKeyboardFocusablePropertyId, oldViewProps.focusable, newViewProps.focusable);
10731073

10741074
winrt::Microsoft::ReactNative::implementation::UpdateUiaProperty(
1075-
provider, UIA_NamePropertyId, oldViewProps.accessibilityLabel, newViewProps.accessibilityLabel);
1075+
provider,
1076+
UIA_NamePropertyId,
1077+
oldViewProps.accessibilityLabel,
1078+
newViewProps.accessibilityLabel.empty() ? DefaultAccessibleName() : newViewProps.accessibilityLabel);
10761079

10771080
winrt::Microsoft::ReactNative::implementation::UpdateUiaProperty(
10781081
provider,
@@ -1222,6 +1225,10 @@ std::string CompositionBaseComponentView::DefaultControlType() const noexcept {
12221225
return "group";
12231226
}
12241227

1228+
std::string CompositionBaseComponentView::DefaultAccessibleName() const noexcept {
1229+
return "";
1230+
}
1231+
12251232
CompositionViewComponentView::CompositionViewComponentView(
12261233
const winrt::Microsoft::ReactNative::Composition::ICompositionContext &compContext,
12271234
facebook::react::Tag tag)

vnext/Microsoft.ReactNative/Fabric/Composition/CompositionViewComponentView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ struct CompositionBaseComponentView : public IComponentView,
6767
winrt::IInspectable EnsureUiaProvider() noexcept override;
6868

6969
virtual std::string DefaultControlType() const noexcept;
70+
virtual std::string DefaultAccessibleName() const noexcept;
7071

7172
protected:
7273
std::array<winrt::Microsoft::ReactNative::Composition::SpriteVisual, SpecialBorderLayerCount>

vnext/Microsoft.ReactNative/Fabric/Composition/ParagraphComponentView.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ void ParagraphComponentView::updateProps(
5454
updateTextAlignment(newViewProps.textAttributes.alignment);
5555
}
5656

57+
updateAccessibilityProps(oldViewProps, newViewProps);
5758
updateBorderProps(oldViewProps, newViewProps);
5859

5960
m_props = std::static_pointer_cast<facebook::react::ParagraphProps const>(props);
@@ -479,6 +480,10 @@ std::string ParagraphComponentView::DefaultControlType() const noexcept {
479480
return "text";
480481
}
481482

483+
std::string ParagraphComponentView::DefaultAccessibleName() const noexcept {
484+
return m_attributedStringBox.getValue().getString();
485+
}
486+
482487
winrt::Microsoft::ReactNative::Composition::IVisual ParagraphComponentView::Visual() const noexcept {
483488
return m_visual;
484489
}

vnext/Microsoft.ReactNative/Fabric/Composition/ParagraphComponentView.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ struct ParagraphComponentView : CompositionBaseComponentView {
4242
facebook::react::SharedTouchEventEmitter touchEventEmitterAtPoint(facebook::react::Point pt) noexcept override;
4343

4444
winrt::Microsoft::ReactNative::Composition::IVisual Visual() const noexcept override;
45-
virtual std::string DefaultControlType() const noexcept;
45+
virtual std::string DefaultControlType() const noexcept override;
46+
virtual std::string DefaultAccessibleName() const noexcept override;
4647

4748
private:
4849
ParagraphComponentView(

0 commit comments

Comments
 (0)