UNPKG

@wordpress/components

Version:
1 lines • 1.9 kB
{"version":3,"file":"hook.mjs","names":[],"sources":["../../../src/navigator/navigator-button/hook.ts"],"sourcesContent":["import { useCallback } from '@wordpress/element';\nimport { escapeAttribute } from '@wordpress/escape-html';\nimport type { WordPressComponentProps } from '../../context';\nimport { useContextSystem } from '../../context';\nimport Button from '../../button';\nimport { useNavigator } from '../use-navigator';\nimport type { NavigatorButtonProps } from '../types';\n\nconst cssSelectorForAttribute = ( attrName: string, attrValue: string ) =>\n\t`[${ attrName }=\"${ attrValue }\"]`;\n\nexport function useNavigatorButton(\n\tprops: WordPressComponentProps< NavigatorButtonProps, 'button' >\n) {\n\tconst {\n\t\tpath,\n\t\tonClick,\n\t\tas = Button,\n\t\tattributeName = 'id',\n\t\t...otherProps\n\t} = useContextSystem( props, 'Navigator.Button' );\n\n\tconst escapedPath = escapeAttribute( path );\n\n\tconst { goTo } = useNavigator();\n\tconst handleClick: React.MouseEventHandler< HTMLButtonElement > =\n\t\tuseCallback(\n\t\t\t( e ) => {\n\t\t\t\te.preventDefault();\n\t\t\t\tgoTo( escapedPath, {\n\t\t\t\t\tfocusTargetSelector: cssSelectorForAttribute(\n\t\t\t\t\t\tattributeName,\n\t\t\t\t\t\tescapedPath\n\t\t\t\t\t),\n\t\t\t\t} );\n\t\t\t\tonClick?.( e );\n\t\t\t},\n\t\t\t[ goTo, onClick, attributeName, escapedPath ]\n\t\t);\n\n\treturn {\n\t\tas,\n\t\tonClick: handleClick,\n\t\t...otherProps,\n\t\t[ attributeName ]: escapedPath,\n\t};\n}\n"],"mappings":";;;;;;AAQA,MAAM,2BAA4B,UAAkB,cACnD,IAAK,SAAU,IAAK,UAAW;AAEhC,SAAgB,mBACf,OACC;CACD,MAAM,EACL,MACA,SACA,KAAK,QACL,gBAAgB,MAChB,GAAG,eACA,iBAAkB,OAAO,kBAAmB;CAEhD,MAAM,cAAc,gBAAiB,IAAK;CAE1C,MAAM,EAAE,SAAS,aAAa;CAgB9B,OAAO;EACN;EACA,SAhBA,aACG,MAAO;GACR,EAAE,eAAe;GACjB,KAAM,aAAa,EAClB,qBAAqB,wBACpB,eACA,WACD,EACD,CAAE;GACF,UAAW,CAAE;EACd,GACA;GAAE;GAAM;GAAS;GAAe;EAAY,CAK1B;EACnB,GAAG;GACD,gBAAiB;CACpB;AACD"}