UNPKG

@wordpress/components

Version:
1 lines • 3.02 kB
{"version":3,"file":"index.mjs","names":[],"sources":["../../../src/higher-order/with-focus-return/index.tsx"],"sourcesContent":["import { Component } from '@wordpress/element';\nimport { createHigherOrderComponent, useFocusReturn } from '@wordpress/compose';\nimport deprecated from '@wordpress/deprecated';\n\n/**\n * Returns true if the given object is component-like. An object is component-\n * like if it is an instance of wp.element.Component, or is a function.\n *\n * @param object Object to test.\n *\n * @return Whether object is component-like.\n */\nfunction isComponentLike( object: any ): object is React.ComponentType {\n\treturn object instanceof Component || typeof object === 'function';\n}\n\ntype Props = {\n\tonFocusReturn?: () => void;\n};\n\n/**\n * Higher Order Component used to be used to wrap disposable elements like\n * sidebars, modals, dropdowns. When mounting the wrapped component, we track a\n * reference to the current active element so we know where to restore focus\n * when the component is unmounted.\n *\n * @param options The component to be enhanced with\n * focus return behavior, or an object\n * describing the component and the\n * focus return characteristics.\n *\n * @return Higher Order Component with the focus restoration behaviour.\n */\nexport default createHigherOrderComponent(\n\t// @ts-expect-error TODO: Reconcile with intended `createHigherOrderComponent` types\n\t( options: React.ComponentType | Record< string, unknown > ) => {\n\t\tconst HoC =\n\t\t\t( { onFocusReturn }: Props = {} ) =>\n\t\t\t( WrappedComponent: React.ComponentType ) => {\n\t\t\t\tconst WithFocusReturn = (\n\t\t\t\t\tprops: Record< string, unknown >\n\t\t\t\t) => {\n\t\t\t\t\tconst ref = useFocusReturn( onFocusReturn );\n\t\t\t\t\treturn (\n\t\t\t\t\t\t<div ref={ ref }>\n\t\t\t\t\t\t\t<WrappedComponent { ...props } />\n\t\t\t\t\t\t</div>\n\t\t\t\t\t);\n\t\t\t\t};\n\n\t\t\t\treturn WithFocusReturn;\n\t\t\t};\n\n\t\tif ( isComponentLike( options ) ) {\n\t\t\tconst WrappedComponent = options;\n\t\t\treturn HoC()( WrappedComponent );\n\t\t}\n\n\t\treturn HoC( options );\n\t},\n\t'withFocusReturn'\n);\n\nexport const Provider = ( { children }: { children: React.ReactNode } ) => {\n\tdeprecated( 'wp.components.FocusReturnProvider component', {\n\t\tsince: '5.7',\n\t\thint: 'This provider is not used anymore. You can just remove it from your codebase',\n\t} );\n\n\treturn children;\n};\n"],"mappings":";;;;;;;;;;;;;AAYA,SAAS,gBAAiB,QAA6C;CACtE,OAAO,kBAAkB,aAAa,OAAO,WAAW;AACzD;;;;;;;;;;;;;;AAmBA,IAAA,4BAAe,4BAEZ,YAA8D;CAC/D,MAAM,OACH,EAAE,kBAAyB,CAAC,OAC5B,qBAA2C;EAC5C,MAAM,mBACL,UACI;GACJ,MAAM,MAAM,eAAgB,aAAc;GAC1C,OACC,oBAAC,OAAD;IAAW;IACV,UAAA,oBAAC,kBAAD,EAAkB,GAAK,MAAS,CAAA;GAC5B,CAAA;EAEP;EAEA,OAAO;CACR;CAED,IAAK,gBAAiB,OAAQ,GAAI;EACjC,MAAM,mBAAmB;EACzB,OAAO,IAAI,CAAC,CAAE,gBAAiB;CAChC;CAEA,OAAO,IAAK,OAAQ;AACrB,GACA,iBACD;AAEA,MAAa,YAAa,EAAE,eAA+C;CAC1E,WAAY,+CAA+C;EAC1D,OAAO;EACP,MAAM;CACP,CAAE;CAEF,OAAO;AACR"}