Sha256: 38b69bbd12e04ba1e489ad4d578738dd946cea1baf577d660bdf984dd55427da

Contents?: true

Size: 1.57 KB

Versions: 61

Compression:

Stored size: 1.57 KB

Contents

import { options } from 'preact';
import { assign } from './util';

let oldDiffHook = options._diff;
options._diff = vnode => {
	if (vnode.type && vnode.type._forwarded && vnode.ref) {
		vnode.props.ref = vnode.ref;
		vnode.ref = null;
	}
	if (oldDiffHook) oldDiffHook(vnode);
};

export const REACT_FORWARD_SYMBOL =
	(typeof Symbol != 'undefined' &&
		Symbol.for &&
		Symbol.for('react.forward_ref')) ||
	0xf47;

/**
 * Pass ref down to a child. This is mainly used in libraries with HOCs that
 * wrap components. Using `forwardRef` there is an easy way to get a reference
 * of the wrapped component instead of one of the wrapper itself.
 * @param {import('./index').ForwardFn} fn
 * @returns {import('./internal').FunctionComponent}
 */
export function forwardRef(fn) {
	// We always have ref in props.ref, except for
	// mobx-react. It will call this function directly
	// and always pass ref as the second argument.
	function Forwarded(props, ref) {
		let clone = assign({}, props);
		delete clone.ref;
		ref = props.ref || ref;
		return fn(
			clone,
			!ref || (typeof ref === 'object' && !('current' in ref)) ? null : ref
		);
	}

	// mobx-react checks for this being present
	Forwarded.$$typeof = REACT_FORWARD_SYMBOL;
	// mobx-react heavily relies on implementation details.
	// It expects an object here with a `render` property,
	// and prototype.render will fail. Without this
	// mobx-react throws.
	Forwarded.render = Forwarded;

	Forwarded.prototype.isReactComponent = Forwarded._forwarded = true;
	Forwarded.displayName = 'ForwardRef(' + (fn.displayName || fn.name) + ')';
	return Forwarded;
}

Version data entries

61 entries across 61 versions & 1 rubygems

Version Path
isomorfeus-preact-10.6.62 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.6.61 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.6.60 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.6.59 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.6.58 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.6.57 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.6.56 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.6.55 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.6.54 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.6.53 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.6.52 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.6.51 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.6.50 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.6.49 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.6.48 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.6.47 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.6.46 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.6.45 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.6.44 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.6.43 node_modules/preact/compat/src/forwardRef.js