Skip to content

Commit ce1b074

Browse files
Nick LefeverSaadnajmi
authored andcommitted
[fabric] Add support for setting a tool tip on View component
Summary: This diff adds the macOS `tooltip` property to the View component and assigns the provided text to the native NSView `toolTip` property. Test Plan: * Run Zeratul with Fabric enabled * Hover over buttons and check that a tool tip is displayed. https://pxl.cl/4nM7h Reviewers: shawndempsey, #rn-desktop Reviewed By: shawndempsey Differential Revision: https://phabricator.intern.facebook.com/D54045717
1 parent 2dddc18 commit ce1b074

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,17 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
588588
needsInvalidateLayer = YES;
589589
}
590590

591+
#if TARGET_OS_OSX // [macOS]
592+
// `tooltip`
593+
if (oldViewProps.tooltip != newViewProps.tooltip) {
594+
if (newViewProps.tooltip.has_value()) {
595+
self.toolTip = RCTNSStringFromStringNilIfEmpty(newViewProps.tooltip.value());
596+
} else {
597+
self.toolTip = nil;
598+
}
599+
}
600+
#endif // macOS]
601+
591602
_needsInvalidateLayer = _needsInvalidateLayer || needsInvalidateLayer;
592603

593604
_props = std::static_pointer_cast<const ViewProps>(props);

packages/react-native/ReactCommon/react/renderer/components/view/platform/macos/react/renderer/components/view/HostPlatformViewProps.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,16 @@ HostPlatformViewProps::HostPlatformViewProps(
6565
rawProps,
6666
"keyUpEvents",
6767
sourceProps.keyUpEvents,
68-
{})) {}
68+
{})),
69+
tooltip(
70+
ReactNativeFeatureFlags::enableCppPropsIteratorSetter()
71+
? sourceProps.tooltip
72+
: convertRawProp(
73+
context,
74+
rawProps,
75+
"tooltip",
76+
sourceProps.tooltip,
77+
{})) {}
6978

7079
#define VIEW_EVENT_CASE_MACOS(eventType) \
7180
case CONSTEXPR_RAW_PROPS_KEY_HASH("on" #eventType): { \
@@ -102,6 +111,7 @@ void HostPlatformViewProps::setProp(
102111
RAW_SET_PROP_SWITCH_CASE_BASIC(enableFocusRing);
103112
RAW_SET_PROP_SWITCH_CASE_BASIC(keyDownEvents);
104113
RAW_SET_PROP_SWITCH_CASE_BASIC(keyUpEvents);
114+
RAW_SET_PROP_SWITCH_CASE_BASIC(tooltip);
105115
}
106116
}
107117

packages/react-native/ReactCommon/react/renderer/components/view/platform/macos/react/renderer/components/view/HostPlatformViewProps.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
#include <react/renderer/components/view/KeyEvent.h>
1818

19+
#include <optional>
20+
#include <string>
21+
1922
#include "HostPlatformViewEvents.h"
2023

2124
namespace facebook::react {
@@ -43,5 +46,7 @@ class HostPlatformViewProps : public BaseViewProps {
4346

4447
std::vector<HandledKey> keyDownEvents{};
4548
std::vector<HandledKey> keyUpEvents{};
49+
50+
std::optional<std::string> tooltip{};
4651
};
4752
} // namespace facebook::react

0 commit comments

Comments
 (0)