@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
8 lines (7 loc) • 2.47 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/components/post-title/use-post-title-focus.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useEffect, useImperativeHandle, useRef } from '@wordpress/element';\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { store as editorStore } from '../../store';\n\n/**\n * Custom hook that manages the focus behavior of the post title input field.\n *\n * @param {Element} forwardedRef - The forwarded ref for the input field.\n *\n * @return {Object} - The ref object.\n */\nexport default function usePostTitleFocus( forwardedRef ) {\n\tconst ref = useRef();\n\n\tconst { isCleanNewPost } = useSelect( ( select ) => {\n\t\tconst { isCleanNewPost: _isCleanNewPost } = select( editorStore );\n\n\t\treturn {\n\t\t\tisCleanNewPost: _isCleanNewPost(),\n\t\t};\n\t}, [] );\n\n\tuseImperativeHandle( forwardedRef, () => ( {\n\t\tfocus: () => {\n\t\t\tref?.current?.focus();\n\t\t},\n\t} ) );\n\n\tuseEffect( () => {\n\t\tif ( ! ref.current ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst { defaultView } = ref.current.ownerDocument;\n\t\tconst { name, parent } = defaultView;\n\t\tconst ownerDocument =\n\t\t\tname === 'editor-canvas' ? parent.document : defaultView.document;\n\t\tconst { activeElement, body } = ownerDocument;\n\n\t\t// Only autofocus the title when the post is entirely empty. This should\n\t\t// only happen for a new post, which means we focus the title on new\n\t\t// post so the author can start typing right away, without needing to\n\t\t// click anything.\n\t\tif ( isCleanNewPost && ( ! activeElement || body === activeElement ) ) {\n\t\t\tref.current.focus();\n\t\t}\n\t}, [ isCleanNewPost ] );\n\n\treturn { ref };\n}\n"],
"mappings": ";AAGA,SAAS,WAAW,qBAAqB,cAAc;AACvD,SAAS,iBAAiB;AAK1B,SAAS,SAAS,mBAAmB;AAStB,SAAR,kBAAoC,cAAe;AACzD,QAAM,MAAM,OAAO;AAEnB,QAAM,EAAE,eAAe,IAAI,UAAW,CAAE,WAAY;AACnD,UAAM,EAAE,gBAAgB,gBAAgB,IAAI,OAAQ,WAAY;AAEhE,WAAO;AAAA,MACN,gBAAgB,gBAAgB;AAAA,IACjC;AAAA,EACD,GAAG,CAAC,CAAE;AAEN,sBAAqB,cAAc,OAAQ;AAAA,IAC1C,OAAO,MAAM;AACZ,WAAK,SAAS,MAAM;AAAA,IACrB;AAAA,EACD,EAAI;AAEJ,YAAW,MAAM;AAChB,QAAK,CAAE,IAAI,SAAU;AACpB;AAAA,IACD;AAEA,UAAM,EAAE,YAAY,IAAI,IAAI,QAAQ;AACpC,UAAM,EAAE,MAAM,OAAO,IAAI;AACzB,UAAM,gBACL,SAAS,kBAAkB,OAAO,WAAW,YAAY;AAC1D,UAAM,EAAE,eAAe,KAAK,IAAI;AAMhC,QAAK,mBAAoB,CAAE,iBAAiB,SAAS,gBAAkB;AACtE,UAAI,QAAQ,MAAM;AAAA,IACnB;AAAA,EACD,GAAG,CAAE,cAAe,CAAE;AAEtB,SAAO,EAAE,IAAI;AACd;",
"names": []
}