Skip to content

Commit

Permalink
remove unneccesary function, simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
LucioChavezFuentes committed Apr 25, 2022
1 parent 3b6048e commit b66427b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/components/Tooltip/TooltipRenderedOnPageBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
30 changes: 10 additions & 20 deletions src/styles/getTooltipStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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}
*/
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit b66427b

Please sign in to comment.