fix(search): custom clear button, fixed #271

This commit is contained in:
qingwei.li 2018-01-21 17:09:40 +08:00 • committed by cinwell.li
commit 864aa181a1
2 changed files with 49 additions and 7 deletions

View file

@ -14,6 +14,11 @@ function style () {
border-bottom: 1px solid #eee; border-bottom: 1px solid #eee;
} }
.search .input-wrap {
display: flex;
align-items: center;
}
.search .results-panel { .search .results-panel {
display: none; display: none;
} }
@ -26,13 +31,31 @@ function style () {
outline: none; outline: none;
border: none; border: none;
width: 100%; width: 100%;
padding: 7px; padding: 0 7px;
line-height: 22px; line-height: 36px;
font-size: 14px; font-size: 14px;
}
.search input::-webkit-search-decoration,
.search input::-webkit-search-cancel-button,
.search input {
-webkit-appearance: none; -webkit-appearance: none;
-moz-appearance: none; -moz-appearance: none;
appearance: none; appearance: none;
} }
.search .clear-button {
width: 36px;
text-align: right;
display: none;
}
.search .clear-button.show {
display: block;
}
.search .clear-button svg {
transform: scale(.5);
}
.search h2 { .search h2 {
font-size: 17px; font-size: 17px;
@ -70,9 +93,18 @@ function style () {
function tpl (opts, defaultValue = '') { function tpl (opts, defaultValue = '') {
const html = const html =
`<input type="search" value="${defaultValue}" />` + `<div class="input-wrap">
'<div class="results-panel"></div>' + <input type="search" value="${defaultValue}" />
'</div>' <div class="clear-button">
<svg width="26" height="24">
<circle cx="12" cy="12" r="11" fill="#ccc" />
<path stroke="white" stroke-width="2" d="M8.25,8.25,15.75,15.75" />
<path stroke="white" stroke-width="2"d="M8.25,15.75,15.75,8.25" />
</svg>
</div>
</div>
<div class="results-panel"></div>
</div>`
const el = Docsify.dom.create('div', html) const el = Docsify.dom.create('div', html)
const aside = Docsify.dom.find('aside') const aside = Docsify.dom.find('aside')
@ -83,9 +115,11 @@ function tpl (opts, defaultValue = '') {
function doSearch (value) { function doSearch (value) {
const $search = Docsify.dom.find('div.search') const $search = Docsify.dom.find('div.search')
const $panel = Docsify.dom.find($search, '.results-panel') const $panel = Docsify.dom.find($search, '.results-panel')
const $clearBtn = Docsify.dom.find($search, '.clear-button')
if (!value) { if (!value) {
$panel.classList.remove('show') $panel.classList.remove('show')
$clearBtn.classList.remove('show')
$panel.innerHTML = '' $panel.innerHTML = ''
return return
} }
@ -102,12 +136,14 @@ function doSearch (value) {
}) })
$panel.classList.add('show') $panel.classList.add('show')
$clearBtn.classList.add('show')
$panel.innerHTML = html || `<p class="empty">${NO_DATA_TEXT}</p>` $panel.innerHTML = html || `<p class="empty">${NO_DATA_TEXT}</p>`
} }
function bindEvents () { function bindEvents () {
const $search = Docsify.dom.find('div.search') const $search = Docsify.dom.find('div.search')
const $input = Docsify.dom.find($search, 'input') const $input = Docsify.dom.find($search, 'input')
const $inputWrap = Docsify.dom.find($search, '.input-wrap')
let timeId let timeId
// Prevent to Fold sidebar // Prevent to Fold sidebar
@ -120,6 +156,13 @@ function bindEvents () {
clearTimeout(timeId) clearTimeout(timeId)
timeId = setTimeout(_ => doSearch(e.target.value.trim()), 100) timeId = setTimeout(_ => doSearch(e.target.value.trim()), 100)
}) })
Docsify.dom.on($inputWrap, 'click', e => {
// click input outside
if (e.target.tagName !== 'INPUT') {
$input.value = ''
doSearch()
}
})
} }
function updatePlaceholder (text, path) { function updatePlaceholder (text, path) {

View file

@ -102,7 +102,6 @@ li input[type=checkbox] {
/* navbar */ /* navbar */
.app-nav { .app-nav {
left: 0;
margin: 25px 60px 0 0; margin: 25px 60px 0 0;
position: absolute; position: absolute;
right: 0; right: 0;