Skip to content

Commit

Permalink
Create blog-search.js (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gorgias-JosephBongrand authored Feb 28, 2025
1 parent 1b6632f commit 7660616
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/js/blog-search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
$(document).ready(function () {
// Elements
const modalTrigger = $('[data-el="modal-trigger"]');
const modal = $('[data-el="modal"]');
const modalTriggerClose = $('[data-el="modal-close"]');
const searchInput = $('[data-el="blog-search"]');
const moreResultsLink = $('[data-el="more-results"]');
const searchWrapper = $('.blog-search_collection-wrapper');
const searchLink = $('.link-underline');
const baseUrl = "/wip/blog-search-results"; // Base URL

let typingTimer;
const doneTypingInterval = 500; // Delay before updating query params

// Function to open the modal
function openModal() {
modal.css({ "display": "flex", "opacity": "0" });

setTimeout(() => {
modal.css("opacity", "1");
searchInput.focus(); // Auto-focus the input when modal opens
}, 100);
}

// Function to close the modal
function closeModal() {
modal.css("opacity", "0");

setTimeout(() => {
modal.css("display", "none");
}, 300);
}

// Function to handle typing in search input
function handleTyping() {
clearTimeout(typingTimer);

let queryValue = searchInput.val().trim();

if (queryValue.length > 0) {
// Remove "is-search-disabled" and set opacity to 1
searchWrapper.removeClass('is-search-disabled').css("opacity", "1");
searchLink.removeClass('is-search-disabled').css("opacity", "1");
} else {
// If input is empty, revert to initial state
searchWrapper.css("opacity", "0").addClass('is-search-disabled');
searchLink.css("opacity", "0").addClass('is-search-disabled');
}

typingTimer = setTimeout(() => {
let queryParams = window.location.search; // Get updated query params

if (queryParams) {
updateMoreResultsLink(queryParams);
}
}, doneTypingInterval);
}

// Function to update "More Results" link with query parameters
function updateMoreResultsLink(queryParams) {
moreResultsLink.attr("href", baseUrl + queryParams);
}

// Event Listeners
modalTrigger.on("click", openModal);
modalTriggerClose.on("click", closeModal);

// Close modal when clicking outside it
modal.on("click", function (event) {
if ($(event.target).is(modal)) closeModal();
});

// Close modal on Escape key press
$(document).on("keydown", function (event) {
if (event.key === "Escape" || event.keyCode === 27) {
closeModal();
}
});

// Detect typing in search input
searchInput.on('input', handleTyping);
});
6 changes: 6 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Webflow.push(function () {
newScript('https://cdn.jsdelivr.net/npm/@finsweet/attributes-mirrorclick@1/mirrorclick.js','body',1);
}


// blog pages
if (path.includes('/blog/')) {
// [Attributes by Finsweet] Code Highlight
Expand All @@ -78,6 +79,7 @@ Webflow.push(function () {
newStyle(scriptBase + '/src/css/blog'+minBase+'.css','body');
newScript(scriptBase + '/src/js/blog'+minBase+'.js','body',1);
}

// product tour page
if (path === '/product-tour'){
newScript(scriptBase + '/src/js/producttour'+minBase+'.js','head',1)
Expand Down Expand Up @@ -170,5 +172,9 @@ Webflow.push(function () {
if(path.includes('/enterprise')){
newScript(scriptBase + '/src/js/enterprise'+minBase+'.js','head', 1);
}

if (path.includes('/wip/blog')){
newScript(scriptBase + '/src/js/blog-search'+minBase+'.js','body', 1);
}
})

0 comments on commit 7660616

Please sign in to comment.