UNPKG

@itwin/itwinui-react

Version:

A react component library for iTwinUI

37 lines (36 loc) 1.27 kB
import * as React from 'react'; import cx from 'classnames'; import { Box } from './Box.js'; import { useIsClient } from '../hooks/useIsClient.js'; export const ButtonBase = React.forwardRef((props, forwardedRef) => { let { as: asProp = 'button', disabled: disabledProp, htmlDisabled, type: typeProp = 'button' === asProp ? 'button' : void 0, ...rest } = props; let isClient = useIsClient(); let ariaDisabled = disabledProp && !htmlDisabled && isClient && 'button' === asProp; let handleIfEnabled = (handler) => (e) => { if (disabledProp) return; handler?.(e); }; let type = 'button' === asProp && disabledProp ? 'button' : typeProp; return React.createElement(Box, { as: asProp, type: type, ref: forwardedRef, 'aria-disabled': ariaDisabled ? 'true' : void 0, 'data-iui-disabled': disabledProp ? 'true' : void 0, disabled: htmlDisabled ?? (!isClient && disabledProp) ? true : void 0, ...rest, className: cx('iui-button-base', props.className), onClick: handleIfEnabled(props.onClick), onPointerDown: handleIfEnabled(props.onPointerDown), onPointerUp: handleIfEnabled(props.onPointerUp), }); }); if ('development' === process.env.NODE_ENV) ButtonBase.displayName = 'ButtonBase';