@amaui/ui-react
Version:
UI for React
184 lines (183 loc) • 9.19 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 utils_1 = require("@amaui/utils");
const style_react_1 = require("@amaui/style-react");
const utils_2 = require("../utils");
const noShrinkStrokeDashOffset = 194;
const useStyle = (0, style_react_1.style)(theme => ({
'@keyframes spin': {
'0%': {
transform: 'rotate(0deg)'
},
'100%': {
transform: 'rotate(360deg)'
}
},
'@keyframes shrink': {
'0%': {
strokeDashoffset: '240px'
},
'50%': {
strokeDashoffset: `${noShrinkStrokeDashOffset}px`,
transform: 'rotate(134deg)'
},
'100%': {
strokeDashoffset: '240px',
transform: 'rotate(360deg)'
}
},
root: {
display: 'inline-flex',
justifyContent: 'center',
alignItems: 'center',
pointerEvents: 'none',
fontSize: '3rem'
},
// Version
version_indeterminate: {
animation: `$spin 1.4s linear infinite`
},
version_determinate: {
transform: 'rotate(-90deg)'
},
// Size
size_extra_small: { fontSize: '1rem' },
size_small: { fontSize: '1.5rem' },
size_regular: { fontSize: '3rem' },
size_large: { fontSize: '3.5rem' },
// Color
color_default: { color: theme.palette.text.default.primary },
color_neutral: { color: theme.palette.color.neutral.main },
color_primary: { color: theme.palette.color.primary.main },
color_secondary: { color: theme.palette.color.secondary.main },
color_tertiary: { color: theme.palette.color.tertiary.main },
color_quaternary: { color: theme.palette.color.quaternary.main },
color_info: { color: theme.palette.color.info.main },
color_success: { color: theme.palette.color.success.main },
color_warning: { color: theme.palette.color.warning.main },
color_error: { color: theme.palette.color.error.main },
// Tonal
tonal_color_neutral: { color: theme.methods.palette.color.value('neutral', 50) },
tonal_color_primary: { color: theme.methods.palette.color.value('primary', 50) },
tonal_color_secondary: { color: theme.methods.palette.color.value('secondary', 50) },
tonal_color_tertiary: { color: theme.methods.palette.color.value('tertiary', 50) },
tonal_color_quaternary: { color: theme.methods.palette.color.value('quaternary', 50) },
tonal_color_info: { color: theme.methods.palette.color.value('info', 50) },
tonal_color_success: { color: theme.methods.palette.color.value('success', 50) },
tonal_color_warning: { color: theme.methods.palette.color.value('warning', 50) },
tonal_color_error: { color: theme.methods.palette.color.value('error', 50) },
svg: {
width: '1em',
height: '1em',
color: 'inherit',
stroke: 'currentcolor',
},
path: {
strokeLinecap: 'square',
transformOrigin: 'center',
strokeDasharray: '240px',
strokeDashoffset: '0px'
},
// Version
path_indeterminate: {
animation: `$shrink 1.4s ease-in-out infinite`
},
path_indeterminate_noShrink: {
strokeDashoffset: `${noShrinkStrokeDashOffset}px`,
animation: 'none'
},
path_determinate: {
strokeDasharray: '101',
strokeDashoffset: '0',
transition: theme.methods.transitions.make('stroke-dashoffset')
},
pathBackground: {
strokeLinecap: 'square',
strokeDasharray: '240px',
strokeDashoffset: '0px',
stroke: 'transparent'
}
}), { name: 'amaui-RoundProgress' });
const RoundProgress = react_1.default.forwardRef((props_, ref) => {
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.amauiRoundProgress) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]);
const { tonal = true, color = 'primary', size = 'regular', version = 'indeterminate', value, noShrink, thickness = 2, rounded: rounded_ = true, pathProps = {}, pathBackgroundProps, className, style, children } = props, other = __rest(props, ["tonal", "color", "size", "version", "value", "noShrink", "thickness", "rounded", "pathProps", "pathBackgroundProps", "className", "style", "children"]);
const { classes } = useStyle();
let rounded = rounded_;
const styles = {
root: {},
circle: {}
};
if (!classes[`size_${size}`])
styles.root.fontSize = (0, utils_1.is)('number', size) ? `${size}px` : size;
if (!classes[`color_${color}`]) {
styles.root.color = color;
if (tonal) {
const palette = theme.methods.color(color);
styles.root.color = theme.methods.palette.color.value(undefined, 50, true, palette);
}
}
if (version === 'determinate') {
rounded = false;
pathProps['pathLength'] = 100;
styles.circle.strokeDashoffset = 101 - value;
// 0%
if (styles.circle.strokeDashoffset === 1)
styles.circle.strokeDashoffset = 0;
other['aria-valuenow'] = value;
other['aria-valuemin'] = 0;
other['aria-valuemax'] = 100;
other['aria-valuetext'] = `${value}%`;
}
return ((0, jsx_runtime_1.jsxs)("span", Object.assign({ ref: ref, className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('RoundedProgress', theme) && [
'amaui-RoundedProgress-root',
`amaui-RoundedProgress-version-${version}`,
`amaui-RoundedProgress-size-${size}`
],
className,
classes.root,
classes[`size_${size}`],
classes[`color_${color}`],
classes[`version_${version}`],
tonal && classes[`tonal_color_${color}`]
]) }, { children: [(0, jsx_runtime_1.jsxs)("svg", Object.assign({ viewBox: '0 0 24 24', className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('RoundedProgress', theme) && [
'amaui-RoundedProgress-svg'
],
classes.svg
]), style: Object.assign(Object.assign({}, styles.root), style) }, other, { children: [(0, jsx_runtime_1.jsx)("circle", Object.assign({ fill: 'none', strokeWidth: thickness, cx: '12', cy: '12', r: '10' }, (!rounded && { strokeLinecap: 'butt' }), pathBackgroundProps, { className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('RoundedProgress', theme) && [
'amaui-RoundedProgress-path-background'
],
pathBackgroundProps === null || pathBackgroundProps === void 0 ? void 0 : pathBackgroundProps.className,
classes.pathBackground,
classes[`path_${version}`]
]) })), (0, jsx_runtime_1.jsx)("circle", Object.assign({ fill: 'none', strokeWidth: thickness, cx: '12', cy: '12', r: '10' }, (!rounded && { strokeLinecap: 'butt' }), pathProps, { className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('RoundedProgress', theme) && [
'amaui-RoundedProgress-path'
],
pathProps === null || pathProps === void 0 ? void 0 : pathProps.className,
classes.path,
classes[`path_${version}`],
noShrink && classes.path_indeterminate_noShrink
]), style: Object.assign(Object.assign({}, styles.circle), pathProps === null || pathProps === void 0 ? void 0 : pathProps.style) }))] })), children] })));
});
RoundProgress.displayName = 'amaui-RoundProgress';
exports.default = RoundProgress;