UNPKG

@wix/design-system

Version:

@wix/design-system

48 lines (45 loc) 1.26 kB
"use strict"; exports.__esModule = true; exports.assignRef = assignRef; exports.mergeRefs = mergeRefs; /** * React.Ref uses the readonly type `React.RefObject` instead of * `React.MutableRefObject`, We pretty much always assume ref objects are * mutable (at least when we create them), so this type is a workaround so some * of the weird mechanics of using refs with TS. */ 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 \"".concat(value, "\" to ref \"").concat(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 */ function mergeRefs() { for (var _len = arguments.length, refs = new Array(_len), _key = 0; _key < _len; _key++) { refs[_key] = arguments[_key]; } if (refs.every(ref => ref == null)) { return null; } return node => { refs.forEach(ref => { assignRef(ref, node); }); }; } //# sourceMappingURL=mergeRefs.js.map