A tooltip component for displaying helpful information on hover, focus, or click.
Import
import { DTooltip } from '@dynamic-framework/ui-react';
TypeScript Interface
export type Props = BaseProps & PropsWithChildren<{
childrenClassName?: string;
offSet?: number;
padding?: number;
withHover?: boolean;
withFocus?: boolean;
withClick?: boolean;
open?: boolean;
size?: ComponentSize;
placement?: Placement;
Component: ReactNode;
}>;
type ComponentSize = 'sm' | 'lg';
type Placement =
| 'top'
| 'top-start'
| 'top-end'
| 'right'
| 'right-start'
| 'right-end'
| 'bottom'
| 'bottom-start'
| 'bottom-end'
| 'left'
| 'left-start'
| 'left-end';
type BaseProps = {
style?: CSSProperties;
className?: string;
dataAttributes?: DataAttributes;
};
Props
The element that triggers the tooltip (what the user hovers over or clicks).
The content to display inside the tooltip.
CSS classes to apply to the trigger element wrapper.
Distance in pixels between the tooltip and the trigger element.
Padding in pixels for the shift middleware to prevent tooltip from touching viewport edges.
If true, tooltip shows on hover.
If true, tooltip shows on focus.
If true, tooltip shows on click.
Initial open state of the tooltip.
Size variant of the tooltip.
Preferred placement of the tooltip relative to the trigger element. Options include 'top', 'right', 'bottom', 'left', and their -start and -end variants.
Additional CSS classes to apply to the tooltip.
Inline styles to apply to the tooltip.
Usage Examples
import { DTooltip, DButton } from '@dynamic-framework/ui-react';
export default function Example() {
return (
<DTooltip Component={<DButton text="Hover me" />}>
This is a helpful tooltip!
</DTooltip>
);
}
Placement Variants
import { DTooltip, DButton } from '@dynamic-framework/ui-react';
export default function Example() {
return (
<div className="d-flex gap-3 justify-content-center align-items-center" style={{ minHeight: '200px' }}>
<DTooltip
placement="top"
Component={<DButton text="Top" size="sm" />}
>
Tooltip on top
</DTooltip>
<DTooltip
placement="right"
Component={<DButton text="Right" size="sm" />}
>
Tooltip on right
</DTooltip>
<DTooltip
placement="bottom"
Component={<DButton text="Bottom" size="sm" />}
>
Tooltip on bottom
</DTooltip>
<DTooltip
placement="left"
Component={<DButton text="Left" size="sm" />}
>
Tooltip on left
</DTooltip>
</div>
);
}
Click to Show
import { DTooltip, DButton } from '@dynamic-framework/ui-react';
export default function Example() {
return (
<DTooltip
withClick
withHover={false}
Component={<DButton text="Click me" />}
>
This tooltip appears on click!
</DTooltip>
);
}
Focus Trigger
import { DTooltip, DInput } from '@dynamic-framework/ui-react';
import { useState } from 'react';
export default function Example() {
const [value, setValue] = useState('');
return (
<DTooltip
withFocus
withHover={false}
Component={
<DInput
label="Username"
value={value}
onChange={setValue}
placeholder="Enter username"
/>
}
>
Username must be at least 3 characters
</DTooltip>
);
}
Size Variants
import { DTooltip, DButton } from '@dynamic-framework/ui-react';
export default function Example() {
return (
<div className="d-flex gap-3">
<DTooltip
size="sm"
Component={<DButton text="Small" size="sm" />}
>
Small tooltip
</DTooltip>
<DTooltip
Component={<DButton text="Default" />}
>
Default tooltip
</DTooltip>
<DTooltip
size="lg"
Component={<DButton text="Large" size="lg" />}
>
Large tooltip with more space
</DTooltip>
</div>
);
}
Custom Offset
import { DTooltip, DButton } from '@dynamic-framework/ui-react';
export default function Example() {
return (
<div className="d-flex gap-3">
<DTooltip
offSet={2}
Component={<DButton text="Close" size="sm" />}
>
Close tooltip
</DTooltip>
<DTooltip
offSet={20}
Component={<DButton text="Far" size="sm" />}
>
Far tooltip
</DTooltip>
</div>
);
}
With Icons
import { DTooltip } from '@dynamic-framework/ui-react';
export default function Example() {
return (
<div className="d-flex gap-3">
<DTooltip
Component={
<button className="btn btn-link p-0">
<i className="bi bi-info-circle" />
</button>
}
>
Click the info icon for more details
</DTooltip>
<DTooltip
Component={
<button className="btn btn-link p-0">
<i className="bi bi-question-circle" />
</button>
}
>
Need help? Contact support
</DTooltip>
</div>
);
}
Rich Content Tooltip
import { DTooltip, DButton } from '@dynamic-framework/ui-react';
export default function Example() {
return (
<DTooltip
size="lg"
Component={<DButton text="Hover for details" />}
>
<div>
<strong>Pro Tip:</strong>
<p className="mb-0 mt-1">You can use keyboard shortcuts to speed up your workflow.</p>
</div>
</DTooltip>
);
}
Multiple Triggers
import { DTooltip, DButton } from '@dynamic-framework/ui-react';
export default function Example() {
return (
<DTooltip
withHover
withClick
withFocus
Component={<DButton text="Hover, Click, or Focus" />}
>
This tooltip shows on hover, click, and focus!
</DTooltip>
);
}