🌈 Create new beautiful Compose Gradients with our new wesite ->
Function

traverseAncestors

Executes block for all ancestors with a matching key.

traverseAncestorsWithKeyDemo

/**
 * TraversableNode example that does not actually do anything but shows the most simplified example.
 *
 * The traversable functions are separated below (for example, traverseAncestorsWithKeyDemo), so
 * they can be referenced in sample javadocs.
 *
 * For a full featured sample, look below at [TraverseModifierDemo].
 */
class CustomTraversableModifierNode : Modifier.Node(), TraversableNode {
    override val traverseKey = TRAVERSAL_NODE_KEY

    fun doSomethingWithAncestor() {}

    fun doSomethingWithChild() {}

    fun doSomethingWithDescendant() {}
}

/**
 * Simplified example of traverseAncestors with a key. For a full featured sample, look below at
 * [TraverseModifierDemo].
 */
@Sampled
fun traverseAncestorsWithKeyDemo() {
    val customTraversableModifierNode = CustomTraversableModifierNode()

    with(customTraversableModifierNode) {
        traverseAncestors(traverseKey) {
            if (it is CustomTraversableModifierNode) {
                it.doSomethingWithAncestor()
            }
            // Return true to continue searching the tree after a match. If you were looking to
            // match only some of the nodes, you could return false and stop executing the search.
            true
        }
    }
}

/**
 * Simplified example of traverseAncestors. For a full featured sample, look below at
 * [TraverseModifierDemo].
 */

traverseAncestorsDemo

@Sampled
fun traverseAncestorsDemo() {
    val customTraversableModifierNode = CustomTraversableModifierNode()

    with(customTraversableModifierNode) {
        traverseAncestors {
            // Because I use the existing key of the class, I can guarantee 'it' will be of the same
            // type as the class, so I can call my functions directly.
            it.doSomethingWithAncestor()

            // Return true to continue searching the tree after a match. If you were looking to
            // match only some of the nodes, you could return false and stop executing the search.
            true
        }
    }
}

/**
 * Simplified example of traverseChildren with a key. For a full featured sample, look below at
 * [TraverseModifierDemo].
 */