From b66427b22296134d5698a001fad2426e156bd0e9 Mon Sep 17 00:00:00 2001 From: LucioChavezFuentes Date: Mon, 25 Apr 2022 22:17:54 +0200 Subject: [PATCH] remove unneccesary function, simplify code --- .../Tooltip/TooltipRenderedOnPageBody.js | 2 +- src/styles/getTooltipStyles.js | 30 +++++++------------ 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/src/components/Tooltip/TooltipRenderedOnPageBody.js b/src/components/Tooltip/TooltipRenderedOnPageBody.js index f3024366681b..4c44cdc68a52 100644 --- a/src/components/Tooltip/TooltipRenderedOnPageBody.js +++ b/src/components/Tooltip/TooltipRenderedOnPageBody.js @@ -53,7 +53,7 @@ class TooltipRenderedOnPageBody extends React.PureComponent { super(props); this.state = { // tooltipTextWidth will update with wrapped text offsetWidth based on maxWidth prop - tooltipTextWidth: undefined, + tooltipTextWidth: this.props.maxWidth, // The width and height of the tooltip itself tooltipWidth: 0, diff --git a/src/styles/getTooltipStyles.js b/src/styles/getTooltipStyles.js index 36c6914a444c..3adaa5aecc1e 100644 --- a/src/styles/getTooltipStyles.js +++ b/src/styles/getTooltipStyles.js @@ -49,24 +49,6 @@ function computeHorizontalShift(windowWidth, xOffset, componentWidth, tooltipWid return 0; } -/** - * Generate wrapper's width using text offsetWidth based on maxWidth prop - * - * @param {Number} maxWidth - Max-width for tooltip's wrapper. - * @param {Number} [tooltipTextWidth] - Tooltip's inner text width. - * @returns {Object} - */ -function getWrapperWidth(maxWidth, tooltipTextWidth) { - if (tooltipTextWidth && tooltipTextWidth < maxWidth) { - return { - // Add horizontal padding to the text width to get the wrapper width. - // TooltipTextWidth ignores the fractions (OffsetWidth) so add 1px to fit the text properly. - width: tooltipTextWidth + (spacing.ph2.paddingHorizontal * 2) + 1, - }; - } - return {width: maxWidth}; -} - /** * Generate styles for the tooltip component. * @@ -85,7 +67,7 @@ function getWrapperWidth(maxWidth, tooltipTextWidth) { * and a negative value shifts it to the left. * @param {Number} [manualShiftVertical] - Any additional amount to manually shift the tooltip up or down. * A positive value shifts it down, and a negative value shifts it up. - * @param {Number} [tooltipTextWidth] - Tooltip's inner text width. + * @param {Number} tooltipTextWidth - Tooltip's inner text width. * @param {Number} maxWidth - Max-width for tooltip's wrapper. * @returns {Object} */ @@ -131,7 +113,15 @@ export default function getTooltipStyles( ...tooltipVerticalPadding, ...spacing.ph2, zIndex: variables.tooltipzIndex, - ...getWrapperWidth(maxWidth, tooltipTextWidth), + + // Generate wrapper's width using text's offsetWidth based on maxWidth prop + width: tooltipTextWidth < maxWidth + + // Add horizontal padding to the text width to get the wrapper width. + // TooltipTextWidth ignores the fractions (OffsetWidth) so add 1px to fit the text properly. + ? tooltipTextWidth + (spacing.ph2.paddingHorizontal * 2) + 1 + + : maxWidth, // Because it uses fixed positioning, the top-left corner of the tooltip is aligned // with the top-left corner of the window by default.