chayns-components
Version:
A set of beautiful React components for developing chayns® applications.
141 lines (137 loc) • 5.59 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _clsx = _interopRequireDefault(require("clsx"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = _interopRequireWildcard(require("react"));
var _TappPortal = _interopRequireDefault(require("../../react-chayns-tapp_portal/component/TappPortal"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/**
* @component
*/
let currentId = 0;
/**
* A floating bubble that is primarily used to power the `ContextMenu` and
* `Tooltip` components.
*/
const Bubble = /*#__PURE__*/_react.default.forwardRef((_ref, ref) => {
let {
position = Bubble.position.TOP_LEFT,
parent,
topDivStyle,
onMouseEnter,
onMouseLeave,
className,
children,
style,
coordinates
} = _ref;
const bubbleRef = (0, _react.useRef)();
const [key] = (0, _react.useState)(() => `cc__bubble${++currentId}`);
const timeoutRef = (0, _react.useRef)();
(0, _react.useEffect)(() => () => clearTimeout(timeoutRef.current), []);
const [isActive, setIsActive] = (0, _react.useState)(false);
const [isHidden, setIsHidden] = (0, _react.useState)(true);
(0, _react.useImperativeHandle)(ref, () => ({
show() {
setIsHidden(false);
clearTimeout(timeoutRef.current);
timeoutRef.current = setTimeout(() => setIsActive(true));
},
hide() {
setIsActive(false);
clearTimeout(timeoutRef.current);
timeoutRef.current = setTimeout(() => setIsHidden(true), 500);
}
}), []);
const bubbleClasses = (0, _clsx.default)(`cc__bubble cc__bubble--position${position}`, isActive && 'cc__bubble--active', isHidden && 'cc__bubble--hide');
const {
x,
y
} = coordinates;
return /*#__PURE__*/_react.default.createElement(_TappPortal.default, {
parent: parent
}, /*#__PURE__*/_react.default.createElement("div", {
className: bubbleClasses,
style: {
top: `${y}px`,
left: `${x}px`,
...topDivStyle
},
ref: bubbleRef,
onMouseEnter: onMouseEnter,
onMouseLeave: onMouseLeave,
key: key
}, /*#__PURE__*/_react.default.createElement("div", {
className: (0, _clsx.default)('cc__bubble__overlay', className),
style: style
}, children)));
});
Bubble.position = {
TOP_LEFT: 0,
BOTTOM_LEFT: 1,
BOTTOM_RIGHT: 2,
TOP_RIGHT: 3,
TOP_CENTER: 4,
BOTTOM_CENTER: 5
};
Bubble.isPositionBottom = position => {
const {
BOTTOM_LEFT,
BOTTOM_CENTER,
BOTTOM_RIGHT
} = Bubble.position;
return [BOTTOM_LEFT, BOTTOM_CENTER, BOTTOM_RIGHT].includes(position);
};
Bubble.propTypes = {
/**
* The children that will be rendered inside of the bubble.
*/
children: _propTypes.default.node,
/**
* A classname string that will be set on the children wrapper element.
*/
className: _propTypes.default.string,
/**
* A React style object that will be applied to the children wrapper
* element.
*/
style: _propTypes.default.PropTypes.objectOf(_propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number])),
/**
* This specifies where the bubble will appear relative to its coordinates.
* Possible values are: `0` for top left, `1` for bottom left, `2` for bottom
* right, `3` for top right, `4` for top center and `5` for bottom center.
*/
position: _propTypes.default.oneOf([0, 1, 2, 3, 4, 5]),
/**
* A DOM element into which the `Bubble`-component will render.
*/
parent: typeof Element !== 'undefined' ? _propTypes.default.instanceOf(Element) : () => {},
/**
* The coordinates where the Bubble will point to. Is provided in an object
* format that should look like this: `{ x: <number>, y: <number> }`
*/
coordinates: _propTypes.default.shape({
x: _propTypes.default.number.isRequired,
y: _propTypes.default.number.isRequired
}),
/**
* A callback that will be called when the mouse enters the Bubble.
*/
onMouseEnter: _propTypes.default.func,
/**
* A callback that will be called when the mouse leaves the Bubble.
*/
onMouseLeave: _propTypes.default.func,
/**
* A React style object that will be supplied to the outer most element of
* the Bubble.
*/
topDivStyle: _propTypes.default.objectOf(_propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number]))
};
Bubble.displayName = 'Bubble';
var _default = Bubble;
exports.default = _default;
//# sourceMappingURL=Bubble.js.map