From 0768862b8b0ae5d7346550b3ba8658eddc6f527d Mon Sep 17 00:00:00 2001 From: Tessa Kelly Date: Wed, 2 Sep 2020 10:17:53 -0700 Subject: [PATCH] initOpen autofocuses on the first element --- src/Nri/Ui/Modal/V11.elm | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Nri/Ui/Modal/V11.elm b/src/Nri/Ui/Modal/V11.elm index 3abe5ccc..75ab6127 100644 --- a/src/Nri/Ui/Modal/V11.elm +++ b/src/Nri/Ui/Modal/V11.elm @@ -79,9 +79,16 @@ init = {-| Pass the id of the element that should receive focus when the modal is closed. -} -initOpen : String -> Model -initOpen = - Opened +initOpen : String -> ( Model, Cmd Msg ) +initOpen returnFocusTo = + ( Opened returnFocusTo, focusFirstElement ) + + +focusFirstElement : Cmd Msg +focusFirstElement = + Dom.focus autofocusId + |> Task.onError (\_ -> Dom.focus firstId) + |> Task.attempt Focused {-| -} @@ -126,11 +133,7 @@ update : { dismissOnEscAndOverlayClick : Bool } -> Msg -> Model -> ( Model, Cmd update { dismissOnEscAndOverlayClick } msg model = case msg of OpenModal returnFocusTo -> - ( Opened returnFocusTo - , Dom.focus autofocusId - |> Task.onError (\_ -> Dom.focus firstId) - |> Task.attempt Focused - ) + ( Opened returnFocusTo, focusFirstElement ) CloseModal by -> let