TooltipBox
Tooltip (TooltipBox
)
Composable wrapping a UI trigger (e.g. a button) and displaying a tooltip when the user hovers over or focuses on the UI trigger.
Tooltips display additional information upon hover or focus that is contextual, helpful, and nonessential while providing the ability to communicate and give clarity to a user.
Focus interaction
As stated by the documentation of Carbon's Tooltip component, tooltips must be triggered by a hover or focus interaction. This composable supports the hover interaction by default, as it's using BasicTooltipBox as the underlying implementation but does not support the focus interaction. However, TooltipBox supports it when a shared MutableInteractionSource is provided to both the UI trigger and itself:
val uiTriggerMutableInteractionSource = remember { MutableInteractionSource() }
TooltipBox(
tooltipText = "To be, or not to be...",
uiTriggerMutableInteractionSource = uiTriggerMutableInteractionSource,
) {
MyUiTrigger(
text = "Hover me",
modifier = Modifier.clickable(
interactionSource = uiTriggerMutableInteractionSource,
indication = null,
onClick = {}
)
)
}
Writing this for every UI trigger that needs the focus interaction can be cumbersome, but most common UI triggers from Carbon provides alternative APIs to ease its usage. For example, the com.gabrieldrn.carbon.button.Button composable has an alt API requiring a tooltipParameters
parameter:
Button(
label = "Hover me",
tooltipParameters = TooltipParameters(
text = "To be, or not to be..."
singleLine = true,
),
onClick = {}
)
Automatic placement
Carbon's documentation states that Tooltips have an automatic placement: "tooltips can detect the edges of the browser to be placed in view so the container does not get cutoff". However, this is currently not possible with the foundation elements of Compose as it would require to make multiple layout passes and sacrifice performance. As a result, it is up to consuming projects to decide the placement of tooltips.
Parameters
Text to display in the tooltip.
Default Modifier to be applied to the whole composable.
Either the text in the tooltip should be displayed in a single line or not. Note that the tooltip width is limited to a maximum width depending on this parameter and the text might be truncated if it exceeds the maximum width.
Placement of the tooltip relative to the UI trigger. Defaults to PopoverCaretTipPlacement.Top.
Alignment of the tooltip relative to the UI trigger. Defaults to PopoverCaretTipAlignment.Center.
A shared MutableInteractionSource that will be used to track the focus interactions of the UI trigger.
UI trigger content that will be wrapped by the tooltip. This is typically a button or an icon that the user can hover over or long-press to see the tooltip.
Tooltip (TooltipBox
)
Convenience composable for TooltipBox that takes a TooltipParameters object as a parameter.
Composable wrapping a UI trigger (e.g. a button) and displaying a tooltip when the user hovers over or focuses on the UI trigger.
Tooltips display additional information upon hover or focus that is contextual, helpful, and nonessential while providing the ability to communicate and give clarity to a user.
Parameters
Object containing the required parameters for the tooltip.
A shared MutableInteractionSource that will be used to track the focus interactions of the UI trigger.
Default Modifier to be applied to the whole composable.
UI trigger content that will be wrapped by the tooltip. This is typically a button or an icon that the user can hover over or long-press to see the tooltip.