Ohh! this works great! ```php FilamentView::registerRenderHook( 'panels::body.start', fn():

Ohh! this works great!

FilamentView::registerRenderHook(
    'panels::body.start',
    fn(): string => new HtmlString(<<<HTML
        <script>
        function autoFocus(element) {
            let input = element.querySelector("[autofocus='autofocus']");
            if (input) {
                setTimeout(function() {
                    input.focus();
                }, 100);
            }
        }

        window.addEventListener("load", function(e) {
            autoFocus(document);

            var observer = new MutationObserver(function(mutations) {
                mutations.forEach(function(mutation) {
                    if (mutation.addedNodes) {
                        mutation.addedNodes.forEach(function(node) {
                            // Check if it's an Element node and has the x-ref attribute 'modalContainer'
                            if (node.nodeType === 1 && node.matches("[x-ref='modalContainer']")) { 
                                autoFocus(node);
                            }
                        });
                    }
                });
            });

            observer.observe(document.body, { childList: true, subtree: true });
        });

        </script>
        HTML
    )
);


This auto selects the first input element on the page (edit page OR modal (even the slideover)).
Was this page helpful?