@amaui/ui-react
Version:
UI for React
105 lines • 4.63 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
const _excluded = ["tonal", "color", "size", "value", "max", "vertical", "horizontal", "indicator", "element", "Component", "className", "children"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
import React from 'react';
import { classNames, style as styleMethod, useAmauiTheme } from '@amaui/style-react';
import SurfaceElement from '../Surface';
import { staticClassName } from '../utils';
const useStyle = styleMethod(theme => ({
root: {
display: 'inline-flex',
position: 'relative'
},
badge: _objectSpread(_objectSpread({
position: 'absolute',
padding: `${theme.methods.space.value(0.25, 'px')} ${theme.methods.space.value(0.75, 'px')}`,
lineHeight: '1px',
borderRadius: theme.methods.shape.radius.value(40, 'px'),
transition: theme.methods.transitions.make('transform'),
pointerEvents: 'none',
userSelect: 'none'
}, theme.typography.values.l3), {}, {
fontSize: `${10 / 16}rem`
}),
// Vertical & horizontal
horizontal_left: {
insetInlineStart: '0px'
},
horizontal_right: {
insetInlineEnd: '0px'
},
vertical_top: {
top: '0px'
},
vertical_horizontal_top_right: {
transform: `translate(${theme.direction === 'rtl' ? '-' : ''}50%, -50%)`
},
vertical_horizontal_top_left: {
transform: `translate(${theme.direction === 'rtl' ? '' : '-'}50%, -50%)`
},
vertical_bottom: {
bottom: '0px'
},
vertical_horizontal_bottom_right: {
transform: `translate(${theme.direction === 'rtl' ? '-' : ''}50%, 50%)`
},
vertical_horizontal_bottom_left: {
transform: `translate(${theme.direction === 'rtl' ? '' : '-'}50%, 50%)`
},
indicator: {
height: '8px',
width: '8px',
padding: '0',
borderRadius: `${theme.shape.radius.unit * 0.5}px`
}
}), {
name: 'amaui-Badge'
});
const Badge = /*#__PURE__*/React.forwardRef((props_, ref) => {
const theme = useAmauiTheme();
const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.amauiBadge?.props?.default), props_), [props_]);
const Surface = React.useMemo(() => theme?.elements?.Surface || SurfaceElement, [theme]);
const {
tonal = false,
color = 'primary',
size = 'regular',
value: value_,
max,
vertical = 'top',
horizontal = 'right',
indicator,
element,
Component = 'span',
className,
children
} = props,
other = _objectWithoutProperties(props, _excluded);
const {
classes
} = useStyle();
const styles = {
badge: {}
};
let value = value_;
if (max !== undefined && value > max) value = `${max}+`;
const useValue = value !== undefined || indicator || element;
const classesBadge = classNames([staticClassName('Badge', theme) && ['amaui-Badge-badge'], classes.badge, classes[`vertical_${vertical}`], classes[`horizontal_${horizontal}`], classes[`vertical_horizontal_${vertical}_${horizontal}`], indicator && classes.indicator]);
return /*#__PURE__*/React.createElement(Component, _extends({
ref: ref,
size: size,
className: classNames([staticClassName('Badge', theme) && ['amaui-Badge-root', `amaui-Badge-size-${size}`], className, classes.root])
}, other), children, useValue && (element ? /*#__PURE__*/React.cloneElement(element, {
className: classNames([classesBadge, element.props?.className])
}) : /*#__PURE__*/React.createElement(Surface, {
tonal: tonal,
color: color,
Component: "span",
className: classesBadge,
style: styles.badge
}, value)));
});
Badge.displayName = 'amaui-Badge';
export default Badge;