Skip to content

Support MergedRef - #246

Open
atootoonchianmeta wants to merge 10 commits into
meta:mainfrom
atootoonchianmeta:add_merged_ref
Open

atootoonchianmeta wants to merge 10 commits into
meta:mainfrom
atootoonchianmeta:add_merged_ref

Conversation

@atootoonchianmeta

Copy link
Copy Markdown
Contributor
  • Update data type of nodeRef in ALSurfaceProps support both object reference and callback reference. The later is used for mergedRef
  • use nodeRef if the data type of nodeRef is object

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 1, 2025

@reshadi reshadi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please also add some example in the test app to use a ref callback.

uiEventMetadata?: EventMetadata,
capability?: ALSurfaceCapability,
nodeRef?: React.RefObject<HTMLElement | null | undefined>,
nodeRef?: React.RefObject<HTMLElement | null | undefined> | React.RefCallback<HTMLElement | null | undefined>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think you can just use React.Ref which is defined as the combination

* also use that for our mount/unmount event.
*/
const nodeRef = props.nodeRef ?? localRef;
const isPropsNodeRef = props.nodeRef != null && typeof props.nodeRef === "object";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We still need to support the function one. What I think you need to do for callback case is to use the localRef bellow, but inside of the useEffect the props.nodeRef with the localRef.currentValue.

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.

Thanks for your feedback. I want to understand your comment correctly. We only use nodeRef to extract the element value, and the element is only available when the nodeRef is an object. Within the useEffect we can only check the type of nodeRef and return the localRef or nodeRef based on the type of nodeRef.

@reshadi reshadi Jul 17, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for your feedback. I want to understand your comment correctly. We only use nodeRef to extract the element value, and the element is only available when the nodeRef is an object. Within the useEffect we can only check the type of nodeRef and return the localRef or nodeRef based on the type of nodeRef.

If we have a nodeRef is available in either case. The difference is how you can read it. For objects you read the .current value, but for function, it will be called.

So, I think you should switch to use a function ref for our own local usage and inside of it:

  • use the passed node
  • update the .current of passed node ref
  • or call the ref that is passed

Take a look at how function refs work in react. You can also look at some of the 'mergeRef` functions in the internal code.

import * as IReactPropsExtension from "hyperion-react/src/IReactPropsExtension";
import * as Types from "hyperion-util/src/Types";
import type * as React from 'react';
import * as React from 'react';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why did you remove the type from here? We should make sure that autologigng does not import react directly as much as possible. So far we have able to get around direct import of react

Comment on lines +241 to +244
if (typeof ref === 'function') {
// Function refs don't expose current; you can't get element here safely
// unless you track it yourself, so return null
return null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is still incorrect. If there is function used for ref, you would return null which will bypass all of the event emitting logic in the rest of the function that calls this function.


let addSurfaceWrapper = props.nodeRef == null;
let localRef = ReactModule.useRef<Element>();
const localRef = ReactModule.useRef<Element | null>(null);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure making this null helps much.
Also, see the line above, if a ref is passed, then we won't add any wrapper, which means your merged ref function is never called!

I see this a core issue we need to think about how to solve! The goal of having refs is to avoid wrappers, but then a function ref will disable almost everything (i.e. no wrapper, and no events)

};

return (
<SurfaceComp surface='nodeRefs'>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

to really test this, you should pass a function ref to Surface and see if everything still works.

@reshadi

reshadi commented Jul 31, 2025

Copy link
Copy Markdown
Contributor

I spent a bunch of time on this, but couldn't figure out any mechanism to handle the ref function callbacks AND avoid adding our own wrapper. Therefore, passing a ref function is the same as not passing ref at all and there is not much we can do differently. So, it might be better leave the code as is to signal to users we are not using it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants