androidx.compose.foundation.text.contextmenu.modifier

Extension functions summary

Modifier

Adds a builder to be run when the text context menu is shown within this hierarchy.

Cmn
Modifier

Adds a filter to be run when the text context menu is shown within this hierarchy.

Cmn

Extension functions

addTextContextMenuComponents

fun Modifier.addTextContextMenuComponents(builder: TextContextMenuBuilderScope.() -> Unit): Modifier

Adds a builder to be run when the text context menu is shown within this hierarchy.

When there are multiple instances of this modifier in a layout hierarchy, the builders are applied in order from bottom to top. They are then filtered by every Modifier.filterTextContextMenuComponents in the hierarchy.

import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.contextmenu.builder.item
import androidx.compose.foundation.text.contextmenu.modifier.addTextContextMenuComponents
import androidx.compose.foundation.text.input.clearText
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.ui.Modifier

val textFieldState = rememberTextFieldState()
BasicTextField(
    state = textFieldState,
    modifier =
        Modifier.addTextContextMenuComponents {
            separator()
            item(key = ClearKeyDataObject, label = "Clear") {
                textFieldState.clearText()
                close()
            }
            separator()
        },
)
Parameters
builder: TextContextMenuBuilderScope.() -> Unit

a snapshot-aware builder function for adding components to the context menu. In this function you can use member functions from the receiver TextContextMenuBuilderScope, such as separator(), to add components. The item function is not in the common source set, but is instead defined as an extension function in the platform specific source sets.

filterTextContextMenuComponents

fun Modifier.filterTextContextMenuComponents(
    filter: (TextContextMenuComponent) -> Boolean
): Modifier

Adds a filter to be run when the text context menu is shown within this hierarchy.

filter will not be passed TextContextMenuSeparator, as they pass by default.

filters added via this modifier will always run after every builder added via Modifier.addTextContextMenuComponents. When there are multiple instances of this modifier in a layout hierarchy, every filter must pass in order for a context menu to be shown. They are always applied after all Modifier.addTextContextMenuComponents have been applied, but the order in which they run should not be depended on.

import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.contextmenu.modifier.filterTextContextMenuComponents
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.ui.Modifier

val textFieldState = rememberTextFieldState()
BasicTextField(
    state = textFieldState,
    modifier =
        Modifier.filterTextContextMenuComponents(
            filter = { component -> component.key === ClearKeyDataObject }
        ),
)
Parameters
filter: (TextContextMenuComponent) -> Boolean

a snapshot-aware lambda that determines whether a TextContextMenuComponent should be included in the context menu.