স্ট্যাক ড্রিল ডাউন

এক নজরে

স্ট্যাক ড্রিল-ডাউন হল একটি হায়ারার্কিক্যাল নেভিগেশন প্যাটার্ন, যা মোবাইল অ্যাপে সাধারণ, যেখানে একটি লিঙ্ক অ্যাক্টিভেট করলে আগেরটির উপরে একটি নতুন ফুল-স্ক্রিন ভিউ পুশ করা হয়। ভিউয়ের কন্টেন্ট অ্যাপ্লিকেশন-সংজ্ঞায়িত — সেটিংসের উপ-পৃষ্ঠা, ফিডের মধ্যে থ্রেড, ফাইল ব্রাউজারের মধ্যে ফোল্ডার, গ্যালারির মধ্যে বিস্তারিত বিবরণ সহ পৃষ্ঠা ইত্যাদি। ব্যবহারকারী বর্তমান ভিউ স্ক্রিনের ডানদিকে সোয়াইপ করে বা ফিরে যাওয়ার বোতামে ট্যাপ করে ফিরে আসেন। ব্রাউজার ইতিহাস সিঙ্ক করা থাকে যাতে OS-লেভেল ব্যাক জেসচার, ডিপ লিঙ্ক এবং ফরওয়ার্ড/ব্যাক নেভিগেশন সবই একসাথে কাজ করে।

এই নির্দেশিকা স্ট্যাককে এইভাবে প্রয়োগ করে:

  • একটি অনুভূমিক CSS স্ক্রল-স্ন্যাপ কন্টেনার যেখানে প্রতিটি ভিউ ঠিক একটি স্ন্যাপ স্টপ। ড্রিল ডাউন করলে নতুন ভিউ যোগ হয় এবং স্মুথ-স্ক্রল করে সেখানে নিয়ে যায়; সোয়াইপ-ব্যাক জেসচার ব্রাউজার নেটিভভাবে ম্যানেজ করে, যার ফলে কোনও পয়েন্টার-ইভেন্ট জাভাস্ক্রিপ্ট ছাড়াই মোমেন্টাম, ভেলোসিটি ও ইন্টারাপশন ট্র্যাকিং ফ্রিতে পাওয়া যায়।
  • স্ট্যাকে একটি scrollsnapchange ইভেন্ট লিসনার যা স্ন্যাপ টার্গেট পরিবর্তন হলে ফায়ার করে। "অ্যাক্টিভ ভিউ পরিবর্তন হয়েছে" — এই তথ্যের জন্য এটিই একমাত্র সোর্স হিসেবে ব্যবহার করা হয়, তাই সোয়াইপ, ক্লিক, প্রোগ্রাম্যাটিক স্ক্রল ও popstate পাথ, সব একটি কলব্যাকে একত্রিত হয় যা inert আপডেট করে, ফোকাস ফিরিয়ে আনে, ব্যবহারকারী সোয়াইপ করে চলে গেছেন এমন ভিউ বাদ দেয় এবং ব্রাউজার ইতিহাস রিকনসাইল করে।
  • pushState / popstate ইন্টিগ্রেশন, যাতে প্রতিটি ড্রিল-ডাউন একটি ইতিহাস এন্ট্রি যোগ করে, OS-লেভেল ব্যাক জেসচার কাজ করে এবং ডিপ লিঙ্ক সরাসরি সঠিক ভিউতে খোলে।
  • প্রতিটি ভিউতে একটি (ঐচ্ছিক) স্ক্রল-চালিত view() অ্যানিমেশন যা সরাসরি সোয়াইপ জেসচারের সাথে যুক্ত একটি প্যারালাক্স + ডিম + শ্যাডো এফেক্ট তৈরি করে, তাই দৃশ্যমান গতি ব্যবহারকারীর আঙুল দ্বারা চালিত হয়, টুইন নয়।

JavaScript-চালিত transform অ্যানিমেশনের চেয়ে এই পদ্ধতি বেশি পছন্দ করা হয় কারণ স্ন্যাপ মেকানিজম ব্যবহারকারীকে প্যানেলের পজিশনের সরাসরি জেসচার কন্ট্রোল দেয় (তাদের আঙুল এটি চালায়, কোনও টুইন নয়) এবং ব্যবহারকারীরা নেটিভ মোবাইল অ্যাপ থেকে যে ধরনের ইন্টার‍্যাকশন প্যাটার্ন আশা করেন তার সাথে এটি মিলে যায়।

প্রয়োগ

১. Markup

স্ট্যাটিক HTML হল একটি খালি স্ট্যাক কন্টেনার; ভিউ জাভাস্ক্রিপ্টে তৈরি করা হয় এবং ব্যবহারকারী নেভিগেট করার সাথে সাথে যোগ করা হয়।

<div class="Stack">
  <!-- Intentionally empty. The initial view is appended by JavaScript
       at init time (step 8). -->
</div>

প্রতিটি ভিউ হল .Stack-এর (স্ন্যাপ টার্গেট) .Stack-view ডাইরেক্ট চাইল্ড যার মধ্যে .Stack-viewContent র‍্যাপার থাকে (যেখানে ভিউ কন্টেন্ট থাকে এবং যেখানে প্যারালাক্স ট্রান্সফর্ম প্রযোজ্য হয় — ধাপ ২ দেখুন)। যেকোনও মুহূর্তে স্ট্যাকে প্রতিটি অ্যাক্টিভ ইতিহাস এন্ট্রির জন্য একটি করে ভিউ থাকে, যা ড্রিল-ডাউন ক্রমে বাঁদিক থেকে ডানদিকে সাজানো থাকে। ব্যবহারকারী রুট থেকে দুটি লেভেলে ড্রিল-ইন করার পরে, রেন্ডার করা DOM দেখতে এইরকম হয়:

