@blueprintjs/core
Version:
Core styles & components
91 lines • 5.05 kB
JavaScript
"use strict";
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.AnchorButton = exports.Button = void 0;
var tslib_1 = require("tslib");
var classnames_1 = tslib_1.__importDefault(require("classnames"));
var React = tslib_1.__importStar(require("react"));
var useInteractiveAttributes_1 = require("../../accessibility/useInteractiveAttributes");
var common_1 = require("../../common");
var props_1 = require("../../common/props");
var icon_1 = require("../icon/icon");
var spinner_1 = require("../spinner/spinner");
var text_1 = require("../text/text");
/**
* Button component.
*
* @see https://blueprintjs.com/docs/#core/components/button
*/
exports.Button = React.forwardRef(function (props, ref) {
var commonAttributes = useSharedButtonAttributes(props, ref);
return (React.createElement("button", tslib_1.__assign({ type: "button" }, (0, props_1.removeNonHTMLProps)(props), commonAttributes), renderButtonContents(props)));
});
exports.Button.displayName = "".concat(props_1.DISPLAYNAME_PREFIX, ".Button");
/**
* AnchorButton component.
*
* @see https://blueprintjs.com/docs/#core/components/button
*/
exports.AnchorButton = React.forwardRef(function (props, ref) {
var href = props.href;
var commonProps = useSharedButtonAttributes(props, ref, {
defaultTabIndex: 0,
disabledTabIndex: -1,
});
return (React.createElement("a", tslib_1.__assign({ role: "button" }, (0, props_1.removeNonHTMLProps)(props), commonProps, { "aria-disabled": commonProps.disabled, href: commonProps.disabled ? undefined : href }), renderButtonContents(props)));
});
exports.AnchorButton.displayName = "".concat(props_1.DISPLAYNAME_PREFIX, ".AnchorButton");
/**
* Most of the button logic lives in this shared hook.
*/
function useSharedButtonAttributes(props, ref, options) {
var _a;
var alignText = props.alignText, fill = props.fill,
// eslint-disable-next-line @typescript-eslint/no-deprecated
large = props.large, _b = props.loading, loading = _b === void 0 ? false : _b,
// eslint-disable-next-line @typescript-eslint/no-deprecated
minimal = props.minimal,
// eslint-disable-next-line @typescript-eslint/no-deprecated
outlined = props.outlined, _c = props.size, size = _c === void 0 ? "medium" : _c,
// eslint-disable-next-line @typescript-eslint/no-deprecated
small = props.small, _d = props.variant, variant = _d === void 0 ? "solid" : _d;
var disabled = props.disabled || loading;
var _e = (0, useInteractiveAttributes_1.useInteractiveAttributes)(!disabled, props, ref, options), active = _e[0], interactiveProps = _e[1];
var className = (0, classnames_1.default)(common_1.Classes.BUTTON, (_a = {},
_a[common_1.Classes.ACTIVE] = active,
_a[common_1.Classes.DISABLED] = disabled,
_a[common_1.Classes.FILL] = fill,
_a[common_1.Classes.LOADING] = loading,
_a), common_1.Classes.alignmentClass(alignText), common_1.Classes.intentClass(props.intent), common_1.Classes.sizeClass(size, { large: large, small: small }), common_1.Classes.variantClass(variant, { minimal: minimal, outlined: outlined }), props.className);
return tslib_1.__assign(tslib_1.__assign({}, interactiveProps), { className: className, disabled: disabled });
}
/**
* Shared rendering code for button contents.
*/
function renderButtonContents(props) {
// eslint-disable-next-line @typescript-eslint/no-deprecated
var children = props.children, ellipsizeText = props.ellipsizeText, endIcon = props.endIcon, icon = props.icon, loading = props.loading, rightIcon = props.rightIcon, text = props.text, textClassName = props.textClassName;
var hasTextContent = !common_1.Utils.isReactNodeEmpty(text) || !common_1.Utils.isReactNodeEmpty(children);
return (React.createElement(React.Fragment, null,
loading && React.createElement(spinner_1.Spinner, { className: common_1.Classes.BUTTON_SPINNER, size: spinner_1.SpinnerSize.SMALL }),
React.createElement(icon_1.Icon, { icon: icon }),
hasTextContent && (React.createElement(text_1.Text, { className: (0, classnames_1.default)(common_1.Classes.BUTTON_TEXT, textClassName), ellipsize: ellipsizeText, tagName: "span" },
text,
children)),
React.createElement(icon_1.Icon, { icon: endIcon !== null && endIcon !== void 0 ? endIcon : rightIcon })));
}
//# sourceMappingURL=buttons.js.map