Skip to content

Commit 1748c75

Browse files
authored
HTML attributes API: configure the addon via HTML attributes (#56)
By default the search modal is shown when pressing `/`. However, we want this to be configurable by the user. This can be achieved by passing `show-modal-keycode` attribute to the web component as follows: ```html <readthedocs-search show-modal-keycode="220"> </readthedocs-search> ``` In this case, it will be shown when pressing `\`. I think this is a good pattern for static values that won't be changed from the dashboard or similar in a dynamic way. Related #13
1 parent 6998852 commit 1748c75

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

public/index.html

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ <h1>Documentation Addons</h1>
1616
<li><a href="/pelican">Pelican</a></li>
1717
</ul>
1818

19+
<h2>Search</h2>
20+
21+
<p>Nothing to see here, just a placeholder to put the <code>readthedocs-search</code> tag.</p>
22+
<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>
24+
1925
<h2>Notification</h2>
2026

2127
<readthedocs-notification class="raised toast"></readthedocs-notification>

src/search.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export class SearchElement extends LitElement {
4141
state: true,
4242
},
4343
cssFormFocusClasses: { state: true },
44+
showModalKeycode: { type: Number, attribute: "show-modal-keycode" },
4445
};
4546

4647
static styles = styleSheet;
@@ -63,6 +64,7 @@ export class SearchElement extends LitElement {
6364
this.currentQueryRequest = null;
6465
this.defaultFilter = null;
6566
this.filters = [];
67+
this.showModalKeycode = 191;
6668
}
6769

6870
loadConfig(config) {
@@ -481,7 +483,7 @@ export class SearchElement extends LitElement {
481483
this.closeModal();
482484
}
483485
// Show the modal with `/`
484-
else if (e.keyCode === 191 && !this.show) {
486+
else if (e.keyCode === this.showModalKeycode && !this.show) {
485487
// prevent opening "Quick Find" in Firefox
486488
e.preventDefault();
487489
this.showModal();

0 commit comments

Comments
 (0)