26 lines
458 B
JavaScript
26 lines
458 B
JavaScript
// src/refs.ts
|
|
import { isFunction } from "@chakra-ui/utils";
|
|
function assignRef(ref, value) {
|
|
if (ref == null)
|
|
return;
|
|
if (isFunction(ref)) {
|
|
ref(value);
|
|
return;
|
|
}
|
|
try {
|
|
ref.current = value;
|
|
} catch (error) {
|
|
throw new Error(`Cannot assign value '${value}' to ref '${ref}'`);
|
|
}
|
|
}
|
|
function mergeRefs(...refs) {
|
|
return (node) => {
|
|
refs.forEach((ref) => assignRef(ref, node));
|
|
};
|
|
}
|
|
|
|
export {
|
|
assignRef,
|
|
mergeRefs
|
|
};
|