baseui
Version:
A React Component library implementing the Base design language
518 lines (508 loc) • 13.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.StartEnhancer = exports.LoadingSpinnerContainer = exports.LoadingSpinner = exports.EndEnhancer = exports.BaseButton = exports.AnchorBaseButton = void 0;
var _styles = require("../styles");
var _constants = require("./constants");
/*
Copyright (c) Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// note: Tried doing a standard override of the styled function, but it didn't work
// it seems like there is some bug when override $as
const createStyledBaseButton = (type, styleFn) => (0, _styles.styled)(type, ({
$theme,
$size,
$colors,
$kind,
$shape,
$isLoading,
$isSelected,
$disabled,
$isFocusVisible,
$as
}) => ({
display: 'inline-flex',
// need to maintain button width while showing loading spinner
flexDirection: $isLoading ? 'column' : 'row',
alignItems: 'center',
justifyContent: 'center',
borderLeftWidth: 0,
borderTopWidth: 0,
borderRightWidth: 0,
borderBottomWidth: 0,
borderLeftStyle: 'none',
borderTopStyle: 'none',
borderRightStyle: 'none',
borderBottomStyle: 'none',
outline: 'none',
boxShadow: $isFocusVisible ? `inset 0 0 0 3px ${$theme.colors.borderAccent}` : 'none',
textDecoration: 'none',
WebkitAppearance: 'none',
transitionProperty: 'background',
transitionDuration: $theme.animation.timing200,
transitionTimingFunction: $theme.animation.linearCurve,
cursor: 'pointer',
':disabled': {
cursor: 'not-allowed',
...getDisabledStyles({
$theme,
$kind,
$disabled,
$isSelected
})
},
marginLeft: 0,
marginTop: 0,
marginRight: 0,
marginBottom: 0,
...getFontStyles({
$theme,
$size
}),
...getBorderRadiiStyles({
$theme,
$size,
$shape
}),
...getPaddingStyles({
$theme,
$size,
$shape
}),
...getColorStyles({
$theme,
$colors,
$kind,
$isLoading,
$isSelected,
$disabled
}),
...getAnchorDisabledStyles({
$as,
$theme,
$kind,
$isSelected,
$disabled
}),
...getShapeStyles({
$shape,
$size
}),
...styleFn?.({
$theme,
$size,
$colors,
$kind,
$shape,
$isLoading,
$isSelected,
$disabled,
$isFocusVisible,
$as
})
}));
const BaseButton = exports.BaseButton = createStyledBaseButton('button');
BaseButton.displayName = 'BaseButton';
const AnchorBaseButton = exports.AnchorBaseButton = createStyledBaseButton('a', ({
$theme,
$kind,
$isSelected,
$disabled
}) => getAnchorDisabledStyles({
$as: 'a',
$theme,
$kind,
$isSelected,
$disabled
}));
BaseButton.displayName = 'BaseButton';
const EndEnhancer = exports.EndEnhancer = (0, _styles.styled)('div', ({
$theme
}) => {
const marginDirection = $theme.direction === 'rtl' ? 'marginRight' : 'marginLeft';
return {
display: 'flex',
[marginDirection]: $theme.sizing.scale300
};
});
EndEnhancer.displayName = "EndEnhancer";
EndEnhancer.displayName = 'EndEnhancer';
const StartEnhancer = exports.StartEnhancer = (0, _styles.styled)('div', ({
$theme
}) => {
const marginDirection = $theme.direction === 'rtl' ? 'marginLeft' : 'marginRight';
return {
display: 'flex',
[marginDirection]: $theme.sizing.scale300
};
});
StartEnhancer.displayName = "StartEnhancer";
StartEnhancer.displayName = 'StartEnhancer';
const LoadingSpinnerContainer = exports.LoadingSpinnerContainer = (0, _styles.styled)('div', ({
$theme,
$size
}) => {
// we don't have a theming value for this
let margins = '3px';
if ($size === _constants.SIZE.mini || $size === _constants.SIZE.compact) {
margins = $theme.sizing.scale0;
}
if ($size === _constants.SIZE.large) {
margins = $theme.sizing.scale100;
}
return {
lineHeight: 0,
position: 'static',
marginBottom: margins,
marginTop: margins
};
});
LoadingSpinnerContainer.displayName = "LoadingSpinnerContainer";
LoadingSpinnerContainer.displayName = 'LoadingSpinnerContainer';
const LoadingSpinner = exports.LoadingSpinner = (0, _styles.styled)('span', ({
$theme,
$kind,
$disabled,
$size
}) => {
const {
foreground,
background
} = getLoadingSpinnerColors({
$theme,
$kind,
$disabled
});
let dimension = $theme.sizing.scale550;
if ($size === _constants.SIZE.mini || $size === _constants.SIZE.compact) {
dimension = $theme.sizing.scale500;
}
if ($size === _constants.SIZE.large) {
dimension = $theme.sizing.scale600;
}
return {
height: dimension,
width: dimension,
borderTopLeftRadius: '50%',
borderTopRightRadius: '50%',
borderBottomRightRadius: '50%',
borderBottomLeftRadius: '50%',
borderLeftStyle: 'solid',
borderTopStyle: 'solid',
borderRightStyle: 'solid',
borderBottomStyle: 'solid',
borderLeftWidth: $theme.sizing.scale0,
borderTopWidth: $theme.sizing.scale0,
borderRightWidth: $theme.sizing.scale0,
borderBottomWidth: $theme.sizing.scale0,
borderTopColor: foreground,
borderLeftColor: background,
borderBottomColor: background,
borderRightColor: background,
boxSizing: 'border-box',
display: 'inline-block',
animationDuration: $theme.animation.timing700,
animationTimingFunction: 'linear',
animationIterationCount: 'infinite',
animationName: {
to: {
transform: 'rotate(360deg)'
},
from: {
transform: 'rotate(0deg)'
}
}
};
});
LoadingSpinner.displayName = "LoadingSpinner";
LoadingSpinner.displayName = 'LoadingSpinner';
// @ts-ignore
function getLoadingSpinnerColors({
$theme,
$kind,
$disabled
}) {
if ($disabled) {
return {
foreground: $theme.colors.buttonDisabledSpinnerForeground,
background: $theme.colors.buttonDisabledSpinnerBackground
};
}
switch ($kind) {
case _constants.KIND.secondary:
{
return {
foreground: $theme.colors.buttonSecondarySpinnerForeground,
background: $theme.colors.buttonSecondarySpinnerBackground
};
}
case _constants.KIND.tertiary:
{
return {
foreground: $theme.colors.buttonTertiarySpinnerForeground,
background: $theme.colors.buttonTertiarySpinnerBackground
};
}
case _constants.KIND.primary:
default:
{
return {
foreground: $theme.colors.buttonPrimarySpinnerForeground,
background: $theme.colors.buttonPrimarySpinnerBackground
};
}
}
}
// @ts-ignore
function getBorderRadiiStyles({
$theme,
$size,
$shape
}) {
let value = $theme.borders.buttonBorderRadius;
if ($shape === _constants.SHAPE.pill) {
if ($size === _constants.SIZE.compact) {
value = '30px';
} else if ($size === _constants.SIZE.large) {
value = '42px';
} else {
value = '38px';
}
} else if ($shape === _constants.SHAPE.circle || $shape === _constants.SHAPE.round) {
value = '50%';
} else if ($size === _constants.SIZE.mini) {
value = $theme.borders.buttonBorderRadiusMini;
}
return {
borderTopRightRadius: value,
borderBottomRightRadius: value,
borderTopLeftRadius: value,
borderBottomLeftRadius: value
};
}
// @ts-ignore
function getFontStyles({
$theme,
$size
}) {
switch ($size) {
case _constants.SIZE.mini:
return $theme.typography.font150;
case _constants.SIZE.compact:
return $theme.typography.font250;
case _constants.SIZE.large:
return $theme.typography.font450;
default:
return $theme.typography.font350;
}
}
function getAnchorDisabledStyles({
$as,
$theme,
$kind,
$isSelected,
$disabled
}) {
if (!($as === 'a' && $disabled)) {
return {};
}
return {
cursor: 'not-allowed',
pointerEvents: 'none',
...getDisabledStyles({
$theme,
$kind,
$isSelected,
$disabled
})
};
}
// @ts-ignore
function getDisabledStyles({
$theme,
$kind,
$isSelected,
$disabled
}) {
if ($disabled && $isSelected) {
if ($kind === _constants.KIND.primary || $kind === _constants.KIND.secondary) {
return {
color: $theme.colors.buttonDisabledActiveText,
backgroundColor: $theme.colors.buttonDisabledActiveFill
};
} else if ($kind === _constants.KIND.tertiary) {
return {
backgroundColor: $theme.colors.buttonTertiaryDisabledActiveFill,
color: $theme.colors.buttonTertiaryDisabledActiveText
};
}
}
return {
backgroundColor: $kind === _constants.KIND.tertiary ? 'transparent' : $theme.colors.buttonDisabledFill,
color: $theme.colors.buttonDisabledText
};
}
// @ts-ignore
function getPaddingStyles({
$theme,
$size,
$shape
}) {
const iconShape = $shape === _constants.SHAPE.square || $shape === _constants.SHAPE.circle || $shape === _constants.SHAPE.round;
switch ($size) {
case _constants.SIZE.mini:
return {
paddingTop: $theme.sizing.scale200,
paddingBottom: $theme.sizing.scale200,
paddingLeft: iconShape ? $theme.sizing.scale200 : $theme.sizing.scale300,
paddingRight: iconShape ? $theme.sizing.scale200 : $theme.sizing.scale300
};
case _constants.SIZE.compact:
return {
paddingTop: $theme.sizing.scale400,
paddingBottom: $theme.sizing.scale400,
paddingLeft: iconShape ? $theme.sizing.scale400 : $theme.sizing.scale500,
paddingRight: iconShape ? $theme.sizing.scale400 : $theme.sizing.scale500
};
case _constants.SIZE.large:
return {
paddingTop: $theme.sizing.scale600,
paddingBottom: $theme.sizing.scale600,
paddingLeft: iconShape ? $theme.sizing.scale600 : $theme.sizing.scale700,
paddingRight: iconShape ? $theme.sizing.scale600 : $theme.sizing.scale700
};
default:
return {
paddingTop: $theme.sizing.scale550,
paddingBottom: $theme.sizing.scale550,
paddingLeft: iconShape ? $theme.sizing.scale550 : $theme.sizing.scale600,
paddingRight: iconShape ? $theme.sizing.scale550 : $theme.sizing.scale600
};
}
}
function getColorStyles({
// @ts-ignore
$theme,
// @ts-ignore
$colors,
// @ts-ignore
$isLoading,
// @ts-ignore
$isSelected,
// @ts-ignore
$kind,
// @ts-ignore
$disabled
}) {
if ($colors) {
return {
color: $colors.color,
backgroundColor: $colors.backgroundColor,
':hover': {
boxShadow: 'inset 999px 999px 0px rgba(0, 0, 0, 0.04)'
},
':active': {
boxShadow: 'inset 999px 999px 0px rgba(0, 0, 0, 0.08)'
}
};
}
if ($disabled) {
return Object.freeze({});
}
switch ($kind) {
case _constants.KIND.primary:
if ($isSelected) {
return {
color: $theme.colors.buttonPrimarySelectedText,
backgroundColor: $theme.colors.buttonPrimarySelectedFill
};
}
return {
color: $theme.colors.buttonPrimaryText,
backgroundColor: $theme.colors.buttonPrimaryFill,
':hover': {
backgroundColor: $isLoading ? $theme.colors.buttonPrimaryActive : $theme.colors.buttonPrimaryHover
},
':active': {
backgroundColor: $theme.colors.buttonPrimaryActive
}
};
case _constants.KIND.secondary:
if ($isSelected) {
return {
color: $theme.colors.buttonPrimaryText,
backgroundColor: $theme.colors.buttonPrimaryFill
};
}
return {
color: $theme.colors.buttonSecondaryText,
backgroundColor: $theme.colors.buttonSecondaryFill,
':hover': {
backgroundColor: $isLoading ? $theme.colors.buttonSecondaryActive : $theme.colors.buttonSecondaryHover
},
':active': {
backgroundColor: $theme.colors.buttonSecondaryActive
}
};
case _constants.KIND.tertiary:
if ($isSelected) {
return {
color: $theme.colors.buttonTertiarySelectedText,
backgroundColor: $theme.colors.buttonTertiarySelectedFill
};
}
return {
color: $theme.colors.buttonTertiaryText,
backgroundColor: $theme.colors.buttonTertiaryFill,
':hover': {
backgroundColor: $isLoading ? $theme.colors.buttonTertiaryActive : $theme.colors.buttonTertiaryHover
},
':active': {
backgroundColor: $theme.colors.buttonTertiaryActive
}
};
default:
return Object.freeze({});
}
}
// @ts-ignore
function getShapeStyles({
$shape,
$size
}) {
if ($shape === _constants.SHAPE.circle || $shape === _constants.SHAPE.square) {
let height, width;
switch ($size) {
case _constants.SIZE.mini:
height = '28px';
width = '28px';
break;
case _constants.SIZE.compact:
height = '36px';
width = '36px';
break;
case _constants.SIZE.large:
height = '56px';
width = '56px';
break;
case _constants.SIZE.default:
default:
height = '48px';
width = '48px';
break;
}
return {
height,
width,
paddingTop: 0,
paddingBottom: 0,
paddingLeft: 0,
paddingRight: 0
};
} else {
return {};
}
}