<div class="Stack">
  <!-- Root view. Has whatever content makes sense as the entry point
       of this section of the app. No back button — there is nothing
       behind the root in the stack. -->
  <div class="Stack-view" inert>
    <div class="Stack-viewContent">
      <!-- Root content; includes <a href> links that drill in. -->
    </div>
  </div>

  <!-- First-level drill-down view. The user got here by activating a
       link in the root view. -->
  <div class="Stack-view" inert>
    <div class="Stack-viewContent">
      <header>
        <!-- DO include a back button. The swipe gesture only works on
             touch — keyboard and pointer users need an explicit control. -->
        <button class="back" aria-label="Back"></button>
        <!-- Title / breadcrumb / etc. -->
      </header>
      <main>
        <!-- View content; may include further drill-down <a href> links. -->
      </main>
    </div>
  </div>

  <!-- Second-level drill-down view. Currently visible — no `inert`
       attribute. Same shape as the first-level view. -->
  <div class="Stack-view">
    <div class="Stack-viewContent">
      <header>
        <button class="back" aria-label="Back"></button>
        <!-- ... -->
      </header>
      <main><!-- ... --></main>
    </div>
  </div>
</div>

মনে রাখবেন:

  • বর্তমানে-দৃশ্যমান ভিউ ছাড়া সব ভিউতে inert অ্যাট্রিবিউট থাকে। এটি ৭ নম্বর ধাপে scrollsnapchange হ্যান্ডলারের মাধ্যমে অটোমেটিক প্রয়োগ করা/সরানো হয় — আপনার ভিউ বিল্ডার থেকে এটি সেট করবেন না।
  • ব্যবহারকারী সোয়াইপ করে যেসব ভিউ পেরিয়ে গেছেন সেগুলি DOM থেকে সরিয়ে দেওয়া হয় (৭ নম্বর ধাপের মাধ্যমেও) যাতে স্ট্যাক কখনও currentDepth + 1-এর বেশি না হয়। ফরওয়ার্ড নেভিগেশন তাদের কাছে ফিরে আসলে, তাদের ক্যাশে করা URL পাথ থেকে চাহিদা অনুযায়ী সেগুলি আবার তৈরি করা হয়।

২. স্টাইল

স্ট্যাক স্ক্রলার

স্ট্যাক হল একটি অনুভূমিক গ্রিড যেখানে প্রতিটি চাইল্ড ভিউ ঠিক কন্টেনারের প্রস্থের সমান, CSS স্ক্রল-স্ন্যাপ প্রতি-স্ন্যাপে-একটি-ভিউ প্রয়োগ করে। এটিই সোয়াইপ-ব্যাক জেসচারকে তার নেটিভ অনুভূতি দেয়।

.Stack {
  /* Use dvh so the height tracks the dynamic viewport on mobile, where
     the address bar can show/hide. svh would clip during the address bar
     animation; vh leaks under it. */
  height: 100dvh;

  /* Lay views out left-to-right, each one full-width, so horizontal
     scrolling moves between them one at a time. */
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 100%;
  grid-template-rows: 100%;
  overflow-x: auto;

  /* `mandatory` guarantees the stack always settles fully on a view —
     never half-way between two. */
  scroll-snap-type: x mandatory;
  /* Prevent the swipe-back gesture from chaining into the browser's
     own history-back gesture (iOS, some Android) or the page's vertical
     scroll. The user is navigating the stack, not the page. */
  overscroll-behavior-x: none;
}

/* Hide the visual scrollbar — the snap and the parallax are the
   affordances; a horizontal scrollbar would look out of place. */
.Stack::-webkit-scrollbar {
  display: none;
}

/* MANDATORY: Opt into smooth programmatic scrolling via CSS, gated on
   prefers-reduced-motion. JS code calls scrollTo/scrollBy with
   behavior: 'auto' which defers to this rule, so the OS-level reduced-
   motion preference automatically downgrades to instant scrolling
   without any per-call JS branching. */
@media (prefers-reduced-motion: no-preference) {
  .Stack {
    scroll-behavior: smooth;
  }
}

.Stack-view {
  scroll-snap-align: start;
  /* `always` prevents the user from blowing through more than one view
     per gesture, so depth changes always happen one step at a time. */
  scroll-snap-stop: always;
}

/* MANDATORY: A separate inner element is required for the parallax
   transform below. Applying transforms directly to the snap target
   (.Stack-view) would feed back into the scroll container's snap
   geometry and the scroller would jump mid-gesture. */
.Stack-viewContent {
  width: 100%;
  height: 100%;
  background-color: #fff;
  /* Each view scrolls its own content vertically, independent of the
     stack's horizontal scroll. */
  overflow-y: auto;
}

"স্ট্যাক" এফেক্ট (প্যারালাক্স / ডিম / শ্যাডো)

স্ক্রল-চালিত view(inline) অ্যানিমেশন স্ট্যাক স্ক্রলারের মাধ্যমে প্রতিটি ভিউয়ের প্রগ্রেস ট্র্যাক করে এবং এক্সিট করা ভিউতে প্যারালাক্স + ডিম প্রয়োগ করে, সাথে ইনকামিং ড্রিল-ডাউন ভিউতে শ্যাডো প্রয়োগ করে যাতে সেগুলি আগের ভিউয়ের উপরে স্ট্যাক করা "কার্ড" হিসেবে পড়া যায়।

