@amaui/ui-react
Version:
UI for React
100 lines (99 loc) • 5.35 kB
JavaScript
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importDefault(require("react"));
const style_react_1 = require("@amaui/style-react");
const Surface_1 = __importDefault(require("../Surface"));
const utils_1 = require("../utils");
const useStyle = (0, style_react_1.style)(theme => ({
root: {
display: 'inline-flex',
position: 'relative'
},
badge: Object.assign(Object.assign({ 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 = react_1.default.forwardRef((props_, ref) => {
var _a;
const theme = (0, style_react_1.useAmauiTheme)();
const props = react_1.default.useMemo(() => { var _a, _b, _c, _d, _e, _f, _g, _h; return (Object.assign(Object.assign(Object.assign({}, (_d = (_c = (_b = (_a = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _a === void 0 ? void 0 : _a.elements) === null || _b === void 0 ? void 0 : _b.all) === null || _c === void 0 ? void 0 : _c.props) === null || _d === void 0 ? void 0 : _d.default), (_h = (_g = (_f = (_e = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _e === void 0 ? void 0 : _e.elements) === null || _f === void 0 ? void 0 : _f.amauiBadge) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]);
const Surface = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Surface) || Surface_1.default; }, [theme]);
const { tonal = false, color = 'primary', size = 'regular', value: value_, max, vertical = 'top', horizontal = 'right', indicator, element, Component = 'span', className, children } = props, other = __rest(props, ["tonal", "color", "size", "value", "max", "vertical", "horizontal", "indicator", "element", "Component", "className", "children"]);
const { classes } = useStyle();
const styles = {
badge: {}
};
let value = value_;
if (max !== undefined && value > max)
value = `${max}+`;
const useValue = value !== undefined || indicator || element;
const classesBadge = (0, style_react_1.classNames)([
(0, utils_1.staticClassName)('Badge', theme) && [
'amaui-Badge-badge'
],
classes.badge,
classes[`vertical_${vertical}`],
classes[`horizontal_${horizontal}`],
classes[`vertical_horizontal_${vertical}_${horizontal}`],
indicator && classes.indicator
]);
return ((0, jsx_runtime_1.jsxs)(Component, Object.assign({ ref: ref, size: size, className: (0, style_react_1.classNames)([
(0, utils_1.staticClassName)('Badge', theme) && [
'amaui-Badge-root',
`amaui-Badge-size-${size}`
],
className,
classes.root
]) }, other, { children: [children, useValue && (element ? react_1.default.cloneElement(element, {
className: (0, style_react_1.classNames)([
classesBadge,
(_a = element.props) === null || _a === void 0 ? void 0 : _a.className
])
}) : ((0, jsx_runtime_1.jsx)(Surface, Object.assign({ tonal: tonal, color: color, Component: 'span', className: classesBadge, style: styles.badge }, { children: value }))))] })));
});
Badge.displayName = 'amaui-Badge';
exports.default = Badge;
;