Skip to content

Commit 7a2739a

Browse files
committed
HTML attributes API: configure the addon via HTML attributes
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 f3dd06a commit 7a2739a

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-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 169)</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

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export class SearchElement extends LitElement {
5454

5555
// TODO: expand the default supported styles
5656
this.className = this.className || "raised floating";
57+
this.showModalKeycode = parseInt(this.getAttribute("show-modal-keycode")) || 169;
5758

5859
this.config = {};
5960
this.show = false;
@@ -481,7 +482,7 @@ export class SearchElement extends LitElement {
481482
this.closeModal();
482483
}
483484
// Show the modal with `/`
484-
else if (e.keyCode === 191 && !this.show) {
485+
else if (e.keyCode === this.showModalKeycode && !this.show) {
485486
// prevent opening "Quick Find" in Firefox
486487
e.preventDefault();
487488
this.showModal();

0 commit comments

Comments
 (0)