Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use onAnimationFrame event for position updates #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions examples/src/SingleSelectExample.elm
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type Msg
= HandleSelectUpdate (SingleSelect.Msg Product)
| HandleSelection ( Product, SingleSelect.Msg Product )
| HandleFormSubmission
| OnViewChange


update : Msg -> Model -> ( Model, Cmd Msg )
Expand All @@ -50,12 +49,6 @@ update msg model =
HandleFormSubmission ->
( { model | wasFormSubmitted = True }, Cmd.none )

OnViewChange ->
let
( updatedSelect, selectCmd ) =
SingleSelect.updatePosition model.select
in
( { model | select = updatedSelect }, selectCmd )


view : Model -> Html Msg
Expand All @@ -65,7 +58,6 @@ view model =
, style "height" "100vh"
, style "padding" "3rem"
, style "overflow" "auto"
, Html.Events.on "scroll" (Decode.succeed OnViewChange)
]
[ h1 [] [ text "SingleSelect Example" ]
, div
Expand Down
13 changes: 7 additions & 6 deletions src/SingleSelect.elm
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type Msg a
| UpKeyPressed Int
| DownKeyPressed Int
| SetSearchText String
| WindowResized ( Int, Int )
| OnViewChanged
| GotAlignment (Result Dom.Error Alignment)
| Open
| Close
Expand Down Expand Up @@ -101,8 +101,9 @@ subscriptions : SmartSelect msg a -> Sub msg
subscriptions (SmartSelect model) =
if model.isOpen then
Sub.batch
[ Browser.Events.onResize (\h w -> model.internalMsg <| WindowResized ( h, w ))
[ Browser.Events.onResize (\_ _ -> model.internalMsg <| OnViewChanged)
, Browser.Events.onMouseDown (Utilities.clickedOutsideSelect (Id.select model.idPrefix) (model.internalMsg Close))
, Browser.Events.onAnimationFrame (\_ -> model.internalMsg <| OnViewChanged)
]

else
Expand Down Expand Up @@ -174,15 +175,15 @@ update msg (SmartSelect model) =
SetSearchText text ->
( SmartSelect { model | searchText = text, focusedOptionIndex = 0 }, Cmd.none )

WindowResized _ ->
OnViewChanged ->
( SmartSelect model, Alignment.getAlignment model.idPrefix (\alignment -> model.internalMsg (GotAlignment alignment)) )

GotAlignment result ->
case result of
Ok alignment ->
case ( result, model.isOpen ) of
( Ok alignment, True ) ->
( SmartSelect { model | alignment = Just alignment }, Cmd.none )

Err _ ->
( _, _ ) ->
( SmartSelect model, Cmd.none )

Open ->
Expand Down