Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the page flickers when the search dialog pops up #860

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source/css/_common/components/third-party/search.styl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
if (hexo-config('local_search.enable') or hexo-config('algolia_search.enable')) {
.search-active {
overflow: hidden;
margin-right: var(--dialog-scrollgutter, 0);
}

.search-pop-overlay {
Expand Down
1 change: 0 additions & 1 deletion source/css/_common/scaffolding/base.styl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ body {
min-height: 100%;
position: relative;
transition: padding $transition-ease;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This transition is necessary for sidebar open & close in scheme Muse & Mist. See source/css/_schemes/Muse/sidebar.styl (padding-right for body.sidebar-active)


if (hexo-config('body_scrollbar.overlay')) {
overflow-x: hidden;
@supports (overflow-x: clip) {
Expand Down
16 changes: 16 additions & 0 deletions source/js/third-party/search/algolia-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ document.addEventListener('DOMContentLoaded', () => {
let isSearching = false;
let pendingQuery = null;

/**
* Sets the CSS variable '--dialog-scrollgutter' to the specified gap value.
* If no gap is provided, it calculates the gap as the difference between
* the window's inner width and the document body's client width.
*
* @param {string} [gap] - The gap value to be set. If not provided, the
* default gap is calculated automatically.
*/
const setGutter = gap => {
const gutter = gap || `${window.innerWidth - document.body.clientWidth}px`;
document.body.style.setProperty('--dialog-scrollgutter', gutter);
};

const searchAlgolia = async(searchText, page = 0) => {
if (isSearching) {
pendingQuery = { searchText, page };
Expand Down Expand Up @@ -96,6 +109,7 @@ document.addEventListener('DOMContentLoaded', () => {
// Handle and trigger popup window
document.querySelectorAll('.popup-trigger').forEach(element => {
element.addEventListener('click', () => {
setGutter();
document.body.classList.add('search-active');
// Wait for search-popup animation to complete
setTimeout(() => input.focus(), 500);
Expand All @@ -104,6 +118,7 @@ document.addEventListener('DOMContentLoaded', () => {

// Monitor main search box
const onPopupClose = () => {
setGutter('0');
document.body.classList.remove('search-active');
};

Expand All @@ -117,6 +132,7 @@ document.addEventListener('DOMContentLoaded', () => {
window.addEventListener('keydown', event => {
if ((event.ctrlKey || event.metaKey) && event.key === 'k') {
event.preventDefault();
setGutter();
document.body.classList.add('search-active');
setTimeout(() => input.focus(), 500);
}
Expand Down
15 changes: 15 additions & 0 deletions source/js/third-party/search/local-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ document.addEventListener('DOMContentLoaded', () => {
const input = document.querySelector('.search-input');
const container = document.querySelector('.search-result-container');

/**
* Sets the CSS variable '--dialog-scrollgutter' to the specified gap value.
* If no gap is provided, it calculates the gap as the difference between
* the window's inner width and the document body's client width.
*
* @param {string} [gap] - The gap value to be set. If not provided, the
* default gap is calculated automatically.
*/
const setGutter = gap => {
document.body.style.setProperty('--dialog-scrollgutter', gap || `${window.innerWidth - document.body.clientWidth}px`);
};

const inputEventFunction = () => {
if (!localSearch.isfetched) return;
const searchText = input.value.trim().toLowerCase();
Expand Down Expand Up @@ -57,6 +69,7 @@ document.addEventListener('DOMContentLoaded', () => {
// Handle and trigger popup window
document.querySelectorAll('.popup-trigger').forEach(element => {
element.addEventListener('click', () => {
setGutter();
document.body.classList.add('search-active');
// Wait for search-popup animation to complete
setTimeout(() => input.focus(), 500);
Expand All @@ -66,6 +79,7 @@ document.addEventListener('DOMContentLoaded', () => {

// Monitor main search box
const onPopupClose = () => {
setGutter('0');
document.body.classList.remove('search-active');
};

Expand All @@ -82,6 +96,7 @@ document.addEventListener('DOMContentLoaded', () => {
window.addEventListener('keydown', event => {
if ((event.ctrlKey || event.metaKey) && event.key === 'k') {
event.preventDefault();
setGutter();
document.body.classList.add('search-active');
setTimeout(() => input.focus(), 500);
if (!localSearch.isfetched) localSearch.fetchData();
Expand Down
Loading