Skip to content

Commit 7cca9f3

Browse files
authored
feat(search): scrollable search result
1. Handles keyboard events 2. Regains scroll position after closing of results due to blur/select event 3. Variation classes added such as scrolling, short, long, very short and very long
1 parent 8bb477b commit 7cca9f3

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

src/definitions/modules/search.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,25 @@ $.fn.search = function(parameters) {
250250
}
251251
}
252252
},
253+
ensureVisible: function ensureVisible($el) {
254+
var elTop, elBottom, resultsScrollTop, resultsHeight;
255+
256+
elTop = $el.position().top;
257+
elBottom = elTop + $el.outerHeight(true);
258+
259+
resultsScrollTop = $results.scrollTop();
260+
resultsHeight = $results.height()
261+
parseInt($results.css('paddingTop'), 0) +
262+
parseInt($results.css('paddingBottom'), 0);
263+
264+
if (elTop < 0) {
265+
$results.scrollTop(resultsScrollTop + elTop);
266+
}
267+
268+
else if (resultsHeight < elBottom) {
269+
$results.scrollTop(resultsScrollTop + (elBottom - resultsHeight));
270+
}
271+
},
253272
handleKeyboard: function(event) {
254273
var
255274
// force selector refresh
@@ -301,6 +320,7 @@ $.fn.search = function(parameters) {
301320
.closest($category)
302321
.addClass(className.active)
303322
;
323+
module.ensureVisible($result.eq(newIndex));
304324
event.preventDefault();
305325
}
306326
else if(keyCode == keys.downArrow) {
@@ -319,6 +339,7 @@ $.fn.search = function(parameters) {
319339
.closest($category)
320340
.addClass(className.active)
321341
;
342+
module.ensureVisible($result.eq(newIndex));
322343
event.preventDefault();
323344
}
324345
}
@@ -952,6 +973,12 @@ $.fn.search = function(parameters) {
952973
debug : settings.debug,
953974
verbose : settings.verbose,
954975
duration : settings.duration,
976+
onShow : function() {
977+
var $firstResult = $module.find(selector.result).eq(0);
978+
if($firstResult.length > 0) {
979+
module.ensureVisible($firstResult);
980+
}
981+
},
955982
onComplete : function() {
956983
callback();
957984
},

src/definitions/modules/search.less

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,121 @@
390390
Variations
391391
*******************************/
392392

393+
& when (@variationSearchScrolling),
394+
(@variationSearchShort),
395+
(@variationSearchLong) {
396+
397+
/*-------------------
398+
Scrolling
399+
--------------------*/
400+
401+
.ui.scrolling.search > .results,
402+
.ui.search.long > .results,
403+
.ui.search.short > .results {
404+
overflow-x: hidden;
405+
overflow-y: auto;
406+
backface-visibility: hidden;
407+
-webkit-overflow-scrolling: touch;
408+
}
409+
}
410+
411+
412+
& when (@variationSearchScrolling) {
413+
@media only screen and (max-width : @largestMobileScreen) {
414+
.ui.scrolling.search > .results {
415+
max-height: @scrollingMobileMaxResultsHeight;
416+
}
417+
}
418+
@media only screen and (min-width: @tabletBreakpoint) {
419+
.ui.scrolling.search > .results {
420+
max-height: @scrollingTabletMaxResultsHeight;
421+
}
422+
}
423+
@media only screen and (min-width: @computerBreakpoint) {
424+
.ui.scrolling.search > .results {
425+
max-height: @scrollingComputerMaxResultsHeight;
426+
}
427+
}
428+
@media only screen and (min-width: @widescreenMonitorBreakpoint) {
429+
.ui.scrolling.search > .results {
430+
max-height: @scrollingWidescreenMaxResultsHeight;
431+
}
432+
}
433+
}
434+
435+
@media only screen and (max-width : @largestMobileScreen) {
436+
& when (@variationSearchShort) {
437+
.ui.search.short > .results {
438+
max-height: @scrollingMobileMaxResultsHeight;
439+
}
440+
.ui.search[class*="very short"] > .results {
441+
max-height: @scrollingMobileMaxResultsHeight * 0.75;
442+
}
443+
}
444+
& when (@variationSearchLong) {
445+
.ui.search.long > .results {
446+
max-height: @scrollingMobileMaxResultsHeight * 2;
447+
}
448+
.ui.search[class*="very long"] > .results {
449+
max-height: @scrollingMobileMaxResultsHeight * 3;
450+
}
451+
}
452+
}
453+
@media only screen and (min-width: @tabletBreakpoint) {
454+
& when (@variationSearchShort) {
455+
.ui.search.short > .results {
456+
max-height: @scrollingTabletMaxResultsHeight;
457+
}
458+
.ui.search[class*="very short"] > .results {
459+
max-height: @scrollingTabletMaxResultsHeight * 0.75;
460+
}
461+
}
462+
& when (@variationSearchLong) {
463+
.ui.search.long > .results {
464+
max-height: @scrollingTabletMaxResultsHeight * 2;
465+
}
466+
.ui.search[class*="very long"] > .results {
467+
max-height: @scrollingTabletMaxResultsHeight * 3;
468+
}
469+
}
470+
}
471+
@media only screen and (min-width: @computerBreakpoint) {
472+
& when (@variationSearchShort) {
473+
.ui.search.short > .results {
474+
max-height: @scrollingComputerMaxResultsHeight;
475+
}
476+
.ui.search[class*="very short"] > .results {
477+
max-height: @scrollingComputerMaxResultsHeight * 0.75;
478+
}
479+
}
480+
& when (@variationSearchLong) {
481+
.ui.search.long > .results {
482+
max-height: @scrollingComputerMaxResultsHeight * 2;
483+
}
484+
.ui.search[class*="very long"] > .results {
485+
max-height: @scrollingComputerMaxResultsHeight * 3;
486+
}
487+
}
488+
}
489+
@media only screen and (min-width: @widescreenMonitorBreakpoint) {
490+
& when (@variationSearchShort) {
491+
.ui.search.short > .results {
492+
max-height: @scrollingWidescreenMaxResultsHeight;
493+
}
494+
.ui.search[class*="very short"] > .results {
495+
max-height: @scrollingWidescreenMaxResultsHeight * 0.75;
496+
}
497+
}
498+
& when (@variationSearchLong) {
499+
.ui.search.long > .results {
500+
max-height: @scrollingWidescreenMaxResultsHeight * 2;
501+
}
502+
.ui.search[class*="very long"] > .results {
503+
max-height: @scrollingWidescreenMaxResultsHeight * 3;
504+
}
505+
}
506+
}
507+
393508
& when (@variationSearchAligned) {
394509
/*-------------------
395510
Left / Right

src/themes/default/globals/variation.variables

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,9 @@
473473
@variationSearchLoading: true;
474474
@variationSearchAligned: true;
475475
@variationSearchFluid: true;
476+
@variationSearchShort: true;
477+
@variationSearchLong: true;
478+
@variationSearchScrolling: true;
476479
@variationSearchSizes: @variationAllSizes;
477480

478481
/* Shape */

src/themes/default/modules/search.variables

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@
5656
@resultTitleFontSize: @relativeMedium;
5757
@resultTitleColor: @darkTextColor;
5858

59+
/* Result Scrolling */
60+
@scrollingMobileMaxResults: 4;
61+
@scrollingTabletMaxResults: 6;
62+
@scrollingComputerMaxResults: 8;
63+
@scrollingWidescreenMaxResults: 12;
64+
65+
@scrollingResultHeight: (@resultVerticalPadding * 2) + @resultLineHeight;
66+
@scrollingMobileMaxResultsHeight: (@scrollingResultHeight * @scrollingMobileMaxResults);
67+
@scrollingTabletMaxResultsHeight: (@scrollingResultHeight * @scrollingTabletMaxResults);
68+
@scrollingComputerMaxResultsHeight: (@scrollingResultHeight * @scrollingComputerMaxResults);
69+
@scrollingWidescreenMaxResultsHeight: (@scrollingResultHeight * @scrollingWidescreenMaxResults);
70+
5971
/* Description */
6072
@resultDescriptionFontSize: @relativeSmall;
6173
@resultDescriptionDistance: 0;

0 commit comments

Comments
 (0)