Skip to content

add transformOrigin style property - #2106

Closed
lelandrichardson wants to merge 2 commits into
react:masterfrom
lelandrichardson:css-transform-origin
Closed

lelandrichardson wants to merge 2 commits into
react:masterfrom
lelandrichardson:css-transform-origin

Conversation

@lelandrichardson

Copy link
Copy Markdown
Contributor

This PR was being discussed in this issue: #1964

I've decided to submit a PR just so that the conversation can have a bit more context with the code involved, etc.

transform-origin spec

How it works

the transform origin property works by defining a translation matrix T which translates to the desired origin, and transforming a transform matrix M into M' by the following formula:

M' = TMT^-1

This is accomplished in the precomputeStyle method and only introduces new code paths if the transformOrigin property is assigned.

Known Issues with this PR

  1. The CSS spec interprets transform-origin: 0px 0px as placing the transform origin at the bottom left of the transformed element, where-as this code puts transformOrigin: { x: 0, y: 0 } to mean the center of the element (the default origin). This issue is difficult to remedy because in precomputeStyle, we don't have any knowledge of the width/height of the element in order to actually calculate what the "bottom left" of the element would be. For similar reasons, this makes it difficult to include other aspects of the CSS spec for this property, such as percentage values, etc.
  2. The transform origin property allows for x, y, and z points. If we want to animate these values with the new animated API, a new AnimatedValueXYZ class would need to be created, or the existing AnimatedXY class should be extended. I've left untouched for now as the z property of transformOrigin is seldom needed.
  3. The transform style property and the transformOrigin style property are not independent of one another, which mens that if we are animating one of them, the other must be included in the passed up style props to .setNativeProps. In order to accomplish this, a small check was added to the AnimatedStyle::getAnimatedValue() method. I'm open to suggestions on how we could potentially make this cleaner.

Example Usage:

                <Animated.View style={{
                    transform: [
                        {
                            rotateY: loading.interpolate({
                                inputRange: [0, 1],
                                outputRange: ['90deg', '0deg']
                            })
                        }
                    ],
                    transformOrigin: { x: -140, }
                }}>
                    ...
                </Animated.View>

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Jul 23, 2015
@browniefed

Copy link
Copy Markdown
Contributor

@lelandrichardson any more work on this?

@felixakiragreen

Copy link
Copy Markdown
Contributor

👍

@vjeux vjeux removed their assignment Oct 16, 2015

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typeof input === 'object' ?
Linter says that this code is not reachable.
@lelandrichardson could you fix? That is the only noticeable thing that stops this PR from being accepted.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@azproduction sorry for not seeing this sooner. I'm happy to fix this or anything else if that's all that's stopping it.

Do we feel like the other 3 issues I brought up are compromises we are willing to make at this time?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is important to have transform-origin CSS-compatible in order to avoid future collisions and misunderstandings.

By default, a layer's anchorPoint is (0.5, 0.5), which lies at the center of the layer

// Obj-C Fix
view.layer.anchorPoint = CGPointMake(0, 0);

Which could be applied somewhere here in React/Views/RCTViewManager.m

Or even we could make a separate property and avoid matrix math on JS-side. This also fixes 3-rd issue.

RCT_CUSTOM_VIEW_PROPERTY(transformOrigin, RCTTransformOrigin, RCTView)
{
  view.layer.anchorPoint = json ? [RCTConvert RCTTransformOrigin:json] : defaultView.layer.anchorPoint;
}

// RCTTransformOrigin and [RCTConvert RCTTransformOrigin] to be defined
// defaultView.layer.anchorPoint to be defined as CGPointMake(0, 0)
  • We could use anchorPoint instead of multiplying matrixes in order to support % values (with the default value of 0, 0).
  • Pixel values we could be casted to % using view.frame.

@vjeux any ideas how to fix it a better way?

@atticoos

atticoos commented Dec 5, 2015

Copy link
Copy Markdown
Contributor

Currently struggling to animate around a specific point, this would be very helpful. Currently using a container to reposition the element I'd like to rotate around a point, and animating that container.

@danleveille

Copy link
Copy Markdown

Any updates on this? 👍

@satya164

Copy link
Copy Markdown
Contributor

ping @lelandrichardson

@lelandrichardson

Copy link
Copy Markdown
Contributor Author

