Skip to content

Commit a470c48

Browse files
authored
feat(ctrl-click): Enable Ctrl-Click on suggestions (#542)
* feat(ctrl-click): Enable Ctrl-Click on suggestions This updates the underlying autocomplete.js to a version that passes the selection context (click or keyboard) to the `autocomplete:selected` event. With the default `handleSelected, and in the case of a click, it will do nothing (as each suggestion already being a `<a href>`, it will already follow the link), but manually change the location for keyboard navigation. When a custom `handleSelected` is set though, default browser clicks on suggestions are disabled and only `handleSelected` is called. This is to keep backward compatibility with all the live websites that might already use `handleSelected`. * fix(test): Making sure it does not crash when no context is passed
1 parent 892f1b0 commit a470c48

5 files changed

Lines changed: 44 additions & 27 deletions

File tree

docs/src/behavior.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,34 @@ docsearch({
2424

2525
## `handleSelected`
2626

27-
This method is called when a suggestion is selected. By default, DocSearch will
28-
redirect the browser to the results page at the related anchor, but you can
29-
override it to add your own behavior.
27+
This method is called when a suggestion is selected (either with a click or
28+
keyboard). By default, DocSearch will redirect the browser to the results page
29+
at the related anchor, but you can override it to add your own behavior.
3030

31-
The method is called with three arguments:
31+
The method is called with the following arguments:
3232

3333
- `input`, a reference to the search `input` element. It comes with the
3434
`.open()`, `.close()`, `.getVal()` and `.setVal()` methods.
3535

36-
- `event`, the actual event triggering the selection. This can come from a click
37-
or a keyboard navigation.
36+
- `event`, the actual event triggering the selection.
3837

39-
- `suggestion`, the object representing the current selection.
38+
- `suggestion`, the object representing the current selection. Contains a `.url`
39+
key representing the destination.
40+
41+
- `datasetNumber`, this should always equal `1` as DocSearch is searching
42+
into one dataset at a time. You can ignore this attribute.
43+
44+
- `context`, additional information about the selection. Contains
45+
a `.selectionMethod` key that can be either `click`, `enterKey`, `tabKey` or
46+
`blur`, depending how the suggestion was selected.
4047

4148
```javascript
4249
docsearch({
4350
[…],
44-
handleSelected: function(input, event, suggestion) {
51+
handleSelected: function(input, event, suggestion, datasetNumber, context) {
52+
// Default implementation is as follow:
53+
input.setVal('');
54+
window.location.assign(suggestion.url);
4555
}
4656
});
4757
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"peerDependencies": {},
7777
"dependencies": {
7878
"algoliasearch": "^3.24.5",
79-
"autocomplete.js": "0.32.0",
79+
"autocomplete.js": "0.33.0",
8080
"hogan.js": "^3.0.2",
8181
"request": "^2.87.0",
8282
"stack-utils": "^1.0.1",

scripts/playground.html

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ <h1>Assets are served from http://127.0.0.1:8080/ </h1>
2626
apiKey: '25626fae796133dc1e734c6bcaaeac3c',
2727
indexName: 'docsearch',
2828
inputSelector: '#q',
29-
// handleSelected(input, event, suggestion) {
30-
// console.info(input);
31-
// console.info(event);
32-
// console.info(suggestion);
33-
// },
29+
handleSelected(input, event, suggestion, datasetNumber, context) {
30+
console.info(input);
31+
console.info(event);
32+
console.info(suggestion);
33+
console.info(datasetNumber);
34+
console.info(context);
35+
},
3436
debug: true // Set debug to true if you want to inspect the dropdown
3537
});
3638
</script>

src/lib/DocSearch.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ class DocSearch {
7878
this.autocompleteOptions.cssClasses.prefix =
7979
this.autocompleteOptions.cssClasses.prefix || 'ds';
8080

81-
// eslint-disable-next-line no-param-reassign
82-
handleSelected = handleSelected || this.handleSelected;
8381

8482
this.isSimpleLayout = layout === 'simple';
8583

@@ -101,19 +99,19 @@ class DocSearch {
10199
},
102100
]);
103101

104-
// If user defined its own handleSelected, we prevent clicks on suggestions
105-
// link to do anything
106-
if (handleSelected) {
102+
const customHandleSelected = handleSelected;
103+
this.handleSelected = customHandleSelected || this.handleSelected;
104+
105+
// We prevent default link clicking if a custom handleSelected is defined
106+
if (customHandleSelected) {
107107
$('.algolia-autocomplete').on('click', '.ds-suggestions a', event => {
108108
event.preventDefault();
109109
});
110110
}
111111

112-
// Click on suggestions will follow the link, but keyboard navigation still
113-
// need the handleSelected
114112
this.autocomplete.on(
115113
'autocomplete:selected',
116-
handleSelected.bind(null, this.autocomplete.autocomplete)
114+
this.handleSelected.bind(null, this.autocomplete.autocomplete)
117115
);
118116

119117
this.autocomplete.on(
@@ -324,7 +322,14 @@ class DocSearch {
324322
return suggestion => template.render(suggestion);
325323
}
326324

327-
handleSelected(input, event, suggestion) {
325+
handleSelected(input, event, suggestion, datasetNumber, context = {}) {
326+
// Do nothing if click on the suggestion, as it's already a <a href>, the
327+
// browser will take care of it. This allow Ctrl-Clicking on results and not
328+
// having the main window being redirected as well
329+
if (context.selectionMethod === 'click') {
330+
return;
331+
}
332+
328333
input.setVal('');
329334
window.location.assign(suggestion.url);
330335
}

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,10 +541,10 @@ atob@^2.1.1:
541541
resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.1.tgz#ae2d5a729477f289d60dd7f96a6314a22dd6c22a"
542542
integrity sha1-ri1acpR38onWDdf5amMUoi3Wwio=
543543

544-
autocomplete.js@0.32.0:
545-
version "0.32.0"
546-
resolved "https://registry.yarnpkg.com/autocomplete.js/-/autocomplete.js-0.32.0.tgz#a00eab44dcb9e573ebd8bb3178e7fe67466cd4f1"
547-
integrity sha512-GYGmOo0r2wLgUEYE5J9z9OSLb8e0SAicgDR1M1pHOvwQ0Hc1SLHR0EqjDhl+lhl01cYq2d7lLbsgRmaizgLqrA==
544+
autocomplete.js@0.33.0:
545+
version "0.33.0"
546+
resolved "https://registry.yarnpkg.com/autocomplete.js/-/autocomplete.js-0.33.0.tgz#33d460367e2e7b6c2fe424353d955e022fe4594c"
547+
integrity sha512-J0F7BkPhYwXvfs8Skp6v2e2IHYv0SL8INyHYwb7nUpvKHr96g6zS8RNEFGEfEuO3ND+XUsesEMM59LlwQoLfoA==
548548
dependencies:
549549
immediate "^3.2.3"
550550

0 commit comments

Comments
 (0)