UNPKG

@metamask/design-system-react

Version:
92 lines 4.65 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BadgeWrapper = void 0; const react_1 = __importStar(require("react")); const types_1 = require("../../types/index.cjs"); const tw_merge_1 = require("../../utils/tw-merge.cjs"); exports.BadgeWrapper = (0, react_1.forwardRef)(({ children, childrenContainerProps, badge, badgeContainerProps, position = types_1.BadgeWrapperPosition.BottomRight, positionAnchorShape = types_1.BadgeWrapperPositionAnchorShape.Circular, positionXOffset = 0, positionYOffset = 0, customPosition, className = '', style, ...props }, ref) => { const [anchorWidth, setAnchorWidth] = (0, react_1.useState)(0); const [anchorHeight, setAnchorHeight] = (0, react_1.useState)(0); const [badgeWidth, setBadgeWidth] = (0, react_1.useState)(0); const [badgeHeight, setBadgeHeight] = (0, react_1.useState)(0); const anchorRef = (0, react_1.useRef)(null); const badgeRef = (0, react_1.useRef)(null); // Measure both elements once on mount (0, react_1.useLayoutEffect)(() => { if (anchorRef.current) { const { width, height } = anchorRef.current.getBoundingClientRect(); setAnchorWidth(width); setAnchorHeight(height); } if (badgeRef.current) { const { width, height } = badgeRef.current.getBoundingClientRect(); setBadgeWidth(width); setBadgeHeight(height); } }, []); // empty deps → run only after first render const finalPositions = (0, react_1.useMemo)(() => { if (customPosition) { return customPosition; } const anchorShapeXOffset = positionAnchorShape === types_1.BadgeWrapperPositionAnchorShape.Rectangular ? 0 : anchorWidth * 0.1464; const anchorShapeYOffset = positionAnchorShape === types_1.BadgeWrapperPositionAnchorShape.Rectangular ? 0 : anchorHeight * 0.1464; const badgeCenteringXOffset = badgeWidth / 2; const badgeCenteringYOffset = badgeHeight / 2; const finalXOffset = anchorShapeXOffset - badgeCenteringXOffset + positionXOffset; const finalYOffset = anchorShapeYOffset - badgeCenteringYOffset + positionYOffset; switch (position) { case types_1.BadgeWrapperPosition.TopRight: return { top: finalYOffset, right: finalXOffset }; case types_1.BadgeWrapperPosition.TopLeft: return { top: finalYOffset, left: finalXOffset }; case types_1.BadgeWrapperPosition.BottomLeft: return { bottom: finalYOffset, left: finalXOffset }; case types_1.BadgeWrapperPosition.BottomRight: default: return { bottom: finalYOffset, right: finalXOffset }; } }, [ position, positionAnchorShape, anchorWidth, anchorHeight, badgeWidth, badgeHeight, positionXOffset, positionYOffset, customPosition, ]); const containerClassName = (0, tw_merge_1.twMerge)('relative inline-flex self-start', className); return (react_1.default.createElement("div", { ref: ref, className: containerClassName, style: style, ...props }, react_1.default.createElement("div", { className: "inline-flex", ref: anchorRef, ...childrenContainerProps }, children), react_1.default.createElement("div", { ref: badgeRef, className: "absolute", style: finalPositions, ...badgeContainerProps }, badge))); }); exports.BadgeWrapper.displayName = 'BadgeWrapper'; //# sourceMappingURL=BadgeWrapper.cjs.map