UNPKG

@wordpress/components

Version:
1 lines • 4.84 kB
{"version":3,"file":"index.cjs","names":["useContext","ToolbarContext","ToolbarButtonContainer","Button","ToolbarItem","forwardRef"],"sources":["../../../src/toolbar/toolbar-button/index.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport type { ForwardedRef, MouseEvent as ReactMouseEvent } from 'react';\nimport { useContext, forwardRef } from '@wordpress/element';\nimport Button from '../../button';\nimport ToolbarItem from '../toolbar-item';\nimport ToolbarContext from '../toolbar-context';\nimport ToolbarButtonContainer from './toolbar-button-container';\nimport type { ToolbarButtonOverriddenProps, ToolbarButtonProps } from './types';\nimport type { WordPressComponentProps } from '../../context';\n\nfunction useDeprecatedProps( {\n\tisDisabled,\n\t...otherProps\n}: React.ComponentProps< typeof ToolbarButton > ) {\n\treturn {\n\t\tdisabled: isDisabled,\n\t\t...otherProps,\n\t};\n}\n\nfunction UnforwardedToolbarButton(\n\tprops: Omit<\n\t\tWordPressComponentProps< ToolbarButtonProps, typeof Button, false >,\n\t\t'accessibleWhenDisabled' // By default, ToolbarButton will be focusable when disabled.\n\t> &\n\t\tToolbarButtonOverriddenProps,\n\tref: ForwardedRef< any >\n) {\n\tconst {\n\t\tchildren,\n\t\tclassName,\n\t\tcontainerClassName,\n\t\textraProps,\n\t\tisActive,\n\t\ttitle,\n\t\t...restProps\n\t} = useDeprecatedProps( props );\n\tconst accessibleToolbarState = useContext( ToolbarContext );\n\n\tif ( ! accessibleToolbarState ) {\n\t\treturn (\n\t\t\t<ToolbarButtonContainer className={ containerClassName }>\n\t\t\t\t<Button\n\t\t\t\t\tref={ ref }\n\t\t\t\t\ticon={ restProps.icon }\n\t\t\t\t\tsize=\"compact\"\n\t\t\t\t\tlabel={ title }\n\t\t\t\t\tshortcut={ restProps.shortcut }\n\t\t\t\t\tdata-subscript={ restProps.subscript }\n\t\t\t\t\tonClick={ (\n\t\t\t\t\t\tevent: ReactMouseEvent<\n\t\t\t\t\t\t\tHTMLButtonElement & HTMLAnchorElement,\n\t\t\t\t\t\t\tMouseEvent\n\t\t\t\t\t\t>\n\t\t\t\t\t) => {\n\t\t\t\t\t\tevent.stopPropagation();\n\t\t\t\t\t\t// TODO: Possible bug; maybe use onClick instead of restProps.onClick.\n\t\t\t\t\t\tif ( restProps.onClick ) {\n\t\t\t\t\t\t\trestProps.onClick( event );\n\t\t\t\t\t\t}\n\t\t\t\t\t} }\n\t\t\t\t\tclassName={ clsx(\n\t\t\t\t\t\t'components-toolbar__control',\n\t\t\t\t\t\tclassName\n\t\t\t\t\t) }\n\t\t\t\t\tisPressed={ isActive }\n\t\t\t\t\taccessibleWhenDisabled\n\t\t\t\t\tdata-toolbar-item\n\t\t\t\t\t{ ...extraProps }\n\t\t\t\t\t{ ...restProps }\n\t\t\t\t>\n\t\t\t\t\t{ children }\n\t\t\t\t</Button>\n\t\t\t</ToolbarButtonContainer>\n\t\t);\n\t}\n\n\t// ToobarItem will pass all props to the render prop child, which will pass\n\t// all props to Button. This means that ToolbarButton has the same API as\n\t// Button.\n\treturn (\n\t\t<ToolbarItem\n\t\t\tclassName={ clsx( 'components-toolbar-button', className ) }\n\t\t\t{ ...extraProps }\n\t\t\t{ ...restProps }\n\t\t\tref={ ref }\n\t\t>\n\t\t\t{ ( toolbarItemProps ) => (\n\t\t\t\t<Button\n\t\t\t\t\tsize=\"compact\"\n\t\t\t\t\tlabel={ title }\n\t\t\t\t\tisPressed={ isActive }\n\t\t\t\t\t{ ...toolbarItemProps }\n\t\t\t\t>\n\t\t\t\t\t{ children }\n\t\t\t\t</Button>\n\t\t\t) }\n\t\t</ToolbarItem>\n\t);\n}\n\n/**\n * ToolbarButton can be used to add actions to a toolbar, usually inside a Toolbar\n * or ToolbarGroup when used to create general interfaces.\n *\n * ```jsx\n * import { Toolbar, ToolbarButton } from '@wordpress/components';\n * import { pencil } from '@wordpress/icons';\n *\n * function MyToolbar() {\n * return (\n *\t\t<Toolbar label=\"Options\">\n *\t\t\t<ToolbarButton\n *\t\t\t\ticon={ pencil }\n *\t\t\t\tlabel=\"Edit\"\n *\t\t\t\tonClick={ () => alert( 'Editing' ) }\n *\t\t\t/>\n *\t\t</Toolbar>\n * );\n * }\n * ```\n */\nexport const ToolbarButton = forwardRef( UnforwardedToolbarButton );\nToolbarButton.displayName = 'ToolbarButton';\nexport default ToolbarButton;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAUA,SAAS,mBAAoB,EAC5B,YACA,GAAG,cAC8C;CACjD,OAAO;EACN,UAAU;EACV,GAAG;CACJ;AACD;AAEA,SAAS,yBACR,OAKA,KACC;CACD,MAAM,EACL,UACA,WACA,oBACA,YACA,UACA,OACA,GAAG,cACA,mBAAoB,KAAM;CAG9B,IAAK,EAAA,GAF0BA,mBAAAA,WAAAA,CAAYC,4BAAAA,OAEf,GAC3B,OACC,iBAAA,GAAA,kBAAA,IAAA,CAACC,+BAAAA,SAAD;EAAwB,WAAY;EACnC,UAAA,iBAAA,GAAA,kBAAA,IAAA,CAACC,uBAAAA,SAAD;GACO;GACN,MAAO,UAAU;GACjB,MAAK;GACL,OAAQ;GACR,UAAW,UAAU;GACrB,kBAAiB,UAAU;GAC3B,UACC,UAII;IACJ,MAAM,gBAAgB;IAEtB,IAAK,UAAU,SACd,UAAU,QAAS,KAAM;GAE3B;GACA,YAAA,GAAY,KAAA,QAAA,CACX,+BACA,SACD;GACA,WAAY;GACZ,wBAAA;GACA,qBAAA;GACA,GAAK;GACL,GAAK;GAEH;EACK,CAAA;CACe,CAAA;CAO1B,OACC,iBAAA,GAAA,kBAAA,IAAA,CAACC,0BAAAA,SAAD;EACC,YAAA,GAAY,KAAA,QAAA,CAAM,6BAA6B,SAAU;EACzD,GAAK;EACL,GAAK;EACC;EAEF,WAAA,qBACH,iBAAA,GAAA,kBAAA,IAAA,CAACD,uBAAAA,SAAD;GACC,MAAK;GACL,OAAQ;GACR,WAAY;GACZ,GAAK;GAEH;EACK,CAAA;CAEG,CAAA;AAEf;;;;;;;;;;;;;;;;;;;;;;AAuBA,MAAa,iBAAA,GAAgBE,mBAAAA,WAAAA,CAAY,wBAAyB;AAClE,cAAc,cAAc"}