/* MANDATORY: Wrap the animation block in @supports. Browsers without
   scroll-driven animations still parse the @keyframes and would
   apply the `to` state as a static style, leaving every view
   permanently transformed. The @supports gate confines the animation
   to browsers where it actually animates. */
@supports (animation-timeline: view()) {
  .Stack-viewContent {
    /* view(inline) tracks this element's progress through its nearest
       scrollable ancestor on the inline (x) axis. */
    animation: parallax linear both;
    animation-timeline: view(inline);
    /* Only animate the EXIT phase — when this view is being covered
       by a deeper one. During its own entry the view stays at rest,
       so the fresh content is fully bright and in position throughout. */
    animation-range: exit 0% exit 100%;
  }

  /* Drill-down views (everything except the root) also get a shadow on
     their left edge during the transition so they feel like cards
     stacking over the previous view. */
  .Stack-view:not(:first-child) .Stack-viewContent {
    animation: parallax linear both, shadow-fade linear both;
    animation-timeline: view(inline), view(inline);
    /* parallax: only exit (the view sliding back as a deeper one comes in).
       shadow-fade: entry through exit (visible the whole time the view is
       transitioning, not when it's at rest). */
    animation-range: exit 0% exit 100%, entry 0% exit 100%;
  }

  @keyframes parallax {
    /* translateX(75%) and brightness(0.8) are examples — adjust to taste. */
    to {
      transform: translateX(75%);
      filter: brightness(0.8);
    }
  }

  @keyframes shadow-fade {
    /* Shadow ramps in during entry, holds across the middle of the
       gesture, and ramps out during exit — so it's only visible while
       the view is mid-transition, not when at rest. */
    0%, 100% { box-shadow: 0 0 1.5rem #0000; }
    25%, 75% { box-shadow: 0 0 1.5rem #0004; }
  }
}

মনে রাখবেন: এই এফেক্ট আধুনিক নেটিভ স্ট্যাক অ্যাপ্লিকেশনে জনপ্রিয় এবং এটি একটি ভালো স্টার্টিং পয়েন্ট, তবে প্রয়োজন অনুযায়ী বর্তমান ট্রানজিশন স্টাইলের সাথে মানানসই করার জন্য নির্দিষ্ট ভিজ্যুয়াল এফেক্ট কাস্টমাইজ করা যেতে পারে।

৩. মডিউলের অবস্থা

স্ট্যাকটি মডিউল স্কোপে চারটি স্টেট ট্র্যাক করে:

const stack = document.querySelector('.Stack');

// Reference to the root view DOM element. The Stack starts empty, so
// this is null until the root view is created — either at init (step 8,
// when the URL is '/') or later by synthesizeRootEntry() (when the user
// landed on a deep link and then navigates back). Held as a mutable
// reference because other code (the scrollsnapchange handler, init, etc.)
// uses identity comparisons against it.
let rootView = null;

// Tracks which element to restore focus to when the user swipes back
// into a previous view. Keyed by the view element itself so entries
// are garbage-collected automatically when the view is pruned.
const returnFocus = new WeakMap();

// Maps history depth -> {urlPath, view}. We MUST maintain this map
// ourselves because the History API does not expose state for entries
// other than the current one — so when a view is pruned on swipe-back,
// we still need to remember which URL it represented in case the user
// later forward-navigates back into it.
const entriesByDepth = new Map();

// Tracked manually because history.state on a popstate event tells us
// the destination depth but not where we came from. We need both to
// compute the direction (back vs forward) and the distance.
let currentDepth = 0;

এছাড়াও, তিনটি অ্যাপ্লিকেশন-নির্দিষ্ট হেল্পার — একমাত্র জায়গা যেখানে আপনার অ্যাপের রাউটিং এবং ভিউ রেন্ডারিং প্লাগ-ইন:

// Resolve a URL path to the data your app needs to render the
// corresponding drill-down view, or return null for paths this section
// of the app does not handle (the root path '/', external links,
// unknown routes). resolveUrl() is for drill-down routes only — the
// root view is rendered separately by createRootView() below.
function resolveUrl(urlPath) {
  // Replace with your routing logic. For example, match `/view/:id`
  // and look the id up in your app state.
}

// Build the root (home) view of the stack. Application-specific content;
// preserve the .Stack-view / .Stack-viewContent wrapper structure (the
// inner element is required for the parallax — see step 2) and DO NOT
// render a back button — the root view has nothing behind it in the
// stack.
function createRootView() {
  const view = document.createElement('div');
  view.className = 'Stack-view';
  view.innerHTML = `
    <div class="Stack-viewContent">
      <!-- Root content. Include <a href> elements pointing at URL paths
           that resolveUrl() accepts, to enable drill-down from here. -->
    </div>
  `;
  return view;
}

// Build a drill-down view DOM element from the resolved route data.
// Customize the inner content freely, but DO preserve the .Stack-view
// / .Stack-viewContent wrapper structure and DO include a back button
// (the swipe gesture only works on touch).
function createDrillDownView(routeData) {
  const view = document.createElement('div');
  view.className = 'Stack-view';
  view.innerHTML = `
    <div class="Stack-viewContent">
      <header>
        <button class="back" aria-label="Back"></button>
        <!-- Title, breadcrumb, or other view chrome derived from
             routeData. -->
      </header>
      <main>
        <!-- View body, also from routeData. Include further <a href>
             elements pointing at URL paths that resolveUrl() also
             accepts, to enable additional drill-downs from this view. -->
      </main>
    </div>
  `;
  return view;
}

function getCurrentUrlPath() {
  return location.pathname;
}

৪. ড্রিল ডাউন করুন

ড্রিল-ডাউন এই ক্রমে চারটি কাজ করে: ইতিহাস এন্ট্রি পুশ করা, নতুন ভিউ তৈরি করা, এটি যোগ করা, এটিতে স্মুথ-স্ক্রল করা। স্ন্যাপ সেটেল হয়ে গেলে scrollsnapchange হ্যান্ডলার (ধাপ ৭) সেখান থেকে শুরু করে।

function drillDown(urlPath) {
  const routeData = resolveUrl(urlPath);
  if (!routeData) return;

  const newDepth = currentDepth + 1;
  // Push BEFORE creating the view so the URL is correct if anything
  // observing history (analytics, etc.) reads it during view creation.
  history.pushState({depth: newDepth}, '', urlPath);

  // pushState truncates forward entries in real browser history;
  // mirror that truncation in our depth map so we don't hold references
  // to views the user can no longer reach.
  for (const d of entriesByDepth.keys()) {
    if (d >= newDepth) entriesByDepth.delete(d);
  }
  currentDepth = newDepth;

  const newView = createDrillDownView(routeData);
  stack.appendChild(newView);
  entriesByDepth.set(newDepth, {urlPath, view: newView});

  // Scroll one viewport-width to the right. behavior: 'auto' defers to
  // the CSS `scroll-behavior` set in step 2, which is smooth unless
  // prefers-reduced-motion is set. The snap container locks onto the
  // new view; the scrollsnapchange listener (step 7) fires when the
  // snap settles.
  stack.scrollBy({left: stack.clientWidth, behavior: 'auto'});
}

৫. ক্লিক ও ফিরে যাওয়ার বোতাম ম্যানেজ করা

স্ট্যাকের মধ্যে লিঙ্ক ক্লিক ইন্টারসেপ্ট করুন এবং সেগুলিকে ড্রিল-ডাউনে কনভার্ট করুন। পরিবর্তক কী-এর আচরণ বজায় রাখুন যাতে cmd/মিডল-ক্লিক এখনও নতুন ট্যাবে লিঙ্ক খোলে।

stack.addEventListener('click', (e) => {
  // Back button: defer to goBack() (defined below), which handles both
  // the normal in-app case and the deep-link case.
  if (e.target.closest('.back')) {
    goBack();
    return;
  }

  // Drill-down link.
  const link = e.target.closest('a');
  if (!link || !stack.contains(link)) return;
  // Let the browser handle modified clicks so users can open links
  // in new tabs / windows. e.button !== 0 filters out middle-clicks.
  if (e.metaKey || e.ctrlKey || e.shiftKey || e.button !== 0) return;

  const urlPath = new URL(link.href).pathname;
  const parentView = link.closest('.Stack-view');
  // If the URL isn't handled by this section of the app (resolveUrl
  // returns null), fall through so the browser navigates normally.
  if (!resolveUrl(urlPath) || !parentView) return;

  e.preventDefault();
  // Record which link the user activated so focus can be restored to
  // it when they swipe (or click) back into this view.
  returnFocus.set(parentView, link);
  drillDown(urlPath);
});

// Going back is usually just history.back(), but there's an important
// edge case: when the user lands directly on a deep-linked URL, there
// is no in-app history entry behind it. Calling history.back() in that
// situation would take them out of the app entirely. MANDATORY: detect
// this case and synthesize a root entry instead, so an in-app Back from
// a deep link lands on the root view and the platform Back from there
// returns the user to where they came from.
function goBack() {
  const atDeepLinkRoot = currentDepth === 0
    && entriesByDepth.get(0)?.view !== rootView;
  if (atDeepLinkRoot) {
    synthesizeRootEntry();
  } else {
    // history.back() fires popstate, which routes through
    // updateFromHistoryState (step 6) and scrolls the stack — the
    // same path a swipe-back converges on.
    history.back();
  }
}

function synthesizeRootEntry() {
  // Push a new history entry pointing at the root URL. This becomes the
  // entry the user "came from"; the original deep-linked entry is now
  // behind us, so platform Back from the root view will return there.
  const newDepth = currentDepth + 1;
  history.pushState({depth: newDepth}, '', '/');

  // Create the root view if it doesn't exist yet (we landed on a deep
  // link and never needed it before now), and insert it at the LEFT end
  // of the stack. Adjust scrollLeft by one viewport width so the user's
  // view doesn't visually jump — they should still be looking at the
  // deep-linked view until the scroll animation below runs.
  if (!rootView) {
    rootView = createRootView();
    stack.prepend(rootView);
    stack.scrollLeft += stack.clientWidth;
  }
  entriesByDepth.set(newDepth, {urlPath: '/', view: rootView});

  // Now scroll to the new entry (the root view). updateFromHistoryState
  // smooth-scrolls one step left, the parallax plays, and
  // scrollsnapchange fires when the root view settles.
  updateFromHistoryState(history.state);
}

৬. ইতিহাস থেকে সিঙ্ক করুন (popstate)

popstate ব্যবহারকারী ব্রাউজার/OS-এর 'ফিরে যান' বা 'এগিয়ে যান' বোতাম ব্যবহার করলে অথবা জাভাস্ক্রিপ্ট history.back() / .go() কল করলে ফায়ার হয়। ইতিহাস পরিবর্তনের প্রতিক্রিয়া হিসাবে স্ট্যাক স্ক্রল করার একমাত্র পথ হল এই হ্যান্ডলার।

window.addEventListener('popstate', (event) => {
  updateFromHistoryState(event.state);
});

function updateFromHistoryState(state, behaviorOverride) {
  const newDepth = state?.depth ?? 0;
  const urlPath = getCurrentUrlPath();

  // Ensure entriesByDepth has an entry for the destination depth.
  // If the URL changed (e.g. forward-nav into a previously-pruned
  // view), clear the cached view reference so the loop below rebuilds.
  const entry = entriesByDepth.get(newDepth) ?? {view: null};
  if (entry.urlPath !== urlPath) {
    entry.urlPath = urlPath;
    entry.view = urlPath === '/' ? rootView : null;
  }
  entriesByDepth.set(newDepth, entry);

  // Rebuild any views between root and the destination that were
  // pruned earlier (when the user swiped back past them). Without
  // this, forward-navigating to a previously-pruned view would have
  // no element to scroll to.
  for (let d = 0; d <= newDepth; d++) {
    const e = entriesByDepth.get(d);
    if (!e || e.view) continue;
    const routeData = resolveUrl(e.urlPath);
    if (!routeData) continue;
    const rebuilt = createDrillDownView(routeData);
    stack.appendChild(rebuilt);
    e.view = rebuilt;
  }

  currentDepth = newDepth;

  const targetView = entriesByDepth.get(newDepth)?.view;
  if (!targetView) return;

  // Compare destination index against current scroll position so we
  // can bail if they're already aligned. This is reached when the
  // scrollsnapchange handler below calls history.go() to sync history
  // after a swipe-back that already completed visually — there's
  // nothing more to scroll.
  const toIdx = [...stack.children].indexOf(targetView);
  const fromIdx = Math.round(stack.scrollLeft / stack.clientWidth);
  if (fromIdx === toIdx) return;

  // Pick a scroll behavior:
  //   - multi-step jumps (e.g. history.go(-3)): 'instant' to skip
  //     intermediate snap points — otherwise smooth-scrolling would
  //     fire scrollsnapchange for each one and do N rounds of
  //     state-transition work for no reason.
  //   - rightward (forward) single-step: 'instant'. Browser-forward is
  //     rare on the web and is often spurious (e.g. iOS Safari treats
  //     edge swipes as forward navigation, even with overscroll-behavior
  //     set). An instant swap reads as "snap" rather than a misleading
  //     drilldown animation the user didn't ask for. The user-initiated
  //     drill-down path (drillDown, step 4) is unaffected — it calls
  //     scrollBy directly and never reaches this code.
  //   - leftward (back) single-step: 'auto' so the CSS `scroll-behavior`
  //     (smooth unless prefers-reduced-motion is set — see step 2)
  //     applies. Back is the common, expected case and benefits from
  //     the animation.
  // NOTE: "forward" here means spatial direction (toIdx > fromIdx),
  // NOT depth direction. synthesizeRootEntry (step 5) pushes a new
  // depth but scrolls LEFT to the root view, which correctly reads as
  // back-style (smooth).
  const forward = toIdx > fromIdx;
  const multiStep = Math.abs(toIdx - fromIdx) > 1;
  const behavior = behaviorOverride ?? (forward || multiStep ? 'instant' : 'auto');
  stack.scrollTo({left: toIdx * stack.clientWidth, behavior});
}

৭. scrollsnapchange: single source of truth

প্রতিটি স্ন্যাপ কমিটের পরে — তা সোয়াইপ, ক্লিক বা প্রোগ্রামেটিক স্ক্রলের মাধ্যমে ট্রিগার করা হোক না কেন — ব্রাউজার স্ক্রল কন্টেনারে একটি scrollsnapchange ইভেন্ট ফায়ার করে, যার ফলে নতুন স্ন্যাপ করা এলিমেন্ট event.snapTargetInline (হরাইজন্টাল স্ন্যাপিংয়ের জন্য) হিসেবে দেখা যায়। এই একটি হ্যান্ডলারের মধ্যে সব স্টেট ট্রানজিশন রাখলে, সোয়াইপ পাথ, ক্লিক পাথ ও popstate পাথ সামঞ্জস্যপূর্ণ থাকে।

হ্যান্ডলারটি একটি স্বতন্ত্র onActiveViewChanged ফাংশনে এক্সট্র্যাক্ট করা হয় যাতে ফলব্যাক (নিচে "ফলব্যাক স্ট্র্যাটেজি" দেখুন) লজিক ডুপ্লিকেট না করেই এটি পুনরায় ব্যবহার করতে পারে।

function onActiveViewChanged(currentView) {
  // Walk the stack in DOM order to update each view's role:
  //  - Views at or before currentView stay in the DOM but get
  //    `inert` (except currentView) so focus, pointer events, and
  //    AT navigation cannot leak into views hidden behind the
  //    parallax.
  //  - Views after currentView are unreachable (the user swiped
  //    back past them) so we drop them from the DOM to free memory.
  //    Their urlPath stays in entriesByDepth so a later forward
  //    navigation can rebuild the view from scratch.
  let seenCurrent = false;
  for (const view of [...stack.children]) {
    if (seenCurrent) {
      for (const e of entriesByDepth.values()) {
        if (e.view === view) e.view = null;
      }
      view.remove();
    } else {
      // MANDATORY: inert non-current views. Without this, tabbing
      // and screen-reader navigation can reach content hidden behind
      // the parallax — a severe accessibility failure that's
      // invisible to sighted users.
      view.toggleAttribute('inert', view !== currentView);
      if (view === currentView) seenCurrent = true;
    }
  }

  // If the visible view's depth doesn't match `currentDepth`, the
  // user got here by swiping (not clicking) — sync history so the
  // browser back/forward buttons stay coherent with what's on screen.
  let currentViewDepth;
  for (const [d, e] of entriesByDepth) {
    if (e.view === currentView) currentViewDepth = d;
  }
  if (currentViewDepth !== undefined && currentViewDepth !== currentDepth) {
    // history.go fires popstate, which re-enters updateFromHistoryState.
    // That call's fromIdx === toIdx check bails out without scrolling.
    history.go(currentViewDepth - currentDepth);
  }

  // Restore focus on the now-active view:
  //  - If we recorded which link the user activated to drill out
  //    of this view, return focus there so a swipe-back lands them
  //    exactly where they left off.
  //  - Otherwise (a freshly-pushed drill-down view), move focus to
  //    the back button so keyboard users have an obvious next action.
  //  - preventScroll is REQUIRED: without it, .focus() scrolls the
  //    snap container to bring the focused element into view, which
  //    fights the snap and can land the user mid-snap.
  const stored = returnFocus.get(currentView);
  if (stored) {
    stored.focus({preventScroll: true});
    returnFocus.delete(currentView);
  } else if (currentView !== rootView) {
    currentView.querySelector('.back')?.focus({preventScroll: true});
  }
}

stack.addEventListener('scrollsnapchange', (event) => {
  // snapTargetInline is the element that was just snapped to on the
  // inline (horizontal) axis. For this stack — where each view is one
  // horizontal snap stop — that's the new active view.
  onActiveViewChanged(event.snapTargetInline);
});

পৃষ্ঠা লোড হওয়ার সময়, URL আগে থেকেই ডিপ ভিউতে (শেয়ার করা লিঙ্ক, বুকমার্ক, ডিপ পৃষ্ঠায় রিফ্রেশ) পয়েন্ট করতে পারে। URL-এর সাথে মেলে এমন প্রাথমিক ভিউ তৈরি করুন — রুট বা ডিপ-লিঙ্ক করা, তবে দুটিই নয় — এটি খালি স্ট্যাকে যোগ করুন, ডেপথ-০ ইতিহাস এন্ট্রি সিড করুন এবং behavior: 'instant'-এর সাথে প্রাথমিক স্ক্রল পাস রান করুন যাতে প্রথম পেইন্টে প্যারালাক্স অ্যানিমেট না হয়।

const initialUrlPath = getCurrentUrlPath();
const initialRouteData = resolveUrl(initialUrlPath);

// Build the initial view: a drill-down view if the URL maps to one,
// otherwise the root view. Whichever it is, that's the only view in
// the stack right now — the other will be created lazily by
// synthesizeRootEntry (step 5) or drillDown (step 4) if the user
// navigates to it.
let initialView;
if (initialRouteData) {
  initialView = createDrillDownView(initialRouteData);
} else {
  rootView = createRootView();
  initialView = rootView;
}
stack.appendChild(initialView);

entriesByDepth.set(0, {urlPath: initialUrlPath, view: initialView});
// replaceState attaches a `depth` to the entry the user landed on, so
// any subsequent pushState / popstate has a base depth to count from.
history.replaceState({depth: 0}, '');

updateFromHistoryState(history.state, 'instant');

পেশাদার পদ্ধতি

  • "অ্যাক্টিভ ভিউ পরিবর্তন হয়েছে" সংক্রান্ত তথ্যের সোর্স হিসেবে scrollsnapchange ইভেন্ট (IntersectionObserver ফলব্যাক সহ — "ফলব্যাক স্ট্র্যাটেজি" দেখুন) ব্যবহার করুন, স্ক্রল-ইভেন্ট কোঅর্ডিনেট নয়। স্ন্যাপ কমিট হল একমাত্র ইভেন্ট যা সোয়াইপ, ক্লিক, প্রোগ্রাম্যাটিক স্ক্রল এবং popstate পাথ জুড়ে ধারাবাহিকভাবে ফায়ার করে।
  • স্ন্যাপ টার্গেটের চাইল্ডে ট্রান্সফর্ম প্রয়োগ করুন, স্ন্যাপ টার্গেটে কখনও নয়। স্ন্যাপ টার্গেটে ট্রান্সফর্ম, স্ক্রল কন্টেনারের স্ন্যাপ জ্যামিতিতে ফিডব্যাক দেয় এবং স্ক্রলার মিড-জেসচারে গ্লিচ করবে।
  • বর্তমানে দেখা যাচ্ছে এমন ভিউ ছাড়া প্রতিটি ভিউতে inert প্রয়োগ করুন। এটি না থাকলে, ফোকাস ও স্ক্রিন-রিডার নেভিগেশন প্যারালাক্সের পিছনে লুকানো ভিউতে লিক হয়ে যায় — যা দৃষ্টিশক্তি সম্পন্ন ব্যবহারকারীদের কাছে অদৃশ্য হলেও অ্যাক্সেসিবিলিটির ক্ষেত্রে গুরুতর ব্যর্থতা।
  • প্রতিটি ড্রিল-ডাউনে একটি ইতিহাস এন্ট্রি পুশ করুন এবং popstate ম্যানেজ করুন যাতে OS-লেভেল ব্যাক জেসচার এবং ব্রাউজারের ব্যাক/ফরওয়ার্ড বোতাম কাজ করে। এই প্যাটার্নটি নেটিভ অ্যাপের মতো অনুভূতি দেয়।
  • DO অ্যাক্টিভ-ভিউ-চেঞ্জড হ্যান্ডলার থেকে ইতিহাস রিকনসাইল করুন যখন সোয়াইপ-ব্যাক এমন একটি ভিউতে ল্যান্ড করে যার গভীরতা currentDepth-এর সাথে মেলে না। এটি না থাকলে, পরবর্তী OS ব্যাক ব্যবহারকারীকে অপ্রত্যাশিত কোথাও ফিরিয়ে দেয় কারণ ব্রাউজারের ইতিহাস কার্সর স্ক্রিনে যা আছে তার সাথে সিঙ্ক হয় না।
  • DOM থেকে ব্যবহারকারীর সোয়াইপ করা ভিউ ছাঁটুন। দীর্ঘ ড্রিল-ডাউন সেশন অন্যথায় ডজন ডজন বিচ্ছিন্ন সাবট্রি জমা করতে পারে। entriesByDepth-এ ক্যাশে করা URL পাথ, ফরোয়ার্ড নেভিগেশন ফিরে এলে যেকোনও ভিউ আবার তৈরি করার জন্য যথেষ্ট।
  • স্ট্যাকের মধ্যে ফোকাস ফিরিয়ে আনার সময় .focus({preventScroll: true}) কল করুন। ডিফল্ট preventScroll: false ব্রাউজারকে ফোকাস টার্গেট ভিউতে স্ক্রল করায়, যা স্ন্যাপ কন্টেনারের সাথে লড়াই করে এবং ব্যবহারকারীকে মিড-স্ন্যাপে ল্যান্ড করতে পারে।
  • ইন্টার্নাল লিঙ্কে cmd/ctrl/মিডল-ক্লিক সংরক্ষণ করুন যাতে URL শেয়ার করা যায় এবং নতুন ট্যাবে খোলা যায়।
  • মাল্টি-স্টেপ হিস্ট্রি জাম্প এবং স্পেশাল-ফরওয়ার্ড পপস্টেট ট্রানজিশনের (toIdx > fromIdx) জন্য behavior: 'instant' ব্যবহার করুন। মাল্টি-স্টেপ জাম্প অন্যথায় প্রতিটি ইন্টারমিডিয়েট স্ন্যাপে scrollsnapchange ফায়ার করবে এবং N রাউন্ডের ইনার্ট/ফোকাস/হিস্ট্রি সংক্রান্ত কাজ করবে। ফরওয়ার্ড পপস্টেট প্রায়ই নকল হয় — iOS Safari এজ সোয়াইপকে ব্রাউজার ফরওয়ার্ড হিসেবে বিবেচনা করে, এমনকি overscroll-behavior-x: none সেট করা থাকলেও এবং ব্যবহারকারী শুরু করেননি এমন "ড্রিলডাউন" অ্যানিমেট করার চেয়ে ইনস্ট্যান্ট সোয়াপ অনেক কম বিভ্রান্তিকর। ব্যবহারকারীর শুরু করা ড্রিল-ডাউন পাথ (drillDown) প্রভাবিত হয় না কারণ এটি সরাসরি স্ক্রল করে, popstate-এর মাধ্যমে নয়।
  • DO respect prefers-reduced-motion: declare scroll-behavior: smooth only inside @media (prefers-reduced-motion: no-preference) and call scrollTo / scrollBy with behavior: 'auto' (not 'smooth') so the OS-level preference takes effect without per-call JS branching. হার্ড-কোডিং behavior: 'smooth' ব্যবহারকারীর সেটিং বাইপাস করে।
  • ড্রিল-ডাউন ট্রিগার হিসেবে আসল <a href> এলিমেন্ট রেন্ডার করুন, <button onclick> বা <div> নয়। আসল অ্যাঙ্কর হোভার, শেয়ার করার সুবিধা, মিডল-ক্লিক, স্ক্রিন-রিডার রোল এবং SEO-তে ফ্রিতে URL প্রিভিউ পায়।
  • প্রতিটি ড্রিল-ডাউন ভিউতে একটি স্পষ্ট ব্যাক বোতাম অন্তর্ভুক্ত করুন। সোয়াইপ জেসচার শুধু টাচ স্ক্রিনে কাজ করে — কীবোর্ড, পয়েন্টার ও ডেস্কটপ ব্যবহারকারীদের জন্য দৃশ্যমান অ্যাফর্ডেন্স প্রয়োজন।
  • popstate হ্যান্ডলার থেকে history.pushState কল করবেন না — ব্যবহারকারী ফিরে যাওয়ার চেষ্টা করার সময় এটি নতুন এন্ট্রি পুশ করে এবং ব্রাউজারের 'ফিরে যান' বোতামটি ভেঙে দেয়।
  • স্ক্রল-চালিত অ্যানিমেশন উপলভ্য থাকলে, scroll ইভেন্ট লিসনারের মাধ্যমে প্যারালাক্স ড্রাইভ করবেন না। CSS পাথ কম্পোজিটরে চলে; JS স্ক্রল লিসেনার মূল থ্রেডে চলে এবং জেসচার চলাকালীন দৃশ্যত ফ্রেম ড্রপ করবে।
  • সোয়াইপ-ব্যাক করার পরে DOM থেকে সরিয়ে দেওয়া ভিউ মিউটেট করবেন না। entriesByDepth-কে ক্যানোনিকাল রেকর্ড হিসেবে বিবেচনা করুন: একটি ছাঁটাই করা এন্ট্রিতে view: null আছে এবং updateFromHistoryState-এ চাহিদা অনুযায়ী পুনর্নির্মাণ করা হয়।

ফলব্যাক স্ট্র্যাটেজি

যেসব ফিচারের জন্য ফলব্যাক প্রয়োজন হতে পারে সেগুলি হল স্ক্রল-স্ন্যাপ-ইভেন্ট এবং স্ক্রল-চালিত-অ্যানিমেশন, এই দুটিরই শক্তিশালী ফলব্যাক বা প্রোগ্রেসিভ এনহ্যান্সমেন্ট স্টোরি আছে এবং এই ব্যবহারের ক্ষেত্রে ব্যবহার করা নিরাপদ:

স্ক্রল স্ন্যাপ ইভেন্ট

scrollsnapchange ইভেন্ট হল "অ্যাক্টিভ ভিউ পরিবর্তন হয়েছে" শনাক্ত করার সবচেয়ে সহজ উপায় — স্ট্যাকে একজন শ্রোতা, স্ন্যাপ কমিট প্রতি ঠিক একবার ফায়ার করা হয়। যেসব ব্রাউজারে এটি নেই, সেখানে একই এফেক্ট IntersectionObserver-এর মাধ্যমে পলিফিল করা যেতে পারে, যা স্ট্যাকের মধ্যে সম্পূর্ণ দৃশ্যমানতার জন্য প্রতিটি ভিউ দেখে। ফলব্যাক, প্রাথমিক পাথ ব্যবহার করে এমন একই onActiveViewChanged ফাংশনে ডিসপ্যাচ করে, তাই সব স্টেট-ট্রানজিশন লজিক এক জায়গায় থাকে।

// MANDATORY when supporting browsers that haven't shipped scroll-snap-events
// yet. Check `HTMLElement.prototype` (not `window` or `document`) — the
// event handler IDL attribute is added to the prototype when the feature
// is supported, regardless of whether any element has the handler set.
if (!('onscrollsnapchange' in HTMLElement.prototype)) {
  const viewObserver = new IntersectionObserver((entries) => {
    for (const entry of entries) {
      // threshold:1 only fires for fully-visible entries, but the
      // observer also emits a "leaving" entry per view that drops below
      // ratio 1. Filter to the entering side, which is the snap-commit
      // moment we're trying to detect.
      if (entry.intersectionRatio === 1) {
        onActiveViewChanged(entry.target);
      }
    }
  }, {root: stack, threshold: 1});

  // Auto-observe every .Stack-view as it's added to the stack, and stop
  // observing as it's removed. Using a MutationObserver lets the primary
  // code (drillDown, updateFromHistoryState, synthesizeRootEntry, init)
  // stay free of fallback wiring.
  new MutationObserver((mutations) => {
    for (const m of mutations) {
      for (const node of m.addedNodes) {
        if (node.classList?.contains('Stack-view')) viewObserver.observe(node);
      }
      for (const node of m.removedNodes) {
        if (node.classList?.contains('Stack-view')) viewObserver.unobserve(node);
      }
    }
  }).observe(stack, {childList: true});

  // Catch up to any views already in the stack at the time this code
  // runs (typically the initial view appended in step 8).
  for (const view of stack.children) viewObserver.observe(view);
}

স্ক্রল-চালিত অ্যানিমেশন

স্ক্রল-চালিত প্যারালাক্স / ডিম / শ্যাডো এফেক্ট হল নেভিগেশন কোরের উপরে একটি প্রোগ্রেসিভ এনহ্যান্সমেন্ট। CSS @supports (animation-timeline: view()) গেট (ধাপ ২-এ দেখানো হয়েছে) অ্যানিমেশনকে শুধু কাজ করে এমন ব্রাউজারে সীমাবদ্ধ করে; অন্য সব জায়গায় ভিউগুলি কোনও ট্রানজিশন ছাড়াই স্ন্যাপ স্টপের মধ্যে কাট করে। প্যারালাক্স ছাড়াই কম্পোনেন্টটি সম্পূর্ণভাবে কাজ করে — স্ন্যাপ, ইতিহাস সিঙ্ক, ফোকাস ম্যানেজমেন্ট এবং inert এখনও কাজ করে।

পুরনো বেসলাইন টার্গেটের জন্য প্যারালাক্স ফলব্যাক প্রয়োজন হলে, স্ট্যাকে scroll লিসেনার অ্যাটাচ করুন এবং স্ক্রলপোর্টের মাধ্যমে প্রতিটি ভিউয়ের প্রগ্রেস বর্ণনা করে একটি CSS কাস্টম প্রপার্টি লিখুন, তারপর সেই প্রপার্টি থেকে transform ও filter ড্রাইভ করুন:

if (!CSS.supports('animation-timeline: view()')) {
  stack.addEventListener('scroll', () => {
    const viewWidth = stack.clientWidth;
    for (const view of stack.children) {
      // Progress: 0 when this view is centered, 1 when it has fully
      // exited to the left. Matches the @keyframes mapping above.
      const offsetLeft = view.offsetLeft - stack.scrollLeft;
      const progress = Math.min(1, Math.max(0, -offsetLeft / viewWidth));
      const content = view.querySelector('.Stack-viewContent');
      content.style.transform = `translateX(${progress * 75}%)`;
      content.style.filter = `brightness(${1 - progress * 0.2})`;
    }
  });
}