Skip to content

Commit

Permalink
Use arrow function for hideTooltipOnClickAway to allow 'this' to refe…
Browse files Browse the repository at this point in the history
…r to DropdownController
  • Loading branch information
laymonage committed Jan 26, 2024
1 parent a89812f commit ea28844
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions client/src/controllers/DropdownController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,11 @@ export class DropdownController extends Controller<HTMLElement> {
* elements outside of the dropdown's DOM).
*/
get hideTooltipOnClickAway() {
// We can't use `this` to access the controller instance inside the plugin
// function, so we need to get these ahead in time.
const reference = this.reference;
const toggleTarget = this.toggleTarget;
const dispatch = this.dispatch.bind(this);

return {
name: 'hideTooltipOnClickAway',
fn(instance: Instance) {
fn: (instance: Instance) => {
const onClick = (e: MouseEvent) => {
const event = dispatch('clickaway', {
const event = this.dispatch('clickaway', {
cancelable: true,
detail: { target: e.target },
});
Expand All @@ -238,8 +232,8 @@ export class DropdownController extends Controller<HTMLElement> {
instance.state.isShown &&
// Hide if the click is outside of the reference element,
// or if the click is on the toggle button itself.
(!reference.contains(e.target as Node) ||
toggleTarget.contains(e.target as Node))
(!this.reference.contains(e.target as Node) ||
this.toggleTarget.contains(e.target as Node))
) {
instance.hide();
}
Expand Down

0 comments on commit ea28844

Please sign in to comment.