Skip to content

Commit bad9007

Browse files
iamAbhi-916rnbot
authored andcommitted
Fix selectable text not receiving pointer events inside ScrollView (#15577)
* Fix selectable text not working inside ScrollView * Change files
1 parent 24f8960 commit bad9007

3 files changed

Lines changed: 37 additions & 4 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Fix selectable text not working inside ScrollView",
4+
"packageName": "react-native-windows",
5+
"email": "74712637+iamAbhi-916@users.noreply.github.com",
6+
"dependentChangeType": "patch"
7+
}

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,26 @@ void ParagraphComponentView::updateTextAlignment(
167167
m_textLayout = nullptr;
168168
}
169169

170+
facebook::react::Tag ParagraphComponentView::hitTest(
171+
facebook::react::Point pt,
172+
facebook::react::Point &localPt,
173+
bool ignorePointerEvents) const noexcept {
174+
facebook::react::Point ptLocal{pt.x - m_layoutMetrics.frame.origin.x, pt.y - m_layoutMetrics.frame.origin.y};
175+
const auto &props = paragraphProps();
176+
const auto &vProps = *viewProps();
177+
178+
if (props.isSelectable && ptLocal.x >= 0 && ptLocal.x <= m_layoutMetrics.frame.size.width && ptLocal.y >= 0 &&
179+
ptLocal.y <= m_layoutMetrics.frame.size.height) {
180+
// claims if pointer events are enabled for this component
181+
if (ignorePointerEvents || vProps.pointerEvents == facebook::react::PointerEventsMode::Auto ||
182+
vProps.pointerEvents == facebook::react::PointerEventsMode::BoxOnly) {
183+
localPt = ptLocal;
184+
return Tag();
185+
}
186+
}
187+
return Super::hitTest(pt, localPt, ignorePointerEvents);
188+
}
189+
170190
bool ParagraphComponentView::IsTextSelectableAtPoint(facebook::react::Point pt) noexcept {
171191
// paragraph-level selectable prop is enabled
172192
const auto &props = paragraphProps();
@@ -534,7 +554,8 @@ void ParagraphComponentView::OnPointerPressed(
534554
return;
535555
}
536556

537-
auto pp = args.GetCurrentPoint(-1);
557+
// Use Tag() to get coordinates in component's local space
558+
auto pp = args.GetCurrentPoint(static_cast<int32_t>(Tag()));
538559

539560
// Ignores right-click
540561
if (pp.Properties().PointerUpdateKind() ==
@@ -545,8 +566,8 @@ void ParagraphComponentView::OnPointerPressed(
545566

546567
auto position = pp.Position();
547568

548-
facebook::react::Point localPt{
549-
position.X - m_layoutMetrics.frame.origin.x, position.Y - m_layoutMetrics.frame.origin.y};
569+
// GetCurrentPoint(Tag()) returns position relative to component origin
570+
facebook::react::Point localPt{position.X, position.Y};
550571

551572
std::optional<int32_t> charPosition = GetTextPositionAtPoint(localPt);
552573

@@ -620,7 +641,7 @@ void ParagraphComponentView::OnPointerMoved(
620641
void ParagraphComponentView::OnPointerReleased(
621642
const winrt::Microsoft::ReactNative::Composition::Input::PointerRoutedEventArgs &args) noexcept {
622643
// Check for right-click to show context menu
623-
auto pp = args.GetCurrentPoint(-1);
644+
auto pp = args.GetCurrentPoint(static_cast<int32_t>(Tag()));
624645
if (pp.Properties().PointerUpdateKind() ==
625646
winrt::Microsoft::ReactNative::Composition::Input::PointerUpdateKind::RightButtonReleased) {
626647
const auto &props = paragraphProps();

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ struct ParagraphComponentView : ParagraphComponentViewT<ParagraphComponentView,
4949
static facebook::react::SharedViewProps defaultProps() noexcept;
5050
const facebook::react::ParagraphProps &paragraphProps() const noexcept;
5151

52+
facebook::react::Tag hitTest(
53+
facebook::react::Point pt,
54+
facebook::react::Point &localPt,
55+
bool ignorePointerEvents = false) const noexcept override;
56+
5257
// Returns true when text is selectable
5358
bool focusable() const noexcept override;
5459

0 commit comments

Comments
 (0)