| 1 |
import { __rest } from "tslib"; |
| 2 |
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; |
| 3 |
import { forwardRef } from 'react'; |
| 4 |
import styles from '@patternfly/react-styles/css/components/Button/button.mjs'; |
| 5 |
import { css } from '@patternfly/react-styles'; |
| 6 |
import { Spinner, spinnerSize } from '../Spinner'; |
| 7 |
import { useOUIAProps } from '../../helpers/OUIA/ouia'; |
| 8 |
import { Badge } from '../Badge'; |
| 9 |
import RhUiStarFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-star-fill-icon'; |
| 10 |
import RhUiStarIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-star-icon'; |
| 11 |
import RhUiSettingsFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-settings-fill-icon'; |
| 12 |
import { hamburgerIcon } from './hamburgerIcon'; |
| 13 |
export var ButtonVariant; |
| 14 |
(function (ButtonVariant) { |
| 15 |
ButtonVariant["primary"] = "primary"; |
| 16 |
ButtonVariant["secondary"] = "secondary"; |
| 17 |
ButtonVariant["tertiary"] = "tertiary"; |
| 18 |
ButtonVariant["danger"] = "danger"; |
| 19 |
ButtonVariant["warning"] = "warning"; |
| 20 |
ButtonVariant["link"] = "link"; |
| 21 |
ButtonVariant["plain"] = "plain"; |
| 22 |
ButtonVariant["control"] = "control"; |
| 23 |
ButtonVariant["stateful"] = "stateful"; |
| 24 |
})(ButtonVariant || (ButtonVariant = {})); |
| 25 |
export var ButtonType; |
| 26 |
(function (ButtonType) { |
| 27 |
ButtonType["button"] = "button"; |
| 28 |
ButtonType["submit"] = "submit"; |
| 29 |
ButtonType["reset"] = "reset"; |
| 30 |
})(ButtonType || (ButtonType = {})); |
| 31 |
export var ButtonSize; |
| 32 |
(function (ButtonSize) { |
| 33 |
ButtonSize["default"] = "default"; |
| 34 |
ButtonSize["sm"] = "sm"; |
| 35 |
ButtonSize["lg"] = "lg"; |
| 36 |
})(ButtonSize || (ButtonSize = {})); |
| 37 |
export var ButtonState; |
| 38 |
(function (ButtonState) { |
| 39 |
ButtonState["read"] = "read"; |
| 40 |
ButtonState["unread"] = "unread"; |
| 41 |
ButtonState["attention"] = "attention"; |
| 42 |
})(ButtonState || (ButtonState = {})); |
| 43 |
const ButtonBase = (_a) => { |
| 44 |
var { children = null, className = '', component = 'button', isClicked = false, isBlock = false, isDisabled = false, isAriaDisabled = false, isLoading = null, isDanger = false, isExpanded, isSettings, isHamburger, hamburgerVariant, isCircle, isDocked = false, isTextExpanded = false, spinnerAriaValueText, spinnerAriaLabelledBy, spinnerAriaLabel, size = ButtonSize.default, inoperableEvents = ['onClick', 'onKeyPress'], isInline = false, isFavorite = false, isFavorited = false, type = ButtonType.button, variant = ButtonVariant.primary, state = ButtonState.unread, hasNoPadding = false, iconPosition = 'start', 'aria-label': ariaLabel = null, icon = null, role, ouiaId, ouiaSafe = true, tabIndex = null, innerRef, countOptions } = _a, props = __rest(_a, ["children", "className", "component", "isClicked", "isBlock", "isDisabled", "isAriaDisabled", "isLoading", "isDanger", "isExpanded", "isSettings", "isHamburger", "hamburgerVariant", "isCircle", "isDocked", "isTextExpanded", "spinnerAriaValueText", "spinnerAriaLabelledBy", "spinnerAriaLabel", "size", "inoperableEvents", "isInline", "isFavorite", "isFavorited", "type", "variant", "state", "hasNoPadding", "iconPosition", 'aria-label', "icon", "role", "ouiaId", "ouiaSafe", "tabIndex", "innerRef", "countOptions"]); |
| 45 |
if (isHamburger && ![true, false].includes(isExpanded)) { |
| 46 |
// eslint-disable-next-line no-console |
| 47 |
console.error('Button: when the isHamburger property is passed in, you must also pass in a boolean value to the isExpanded property. It is expected that a hamburger button controls the expansion of other content.'); |
| 48 |
} |
| 49 |
// TODO: Remove isSettings || isHamburger || isFavorite conditional in breaking change to throw this warning for any button that does not have children or aria name |
| 50 |
if ((isSettings || isHamburger || isFavorite) && !ariaLabel && !children && !props['aria-labelledby']) { |
| 51 |
// eslint-disable-next-line no-console |
| 52 |
console.error('Button: you must provide either visible text content or an accessible name via the aria-label or aria-labelledby properties.'); |
| 53 |
} |
| 54 |
const ouiaProps = useOUIAProps(Button.displayName, ouiaId, ouiaSafe, variant); |
| 55 |
const Component = component; |
| 56 |
const isButtonElement = Component === 'button'; |
| 57 |
const isInlineSpan = isInline && Component === 'span'; |
| 58 |
const isIconAlignedAtEnd = iconPosition === 'end' || iconPosition === 'right'; |
| 59 |
const shouldOverrideIcon = isSettings || isHamburger || isFavorite; |
| 60 |
const preventedEvents = inoperableEvents.reduce((handlers, eventToPrevent) => (Object.assign(Object.assign({}, handlers), { [eventToPrevent]: (event) => { |
| 61 |
event.preventDefault(); |
| 62 |
} })), {}); |
| 63 |
const getDefaultTabIdx = () => { |
| 64 |
if (isDisabled) { |
| 65 |
return isButtonElement ? null : -1; |
| 66 |
} |
| 67 |
else if (isAriaDisabled) { |
| 68 |
return null; |
| 69 |
} |
| 70 |
else if (isInlineSpan) { |
| 71 |
return 0; |
| 72 |
} |
| 73 |
}; |
| 74 |
const renderIcon = () => { |
| 75 |
let iconContent; |
| 76 |
if (isFavorite) { |
| 77 |
iconContent = (_jsxs(_Fragment, { children: [_jsx("span", { className: css('pf-v6-c-button__icon-favorite'), children: _jsx(RhUiStarIcon, {}) }), _jsx("span", { className: css('pf-v6-c-button__icon-favorited'), children: _jsx(RhUiStarFillIcon, {}) })] })); |
| 78 |
} |
| 79 |
if (isSettings) { |
| 80 |
iconContent = _jsx(RhUiSettingsFillIcon, {}); |
| 81 |
} |
| 82 |
if (isHamburger) { |
| 83 |
iconContent = hamburgerIcon; |
| 84 |
} |
| 85 |
if (icon && !shouldOverrideIcon) { |
| 86 |
iconContent = icon; |
| 87 |
} |
| 88 |
return (iconContent && (_jsx("span", { className: css(styles.buttonIcon, children && styles.modifiers[isIconAlignedAtEnd ? 'end' : 'start']), children: iconContent }))); |
| 89 |
}; |
| 90 |
const _icon = renderIcon(); |
| 91 |
const _children = children && _jsx("span", { className: css('pf-v6-c-button__text'), children: children }); |
| 92 |
// We only want to render the aria-disabled attribute when true, similar to the disabled attribute natively. |
| 93 |
const shouldRenderAriaDisabled = isAriaDisabled || (!isButtonElement && isDisabled); |
| 94 |
return (_jsxs(Component, Object.assign({ "aria-expanded": isExpanded }, props, (isAriaDisabled ? preventedEvents : null), (shouldRenderAriaDisabled && { 'aria-disabled': true }), { "aria-label": ariaLabel, className: css(styles.button, styles.modifiers[variant], isSettings && styles.modifiers.settings, isHamburger && styles.modifiers.hamburger, isHamburger && hamburgerVariant && styles.modifiers[hamburgerVariant], isBlock && styles.modifiers.block, isDisabled && !isButtonElement && styles.modifiers.disabled, isAriaDisabled && styles.modifiers.ariaDisabled, isClicked && styles.modifiers.clicked, isInline && variant === ButtonVariant.link && styles.modifiers.inline, isFavorite && styles.modifiers.favorite, isFavorite && isFavorited && styles.modifiers.favorited, isDanger && (variant === ButtonVariant.secondary || variant === ButtonVariant.link) && styles.modifiers.danger, isLoading !== null && variant !== ButtonVariant.plain && styles.modifiers.progress, isLoading && styles.modifiers.inProgress, hasNoPadding && variant === ButtonVariant.plain && styles.modifiers.noPadding, variant === ButtonVariant.stateful && styles.modifiers[state], size === ButtonSize.sm && styles.modifiers.small, size === ButtonSize.lg && styles.modifiers.displayLg, isCircle && styles.modifiers.circle, isDocked && styles.modifiers.docked, isDocked && isTextExpanded && styles.modifiers.textExpanded, className), disabled: isButtonElement ? isDisabled : null, tabIndex: tabIndex !== null ? tabIndex : getDefaultTabIdx(), type: isButtonElement || isInlineSpan ? type : null, role: isInlineSpan ? 'button' : role, ref: innerRef }, ouiaProps, { children: [isLoading && (_jsx("span", { className: css(styles.buttonProgress), children: _jsx(Spinner, { size: spinnerSize.md, isInline: isInline, "aria-valuetext": spinnerAriaValueText, "aria-label": spinnerAriaLabel, "aria-labelledby": spinnerAriaLabelledBy }) })), isIconAlignedAtEnd ? (_jsxs(_Fragment, { children: [_children, _icon] })) : (_jsxs(_Fragment, { children: [_icon, _children] })), countOptions && (_jsx("span", { className: css(styles.buttonCount, countOptions.className), children: _jsx(Badge, { isRead: countOptions.isRead, isDisabled: isDisabled, children: countOptions.count }) }))] }))); |
| 95 |
}; |
| 96 |
export const Button = forwardRef((props, ref) => _jsx(ButtonBase, Object.assign({ innerRef: ref }, props))); |
| 97 |
Button.displayName = 'Button'; |
| 98 |
//# sourceMappingURL=Button.js.map |