@amaui/ui-react
Version:
UI for React
107 lines (106 loc) • 6.23 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 Surface_1 = __importDefault(require("../Surface"));
const Interaction_1 = __importDefault(require("../Interaction"));
const Line_1 = __importDefault(require("../Line"));
const utils_2 = require("../utils");
const useStyle = (0, style_react_1.style)(theme => ({
root: {
position: 'relative',
borderRadius: `${theme.shape.radius.unit * 3}px`,
maxWidth: '340px',
overflow: 'hidden',
transition: theme.methods.transitions.make('border-radius'),
// reset
textDecoration: 'none',
},
button: {
cursor: 'pointer',
userSelect: 'none',
'&:active': {
borderRadius: `${theme.shape.radius.unit * 6}px`,
}
},
href: {
'&:active': {
borderRadius: `${theme.shape.radius.unit * 6}px`,
}
},
disabled: {
cursor: 'pointer',
pointerEvents: 'none',
opacity: theme.palette.visual_contrast.default.opacity.disabled
}
}), { name: 'amaui-Card' });
const Card = 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.amauiCard) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]);
const Line = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Line) || Line_1.default; }, [theme]);
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 Interaction = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Interaction) || Interaction_1.default; }, [theme]);
const { tonal = true, color: color_ = 'primary', version = 'filled', elevation: elevation_ = 0, focus: focus_, selected, button, href, disabled, onFocus: onFocus_, onBlur: onBlur_, InteractionProps, Component = props.href ? 'a' : 'div', className, children } = props, other = __rest(props, ["tonal", "color", "version", "elevation", "focus", "selected", "button", "href", "disabled", "onFocus", "onBlur", "InteractionProps", "Component", "className", "children"]);
const { classes } = useStyle();
const [focus, setFocus] = react_1.default.useState(focus_ !== undefined ? focus_ : false);
let color = color_;
let elevation = elevation_;
if (disabled) {
color = 'neutral';
elevation = 0;
}
const onFocus = react_1.default.useCallback((event) => {
if (focus_ === undefined && !disabled) {
setFocus(true);
if ((0, utils_1.is)('function', onFocus_))
onFocus_(event);
}
}, [focus_, disabled, onFocus_]);
const onBlur = react_1.default.useCallback((event) => {
if (focus_ === undefined && !disabled) {
setFocus(false);
if ((0, utils_1.is)('function', onBlur_))
onBlur_(event);
}
}, [focus_, disabled, onBlur_]);
return ((0, jsx_runtime_1.jsxs)(Surface, Object.assign({ ref: ref, gap: 0, tabIndex: (href || button) && !disabled ? 0 : undefined, color: color, tonal: tonal, version: version, elevation: elevation, href: href, onFocus: onFocus, onBlur: onBlur, disabled: disabled, AdditionalProps: {
Component
}, Component: Line, className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Card', theme) && [
`amaui-Card-root`,
focus && `amaui-Card-focus`,
disabled && `amaui-Card-disabled`
],
className,
classes.root,
href && classes.href,
button && classes.button,
disabled && [
classes.disabled,
classes[`disabled_version_${version}`]
]
]) }, other, { children: [(href || button) && ((0, jsx_runtime_1.jsx)(Interaction, Object.assign({ border: false, pulse: focus, selected: selected }, InteractionProps))), react_1.default.Children.toArray(children).map((item) => (react_1.default.cloneElement(item, {
tonal: item.props.tonal !== undefined ? item.props.tonal : tonal,
version: item.props.version !== undefined ? item.props.version : version,
elevation: item.props.elevation !== undefined ? item.props.elevation : elevation,
color: item.props.color !== undefined ? item.props.color : color
})))] })));
});
Card.displayName = 'amaui-Card';
exports.default = Card;
;