UNPKG

@wordpress/compose

Version:
8 lines (7 loc) 4.82 kB
{ "version": 3, "sources": ["../../../src/higher-order/with-global-events/index.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { Component, forwardRef } from '@wordpress/element';\nimport deprecated from '@wordpress/deprecated';\n\n/**\n * Internal dependencies\n */\nimport { createHigherOrderComponent } from '../../utils/create-higher-order-component';\nimport Listener from './listener';\n\n/**\n * Listener instance responsible for managing document event handling.\n */\nconst listener = new Listener();\n\n/**\n * Higher-order component creator which, given an object of DOM event types and\n * values corresponding to a callback function name on the component, will\n * create or update a window event handler to invoke the callback when an event\n * occurs. On behalf of the consuming developer, the higher-order component\n * manages unbinding when the component unmounts, and binding at most a single\n * event handler for the entire application.\n *\n * @deprecated\n *\n * @param {Record<keyof GlobalEventHandlersEventMap, string>} eventTypesToHandlers Object with keys of DOM\n * event type, the value a\n * name of the function on\n * the original component's\n * instance which handles\n * the event.\n *\n * @return {any} Higher-order component.\n */\nexport default function withGlobalEvents( eventTypesToHandlers ) {\n\tdeprecated( 'wp.compose.withGlobalEvents', {\n\t\tsince: '5.7',\n\t\talternative: 'useEffect',\n\t} );\n\n\t// @ts-ignore We don't need to fix the type-related issues because this is deprecated.\n\treturn createHigherOrderComponent( ( WrappedComponent ) => {\n\t\tclass Wrapper extends Component {\n\t\t\tconstructor( /** @type {any} */ props ) {\n\t\t\t\tsuper( props );\n\n\t\t\t\tthis.handleEvent = this.handleEvent.bind( this );\n\t\t\t\tthis.handleRef = this.handleRef.bind( this );\n\t\t\t}\n\n\t\t\tcomponentDidMount() {\n\t\t\t\tObject.keys( eventTypesToHandlers ).forEach( ( eventType ) => {\n\t\t\t\t\tlistener.add( eventType, this );\n\t\t\t\t} );\n\t\t\t}\n\n\t\t\tcomponentWillUnmount() {\n\t\t\t\tObject.keys( eventTypesToHandlers ).forEach( ( eventType ) => {\n\t\t\t\t\tlistener.remove( eventType, this );\n\t\t\t\t} );\n\t\t\t}\n\n\t\t\thandleEvent( /** @type {any} */ event ) {\n\t\t\t\tconst handler =\n\t\t\t\t\teventTypesToHandlers[\n\t\t\t\t\t\t/** @type {keyof GlobalEventHandlersEventMap} */ (\n\t\t\t\t\t\t\tevent.type\n\t\t\t\t\t\t)\n\t\t\t\t\t];\n\t\t\t\tif ( typeof this.wrappedRef[ handler ] === 'function' ) {\n\t\t\t\t\tthis.wrappedRef[ handler ]( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\thandleRef( /** @type {any} */ el ) {\n\t\t\t\tthis.wrappedRef = el;\n\t\t\t\t// Any component using `withGlobalEvents` that is not setting a `ref`\n\t\t\t\t// will cause `this.props.forwardedRef` to be `null`, so we need this\n\t\t\t\t// check.\n\t\t\t\tif ( this.props.forwardedRef ) {\n\t\t\t\t\tthis.props.forwardedRef( el );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\trender() {\n\t\t\t\treturn (\n\t\t\t\t\t<WrappedComponent\n\t\t\t\t\t\t{ ...this.props.ownProps }\n\t\t\t\t\t\tref={ this.handleRef }\n\t\t\t\t\t/>\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\treturn forwardRef( ( props, ref ) => {\n\t\t\treturn <Wrapper ownProps={ props } forwardedRef={ ref } />;\n\t\t} );\n\t}, 'withGlobalEvents' );\n}\n"], "mappings": ";AAGA,SAAS,WAAW,kBAAkB;AACtC,OAAO,gBAAgB;AAKvB,SAAS,kCAAkC;AAC3C,OAAO,cAAc;AA8EhB;AAzEL,IAAM,WAAW,IAAI,SAAS;AAqBf,SAAR,iBAAmC,sBAAuB;AAChE,aAAY,+BAA+B;AAAA,IAC1C,OAAO;AAAA,IACP,aAAa;AAAA,EACd,CAAE;AAGF,SAAO,2BAA4B,CAAE,qBAAsB;AAAA,IAC1D,MAAM,gBAAgB,UAAU;AAAA,MAC/B,YAAgC,OAAQ;AACvC,cAAO,KAAM;AAEb,aAAK,cAAc,KAAK,YAAY,KAAM,IAAK;AAC/C,aAAK,YAAY,KAAK,UAAU,KAAM,IAA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