@wix/design-system
Version:
@wix/design-system
34 lines • 828 B
JavaScript
export function assignRef(ref, value) {
if (ref == null) {
return;
}
if (typeof ref === 'function') {
ref(value);
}
else {
try {
ref.current = value;
}
catch (error) {
throw new Error(`Cannot assign value "${value}" to ref "${ref}"`);
}
}
}
/**
* Passes or assigns a value to multiple refs (typically a DOM node). Useful for
* dealing with components that need an explicit ref for DOM calculations but
* also forwards refs assigned by an app.
*
* @param refs Refs to fork
*/
export function mergeRefs(...refs) {
if (refs.every(ref => ref == null)) {
return null;
}
return (node) => {
refs.forEach(ref => {
assignRef(ref, node);
});
};
}
//# sourceMappingURL=mergeRefs.js.map