Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

useMergedRefs.js 1.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.mergeRefs = mergeRefs;
  4. exports.default = void 0;
  5. var _react = require("react");
  6. var toFnRef = function toFnRef(ref) {
  7. return !ref || typeof ref === 'function' ? ref : function (value) {
  8. ref.current = value;
  9. };
  10. };
  11. function mergeRefs(refA, refB) {
  12. var a = toFnRef(refA);
  13. var b = toFnRef(refB);
  14. return function (value) {
  15. if (a) a(value);
  16. if (b) b(value);
  17. };
  18. }
  19. /**
  20. * Create and returns a single callback ref composed from two other Refs.
  21. *
  22. * ```tsx
  23. * const Button = React.forwardRef((props, ref) => {
  24. * const [element, attachRef] = useCallbackRef<HTMLButtonElement>();
  25. * const mergedRef = useMergedRefs(ref, attachRef);
  26. *
  27. * return <button ref={mergedRef} {...props}/>
  28. * })
  29. * ```
  30. *
  31. * @param refA A Callback or mutable Ref
  32. * @param refB A Callback or mutable Ref
  33. */
  34. function useMergedRefs(refA, refB) {
  35. return (0, _react.useMemo)(function () {
  36. return mergeRefs(refA, refB);
  37. }, [refA, refB]);
  38. }
  39. var _default = useMergedRefs;
  40. exports.default = _default;