@amaui/ui-react
Version:
UI for React
119 lines • 5.21 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", "version", "elevation", "focus", "selected", "button", "href", "disabled", "onFocus", "onBlur", "InteractionProps", "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 { is } from '@amaui/utils';
import { classNames, style as styleMethod, useAmauiTheme } from '@amaui/style-react';
import SurfaceElement from '../Surface';
import InteractionElement from '../Interaction';
import LineElement from '../Line';
import { staticClassName } from '../utils';
const useStyle = styleMethod(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 = /*#__PURE__*/React.forwardRef((props_, ref) => {
const theme = useAmauiTheme();
const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.amauiCard?.props?.default), props_), [props_]);
const Line = React.useMemo(() => theme?.elements?.Line || LineElement, [theme]);
const Surface = React.useMemo(() => theme?.elements?.Surface || SurfaceElement, [theme]);
const Interaction = React.useMemo(() => theme?.elements?.Interaction || InteractionElement, [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 = _objectWithoutProperties(props, _excluded);
const {
classes
} = useStyle();
const [focus, setFocus] = React.useState(focus_ !== undefined ? focus_ : false);
let color = color_;
let elevation = elevation_;
if (disabled) {
color = 'neutral';
elevation = 0;
}
const onFocus = React.useCallback(event => {
if (focus_ === undefined && !disabled) {
setFocus(true);
if (is('function', onFocus_)) onFocus_(event);
}
}, [focus_, disabled, onFocus_]);
const onBlur = React.useCallback(event => {
if (focus_ === undefined && !disabled) {
setFocus(false);
if (is('function', onBlur_)) onBlur_(event);
}
}, [focus_, disabled, onBlur_]);
return /*#__PURE__*/React.createElement(Surface, _extends({
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: classNames([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), (href || button) && /*#__PURE__*/React.createElement(Interaction, _extends({
border: false,
pulse: focus,
selected: selected
}, InteractionProps)), React.Children.toArray(children).map(item => /*#__PURE__*/React.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';
export default Card;