Skip to content

Commit

Permalink
fix: tooltip custom class (react) (#2012)
Browse files Browse the repository at this point in the history
* fix: add custom class prop

* docs: add custom class example

* fix: hello linter my old friend
  • Loading branch information
pixelflips authored Oct 21, 2024
1 parent 36c50d9 commit 2fe5d18
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
8 changes: 5 additions & 3 deletions packages/sage-react/lib/Tooltip/Tooltip.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import React, { Children, useState, cloneElement } from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import { TooltipElement } from './TooltipElement';
import {
TOOLTIP_POSITIONS
} from './configs';
import { TOOLTIP_POSITIONS } from './configs';

export const Tooltip = ({
children,
content,
position,
tooltipCustomClass,
...rest
}) => {
const [active, setActive] = useState(false);
Expand Down Expand Up @@ -38,6 +37,7 @@ export const Tooltip = ({
content={content}
parentDomRect={parentDomRect}
position={position}
tooltipCustomClass={tooltipCustomClass}
/>,
document.body
)}
Expand All @@ -50,10 +50,12 @@ Tooltip.POSITIONS = TOOLTIP_POSITIONS;

Tooltip.defaultProps = {
position: TOOLTIP_POSITIONS.DEFAULT,
tooltipCustomClass: '',
};

Tooltip.propTypes = {
children: PropTypes.node.isRequired,
content: PropTypes.oneOfType([PropTypes.node, PropTypes.string]).isRequired,
position: PropTypes.oneOf(Object.values(TOOLTIP_POSITIONS)),
tooltipCustomClass: PropTypes.string,
};
26 changes: 26 additions & 0 deletions packages/sage-react/lib/Tooltip/Tooltip.story.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,29 @@ export const Default = Template.bind({});
export const Static = () => (
<Tooltip.Element content="Testing static tooltip" />
);

export const CustomClass = Template.bind({});
CustomClass.args = {
children: <Button>Button</Button>,
content: 'This content and sizing is styled with the applied custom class. Use at your own risk',
position: Tooltip.POSITIONS.DEFAULT,
tooltipCustomClass: 'custom-tooltip-class',
};

CustomClass.decorators = [
(Story) => (
<>
<style>
{`
.custom-tooltip-class {
color: #ff3e15;
height: 200px;
max-width: 800px;
width: 400px;
}
`}
</style>
<Story />
</>
),
];
8 changes: 5 additions & 3 deletions packages/sage-react/lib/Tooltip/TooltipElement.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import React, { useState, useRef, useLayoutEffect } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import {
TOOLTIP_POSITIONS
} from './configs';
import { TOOLTIP_POSITIONS } from './configs';

/* eslint-disable react-hooks/exhaustive-deps */

Expand All @@ -13,12 +11,14 @@ export const TooltipElement = ({
content,
parentDomRect,
position,
tooltipCustomClass,
}) => {
const tooltipRef = useRef(null);
const [coordinates, setCoordinates] = useState({ top: null, left: null });

const classNames = classnames(
'sage-tooltip',
tooltipCustomClass,
{
'sage-tooltip--static': !parentDomRect,
[`sage-tooltip--${position}`]: position,
Expand Down Expand Up @@ -86,6 +86,7 @@ TooltipElement.POSITIONS = TOOLTIP_POSITIONS;
TooltipElement.defaultProps = {
parentDomRect: null,
position: TOOLTIP_POSITIONS.DEFAULT,
tooltipCustomClass: '',
};

TooltipElement.propTypes = {
Expand All @@ -98,4 +99,5 @@ TooltipElement.propTypes = {
width: PropTypes.number,
}),
position: PropTypes.oneOf(Object.values(TOOLTIP_POSITIONS)),
tooltipCustomClass: PropTypes.string,
};

0 comments on commit 2fe5d18

Please sign in to comment.