@chayns-components/core
Version:
A set of beautiful React components for developing your own applications with chayns.
183 lines (182 loc) • 6.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useMeasuredClone = exports.useIsMeasuredClone = exports.useIsInsideDialog = exports.useElementSize = void 0;
var _react = _interopRequireWildcard(require("react"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const isSameRect = (a, b) => {
if (!a || !b) return false;
return a.width === b.width && a.height === b.height && a.x === b.x && a.y === b.y;
};
const useElementSize = (ref, {
shouldUseChildElement = false,
shouldUseParentElement = false
} = {}) => {
const [size, setSize] = (0, _react.useState)();
(0, _react.useEffect)(() => {
let target = ref.current;
if (shouldUseParentElement) {
var _ref$current;
target = ((_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.parentElement) ?? null;
}
if (shouldUseChildElement) {
var _ref$current2;
target = (_ref$current2 = ref.current) === null || _ref$current2 === void 0 ? void 0 : _ref$current2.firstElementChild;
}
if (!target) return undefined;
let frameId;
const updateSize = nextSize => {
if (frameId) {
window.cancelAnimationFrame(frameId);
}
frameId = window.requestAnimationFrame(() => {
setSize(currentSize => isSameRect(currentSize, nextSize) ? currentSize : nextSize);
});
};
updateSize(target.getBoundingClientRect());
const observer = new ResizeObserver(([entry]) => {
if (!entry || entry.target !== target) return;
updateSize(entry.contentRect);
});
observer.observe(target);
return () => {
if (frameId) {
window.cancelAnimationFrame(frameId);
}
observer.disconnect();
};
}, [ref, shouldUseChildElement, shouldUseParentElement]);
return size;
};
exports.useElementSize = useElementSize;
const cloneWithTabIndex = node => {
if (! /*#__PURE__*/(0, _react.isValidElement)(node)) return node;
const element = node;
if (element.type === _react.default.Fragment) {
const children = _react.Children.map(element.props.children, cloneWithTabIndex);
return /*#__PURE__*/(0, _react.cloneElement)(element, {
...element.props,
children
});
}
const children = element.props.children ? _react.Children.map(element.props.children, cloneWithTabIndex) : element.props.children;
if (element.type.displayName === 'Button') {
return (
/*#__PURE__*/
// eslint-disable-next-line react/button-has-type
_react.default.createElement("button", {
tabIndex: -1
}, children)
);
}
return /*#__PURE__*/(0, _react.cloneElement)(element, {
...element.props,
tabIndex: -1,
children
});
};
const getClonedElement = content => {
const preventEvents = {
onClick: e => e.stopPropagation(),
onMouseDown: e => e.stopPropagation(),
onMouseUp: e => e.stopPropagation(),
onKeyDown: e => e.stopPropagation(),
onKeyUp: e => e.stopPropagation(),
onFocus: e => e.stopPropagation(),
onBlur: e => e.stopPropagation()
};
if (typeof content === 'string') {
return /*#__PURE__*/_react.default.createElement("span", {
tabIndex: -1,
"data-measured-clone": true
}, content);
}
if (/*#__PURE__*/(0, _react.isValidElement)(content)) {
return cloneWithTabIndex(/*#__PURE__*/(0, _react.cloneElement)(content, {
...preventEvents,
'data-measured-clone': true
}));
}
return content;
};
const useMeasuredClone = ({
content,
shouldPreventTextWrapping = true
}) => {
const ref = (0, _react.useRef)(null);
const [size, setSize] = (0, _react.useState)({
width: 0,
height: 0
});
const clonedElement = getClonedElement(content);
(0, _react.useEffect)(() => {
const measure = () => {
if (!ref.current) return;
const {
offsetWidth: width,
offsetHeight: height
} = ref.current;
setSize({
width: width + (shouldPreventTextWrapping ? 10 : 0),
height
});
};
measure();
const observer = new ResizeObserver(measure);
if (ref.current) observer.observe(ref.current);
return () => observer.disconnect();
}, [shouldPreventTextWrapping]);
const measuredElement = /*#__PURE__*/_react.default.createElement("div", {
"data-measured-clone": "true",
ref: ref,
style: {
position: 'fixed',
opacity: 0,
pointerEvents: 'none',
userSelect: 'none',
zIndex: -1,
height: 'auto',
width: 'auto',
visibility: 'hidden'
},
inert: "true",
tabIndex: -1
}, clonedElement);
return {
measuredElement,
width: size.width,
height: size.height
};
};
exports.useMeasuredClone = useMeasuredClone;
const useIsMeasuredClone = () => {
const ref = (0, _react.useRef)(null);
const [isClone, setIsClone] = (0, _react.useState)(false);
(0, _react.useEffect)(() => {
if (!ref.current) return;
let el = ref.current;
while (el) {
if (el.hasAttribute('data-measured-clone')) {
setIsClone(true);
return;
}
el = el.parentElement;
}
setIsClone(false);
}, []);
return [isClone, ref];
};
exports.useIsMeasuredClone = useIsMeasuredClone;
const useIsInsideDialog = ref => {
const [isInsideDialog, setIsInsideDialog] = (0, _react.useState)(false);
(0, _react.useEffect)(() => {
if (!ref.current) {
return;
}
setIsInsideDialog(ref.current.closest('.dialog-inner') !== null);
}, [ref]);
return isInsideDialog;
};
exports.useIsInsideDialog = useIsInsideDialog;
//# sourceMappingURL=element.js.map