microsoft / react-native-macos

A framework for building native macOS apps with React.
https://microsoft.github.io/react-native-windows/
MIT License
3.39k stars 130 forks source link

Input focus area doesn't move when parent view is transformed #2063

Open mojocakes opened 6 months ago

mojocakes commented 6 months ago

Environment

react-native -v:0.71.15
npm ls react-native-macos:0.71.36
node -v:v18.12.1
npm -v:8.19.2
yarn --version:1.22.19
xcodebuild -version: Xcode 15.2 Build version 15C500

Steps to reproduce the bug

When using the translate style to manipulate the position of a <View> component, the new position doesn't seem to be applied to the focus area of any child <TextInput> components.

The following JSX in the demo project will recreate the issue:

<View style={{
    backgroundColor: 'white',
    borderColor: 'red',
    borderWidth: 1,
    margin: 20,
    padding: 20,
    transform: [{ translateY: -100 }],
}}>
  <TextInput placeholder="Write here..." style={{
      borderColor: 'orange',
      borderWidth: 1,
  }}/>
</View>

Expected Behavior

The input focus area should be anchored to the bounding box of the input

Actual Behavior

You can see the blue input focus area is no longer positioned above the input:

Screenshot 2024-01-30 at 12 05 19 pm

Reproducible Demo

No response

Additional context

No response

AdrianFahrbach commented 5 months ago

I got the same issue!

In my case I got a drawer element with an input that gets moved out of the screen. See here:

https://github.com/microsoft/react-native-macos/assets/45072099/28ec29ce-33b8-4307-8fc9-8741a77148a1

The solution in my case is to blur the input field before moving it. This only works with multiline input fields though, so I will make the single line input a multiline field. I also already tried setting the multiline prop right before blurring the input, but react-native absolutely does not like that and throws an error.

Saadnajmi commented 5 months ago

I have an idea of why this happens, not sure it'll fix it.

We override drawFocusRingMask so we can support corner radius, etc. Perhaps we also need to account for translation there? Because it's mask (I.E: the thing you put on top of a rect, not where the rect is) I'm not sure if that's the right path forward, but maybe a shot?

https://github.com/search?q=repo%3Amicrosoft%2Freact-native-macos%20drawFocusRing&type=code

AdrianFahrbach commented 5 months ago

It also looks like the focus ring is the only clickable area of the input fields. In this video the input fields are transformed down by 40.

https://github.com/microsoft/react-native-macos/assets/45072099/ecdc6049-5bea-41d2-b40d-8b84d4e299a7

We override drawFocusRingMask so we can support corner radius, etc.

In my case the focus ring does not follow the border radius of the input field. Increasing the border radius makes this a lot more visible: screenshot 2024-02-26 at 23 28 52@2x

Is this intentional or should the focus ring have the same border radius as well? Second question: Can I maybe help you by testing your theory by removing some lines in the node_modules?

Saadnajmi commented 5 months ago

@AdrianFahrbach Oof, yes both of examples look wrong to me. You should be able to just make local edits and build, yeah. I'm not sure what local edits per say, but stuff around drawFocusRingMask or any of these overrides to start: https://developer.apple.com/documentation/appkit/nsview/appearance#1662106

Also, if you replace the TextInput with a normal View, do you still repro? That would tell me it's TextInput specific.

AdrianFahrbach commented 5 months ago

The focus is rounded on a regular view, it suffers from the same transform problem though:

https://github.com/microsoft/react-native-macos/assets/45072099/2ebd473f-4c99-4471-bfd6-0f7cb0097abd

I did try removing the following from react-native-macos/React/Views/RCTView.m, but I didn't get any focus border at all then.

#pragma mark Focus ring

- (CGRect)focusRingMaskBounds
{
  return self.bounds;
}

- (void)drawFocusRingMask
{
  if ([self enableFocusRing]) {
    CGContextRef context = NSGraphicsContext.currentContext.CGContext;
    RCTCornerInsets cornerInsets = RCTGetCornerInsets(self.cornerRadii, NSEdgeInsetsZero);
    CGPathRef path = RCTPathCreateWithRoundedRect(self.bounds, cornerInsets, NULL);

    CGContextAddPath(context, path);
    CGContextFillPath(context);
    CGPathRelease(path);
  }
}
Saadnajmi commented 4 months ago

We will probably fix https://github.com/microsoft/react-native-macos/issues/2038 before this one to see if they are related

AdrianFahrbach commented 2 months ago

This is still an issue after the fix. Not much of an annoyance though.