Tooltip
Description
Tooltips display informative text when users hover over or focus on an element.
Installation
yarn add @commercetools-uikit/tooltip
npm --save install @commercetools-uikit/tooltip
Additionally install the peer dependencies (if not present)
yarn add react
npm --save install react
Usage
import { forwardRef } from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import styled from '@emotion/styled';
import Tooltip from '@commercetools-uikit/tooltip';
const ExampleStandard = () => (
<Tooltip
placement="left"
title="If you buy a pizza, you will also get a free ice cream :)"
>
<button onClick={() => {}}>Submit</button>
</Tooltip>
);
const ExampleWithDisabledElements = () => (
<Tooltip
placement="left"
title="You do not have permission to delete the database"
>
<button disabled onClick={() => {}} style={{ pointerEvents: 'none' }}>
Delete production database
</button>
</Tooltip>
);
const Wrapper = forwardRef((props, ref) => (
<div ref={ref} style={{ display: 'block' }} {...props}>
{props.children}
</div>
));
Wrapper.propTypes = {
children: PropTypes.node.isRequired,
};
const FullWidthButton = styled.button`
display: block;
width: 100%;
`;
const ExampleWithCustomWrapper = () => (
<Tooltip title="Delete" components={{ WrapperComponent: Wrapper }}>
<FullWidthButton>Submit</FullWidthButton>
</Tooltip>
);
const Body = styled.div`
color: red;
`;
const ExampleWithCustomBody = () => (
<Tooltip title="Delete" components={{ BodyComponent: Body }}>
<button>Submit</button>
</Tooltip>
);
const Portal = (props) => ReactDOM.createPortal(props.children, document.body);
const ExampleWithCustomPortal = () => (
<Tooltip title="Delete" components={{ TooltipWrapperComponent: Portal }}>
<button>Submit</button>
</Tooltip>
);
const ExampleWithConditionals = (props) => (
<Tooltip
off={props.isDisabled}
title="You do not have permission to perform this action"
>
<button disabled={props.isDisabled}>Submit</button>
</Tooltip>
);
ExampleWithConditionals.propTypes = {
isDisabled: PropTypes.bool,
};
const ExampleWithCustomPopperBehavior = (props) => (
<Tooltip
placement="left"
title="I will always be on the left side"
modifiers={{
preventOverflow: {
enabled: false,
},
flip: {
enabled: false,
},
}}
>
<button disabled={props.isDisabled}>Submit</button>
</Tooltip>
);
ExampleWithCustomPopperBehavior.propTypes = {
isDisabled: PropTypes.bool,
};
export {
ExampleStandard,
ExampleWithDisabledElements,
ExampleWithCustomWrapper,
ExampleWithCustomBody,
ExampleWithCustomPortal,
ExampleWithConditionals,
ExampleWithCustomPopperBehavior,
};
Properties
Props | Type | Required | Default | Description |
---|
children | ReactElement | ✅ | | |
showAfter | number | | 300 | Delay (in milliseconds) between the start of the user interaction, and showing the tooltip. |
closeAfter | number | | 200 | Delay (in milliseconds) between the end of the user interaction, and the closing of the tooltip. |
styles | Record | | | Custom css-in-js object styles for the tooltip body. |
off | boolean | | false | Determines if the tooltip should not appear. |
id | string | | | An identifier for the tooltip, used for aria-describedby . |
onClose | Function See signature. | | | A callback function, called when the tooltip is closing. |
onOpen | Function See signature. | | | A callback function, called when the tooltip is opening. |
isOpen | boolean | | | |
placement | union Possible values:
, 'top', 'top-start', 'top-end', 'right', 'right-start', 'right-end', 'bottom', 'bottom-start', 'bottom-end', 'left', 'left-start', 'left-end' | | 'top' | How the tooltip is positioned relative to the child element. |
title | string | ✅ | | The message to show in the tooltip. |
modifiers | Modifiers | | | Provides a way to fine-tune an appearance of underlying Popper tooltip element. For more information, please check Popper.js documentation. |
components | Object See signature. | | | Customize the appearance of certain elements of the tooltip. |
horizontalConstraint | union Possible values:
, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 'scale', 'auto' | | 'scale' | Horizontal size limit of the tooltip. |
Signatures
Signature onClose
(e?: ChangeEvent | FocusEvent) => void
Signature onOpen
(e?: ChangeEvent | FocusEvent) => void
Signature components
{
BodyComponent?: ComponentType;
TooltipWrapperComponent?: ComponentType;
WrapperComponent?: ComponentType;
}