@atlaskit/breadcrumbs
Version:
Breadcrumbs are a navigation system used to show a user's location in a site or app.
42 lines (38 loc) • 2.11 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = useOverflowable;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = require("react");
// eslint-disable-next-line @repo/internal/react/require-jsdoc
function useOverflowable(truncationWidth, buttonRefCurrent, iconWidthAllowance) {
var shouldCheckWidth = truncationWidth && buttonRefCurrent;
// Default to true to match hasOverflow = true default in Step component.
// This should ensure the icon never appears and then quickly disappears
var _useState = (0, _react.useState)(Boolean(shouldCheckWidth)),
_useState2 = (0, _slicedToArray2.default)(_useState, 2),
hasOverflow = _useState2[0],
setOverflow = _useState2[1];
var _useState3 = (0, _react.useState)(false),
_useState4 = (0, _slicedToArray2.default)(_useState3, 2),
showTooltip = _useState4[0],
setShowTooltip = _useState4[1];
// Need to recalculate on every render cycle as text/icons/width changing will change the outcome
(0, _react.useEffect)(function () {
if (shouldCheckWidth) {
// Calculate if the button width will be larger than the truncation width after allowing for icon widths.
// The button having a width greater than the truncationWidth, with icons, indicates the icons should be hidden to avoid going over the width limit
var shouldOverflow = buttonRefCurrent.clientWidth + iconWidthAllowance > truncationWidth;
// The button width can already be equal to the truncationWidth which is an indicator that truncation is occurring and a tooltip should be displayed
var shouldShowTooltip = buttonRefCurrent.clientWidth + iconWidthAllowance >= truncationWidth;
setOverflow(shouldOverflow);
setShowTooltip(shouldShowTooltip);
} else {
setOverflow(false);
setShowTooltip(false);
}
}, [truncationWidth, buttonRefCurrent, iconWidthAllowance, shouldCheckWidth]);
return [hasOverflow, showTooltip];
}