@amaui/ui-react
Version:
UI for React
134 lines (133 loc) • 6.95 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 Type_1 = __importDefault(require("../Type"));
const utils_1 = require("../utils");
const useStyle = (0, style_react_1.style)(theme => ({
root: {
position: 'relative',
display: 'inline-flex',
transition: theme.methods.transitions.make(['color', 'text-decoration'], { duration: 'xxs' }),
'&::before': {
content: "''",
display: 'inline-flex',
position: 'absolute',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
background: 'currentColor',
width: '100%',
height: '100%',
zIndex: '0',
padding: '0.14em 0.77em',
borderRadius: `${(theme.shape.radius.unit / 8) * 0.4}em`,
transition: theme.methods.transitions.make(['opacity', 'background']),
opacity: '0'
}
},
focus: {
'&::before': {
opacity: theme.palette.visual_contrast.default.opacity.selected
}
},
// Underline
underline: {
textDecoration: 'underline'
},
underline_hover: {
'&:hover': {
textDecoration: 'underline'
}
},
// 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', 30) },
tonal_color_primary: { color: theme.methods.palette.color.value('primary', 30) },
tonal_color_secondary: { color: theme.methods.palette.color.value('secondary', 30) },
tonal_color_tertiary: { color: theme.methods.palette.color.value('tertiary', 30) },
tonal_color_quaternary: { color: theme.methods.palette.color.value('quaternary', 30) },
tonal_color_info: { color: theme.methods.palette.color.value('info', 30) },
tonal_color_success: { color: theme.methods.palette.color.value('success', 30) },
tonal_color_warning: { color: theme.methods.palette.color.value('warning', 30) },
tonal_color_error: { color: theme.methods.palette.color.value('error', 30) }
}), { name: 'amaui-Link' });
const Link = 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.amauiLink) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]);
const Type = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Type) || Type_1.default; }, [theme]);
const { tonal = true, color: color_ = 'primary', version = 'b2', underline = true, Component = 'a', className, style, children } = props, other = __rest(props, ["tonal", "color", "version", "underline", "Component", "className", "style", "children"]);
const { classes } = useStyle();
const [focus, setFocus] = react_1.default.useState(false);
const [hover, setHover] = react_1.default.useState(false);
const styles = {
root: {}
};
const onFocus = react_1.default.useCallback((event) => {
setFocus(true);
}, []);
const onBlur = react_1.default.useCallback((event) => {
setFocus(false);
}, []);
const onMouseEnter = react_1.default.useCallback((event) => {
setHover(true);
}, []);
const onMouseLeave = react_1.default.useCallback((event) => {
setHover(false);
}, []);
const color = color_;
let palette;
if (!classes[`color_${color}`]) {
styles.root.color = color;
palette = theme.methods.color(color);
if (tonal)
styles.root.color = theme.methods.palette.color.value(undefined, 30, true, palette);
}
if (underline === true && !hover) {
styles.root.textDecorationColor = theme.methods.palette.color.value(color, 70, true, palette);
}
if (hover) {
styles.root.color = theme.methods.palette.color.value(color, 10, true, palette);
}
return ((0, jsx_runtime_1.jsx)(Type, Object.assign({ ref: ref, onFocus: onFocus, onBlur: onBlur, onMouseEnter: onMouseEnter, onMouseLeave: onMouseLeave, onTouchStart: onMouseEnter, onTouchEnd: onMouseLeave, version: version, role: 'link', Component: Component, className: (0, style_react_1.classNames)([
(0, utils_1.staticClassName)('Link', theme) && [
'amaui-Link-root',
hover && `amaui-Link-hover`,
focus && `amaui-Link-focus`
],
className,
classes.root,
classes[`color_${color}`],
tonal && classes[`tonal_color_${color}`],
underline === true && classes.underline,
underline === 'hover' && classes.underline_hover,
focus && classes.focus
]), style: Object.assign(Object.assign({}, style), styles.root) }, other, { children: children })));
});
Link.displayName = 'amaui-Link';
exports.default = Link;