Skip to content

Commit 1f070a8

Browse files
authored
doc(ctrl-click): Add documentation about Ctrl-clicking results (#269)
This should explain how one can use the new `context` parameter to allow opening results in new tab without sacrificing keyboard navigation.
1 parent 7c8e142 commit 1f070a8

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

README.md

+41-8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ This JavaScript library adds a fast and fully-featured auto-completion menu to y
4242
- [Custom source](#custom-source)
4343
- [Security](#security)
4444
- [User-generated data: protecting against XSS](#user-generated-data-protecting-against-xss)
45+
- [FAQ](#faq)
46+
- [How can I `Control`-click on results and have them open in a new tab?](#how-can-i-control-click-on-results-and-have-them-open-in-a-new-tab)
4547
- [Events](#events)
4648
- [API](#api)
4749
- [jQuery](#jquery-1)
@@ -134,8 +136,8 @@ var autocomplete = require('autocomplete.js');
134136
}
135137
}
136138
}
137-
]).on('autocomplete:selected', function(event, suggestion, dataset) {
138-
console.log(suggestion, dataset);
139+
]).on('autocomplete:selected', function(event, suggestion, dataset, context) {
140+
console.log(event, suggestion, dataset, context);
139141
});
140142
</script>
141143
```
@@ -164,8 +166,8 @@ var autocomplete = require('autocomplete.js');
164166
}
165167
}
166168
}
167-
]).on('autocomplete:selected', function(event, suggestion, dataset) {
168-
console.log(suggestion, dataset);
169+
]).on('autocomplete:selected', function(event, suggestion, dataset, context) {
170+
console.log(event, suggestion, dataset, context);
169171
});
170172
</script>
171173
```
@@ -203,7 +205,7 @@ var autocomplete = require('autocomplete.js');
203205
};
204206
205207
$scope.$on('autocomplete:selected', function(event, suggestion, dataset) {
206-
console.log(suggestion, dataset);
208+
console.log(event, suggestion, dataset, context);
207209
});
208210
}]);
209211
</script>
@@ -592,6 +594,35 @@ If you did specify custom highlighting pre/post tags, you can specify them as 2n
592594
}
593595
```
594596

597+
## FAQ
598+
599+
### How can I `Control`-click on results and have them open in a new tab?
600+
601+
You'll need to update your suggestion templates to make them as `<a href>` links
602+
and not simple divs. `Control`-clicking on them will trigger the default browser
603+
behavior and open suggestions in a new tab.
604+
605+
To also support keyboard navigation, you'll need to listen to the
606+
`autocomplete:selected` event and change `window.location` to the destination
607+
URL.
608+
609+
Note that you might need to check the value of `context.selectionMethod` in
610+
`autocomplete:selected` first. If it's equal to `click`, you should `return`
611+
early, otherwise your main window will **also** follow the link.
612+
613+
Here is an example of how it would look like:
614+
615+
```javascript
616+
autocomplete(…).on('autocomplete:selected', function(event, suggestion, dataset, context) {
617+
// Do nothing on click, as the browser will already do it
618+
if (context.selectionMethod === 'click') {
619+
return;
620+
}
621+
// Change the page, for example, on other events
622+
window.location.assign(suggestion.url);
623+
});
624+
```
625+
595626
## Events
596627

597628
The autocomplete component triggers the following custom events.
@@ -615,9 +646,11 @@ The autocomplete component triggers the following custom events.
615646
the dataset the suggestion belongs to.
616647

617648
* `autocomplete:selected` – Triggered when a suggestion from the dropdown menu is
618-
selected. The event handler will be invoked with 3 arguments: the jQuery
619-
event object, the suggestion object, and the name of the dataset the
620-
suggestion belongs to.
649+
selected. The event handler will be invoked with the following arguments: the jQuery
650+
event object, the suggestion object, the name of the dataset the
651+
suggestion belongs to and a `context` object. The `context` contains
652+
a `.selectionMethod` key that can be either `click`, `enterKey`, `tabKey` or
653+
`blur`, depending how the suggestion was selected.
621654

622655
* `autocomplete:cursorremoved` – Triggered when the cursor leaves the selections
623656
or its current index is lower than 0

0 commit comments

Comments
 (0)