@fluentui/react-northstar
Version:
A themable React component library.
213 lines (211 loc) • 7.62 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _invoke from "lodash/invoke";
import { buttonBehavior } from '@fluentui/accessibility';
import { compose, getElementType, useAccessibility, useFluentContext, useStyles, useTelemetry, useUnhandledProps } from '@fluentui/react-bindings';
import * as customPropTypes from '@fluentui/react-proptypes';
import * as PropTypes from 'prop-types';
import * as React from 'react';
import { childrenExist, createShorthandFactory, commonPropTypes, rtlTextContainer, createShorthand } from '../../utils';
import { Box } from '../Box/Box';
import { Loader } from '../Loader/Loader';
import { ButtonGroup } from './ButtonGroup';
import { ButtonContent } from './ButtonContent';
export var buttonClassName = 'ui-button';
/**
* A Button enables users to take an action, such as submitting a form, opening a dialog, etc.
*
* @accessibility
* Implements [ARIA Button](https://www.w3.org/TR/wai-aria-practices-1.1/#button) design pattern.
*/
export var Button = /*#__PURE__*/function () {
var Button = compose(function (props, ref, composeOptions) {
var context = useFluentContext();
var _useTelemetry = useTelemetry(composeOptions.displayName, context.telemetry),
setStart = _useTelemetry.setStart,
setEnd = _useTelemetry.setEnd;
setStart();
var accessibility = props.accessibility,
active = props.active,
as = props.as,
children = props.children,
content = props.content,
icon = props.icon,
loader = props.loader,
disabled = props.disabled,
disabledFocusable = props.disabledFocusable,
flat = props.flat,
iconPosition = props.iconPosition,
loading = props.loading,
text = props.text,
primary = props.primary,
inverted = props.inverted,
size = props.size,
iconOnly = props.iconOnly,
fluid = props.fluid,
circular = props.circular,
className = props.className,
styles = props.styles,
tinted = props.tinted,
variables = props.variables,
design = props.design;
var hasChildren = childrenExist(children);
var getA11yProps = useAccessibility(accessibility, {
debugName: composeOptions.displayName,
mapPropsToBehavior: function mapPropsToBehavior() {
return {
as: as,
active: active,
disabled: disabled,
disabledFocusable: disabledFocusable
};
},
actionHandlers: {
performClick: function performClick(event) {
event.preventDefault();
handleClick(event);
}
},
rtl: context.rtl
});
var _useStyles = useStyles(composeOptions.displayName, {
className: composeOptions.className,
mapPropsToStyles: function mapPropsToStyles() {
return {
text: text,
primary: primary,
disabled: disabled,
tinted: tinted,
disabledFocusable: disabledFocusable,
flat: flat,
circular: circular,
size: size,
loading: loading,
inverted: inverted,
iconOnly: iconOnly,
iconPosition: iconPosition,
fluid: fluid,
hasContent: !!content
};
},
mapPropsToInlineStyles: function mapPropsToInlineStyles() {
return {
className: className,
design: design,
styles: styles,
variables: variables
};
},
rtl: context.rtl,
composeOptions: composeOptions,
unstable_props: props
}),
classes = _useStyles.classes,
resolvedStyles = _useStyles.styles;
var slotProps = composeOptions.resolveSlotProps(props);
var unhandledProps = useUnhandledProps(composeOptions.handledProps, props);
var ElementType = getElementType(props);
var renderIcon = function renderIcon() {
return createShorthand(composeOptions.slots.icon, icon, {
defaultProps: function defaultProps() {
return getA11yProps('icon', Object.assign({
styles: resolvedStyles.icon
}, slotProps.icon));
}
});
};
var renderLoader = function renderLoader() {
return createShorthand(composeOptions.slots.loader, loader || {}, {
defaultProps: function defaultProps() {
return getA11yProps('loader', Object.assign({
styles: resolvedStyles.loader
}, slotProps.loader));
}
});
};
var renderContent = function renderContent() {
return createShorthand(composeOptions.slots.content, content, {
defaultProps: function defaultProps() {
return getA11yProps('content', slotProps.content);
}
});
};
var handleClick = function handleClick(e) {
if (disabled || disabledFocusable) {
e.preventDefault();
return;
}
_invoke(props, 'onClick', e, props);
};
var handleFocus = function handleFocus(e) {
_invoke(props, 'onFocus', e, props);
};
var result = /*#__PURE__*/React.createElement(ElementType, _extends({}, rtlTextContainer.getAttributes({
forElements: [children]
}), getA11yProps('root', Object.assign({
onClick: handleClick,
className: classes.root,
onFocus: handleFocus,
ref: ref
}, unhandledProps))), hasChildren ? children : /*#__PURE__*/React.createElement(React.Fragment, null, loading && renderLoader(), iconPosition !== 'after' && renderIcon(), renderContent(), iconPosition === 'after' && renderIcon()));
setEnd();
return result;
}, {
className: buttonClassName,
displayName: 'Button',
slots: {
content: ButtonContent,
icon: Box,
loader: Loader
},
slotProps: function slotProps(props) {
return {
content: {
size: props.size
},
loader: {
role: undefined,
secondary: props.primary
}
};
},
shorthandConfig: {
mappedProp: 'content'
},
handledProps: ['accessibility', 'as', 'children', 'circular', 'className', 'content', 'design', 'disabled', 'tinted', 'disabledFocusable', 'flat', 'fluid', 'icon', 'iconOnly', 'iconPosition', 'inverted', 'loader', 'loading', 'onClick', 'onFocus', 'primary', 'text', 'secondary', 'size', 'styles', 'variables']
});
Button.defaultProps = {
as: 'button',
accessibility: buttonBehavior,
size: 'medium'
};
Button.propTypes = Object.assign({}, commonPropTypes.createCommon({
content: 'shorthand'
}), {
circular: PropTypes.bool,
disabled: PropTypes.bool,
disabledFocusable: PropTypes.bool,
flat: PropTypes.bool,
fluid: PropTypes.bool,
icon: customPropTypes.shorthandAllowingChildren,
iconOnly: PropTypes.bool,
iconPosition: PropTypes.oneOf(['before', 'after']),
inverted: PropTypes.bool,
loader: customPropTypes.itemShorthandWithoutJSX,
loading: PropTypes.bool,
onClick: PropTypes.func,
onFocus: PropTypes.func,
tinted: customPropTypes.every([customPropTypes.disallow(['secondary']), PropTypes.bool]),
primary: customPropTypes.every([customPropTypes.disallow(['secondary']), PropTypes.bool]),
text: PropTypes.bool,
secondary: customPropTypes.every([customPropTypes.disallow(['primary']), PropTypes.bool]),
size: PropTypes.oneOf(['medium', 'small'])
});
Button.Group = ButtonGroup;
Button.Content = ButtonContent;
Button.create = createShorthandFactory({
Component: Button,
mappedProp: 'content'
});
return Button;
}();
//# sourceMappingURL=Button.js.map