Skip to content

Commit 25f47aa

Browse files
committed
Prevent search item from being made active just because mouse pointer is already over it
1 parent 8f2efc5 commit 25f47aa

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This applies from version 0.5.0 onwards, as some versions before that have broke
2424
- Fix issues with back/forward buttons.
2525
- Stop using [:target](https://developer.mozilla.org/en-US/docs/Web/CSS/:target) for styling item pointed to by fragment identifier, as it [doesn't work well with back/forward buttons](https://github.com/whatwg/html/issues/639). Handle highlighting of item that has "focus" in JavaScript instead.
2626
- Prevent having multiple items with the same fragment identifier. The fragment identifier for an item is its disambiguated preferred term, with spaces replaced by underscores.
27+
- Prevent search item from being made active just because mouse pointer is already over it.
2728

2829
### Removed
2930

src/Components/SearchDialog.elm

+45-12
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ port module Components.SearchDialog exposing
55
, SearchResult
66
, hide
77
, init
8-
, makeFirstSearchResultActive
98
, onChangeSearchString
109
, onHide
1110
, onShow
1211
, searchResult
1312
, searchResultHref
13+
, searchStringWasJustUpdated
1414
, show
1515
, update
1616
, view
@@ -51,6 +51,7 @@ type Model parentMsg
5151
{ idPrefix : String
5252
, visibility : GradualVisibility
5353
, config : Config parentMsg
54+
, searchStringWasJustUpdated : Bool
5455
, activeSearchResultIndex : Maybe SearchResultIndex
5556
}
5657

@@ -85,6 +86,7 @@ init idPrefix properties =
8586
{ idPrefix = idPrefix
8687
, visibility = Invisible
8788
, config = configFromProperties properties
89+
, searchStringWasJustUpdated = False
8890
, activeSearchResultIndex = Nothing
8991
}
9092

@@ -95,6 +97,7 @@ innerModel :
9597
{ idPrefix : String
9698
, visibility : GradualVisibility
9799
, config : Config parentMsg
100+
, searchStringWasJustUpdated : Bool
98101
, activeSearchResultIndex : Maybe SearchResultIndex
99102
}
100103
innerModel model =
@@ -122,7 +125,9 @@ type Msg
122125
| Show
123126
| StartHiding
124127
| CompleteHiding
125-
| MakeSearchResultActive SearchResultIndex
128+
| SearchStringWasJustUpdated
129+
| SearchStringWasNotJustUpdated
130+
| MakeSearchResultActive Bool SearchResultIndex
126131
| MakeSearchResultInactive SearchResultIndex
127132
| MakePreviousOrNextSearchResultActive Int Bool
128133
| LoadUrl String
@@ -139,15 +144,21 @@ hide =
139144
StartHiding
140145

141146

142-
makeFirstSearchResultActive : Msg
143-
makeFirstSearchResultActive =
144-
MakeSearchResultActive 0
147+
searchStringWasJustUpdated : Msg
148+
searchStringWasJustUpdated =
149+
SearchStringWasJustUpdated
145150

146151

147152
update : (Model parentMsg -> parentModel) -> (Msg -> parentMsg) -> Msg -> Model parentMsg -> ( parentModel, Cmd parentMsg )
148153
update updateParentModel toParentMsg msg model =
149154
let
150-
model_ : { idPrefix : String, visibility : GradualVisibility, config : Config parentMsg, activeSearchResultIndex : Maybe SearchResultIndex }
155+
model_ :
156+
{ idPrefix : String
157+
, visibility : GradualVisibility
158+
, config : Config parentMsg
159+
, searchStringWasJustUpdated : Bool
160+
, activeSearchResultIndex : Maybe SearchResultIndex
161+
}
151162
model_ =
152163
innerModel model
153164

@@ -175,11 +186,30 @@ update updateParentModel toParentMsg msg model =
175186
|> Maybe.withDefault Cmd.none
176187
)
177188

