Skip to content

Commit 1f2f383

Browse files
authored
feat(macos): support selectionColor via NSTextView.selectedTextAttributes (#265)
1 parent fa2c601 commit 1f2f383

5 files changed

Lines changed: 16 additions & 8 deletions

File tree

apps/macos-example/src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export default function App() {
8383
onLinkPress={handleLinkPress}
8484
markdownStyle={markdownStyle}
8585
contextMenuItems={contextMenuItems}
86+
selectionColor="#DCDDFE"
8687
/>
8788
</ScrollView>
8889
{lastLink != null && (

ios/utils/ENRMUIKit.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,16 @@ static inline void ENRMSetCursorColor(ENRMPlatformTextView *textView, RCTUIColor
203203
#endif
204204
}
205205

206-
/// Cross-platform selection color: iOS uses tintColor (affects both cursor and selection);
207-
/// macOS selection highlight is managed by the system and not directly settable.
206+
/// Cross-platform selection color: iOS uses tintColor (also affects the caret
207+
/// and selection handles); macOS sets the selection background via
208+
/// `selectedTextAttributes`. Pass `nil` to restore the system default.
208209
static inline void ENRMSetSelectionColor(ENRMPlatformTextView *textView, RCTUIColor *color)
209210
{
210211
#if !TARGET_OS_OSX
211212
textView.tintColor = color;
213+
#else
214+
RCTUIColor *resolved = color ?: [NSColor selectedTextBackgroundColor];
215+
textView.selectedTextAttributes = @{NSBackgroundColorAttributeName : resolved};
212216
#endif
213217
}
214218

src/EnrichedMarkdownNativeComponent.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,10 @@ export interface NativeProps extends ViewProps {
277277
* Color of the text selection highlight.
278278
*
279279
* On iOS, this also affects the caret and selection handle colors
280-
* (they share a single tint).
280+
* (they share a single tint). On macOS, only the selection background
281+
* is affected.
281282
*
282-
* @platform ios, android, web
283+
* @platform ios, android, macos, web
283284
*/
284285
selectionColor?: ColorValue;
285286
/**

src/EnrichedMarkdownTextNativeComponent.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,10 @@ export interface NativeProps extends ViewProps {
277277
* Color of the text selection highlight.
278278
*
279279
* On iOS, this also affects the caret and selection handle colors
280-
* (they share a single tint).
280+
* (they share a single tint). On macOS, only the selection background
281+
* is affected.
281282
*
282-
* @platform ios, android, web
283+
* @platform ios, android, macos, web
283284
*/
284285
selectionColor?: ColorValue;
285286
/**

src/types/MarkdownTextProps.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ export interface EnrichedMarkdownTextProps extends Omit<ViewProps, 'style'> {
9595
* Color of the text selection highlight.
9696
*
9797
* On iOS, this also affects the caret and selection handle colors
98-
* (they share a single tint).
98+
* (they share a single tint). On macOS, only the selection background
99+
* is affected.
99100
*
100-
* @platform ios, android, web
101+
* @platform ios, android, macos, web
101102
*/
102103
selectionColor?: ColorValue;
103104
/**

0 commit comments

Comments
 (0)