Skip to content

Commit 47e9148

Browse files
committed
Cancel search with cancel button or ESC
1 parent 6873e7e commit 47e9148

3 files changed

Lines changed: 41 additions & 5 deletions

File tree

layouts/header.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
<li><a href="/v3/" class="nav-api">API</a></li>
88
<li><a href="/changes/" class="nav-blog">Blog</a></li>
99
<li><a href="https://github.com/contact">Support</a></li>
10-
<li class="search-container">
10+
<li id="search-container">
1111
<input type="text" id="search" /><label class="search-placeholder">Search</label>
12+
<div class="cancel-search"></div>
1213
<ul id="search-results">
1314

1415
</ul>

static/shared/css/documentation.css

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ a img {
335335

336336
/** Search **/
337337

338-
#header .nav .search-container {
338+
#header .nav #search-container {
339339
position: relative;
340340
margin-left: 5px;
341341
}
@@ -357,11 +357,11 @@ a img {
357357
transition: width 0.3s ease-in-out 0s;
358358
}
359359

360-
#search:focus {
360+
#search:focus, #search-container.active #search {
361361
width: 120px;
362362
}
363363

364-
#search:focus + .search-placeholder {
364+
#search:focus + .search-placeholder, #search-container.active .search-placeholder {
365365
opacity: 0;
366366
}
367367

@@ -382,6 +382,20 @@ a img {
382382
transition: opacity 0.3s ease-in-out 0s;
383383
}
384384

385+
.cancel-search {
386+
position: absolute;
387+
right: 4px;
388+
top: 2px;
389+
background: url(/shared/images/cancel.png) 0 0 no-repeat;
390+
width: 14px;
391+
height: 14px;
392+
display: none;
393+
}
394+
395+
#search-container.active .cancel-search {
396+
display: block;
397+
}
398+
385399
#search-results {
386400
position: absolute;
387401
top: 30px;
@@ -393,7 +407,14 @@ a img {
393407
z-index: 100;
394408
overflow: hidden;
395409
box-shadow: 0px 1px 3px rgba(0,0,0,0.4);
396-
display: none;
410+
visibility: hidden;
411+
opacity: 0;
412+
}
413+
414+
#search-container.active #search:focus ~ #search-results {
415+
visibility: visible;
416+
opacity: 1;
417+
-webkit-transition: opacity 0.3s ease-in-out 0s;
397418
}
398419

399420
#search-results li {

static/shared/js/documentation.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,20 @@ $(function() {
124124
searchForString($(this).val());
125125
});
126126

127+
// Press ESC to exit search
128+
$("#search").keydown(function(e) {
129+
if (e.keyCode == 27) exitSearch();
130+
});
131+
132+
$(".cancel-search").click(function(e) {
133+
exitSearch();
134+
});
135+
136+
function exitSearch() {
137+
$("#search-container").removeClass("active");
138+
$("#search").val("");
139+
}
140+
127141
function searchForString(searchString) {
128142
searchHits = [];
129143
searchString = searchString.toLowerCase();

0 commit comments

Comments
 (0)