{"version":3,"file":"useButton-0838fc3e.cjs","sources":["../../node_modules/@mui/base/useButton/useButton.js"],"sourcesContent":["'use client';\n\nimport _extends from \"@babel/runtime/helpers/esm/extends\";\nimport * as React from 'react';\nimport { unstable_useForkRef as useForkRef, unstable_useIsFocusVisible as useIsFocusVisible } from '@mui/utils';\nimport { extractEventHandlers } from '../utils/extractEventHandlers';\n/**\n *\n * Demos:\n *\n * - [Button](https://mui.com/base-ui/react-button/#hook)\n *\n * API:\n *\n * - [useButton API](https://mui.com/base-ui/react-button/hooks-api/#use-button)\n */\nexport function useButton(parameters = {}) {\n  const {\n    disabled = false,\n    focusableWhenDisabled,\n    href,\n    rootRef: externalRef,\n    tabIndex,\n    to,\n    type\n  } = parameters;\n  const buttonRef = React.useRef();\n  const [active, setActive] = React.useState(false);\n  const {\n    isFocusVisibleRef,\n    onFocus: handleFocusVisible,\n    onBlur: handleBlurVisible,\n    ref: focusVisibleRef\n  } = useIsFocusVisible();\n  const [focusVisible, setFocusVisible] = React.useState(false);\n  if (disabled && !focusableWhenDisabled && focusVisible) {\n    setFocusVisible(false);\n  }\n  React.useEffect(() => {\n    isFocusVisibleRef.current = focusVisible;\n  }, [focusVisible, isFocusVisibleRef]);\n  const [hostElementName, setHostElementName] = React.useState('');\n  const createHandleMouseLeave = otherHandlers => event => {\n    var _otherHandlers$onMous;\n    if (focusVisible) {\n      event.preventDefault();\n    }\n    (_otherHandlers$onMous = otherHandlers.onMouseLeave) == null || _otherHandlers$onMous.call(otherHandlers, event);\n  };\n  const createHandleBlur = otherHandlers => event => {\n    var _otherHandlers$onBlur;\n    handleBlurVisible(event);\n    if (isFocusVisibleRef.current === false) {\n      setFocusVisible(false);\n    }\n    (_otherHandlers$onBlur = otherHandlers.onBlur) == null || _otherHandlers$onBlur.call(otherHandlers, event);\n  };\n  const createHandleFocus = otherHandlers => event => {\n    var _otherHandlers$onFocu2;\n    // Fix for https://github.com/facebook/react/issues/7769\n    if (!buttonRef.current) {\n      buttonRef.current = event.currentTarget;\n    }\n    handleFocusVisible(event);\n    if (isFocusVisibleRef.current === true) {\n      var _otherHandlers$onFocu;\n      setFocusVisible(true);\n      (_otherHandlers$onFocu = otherHandlers.onFocusVisible) == null || _otherHandlers$onFocu.call(otherHandlers, event);\n    }\n    (_otherHandlers$onFocu2 = otherHandlers.onFocus) == null || _otherHandlers$onFocu2.call(otherHandlers, event);\n  };\n  const isNativeButton = () => {\n    const button = buttonRef.current;\n    return hostElementName === 'BUTTON' || hostElementName === 'INPUT' && ['button', 'submit', 'reset'].includes(button == null ? void 0 : button.type) || hostElementName === 'A' && (button == null ? void 0 : button.href);\n  };\n  const createHandleClick = otherHandlers => event => {\n    if (!disabled) {\n      var _otherHandlers$onClic;\n      (_otherHandlers$onClic = otherHandlers.onClick) == null || _otherHandlers$onClic.call(otherHandlers, event);\n    }\n  };\n  const createHandleMouseDown = otherHandlers => event => {\n    var _otherHandlers$onMous2;\n    if (!disabled) {\n      setActive(true);\n      document.addEventListener('mouseup', () => {\n        setActive(false);\n      }, {\n        once: true\n      });\n    }\n    (_otherHandlers$onMous2 = otherHandlers.onMouseDown) == null || _otherHandlers$onMous2.call(otherHandlers, event);\n  };\n  const createHandleKeyDown = otherHandlers => event => {\n    var _otherHandlers$onKeyD;\n    (_otherHandlers$onKeyD = otherHandlers.onKeyDown) == null || _otherHandlers$onKeyD.call(otherHandlers, event);\n    if (event.defaultMuiPrevented) {\n      return;\n    }\n    if (event.target === event.currentTarget && !isNativeButton() && event.key === ' ') {\n      event.preventDefault();\n    }\n    if (event.target === event.currentTarget && event.key === ' ' && !disabled) {\n      setActive(true);\n    }\n\n    // Keyboard accessibility for non interactive elements\n    if (event.target === event.currentTarget && !isNativeButton() && event.key === 'Enter' && !disabled) {\n      var _otherHandlers$onClic2;\n      (_otherHandlers$onClic2 = otherHandlers.onClick) == null || _otherHandlers$onClic2.call(otherHandlers, event);\n      event.preventDefault();\n    }\n  };\n  const createHandleKeyUp = otherHandlers => event => {\n    var _otherHandlers$onKeyU;\n    // calling preventDefault in keyUp on a <button> will not dispatch a click event if Space is pressed\n    // https://codesandbox.io/p/sandbox/button-keyup-preventdefault-dn7f0\n\n    if (event.target === event.currentTarget) {\n      setActive(false);\n    }\n    (_otherHandlers$onKeyU = otherHandlers.onKeyUp) == null || _otherHandlers$onKeyU.call(otherHandlers, event);\n\n    // Keyboard accessibility for non interactive elements\n    if (event.target === event.currentTarget && !isNativeButton() && !disabled && event.key === ' ' && !event.defaultMuiPrevented) {\n      var _otherHandlers$onClic3;\n      (_otherHandlers$onClic3 = otherHandlers.onClick) == null || _otherHandlers$onClic3.call(otherHandlers, event);\n    }\n  };\n  const updateHostElementName = React.useCallback(instance => {\n    var _instance$tagName;\n    setHostElementName((_instance$tagName = instance == null ? void 0 : instance.tagName) != null ? _instance$tagName : '');\n  }, []);\n  const handleRef = useForkRef(updateHostElementName, externalRef, focusVisibleRef, buttonRef);\n  const buttonProps = {};\n  if (tabIndex !== undefined) {\n    buttonProps.tabIndex = tabIndex;\n  }\n  if (hostElementName === 'BUTTON') {\n    buttonProps.type = type != null ? type : 'button';\n    if (focusableWhenDisabled) {\n      buttonProps['aria-disabled'] = disabled;\n    } else {\n      buttonProps.disabled = disabled;\n    }\n  } else if (hostElementName !== '') {\n    if (!href && !to) {\n      buttonProps.role = 'button';\n      buttonProps.tabIndex = tabIndex != null ? tabIndex : 0;\n    }\n    if (disabled) {\n      buttonProps['aria-disabled'] = disabled;\n      buttonProps.tabIndex = focusableWhenDisabled ? tabIndex != null ? tabIndex : 0 : -1;\n    }\n  }\n  const getRootProps = (externalProps = {}) => {\n    const externalEventHandlers = _extends({}, extractEventHandlers(parameters), extractEventHandlers(externalProps));\n    const props = _extends({\n      type\n    }, externalEventHandlers, buttonProps, externalProps, {\n      onBlur: createHandleBlur(externalEventHandlers),\n      onClick: createHandleClick(externalEventHandlers),\n      onFocus: createHandleFocus(externalEventHandlers),\n      onKeyDown: createHandleKeyDown(externalEventHandlers),\n      onKeyUp: createHandleKeyUp(externalEventHandlers),\n      onMouseDown: createHandleMouseDown(externalEventHandlers),\n      onMouseLeave: createHandleMouseLeave(externalEventHandlers),\n      ref: handleRef\n    });\n\n    // onFocusVisible can be present on the props or parameters,\n    // but it's not a valid React event handler so it must not be forwarded to the inner component.\n    // If present, it will be handled by the focus handler.\n    delete props.onFocusVisible;\n    return props;\n  };\n  return {\n    getRootProps,\n    focusVisible,\n    setFocusVisible,\n    active,\n    rootRef: handleRef\n  };\n}"],"names":["useButton","parameters","disabled","focusableWhenDisabled","href","externalRef","tabIndex","to","type","buttonRef","React","active","setActive","isFocusVisibleRef","handleFocusVisible","handleBlurVisible","focusVisibleRef","useIsFocusVisible","focusVisible","setFocusVisible","hostElementName","setHostElementName","createHandleMouseLeave","otherHandlers","event","_otherHandlers$onMous","createHandleBlur","_otherHandlers$onBlur","createHandleFocus","_otherHandlers$onFocu2","_otherHandlers$onFocu","isNativeButton","button","createHandleClick","_otherHandlers$onClic","createHandleMouseDown","_otherHandlers$onMous2","createHandleKeyDown","_otherHandlers$onKeyD","_otherHandlers$onClic2","createHandleKeyUp","_otherHandlers$onKeyU","_otherHandlers$onClic3","updateHostElementName","instance","_instance$tagName","handleRef","useForkRef","buttonProps","externalProps","externalEventHandlers","_extends","extractEventHandlers","props"],"mappings":"2bAgBO,SAASA,EAAUC,EAAa,GAAI,CACzC,KAAM,CACJ,SAAAC,EAAW,GACX,sBAAAC,EACA,KAAAC,EACA,QAASC,EACT,SAAAC,EACA,GAAAC,EACA,KAAAC,CACD,EAAGP,EACEQ,EAAYC,EAAM,SAClB,CAACC,EAAQC,CAAS,EAAIF,EAAM,SAAS,EAAK,EAC1C,CACJ,kBAAAG,EACA,QAASC,EACT,OAAQC,EACR,IAAKC,CACN,EAAGC,EAAiB,kBAAA,EACf,CAACC,EAAcC,CAAe,EAAIT,EAAM,SAAS,EAAK,EACxDR,GAAY,CAACC,GAAyBe,GACxCC,EAAgB,EAAK,EAEvBT,EAAM,UAAU,IAAM,CACpBG,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