UNPKG

@metamask/design-system-react-native

Version:
110 lines 5.04 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 design_system_twrnc_preset_1 = require("@metamask/design-system-twrnc-preset"); const react_1 = __importStar(require("react")); const react_native_1 = require("react-native/index.js"); const types_1 = require("../../types/index.cjs"); const BadgeWrapper = ({ children, childrenContainerProps, badge, badgeContainerProps, position = types_1.BadgeWrapperPosition.BottomRight, positionAnchorShape = types_1.BadgeWrapperPositionAnchorShape.Circular, positionXOffset = 0, positionYOffset = 0, customPosition, twClassName = '', style, ...props }) => { const tw = (0, design_system_twrnc_preset_1.useTailwind)(); 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); // Fetching the dimensions of the anchor and bagde element to properly position the badge const getAnchorSize = (0, react_1.useCallback)((event) => { const { width, height } = event.nativeEvent.layout; setAnchorWidth(width); setAnchorHeight(height); }, []); const getBadgeSize = (0, react_1.useCallback)((event) => { const { width, height } = event.nativeEvent.layout; setbadgeWidth(width); setbadgeHeight(height); }, []); const finalPositions = (0, react_1.useMemo)(() => { if (customPosition) { return customPosition; } // 0.1464 is a mathematical coeeficient to move // from a 0,0 corner of a rectangular shape to the closest "corner" // of a circular shape anchor element const anchorShapeXOffset = positionAnchorShape === types_1.BadgeWrapperPositionAnchorShape.Rectangular ? 0 : anchorWidth * 0.1464; const anchorShapeYOffset = positionAnchorShape === types_1.BadgeWrapperPositionAnchorShape.Rectangular ? 0 : anchorHeight * 0.1464; // This is to center the badge in the corner of the anchor element 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.BottomLeft: return { bottom: finalYOffset, left: finalXOffset, }; case types_1.BadgeWrapperPosition.TopLeft: return { top: finalYOffset, left: finalXOffset, }; case types_1.BadgeWrapperPosition.BottomRight: default: return { bottom: finalYOffset, right: finalXOffset, }; } }, [ position, positionAnchorShape, anchorWidth, anchorHeight, badgeWidth, badgeHeight, positionXOffset, positionYOffset, customPosition, ]); return (<react_native_1.View style={[tw `relative self-start ${twClassName}`, style]} {...props}> <react_native_1.View onLayout={getAnchorSize} {...childrenContainerProps}> {children} </react_native_1.View> <react_native_1.View onLayout={getBadgeSize} style={[tw `absolute`, { ...finalPositions }]} {...badgeContainerProps}> {badge} </react_native_1.View> </react_native_1.View>); }; exports.BadgeWrapper = BadgeWrapper; //# sourceMappingURL=BadgeWrapper.cjs.map