@amaui/ui-react
Version:
UI for React
440 lines (439 loc) • 21.5 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 Grow_1 = __importDefault(require("../Grow"));
const Modal_1 = __importDefault(require("../Modal"));
const Append_1 = __importDefault(require("../Append"));
const Surface_1 = __importDefault(require("../Surface"));
const utils_2 = require("../utils");
const useStyle = (0, style_react_1.style)(theme => ({
root: {
inset: '0px auto auto 0px'
},
nameRoot: {
pointerEvents: 'all'
// whiteSpace: 'nowrap'
},
nameRoot_position_top: { paddingBlock: theme.methods.space.value(2, 'px') },
nameRoot_position_bottom: { paddingBlock: theme.methods.space.value(2, 'px') },
nameRoot_position_left: { paddingInline: theme.methods.space.value(2, 'px') },
nameRoot_position_right: { paddingInline: theme.methods.space.value(2, 'px') },
nameRoot_noMargin: { padding: '0' },
name: Object.assign(Object.assign({}, theme.typography.values.b3), { borderRadius: `${(0, utils_1.clamp)(theme.shape.radius.unit / 2, 0, 8)}px`, padding: `${theme.methods.space.value(0.5, 'px')} ${theme.methods.space.value(1, 'px')}`, lineHeight: '1.455' }),
arrow: {
'&::before': {
content: "''",
width: '8px',
height: '8px',
background: 'inherit',
position: 'absolute',
zIndex: '0'
}
},
arrow_position_top_alignment_start: {
'&::before': {
bottom: '-4px',
left: 'clamp(11px, 14%, 24px)',
transform: 'translate(0, 0) rotate(45deg)'
}
},
arrow_position_top_alignment_center: {
'&::before': {
bottom: '-4px',
left: '50%',
transform: 'translate(-50%, 0) rotate(45deg)'
}
},
arrow_position_top_alignment_end: {
'&::before': {
bottom: '-4px',
right: 'clamp(11px, 14%, 24px)',
transform: 'translate(0, 0) rotate(45deg)'
}
},
arrow_position_bottom_alignment_start: {
'&::before': {
top: '-4px',
left: 'clamp(11px, 14%, 24px)',
transform: 'translate(0, 0) rotate(45deg)'
}
},
arrow_position_bottom_alignment_center: {
'&::before': {
top: '-4px',
left: '50%',
transform: 'translate(-50%, 0) rotate(45deg)'
}
},
arrow_position_bottom_alignment_end: {
'&::before': {
top: '-4px',
right: 'clamp(11px, 14%, 24px)',
transform: 'translate(0, 0) rotate(45deg)'
}
},
arrow_position_left_alignment_start: {
'&::before': {
right: '-4px',
top: 'clamp(5.4px, 14%, 24px)',
transform: 'translate(0, 0) rotate(45deg)'
}
},
arrow_position_left_alignment_center: {
'&::before': {
right: '-4px',
top: '50%',
transform: 'translate(0, -50%) rotate(45deg)'
}
},
arrow_position_left_alignment_end: {
'&::before': {
right: '-4px',
bottom: 'clamp(5.4px, 14%, 24px)',
transform: 'translate(0, 0) rotate(45deg)'
}
},
arrow_position_right_alignment_start: {
'&::before': {
left: '-4px',
top: 'clamp(5.4px, 14%, 24px)',
transform: 'translate(0, 0) rotate(45deg)'
}
},
arrow_position_right_alignment_center: {
'&::before': {
left: '-4px',
top: '50%',
transform: 'translate(0, -50%) rotate(45deg)'
}
},
arrow_position_right_alignment_end: {
'&::before': {
left: '-4px',
bottom: 'clamp(5.4px, 14%, 24px)',
transform: 'translate(0, 0) rotate(45deg)'
}
},
// maxWidth
maxWidth_xxs: { maxWidth: `320px` },
maxWidth_xs: { maxWidth: `400px` },
maxWidth_sm: { maxWidth: `480px` },
maxWidth_rg: { maxWidth: `560px` },
maxWidth_lg: { maxWidth: `800px` },
maxWidth_xl: { maxWidth: `1120px` },
maxWidth_xxl: { maxWidth: `1360px` },
maxWidth_unset: { maxWidth: `unset` },
fullWidth: { maxWidth: `${((0, utils_1.isEnvironment)('browser') ? window.innerWidth : 1400) - +theme.methods.space.value('rg', '', 1)}px` },
nowrap: {
whiteSpace: 'nowrap'
}
}), { name: 'amaui-Tooltip' });
const Tooltip = 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.amauiTooltip) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]);
const Grow = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Grow) || Grow_1.default; }, [theme]);
const Append = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Append) || Append_1.default; }, [theme]);
const Modal = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Modal) || Modal_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 { tonal = true, color = 'inverted', open: open_, openDefault, name, label: label_, parent, position = 'bottom', switch: switch_ = true, alignment = 'center', portal = true, fullWidth, maxWidth = 'unset', arrow, anchor: anchor_, anchorElement, noMargin, classNameSwitch, transformOrigin, transformOriginSwitch, transformOriginRtl, transformOriginRtlSwitch, click, touch: touch_ = true, longPress: longPress_ = false, hover: hover_ = true, focus: focus_ = false, inset, nowrap, follow, interactive, onOpen: onOpen_, onClose: onClose_, onExited, TransitionComponent = Grow, TransitionComponentProps, AppendProps, ModalProps, LabelProps,
// other
elevation, className, style, children } = props, other = __rest(props, ["tonal", "color", "open", "openDefault", "name", "label", "parent", "position", "switch", "alignment", "portal", "fullWidth", "maxWidth", "arrow", "anchor", "anchorElement", "noMargin", "classNameSwitch", "transformOrigin", "transformOriginSwitch", "transformOriginRtl", "transformOriginRtlSwitch", "click", "touch", "longPress", "hover", "focus", "inset", "nowrap", "follow", "interactive", "onOpen", "onClose", "onExited", "TransitionComponent", "TransitionComponentProps", "AppendProps", "ModalProps", "LabelProps", "elevation", "className", "style", "children"]);
const [init, setInit] = react_1.default.useState(false);
const [open, setOpen] = react_1.default.useState(openDefault !== undefined ? openDefault : open_);
const [hover, setHover] = react_1.default.useState(false);
const [touch, setTouch] = react_1.default.useState(false);
const [focus, setFocus] = react_1.default.useState(false);
const [longPress, setLongPress] = react_1.default.useState(false);
const [anchor, setAnchor] = react_1.default.useState(anchor_);
const [inProp, setInProp] = react_1.default.useState(open_);
const refs = {
open: react_1.default.useRef(false),
inProp: react_1.default.useRef(inProp),
longPress: react_1.default.useRef(false),
longPressTimer: react_1.default.useRef(undefined),
props: react_1.default.useRef(undefined)
};
refs.props.current = props;
refs.open.current = open;
refs.inProp.current = inProp;
const { classes } = useStyle();
const styles = {
name: {},
nameRoot: {}
};
const label = name !== undefined ? name : label_;
const onMouseDown = react_1.default.useCallback((event) => {
if (longPress_)
refs.longPressTimer.current = setTimeout(() => setLongPress(true), 700);
if ((0, utils_1.is)('function', children === null || children === void 0 ? void 0 : children.props.onMouseDown))
children.props.onMouseDown(event);
}, []);
const onMouseUp = react_1.default.useCallback((event) => {
if (longPress_) {
clearTimeout(refs.longPressTimer.current);
setLongPress(false);
}
if ((0, utils_1.is)('function', children === null || children === void 0 ? void 0 : children.props.onMouseUp))
children.props.onMouseUp(event);
}, []);
const onMouseEnter = react_1.default.useCallback((event) => {
if (hover_)
setHover(true);
if ((0, utils_1.is)('function', children === null || children === void 0 ? void 0 : children.props.onMouseEnter))
children.props.onMouseEnter(event);
}, []);
const onMouseLeave = react_1.default.useCallback((event) => {
if (refs.longPress.current) {
setLongPress(false);
if (refs.props.current.open === undefined) {
if (!inProp)
onClose();
else
setInProp(false);
}
}
if (hover_)
setHover(false);
if ((0, utils_1.is)('function', children === null || children === void 0 ? void 0 : children.props.onMouseLeave))
children.props.onMouseLeave(event);
}, []);
const onTouchStart = react_1.default.useCallback((event) => {
if (touch_)
setTouch(true);
if ((0, utils_1.is)('function', children === null || children === void 0 ? void 0 : children.props.onTouchStart))
children.props.onTouchStart(event);
}, []);
const onTouchEnd = react_1.default.useCallback((event) => {
if (touch_)
setTouch(false);
if ((0, utils_1.is)('function', children === null || children === void 0 ? void 0 : children.props.onTouchEnd))
children.props.onTouchEnd(event);
}, []);
const onFocus = react_1.default.useCallback((event) => {
if (focus_)
setFocus(true);
if ((0, utils_1.is)('function', children === null || children === void 0 ? void 0 : children.props.onFocus))
children.props.onFocus(event);
}, []);
const onBlur = react_1.default.useCallback((event) => {
if (refs.longPress.current) {
setLongPress(false);
if (refs.props.current.open === undefined) {
if (!inProp)
onClose();
else
setInProp(false);
}
}
if (focus_)
setFocus(false);
if ((0, utils_1.is)('function', children === null || children === void 0 ? void 0 : children.props.onBlur))
children.props.onBlur(event);
}, []);
const onMouseMove = react_1.default.useCallback((event) => {
if (follow) {
setAnchor({
x: event.clientX,
y: event.clientY,
width: 10,
height: 20
});
}
if ((0, utils_1.is)('function', children === null || children === void 0 ? void 0 : children.props.onMouseMove))
children.props.onMouseMove(event);
}, []);
const onOpen = () => {
if (!open)
setOpen(true);
if (!inProp)
setInProp(true);
if ((0, utils_1.is)('function', onOpen_) && !open)
onOpen_();
};
const onClose = () => {
setOpen(false);
if ((0, utils_1.is)('function', onClose_))
onClose_();
if ((0, utils_1.is)('function', onExited))
onExited();
};
const onPreClose = () => {
if (!refs.inProp.current)
onClose();
else
setInProp(false);
};
const onClick = react_1.default.useCallback((event) => {
!refs.open.current ? onOpen() : onPreClose();
}, [onOpen, onPreClose]);
react_1.default.useEffect(() => {
setInit(true);
}, []);
react_1.default.useEffect(() => {
setAnchor(anchor_);
}, [anchor_]);
react_1.default.useEffect(() => {
if (init) {
refs.open.current = open_;
if (open_)
onOpen();
else
onPreClose();
}
}, [open_]);
react_1.default.useEffect(() => {
if (init && refs.props.current.open === undefined) {
refs.open.current = (touch || hover || longPress);
if (refs.open.current)
onOpen();
else {
if (!inProp)
onClose();
else
setInProp(false);
}
}
}, [touch, hover, longPress]);
react_1.default.useEffect(() => {
if (init && refs.props.current.open === undefined) {
refs.open.current = focus;
if (refs.open.current)
onOpen();
else {
if (!inProp)
onClose();
else
setInProp(false);
}
}
}, [focus]);
react_1.default.useEffect(() => {
if (init) {
if (longPress)
refs.open.current = longPress;
refs.longPress.current = longPress;
if (refs.open.current)
onOpen();
}
}, [longPress]);
const resolvePosition = (switched = false) => {
if (!switched)
return position;
if (position === 'top')
return 'bottom';
if (position === 'left')
return 'right';
if (position === 'right')
return 'left';
if (position === 'bottom')
return 'top';
};
return ((0, jsx_runtime_1.jsx)(Append, Object.assign({ open: open, portal: portal, anchor: anchor, anchorElement: anchorElement, position: position, alignment: alignment, switch: switch_, inset: inset, parent: parent, padding: [8, 8] }, AppendProps, { element: items => {
var _a;
const rtl = theme.direction === 'rtl';
const switched = items.values.switch;
if (!rtl) {
styles.nameRoot.transformOrigin = (!switched ? transformOrigin : transformOriginSwitch) || transformOrigin;
}
else {
styles.nameRoot.transformOrigin = (!switched ? transformOriginRtl : transformOriginRtlSwitch) || transformOriginRtl;
}
if (!styles.nameRoot.transformOrigin)
styles.nameRoot.transformOrigin = transformOrigin;
return ((0, jsx_runtime_1.jsx)(Modal, Object.assign({ ref: item => {
if (ref) {
if ((0, utils_1.is)('function', ref))
ref(item);
else
ref.current = item;
}
items.ref.current = item;
}, open: open }, (interactive && {
onMouseEnter,
onTouchStart,
onMouseLeave,
onTouchEnd
}), { modalWrapper: false, portal: false, background: false, freezeScroll: false, focus: false, disableKeyboardClose: true }, ModalProps, { className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Tooltip', theme) && [
'amaui-Tooltip-root',
open && `amaui-Button-open`,
touch && 'amaui-Tooltip-touch',
longPress && 'amaui-Tooltip-long-press',
hover && 'amaui-Tooltip-hover',
focus && 'amaui-Tooltip-focus',
`amaui-Tooltip-position-${position}`
],
className,
classes.root,
ModalProps === null || ModalProps === void 0 ? void 0 : ModalProps.className,
switched && classNameSwitch
]), style: Object.assign(Object.assign(Object.assign({}, items.style), style), ModalProps === null || ModalProps === void 0 ? void 0 : ModalProps.style) }, other, { children: (0, jsx_runtime_1.jsx)(TransitionComponent, Object.assign({ in: inProp, onExited: onClose, add: true, removeOnExited: true }, TransitionComponentProps, { children: (0, jsx_runtime_1.jsx)("div", Object.assign({ role: 'tooltip', "aria-label": (0, utils_1.is)('simple', label) ? label : undefined, className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Tooltip', theme) && [
'amaui-Tooltip-name-root'
],
classes.nameRoot,
classes[`nameRoot_position_${resolvePosition(items.values.switch)}`],
classes[`maxWidth_${maxWidth}`],
noMargin && classes.nameRoot_noMargin,
fullWidth && classes.fullWidth,
nowrap && classes.nowrap
]), style: styles.nameRoot }, { children: (0, utils_1.is)('simple', label) ?
(0, jsx_runtime_1.jsx)(Surface, Object.assign({ tonal: tonal, color: color }, LabelProps, { className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Tooltip', theme) && [
'amaui-Tooltip-name'
],
LabelProps === null || LabelProps === void 0 ? void 0 : LabelProps.className,
classes.name,
arrow && [
classes.arrow,
classes[`arrow_position_${position}_alignment_${alignment}`]
]
]), style: Object.assign(Object.assign({}, styles.name), LabelProps === null || LabelProps === void 0 ? void 0 : LabelProps.style) }, { children: (0, jsx_runtime_1.jsx)("span", Object.assign({ className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Tooltip', theme) && [
'amaui-Tooltip-name-text'
],
]) }, { children: label })) })) :
react_1.default.cloneElement(label, {
className: (0, style_react_1.classNames)([
(_a = label === null || label === void 0 ? void 0 : label.props) === null || _a === void 0 ? void 0 : _a.className,
arrow && [
classes.arrow,
classes[`arrow_position_${position}_alignment_${alignment}`]
]
])
}) })) })) })));
} }, { children: children && (react_1.default.cloneElement(children, Object.assign(Object.assign({ onMouseMove }, (click && {
onClick: (event) => {
var _a, _b;
if ((0, utils_1.is)('function', (_a = children === null || children === void 0 ? void 0 : children.props) === null || _a === void 0 ? void 0 : _a.onClick))
(_b = children === null || children === void 0 ? void 0 : children.props) === null || _b === void 0 ? void 0 : _b.onClick(event);
onClick(event);
}
})), { onFocus,
onBlur,
onMouseDown,
onMouseUp,
onMouseEnter,
onMouseLeave,
onTouchStart,
onTouchEnd }))) })));
});
Tooltip.displayName = 'amaui-Tooltip';
exports.default = Tooltip;
;