feat(js): add text input and key input support (#14470) ba0755c07b
* feat(js): Add text input and key input feature for JS

* refining the implementation during testing. patching edge cases across devices. addresses positioning issues with proxy/overlay being position: absolute and scrolling into view

* console warn if canvas parent does not have a set position value which can cause overlay alignment issues when scrolled to

* Fix multiline paste, ensure proxy has pointer-events: none, and ensure aria-hidden is resepcted accurately

* add aria-describedby ID if present for the input proxy

* copilot fixes

* remove test about children under a text field

* ran simplify

* remove cut event listener for now to prevent keyInput side effect with backspace

Co-authored-by: Zachary Plata <zach@rive.app>
diff --git a/.rive_head b/.rive_head
index 4e00c48..e0af92f 100644
--- a/.rive_head
+++ b/.rive_head
@@ -1 +1 @@
-dcd60ea7f53e84aafd1b10001667d8946a2b8c85
+ba0755c07bebb845c10d2fb7cb565c9e0b161d78
diff --git a/include/rive/animation/state_machine_instance.hpp b/include/rive/animation/state_machine_instance.hpp
index 691ba7a..dc48238 100644
--- a/include/rive/animation/state_machine_instance.hpp
+++ b/include/rive/animation/state_machine_instance.hpp
@@ -285,6 +285,10 @@
     /// Returns true if the text was handled.
     bool textInput(const std::string& text);
 
+    /// The selected text of the focused element, via the active focus manager.
+    /// Empty when nothing with a selection is focused.
+    std::string selectedText() const;
+
     void clearDataContext();
     void relinkDataContext() override;
     void rebuildDataBind(DataBind*) override;
diff --git a/src/animation/state_machine_instance.cpp b/src/animation/state_machine_instance.cpp
index 04db14a..e0d6d1d 100644
--- a/src/animation/state_machine_instance.cpp
+++ b/src/animation/state_machine_instance.cpp
@@ -3742,3 +3742,13 @@
     auto* fm = focusManager();
     return fm->textInput(text);
 }
+
+std::string StateMachineInstance::selectedText() const
+{
+    const auto* fm = focusManager();
+    if (!fm)
+    {
+        return "";
+    }
+    return fm->selectedText();
+}
diff --git a/tests/unit_tests/runtime/text_input_test.cpp b/tests/unit_tests/runtime/text_input_test.cpp
index a869dc5..e85cfe9 100644
--- a/tests/unit_tests/runtime/text_input_test.cpp
+++ b/tests/unit_tests/runtime/text_input_test.cpp
@@ -971,6 +971,48 @@
 
     manager.setFocus(nullptr);
 }
+TEST_CASE("state machine selectedText reports the focused input's selection",
+          "[text_input]")
+{
+    auto file = ReadRiveFile("assets/text_input.riv");
+    auto artboard = file->artboardNamed("Text Input - Multiline");
+    REQUIRE(artboard != nullptr);
+    auto stateMachine = artboard->stateMachineAt(0);
+    REQUIRE(stateMachine != nullptr);
+    stateMachine->advanceAndApply(0.0f);
+
+    auto textInput = artboard->objects<TextInput>().first();
+    REQUIRE(textInput != nullptr);
+    auto focusData = artboard->objects<FocusData>().first();
+    REQUIRE(focusData != nullptr);
+
+    textInput->rawTextInput()->text("hello world");
+    stateMachine->advanceAndApply(0.0f);
+
+    // Nothing focused yet.
+    CHECK(stateMachine->selectedText().empty());
+
+    stateMachine->setFocus(focusData);
+    textInput->rawTextInput()->clearSelection();
+    CHECK(stateMachine->selectedText().empty());
+
+    textInput->rawTextInput()->selectAll();
+    CHECK(stateMachine->selectedText() == "hello world");
+
+    // A partial selection reports just that range (2nd-4th characters).
+    textInput->rawTextInput()->cursor(
+        Cursor(CursorPosition(1), CursorPosition(4)));
+    CHECK(stateMachine->selectedText() == "ell");
+
+    // An obscured field keeps its selection off the clipboard.
+    textInput->obscured(true);
+    CHECK(stateMachine->selectedText().empty());
+    textInput->obscured(false);
+
+    stateMachine->clearFocus();
+    CHECK(stateMachine->selectedText().empty());
+}
+
 TEST_CASE("tab traversal into a text input selects all", "[text_input]")
 {
     auto file = ReadRiveFile("assets/text_input.riv");