From 864aa181a19ff500e0f62c7d3c6e4921ab4b1e60 Mon Sep 17 00:00:00 2001 From: "qingwei.li" Date: Sun, 21 Jan 2018 17:09:40 +0800 Subject: [PATCH] fix(search): custom clear button, fixed #271 --- src/plugins/search/component.js | 55 +++++++++++++++++++++++++++++---- src/themes/basic/_layout.css | 1 - 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/plugins/search/component.js b/src/plugins/search/component.js index abc12dead..b36510386 100644 --- a/src/plugins/search/component.js +++ b/src/plugins/search/component.js @@ -14,6 +14,11 @@ function style () { border-bottom: 1px solid #eee; } +.search .input-wrap { + display: flex; + align-items: center; +} + .search .results-panel { display: none; } @@ -26,13 +31,31 @@ function style () { outline: none; border: none; width: 100%; - padding: 7px; - line-height: 22px; + padding: 0 7px; + line-height: 36px; font-size: 14px; +} + +.search input::-webkit-search-decoration, +.search input::-webkit-search-cancel-button, +.search input { -webkit-appearance: none; -moz-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 { font-size: 17px; @@ -70,9 +93,18 @@ function style () { function tpl (opts, defaultValue = '') { const html = - `` + - '
' + - '' + `
+ +
+ + + + + +
+
+
+ ` const el = Docsify.dom.create('div', html) const aside = Docsify.dom.find('aside') @@ -83,9 +115,11 @@ function tpl (opts, defaultValue = '') { function doSearch (value) { const $search = Docsify.dom.find('div.search') const $panel = Docsify.dom.find($search, '.results-panel') + const $clearBtn = Docsify.dom.find($search, '.clear-button') if (!value) { $panel.classList.remove('show') + $clearBtn.classList.remove('show') $panel.innerHTML = '' return } @@ -94,7 +128,7 @@ function doSearch (value) { let html = '' matchs.forEach(post => { html += `
- +

${post.title}

${post.content}

@@ -102,12 +136,14 @@ function doSearch (value) { }) $panel.classList.add('show') + $clearBtn.classList.add('show') $panel.innerHTML = html || `

${NO_DATA_TEXT}

` } function bindEvents () { const $search = Docsify.dom.find('div.search') const $input = Docsify.dom.find($search, 'input') + const $inputWrap = Docsify.dom.find($search, '.input-wrap') let timeId // Prevent to Fold sidebar @@ -120,6 +156,13 @@ function bindEvents () { clearTimeout(timeId) 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) { diff --git a/src/themes/basic/_layout.css b/src/themes/basic/_layout.css index 368f465b5..6e6d88941 100644 --- a/src/themes/basic/_layout.css +++ b/src/themes/basic/_layout.css @@ -102,7 +102,6 @@ li input[type=checkbox] { /* navbar */ .app-nav { - left: 0; margin: 25px 60px 0 0; position: absolute; right: 0;