Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow to pass ref to ArcherElement children #185

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 31 additions & 4 deletions src/ArcherElement/ArcherElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ type ArcherElementProps = {
*/
id: string;
relations?: Array<RelationType>;
children: React.ReactElement<React.ComponentProps<any>, any>;
children: React.ReactElement;
};

const ArcherElement = ({ id, relations = [], children }: ArcherElementProps) => {
const encodedId = useMemo(() => encodeId(id), [id]);
const context = useContext(ArcherContainerContext);
const ref = useRef<HTMLElement>();
const archerRef = useRef<HTMLElement>();

const registerTransitions = useCallback(
(newRelations: Array<RelationType>) => {
Expand Down Expand Up @@ -54,7 +54,7 @@ const ArcherElement = ({ id, relations = [], children }: ArcherElementProps) =>
}, [context, encodedId]);

useLayoutEffect(() => {
registerChild(ref.current);
registerChild(archerRef.current);

return () => unregisterChild();
}, [registerChild, unregisterChild]);
Expand All @@ -70,7 +70,34 @@ const ArcherElement = ({ id, relations = [], children }: ArcherElementProps) =>
// Now, we'll render this child by getting its ref. The ref will be used to compute the element's position.
// I'm pretty sure there's a cleaner way to get the ref of the child... feel free to suggest it!
const child = children;
return React.cloneElement(child, { ...child.props, ref });
return React.cloneElement(child, {
...child.props,
ref: (node: HTMLElement) => {
archerRef.current = node;

// @ts-expect-error could not find a proper type for Children
const ref = child.ref as React.MutableRefObject<any> | React.LegacyRef<any>;

if (!ref) {
return;
}

if (typeof ref === 'string') {
console.error('[React Archer] Legacy string refs are not supported');
return;
}

if (typeof ref === 'function') {
ref(node);
return;
}

if (ref && ref.current !== undefined) {
// @ts-expect-error LegacyRef cast above is annoying - it marks the ref as non mutable
ref.current = node;
}
},
});
};

export default ArcherElement;