Pimp Lenis Anchor Scroll

Hello Bricksforge,

Lenis is a fantastic extension for smooth scrolling. I’d appreciate it if the following points could be enhanced for anchor links:

  1. When I’m on a page and click an anchor link that points to a different page, the site should navigate to that page and then scroll to that anchor. I already have the following script working:
// Function to handle scroll to hash with a promise
function scrollToHash(hash, isNewPage = false) {
  
  if (!hash) return;
  
  // For same page links, scroll immediately
  if (!isNewPage) {
    const target = document.getElementById(hash.replace('#', ''));
    const headerOffset = window.innerWidth < 768 ? -78 : -0;
    if (target && window.brfScrollSmoother && window.brfScrollSmoother.smoother) {
      window.brfScrollSmoother.smoother.scrollTo(target, {
        duration: 1.2,
        immediate: false,
        offset: headerOffset
      });
    }
    return;
  }
  // For new page navigation, wait for everything to be ready
  return new Promise((resolve) => {
    setTimeout(() => {
      const target = document.getElementById(hash.replace('#', ''));
      const headerOffset = window.innerWidth < 768 ? -78 : -0;
      if (target && window.brfScrollSmoother && window.brfScrollSmoother.smoother) {
        // Remove hash from URL to prevent browser jump
        history.pushState("", document.title, window.location.pathname + window.location.search);
        
        window.brfScrollSmoother.smoother.scrollTo(target, {
          duration: 1.2,
          immediate: false,
          offset: headerOffset
        });
      }
      resolve();
    }, 500);
  });
}
// Handle all anchor links
document.querySelectorAll('a[href*="#"]').forEach(link => {
  link.addEventListener('click', (e) => {
    const href = link.getAttribute('href');
    const currentUrl = window.location.origin + window.location.pathname;
    const isExternal = href.includes(window.location.origin) || href.startsWith('http');
    const targetUrl = isExternal ? href.split('#')[0] : currentUrl;
    const hash = isExternal ? href.split('#')[1] : href.replace('#', '');
    
    // If we're navigating to the same page
    if (targetUrl === currentUrl) {
      e.preventDefault();
      scrollToHash(hash, false); // No delay for same page
    }
    // For different pages, modify the href to prevent automatic jump
    else if (hash) {
      e.preventDefault();
      sessionStorage.setItem('scrollToHash', hash);
      window.location.href = targetUrl;
    }
  });
});
// Handle stored hash from previous page
const storedHash = sessionStorage.getItem('scrollToHash');
if (storedHash) {
  scrollToHash(storedHash, true); // Use delay for new page
  sessionStorage.removeItem('scrollToHash');
}
  1. When I enter a URL in the browser that includes an anchor, the page should load, the URL be cleaned up, and it should automatically scroll down to that anchor. I haven’t achieved this via script yet.

Would it be possible to add these as options for Lenis directly in Bricksforge?

Many thanks.