Skip to content

Commit 2ec01e9

Browse files
authored
Search: define which element & event shows the modal (#58)
Define two new attributes `show-modal-selector` and `show-modal-event` to allow the user defining which HTML element will show the modal. ![Peek 2023-04-20 09-45](https://user-images.githubusercontent.com/244656/233418904-c8cf3246-5deb-4320-ac8d-4139e999f9f3.gif) In this example, when clicking on the `input` it will show the modal.
1 parent 1748c75 commit 2ec01e9

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/mirrors-prettier
3+
rev: "v2.7.1"
4+
hooks:
5+
- id: prettier
6+
args: ["--write", "src/**", "*.js", "*.json"]

public/index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ <h2>Search</h2>
2020

2121
<p>Nothing to see here, just a placeholder to put the <code>readthedocs-search</code> tag.</p>
2222
<p>Currently, it modifies the tag to be shown when pressing <code>\</code> (keycode 220) instead of <code>/</code> (keycode 191)</p>
23-
<readthedocs-search show-modal-keycode="220"></readthedocs-search>
23+
<readthedocs-search
24+
trigger-keycode="220"
25+
trigger-selector="#search-input"
26+
></readthedocs-search>
27+
28+
<p>Clicking in the following input should show the search modal: <input id="search-input" placeholder="Search docs" /></p>
2429

2530
<h2>Notification</h2>
2631

src/search.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ export class SearchElement extends LitElement {
4141
state: true,
4242
},
4343
cssFormFocusClasses: { state: true },
44-
showModalKeycode: { type: Number, attribute: "show-modal-keycode" },
44+
triggerKeycode: { type: Number, attribute: "trigger-keycode" },
45+
triggerSelector: { type: String, attribute: "trigger-selector" },
46+
triggerEvent: { type: String, attribute: "trigger-event" },
4547
};
4648

4749
static styles = styleSheet;
@@ -64,7 +66,9 @@ export class SearchElement extends LitElement {
6466
this.currentQueryRequest = null;
6567
this.defaultFilter = null;
6668
this.filters = [];
67-
this.showModalKeycode = 191;
69+
this.triggerKeycode = 191;
70+
this.triggerSelector = null;
71+
this.triggerEvent = "focusin";
6872
}
6973

7074
loadConfig(config) {
@@ -483,20 +487,43 @@ export class SearchElement extends LitElement {
483487
this.closeModal();
484488
}
485489
// Show the modal with `/`
486-
else if (e.keyCode === this.showModalKeycode && !this.show) {
490+
else if (e.keyCode === this.triggerKeycode && !this.show) {
487491
// prevent opening "Quick Find" in Firefox
488492
e.preventDefault();
489493
this.showModal();
490494
}
491495
};
492496

497+
_handleShowModalUser = (e) => {
498+
e.preventDefault();
499+
this.showModal();
500+
};
501+
493502
connectedCallback() {
494503
super.connectedCallback();
495504
// open search modal if "forward slash" button is pressed
496505
document.addEventListener("keydown", this._handleShowModal);
506+
507+
if (this.triggerSelector) {
508+
let element = document.querySelector(this.triggerSelector);
509+
if (element !== undefined) {
510+
element.addEventListener(this.triggerEvent, this._handleShowModalUser);
511+
}
512+
}
497513
}
514+
498515
disconnectedCallback() {
499516
document.removeEventListener("keydown", this._handleShowModal);
517+
if (this.triggerSelector) {
518+
let element = document.querySelector(this.triggerSelector);
519+
if (element !== undefined) {
520+
element.removeEventListener(
521+
this.triggerEvent,
522+
this._handleShowModalUser
523+
);
524+
}
525+
}
526+
500527
super.disconnectedCallback();
501528
}
502529
}

0 commit comments

Comments
 (0)