178-
MakeSearchResultActive searchResultIndex ->
179-
( { model_ | activeSearchResultIndex = Just searchResultIndex }
180-
, scrollSearchResultIntoView <| searchResultId searchResultIndex model_.idPrefix
189+
SearchStringWasJustUpdated ->
190+
( { model_
191+
| searchStringWasJustUpdated = True
192+
, activeSearchResultIndex = Just 0
193+
}
194+
, Process.sleep 200
195+
|> Task.perform (always <| SearchStringWasNotJustUpdated)
196+
|> Cmd.map toParentMsg
181197
)
182198

199+
SearchStringWasNotJustUpdated ->
200+
( { model_ | searchStringWasJustUpdated = False }, Cmd.none )
201+
202+
MakeSearchResultActive becauseOfMouseEnter searchResultIndex ->
203+
if becauseOfMouseEnter && model_.searchStringWasJustUpdated then
204+
-- Prevent a search result being made active just because the mouse pointer was already over it.
205+
-- We want to keep the first result (if any) active as the search string was just updated.
206+
( model_, Cmd.none )
207+
208+
else
209+
( { model_ | activeSearchResultIndex = Just searchResultIndex }
210+
, scrollSearchResultIntoView <| searchResultId searchResultIndex model_.idPrefix
211+
)
212+
183213
MakeSearchResultInactive searchResultIndex ->
184214
( { model_
185215
| activeSearchResultIndex =
@@ -263,13 +293,15 @@ loadUrl :
263293
{ idPrefix : String
264294
, visibility : GradualVisibility
265295
, config : Config parentMsg
296+
, searchStringWasJustUpdated : Bool
266297
, activeSearchResultIndex : Maybe SearchResultIndex
267298
}
268299
-> String
269300
->
270301
( { idPrefix : String
271302
, visibility : GradualVisibility
272303
, config : Config parentMsg
304+
, searchStringWasJustUpdated : Bool
273305
, activeSearchResultIndex : Maybe SearchResultIndex
274306
}
275307
, Cmd parentMsg
@@ -374,6 +406,7 @@ view toParentMsg model searchString messageAboveSearchResults searchResults =
374406
{ idPrefix : String
375407
, visibility : GradualVisibility
376408
, config : Config parentMsg
409+
, searchStringWasJustUpdated : Bool
377410
, activeSearchResultIndex : Maybe SearchResultIndex
378411
}
379412
model_ =
@@ -467,13 +500,13 @@ view toParentMsg model searchString messageAboveSearchResults searchResults =
467500

468501
else if event == Extras.HtmlEvents.home then
469502
Just
470-
( toParentMsg <| MakeSearchResultActive 0
503+
( toParentMsg <| MakeSearchResultActive False 0
471504
, True
472505
)
473506

474507
else if event == Extras.HtmlEvents.end then
475508
Just
476-
( toParentMsg <| MakeSearchResultActive (List.length searchResults - 1)
509+
( toParentMsg <| MakeSearchResultActive False (List.length searchResults - 1)
477510
, True
478511
)
479512

@@ -511,7 +544,7 @@ view toParentMsg model searchString messageAboveSearchResults searchResults =
511544
, Accessibility.Role.option
512545
, Accessibility.Key.tabbable False
513546
, Html.Attributes.id <| searchResultId index model_.idPrefix
514-
, Html.Events.onMouseEnter (toParentMsg <| MakeSearchResultActive index)
547+
, Html.Events.onMouseEnter (toParentMsg <| MakeSearchResultActive True index)
515548
, Html.Events.onMouseLeave (toParentMsg <| MakeSearchResultInactive index)
516549
]
517550
[ Html.a

src/Pages/ListAll.elm

+1-5
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,7 @@ update msg model =
364364
}
365365
}
366366
in
367-
if List.length results > 0 then
368-
update (SearchDialogMsg Components.SearchDialog.makeFirstSearchResultActive) model1
369-
370-
else
371-
( model1, Cmd.none )
367+
update (SearchDialogMsg Components.SearchDialog.searchStringWasJustUpdated) model1
372368

373369
ChangeTheme theme ->
374370
let

0 commit comments

Comments
 (0)