@atlaskit/button
Version:
A button triggers an event or action. They let users know what will happen next.
98 lines (90 loc) • 4.25 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
exports.defaultThemeFn = defaultThemeFn;
exports.getCustomCss = getCustomCss;
exports.getSpecifiers = getSpecifiers;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _components = require("@atlaskit/theme/components");
var _css = require("../shared/css");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } // eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled -- Ignored via go/DSP-18766
var stateToSelectorMap = {
focus: '&:focus',
focusSelected: '&:focus',
hover: '&:hover',
active: '&:active',
disabled: '&[disabled]'
};
// Mapping the new clean css back to the legacy theme format.
// The legacy theme format has all styles at the top level (no nested selectors)
// and uses `getSpecifiers()` to apply the style to all pseudo states
function getCustomCss(_ref) {
var _ref$appearance = _ref.appearance,
appearance = _ref$appearance === void 0 ? 'default' : _ref$appearance,
_ref$spacing = _ref.spacing,
spacing = _ref$spacing === void 0 ? 'default' : _ref$spacing,
_ref$isSelected = _ref.isSelected,
isSelected = _ref$isSelected === void 0 ? false : _ref$isSelected,
_ref$shouldFitContain = _ref.shouldFitContainer,
shouldFitContainer = _ref$shouldFitContain === void 0 ? false : _ref$shouldFitContain,
_ref$iconIsOnlyChild = _ref.iconIsOnlyChild,
iconIsOnlyChild = _ref$iconIsOnlyChild === void 0 ? false : _ref$iconIsOnlyChild,
_ref$isLoading = _ref.isLoading,
isLoading = _ref$isLoading === void 0 ? false : _ref$isLoading,
state = _ref.state;
var result = (0, _css.getCss)({
appearance: appearance,
spacing: spacing,
isSelected: isSelected,
shouldFitContainer: shouldFitContainer,
isOnlySingleIcon: iconIsOnlyChild
});
// we need to disable the default browser focus styles always
// this is because we are not expressing that we can have two pseudo states at a time
result.outline = 'none';
// Pulling relevant styles up to the top level
var selector = stateToSelectorMap[state];
if (selector) {
result = _objectSpread(_objectSpread({}, result), result[selector]);
}
if (isLoading) {
result = _objectSpread(_objectSpread({}, result), result['&[data-has-overlay="true"]']);
}
// Delete all selectors and just keep root styles
Object.keys(result).forEach(function (key) {
// want to keep this one
if (key === '&::-moz-focus-inner') {
return;
}
// Not using .startsWith for ie11
if (key.indexOf('&') === 0) {
delete result[key];
}
});
return result;
}
// This styling approach works by generating a 'style' and applying with maximum specificity
// To do this we are overwriting all pseudo selectors
function getSpecifiers(styles) {
return {
'&, &:hover, &:active, &:focus, &:focus-visible, &:visited, &:disabled, &[disabled]': styles
};
}
function defaultThemeFn(current, values) {
return current(values);
}
var Theme = (0, _components.createTheme)(function (themeProps) {
return {
buttonStyles: getCustomCss(themeProps),
// No styles being applied directly to spinner by default
// Keeping this for legacy compat. We could remove it, but given
// that we are changing theme soon there is no point
spinnerStyles: {}
};
});
// eslint-disable-next-line @repo/internal/react/require-jsdoc
var _default = exports.default = Theme;