@satya164 @danleveille I'm happy to bring this work up to date, but I don't want to waste the time if it will never get merged... Right now I think there are too many issues with this property diverting from the web spec. I'm hoping someone from the core team can comment on next steps or whether or not these differences are small enough to move forward with this PR.

@satya164

Copy link
Copy Markdown
Contributor

cc @mkonicek

@mkonicek

mkonicek commented Jan 6, 2016

Copy link
Copy Markdown
Contributor

Maybe @sahrens has some context?

@satya164

Copy link
Copy Markdown
Contributor

Closing since no activity on the PR. let's re-open if you wanna work on it again.

@satya164 satya164 closed this Jan 26, 2016
@stephensprinkle-zz

Copy link
Copy Markdown

👍

@sahrens

sahrens commented Oct 20, 2017 •

Copy link
Copy Markdown
Contributor

There is still a lot of demand for this. Maybe we could just name it rn-transform-origin or something and get it in? Then we don't have to worry too much about deviating from the spec, and we can implement it correctly in the future without a collision.

@sahrens sahrens reopened this Oct 20, 2017
@stale

stale Bot commented Dec 19, 2017

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions.

@stale stale Bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Dec 19, 2017
@stale stale Bot closed this Dec 26, 2017
@satya164 satya164 reopened this Dec 26, 2017
@stale stale Bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Dec 26, 2017
@stale

stale Bot commented Feb 24, 2018

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions.

@stale stale Bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Feb 24, 2018
@hramos hramos added Type: Discussion Long running discussion. Core Team and removed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. GH Review: review-needed Stale There has been a lack of activity on this issue and it may be closed soon. labels Feb 27, 2018
@react-native-bot react-native-bot added Missing Changelog This PR appears to be missing a changelog, or they are incorrectly formatted. Ran Commands One of our bots successfully processed a command. labels Mar 16, 2018
@react-native-bot react-native-bot added the Missing Test Plan This PR appears to be missing a test plan. label May 15, 2018
@cinder92

cinder92 commented Jun 7, 2018

Copy link
Copy Markdown

Any update on this?

@hramos

hramos commented Jul 20, 2018

Copy link
Copy Markdown
Contributor

This PR has been inactive for a long while, and does not seem to be on a path towards getting merged in. If anyone wants to take over, please do send a new PR.

@hramos hramos closed this Jul 20, 2018
gabrieldonadel pushed a commit to gabrieldonadel/react-native that referenced this pull request Apr 23, 2026
…tion fixes (react#2912)

## Summary

TextInput commands (`setGhostText`, `blur`, `setSelection`) don't work
correctly under Fabric on macOS. This PR fixes three issues:

- **Add missing `setGhostText` command to Fabric**: Ghost text was
implemented for Paper in react#1890 / react#1897 (with undo fixes in react#2105 /
react#2106), but the Fabric command dispatcher and component view never got
the implementation. This ports the ghost text logic (insertion, removal,
attributes, delegate cleanup) from `RCTBaseTextInputView` to
`RCTTextInputComponentView`.
- **Fix `blur` command on macOS Fabric**: `blur` was a silent no-op
because it compared `[window firstResponder]` against the NSTextField,
but on macOS the firstResponder during editing is the field editor
(NSTextView), not the text field itself. Fixed by checking
`currentEditor` on NSTextField.
- **Fix ghost text being selectable on Fabric**: Ghost text could be
selected by the user on Fabric (but not Paper). Added cleanup in
`textInputDidChangeSelection` to clear ghost text when the user changes
selection, matching Paper behavior.
- **Fix `setTextAndSelection` delegate notification**: Changed
`notifyDelegate:YES` to `notifyDelegate:NO` to match Paper's
`setSelectionStart:selectionEnd:` behavior and prevent spurious delegate
callbacks during programmatic selection changes.

## Test plan

- [x] Verified `setGhostText` command works via RNTester on macOS Fabric
- [x] Verified `blur` command works on macOS Fabric  
- [x] Verified ghost text is not selectable on Fabric
- [x] Verified `focus`, `clear`, `setSelection` commands still work
- [x] JS TextInput tests pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Missing Changelog This PR appears to be missing a changelog, or they are incorrectly formatted. Missing Test Plan This PR appears to be missing a test plan. Ran Commands One of our bots successfully processed a command. Type: Discussion Long running discussion.

Projects

None yet

Development

Successfully merging this pull request may close these issues.