खास जानकारी
नेविगेशन पैनल, एक ऐसा पैनल होता है जो व्यूपोर्ट के किनारे से स्लाइड होकर पेज के कॉन्टेंट के ऊपर आ जाता है. इससे उसके पीछे मौजूद कॉन्टेंट धुंधला हो जाता है. इसे ट्रिगर बटन से खोला जाता है. इसे बंद करने के लिए, पैनल को स्क्रीन से बाहर स्वाइप करें, धुंधले बैकड्रॉप पर टैप करें या Escape दबाएं.
इस गाइड में, ड्रॉर को इस तरह लागू किया गया है:
popover="manual"एलिमेंट को टॉप लेयर पर प्रमोट किया गया है, ताकि पैनल और उसका::backdrop, पेज पर मौजूद हर दूसरे एलिमेंट को ओवरले कर सके. भले ही, स्टैकिंग कॉन्टेक्स्ट कुछ भी हो.- हॉरिज़ॉन्टल स्क्रोलिंग कंटेनर, जिसमें दो सीएसएस स्क्रोल-स्नैप स्टॉप होते हैं — एक "खुला" और एक "बंद" — ताकि स्वाइप जेस्चर को ब्राउज़र नेटिव तौर पर हैंडल कर सके. यह बिना किसी JavaScript पॉइंटर-इवेंट कोड के, मोमेंटम, वेलोसिटी, और इंटरप्शन ट्रैकिंग की सुविधा मुफ़्त में देता है.
- स्क्रोल-ड्रिवन ऐनिमेशन, जो बैकड्रॉप की ओपैसिटी को स्क्रोल पोज़िशन से जोड़ता है. इससे उपयोगकर्ता के पैनल को खींचने और छोड़ने पर, डिम आसानी से फ़ेड इन और फ़ेड आउट होता है.
- पैनल पर मौजूद एक
IntersectionObserver, जो यह पता लगाता है कि पैनल कब पूरी तरह से व्यूपोर्ट में आया या पूरी तरह से बाहर गया. साथ ही, वह इन पलों का इस्तेमाल करके फ़ोकस,aria-expanded, औरinertको अपडेट करता है.
यह तरीका, JavaScript से चलने वाले transform ऐनिमेशन से बेहतर है. इसकी वजह यह है कि स्क्रोल करने के तरीके से, उपयोगकर्ता को पैनल की पोज़िशन पर सीधा कंट्रोल मिलता है. पैनल को उपयोगकर्ता की उंगली से कंट्रोल किया जाता है, न कि किसी ट्वीन से. साथ ही, यह इंटरैक्शन पैटर्न से काफ़ी मिलता-जुलता है. उपयोगकर्ता, खास ऑपरेटिंग सिस्टम के लिए बने ऐप्लिकेशन में इस पैटर्न का इस्तेमाल करते हैं.
लागू करना
1. मार्कअप
ड्रॉअर एक पॉपओवर होता है, जिसमें हॉरिज़ॉन्टल स्क्रोलर होता है. इसमें दिखने वाली "शीट" होती है. ट्रिगर बटन, पेज के कॉन्टेंट में मौजूद होता है.
<!-- popover="manual" is REQUIRED. Do not use popover="auto" or "hint". -->
<div class="Drawer" id="drawer" popover="manual">
<div class="Drawer-scroller">
<nav class="Drawer-sheet" tabindex="-1">
<!-- tabindex="-1" makes the sheet programmatically focusable so we
can move focus into it when the drawer opens, without adding it
to the natural tab order. -->
<ul>
<li><a href="/page-1">Page 1</a></li>
<li><a href="/page-2">Page 2</a></li>
<!-- ... -->
</ul>
</nav>
</div>
</div>
<main>
<header>
<!-- aria-controls links the trigger to the drawer; aria-expanded
reflects the current state for assistive tech. -->
<button id="drawer-open"
aria-label="Menu"
aria-expanded="false"
aria-controls="drawer">
<!-- MANDATORY: Inline decorative SVGs MUST define aria-hidden="true" -->
<svg aria-hidden="true" viewBox="0 0 24 24">...</svg>
</button>
</header>
<!-- Page content. -->
</main>
2. स्टाइल
पॉपओवर को रीसेट करना और व्यूपोर्ट को भरना
पॉपओवर को पूरे व्यूपोर्ट को कवर करना चाहिए, ताकि इसका ::backdrop पूरे पेज को धुंधला कर दे और स्वाइप करने की जगह एक किनारे से दूसरे किनारे तक फैल जाए. डिफ़ॉल्ट उपयोगकर्ता-एजेंट पॉपओवर स्टाइल (सेंटर में, अपने-आप साइज़ बदलने वाली, बॉर्डर वाली) की वजह से, पॉपओवर सही से नहीं दिखता. इसलिए, इसे रीसेट करना ज़रूरी है.
.Drawer {
/* min() caps the sheet width on large screens but on a phone leaves
a 20% peek of page content visible, which is the affordance that
tells the user they can tap outside to dismiss. */
--drawer-width: min(20em, 80dvw);
/* Custom property driven by the scroll-driven animation below.
0 = drawer fully closed (transparent backdrop).
1 = drawer fully open (visible backdrop). */
--drawer-backdrop: 0;
/* Reset UA popover style that would constrain the element. */
width: auto;
height: auto;
background: transparent;
border: 0;
overflow: visible;
}
/* Style the popover's ::backdrop to achieve the overlay effect and
provide visual affordances indicating that the rest of the page is inert */
.Drawer::backdrop {
background: #000;
/* Use calc() to limit the opacity range so the content beneath is visible */
opacity: calc(var(--drawer-backdrop) / 2);
}
स्क्रोल स्नैप की मदद से स्वाइप करने की सुविधा वाला इंटरफ़ेस बनाना
स्क्रोलर, व्यूपोर्ट से ज़्यादा चौड़ा हॉरिज़ॉन्टल ग्रिड होता है: कॉलम 1 में शीट (चौड़ाई --drawer-width) होती है, कॉलम 2 व्यूपोर्ट की चौड़ाई का खाली स्यूडो-एलिमेंट स्पेसर होता है. दोनों कॉलम के बीच स्नैप करने से, ड्रॉवर खुलता और बंद होता है.
.Drawer-scroller {
position: relative;
display: grid;
/* Sheet on the left, full-viewport spacer on the right. The user
scrolls between the two snap stops to open and close. */
grid-template-columns: var(--drawer-width) 100%;
overflow-x: scroll;
/* Stop the swipe from chaining into the page's vertical scroll
when the user reaches either snap edge. */
overscroll-behavior: none;
scrollbar-width: none;
/* `mandatory` guarantees the drawer always settles fully open or
fully closed — never half-open after a partial swipe. */
scroll-snap-type: x mandatory;
}
/* Enable smooth scrolling natively, but only if the user has not
requested reduced motion. */
@media (prefers-reduced-motion: no-preference) {
.Drawer-scroller {
scroll-behavior: smooth;
}
}
/* The empty spacer that creates the "closed" snap stop. */
.Drawer-scroller::after {
content: '';
scroll-snap-align: end;
/* Open the popover already scrolled to this stop (drawer off-screen),
so the JS only needs to scroll to the open position to
animate it in. */
scroll-initial-target: nearest;
}
.Drawer-sheet {
display: grid;
grid-template-rows: auto 1fr;
/* Use `svh` (small viewport height) — not `vh` or `dvh` — so the
sheet height does not jump when the iOS Safari address bar
resizes mid-swipe. */
height: 100svh;
background: #333;
color: #fff;
overflow-y: auto;
scroll-snap-align: start;
scrollbar-width: none;
}
बैकड्रॉप की अपारदर्शिता को स्क्रोल पोज़िशन से जोड़ना
स्क्रोल-ड्रिवन ऐनिमेशन, स्क्रोलर की रेंज में --drawer-backdrop को 1 (खुला) से 0 (बंद) पर मैप करता है. इससे बैकड्रॉप, ड्रैग के साथ पूरी तरह सिंक होकर फ़ेड इन और फ़ेड आउट होता है.
/* MANDATORY: Wrap this entire block in @supports. Browsers that don't
support animation-timeline still parse the @keyframes and would
apply the animation's `0%` value (--drawer-backdrop: 1) at all
times, leaving the backdrop permanently opaque. The @supports gate
ensures the animation is only registered where it actually works. */
@supports (animation-timeline: scroll()) {
.Drawer {
/* timeline-scope lets .Drawer reference a scroll-timeline that
is defined on its descendant (the scroller). Without this, the
timeline name is not visible to the .Drawer element. */
timeline-scope: --drawer-fade;
animation: fade-drawer-backdrop linear both;
animation-timeline: --drawer-fade;
}
.Drawer-scroller {
/* The horizontal scroll position of this element drives the
timeline named `--drawer-fade`. */
scroll-timeline: --drawer-fade x;
}
/* @property is REQUIRED. Without registering --drawer-backdrop with
a `<number>` syntax, the browser treats it as a string and cannot
interpolate it — the backdrop would jump from 0 to 1 with no
fade. */
@property --drawer-backdrop {
syntax: '<number>';
inherits: true;
initial-value: 0;
}
@keyframes fade-drawer-backdrop {
/* Scroll position 0 = drawer fully open = backdrop visible. */
0% { --drawer-backdrop: 1 }
/* Scroll position 100% = drawer fully closed = backdrop hidden. */
100% { --drawer-backdrop: 0 }
}
}
3. ड्रॉर खोलना और बंद करना
इसे खोलने के लिए दो चरण हैं: पॉपओवर को सबसे ऊपर वाली लेयर पर प्रमोट करें. इसके बाद, शीट को स्क्रोल करके व्यू में लाएं. शीट को बंद करने के लिए, सिर्फ़ एक चरण पूरा करना होता है: स्क्रोल करके स्पेसर पर वापस जाएं. शीट के पूरी तरह से ऑफ़-स्क्रीन होने पर, ऑब्ज़र्वर (चौथा चरण) पॉपओवर को छिपा देता है.
const drawer = document.getElementById('drawer');
const openBtn = document.getElementById('drawer-open');
const scroller = drawer.querySelector('.Drawer-scroller');
const sheet = drawer.querySelector('.Drawer-sheet');
function openDrawer() {
// Show the popover first so the element is in the top layer before
// we trigger any scrolling. `scroll-initial-target` (set on the
// ::after spacer) places the initial scroll position at the closed
// stop, so the drawer enters the top layer already off-screen.
drawer.showPopover();
// Scroll the sheet into view. The `behavior: 'auto'` option defers
// to the CSS `scroll-behavior` property, which will be smooth unless
// the user prefers reduced motion. Snap takes over at the end and
// locks the drawer fully open.
scroller.scrollTo({left: 0, behavior: 'auto'});
}
function closeDrawer() {
// Scroll back to the spacer. Do NOT call hidePopover() here —
// doing so would remove the element from the top layer mid-animation
// and the close animation would not be visible. The
// IntersectionObserver in step 4 hides the popover once the sheet
// has actually left the viewport.
scroller.scrollTo({left: scroller.offsetWidth, behavior: 'auto'});
}
4. खुले और बंद होने की स्थिति का पता लगाना
ड्रॉअर की स्थिति के लिए, स्क्रोल पोज़िशन के बजाय शीट पर IntersectionObserver का इस्तेमाल करें. ऑब्ज़र्वर इस बात की परवाह किए बिना ट्रिगर होता है कि शीट कैसे मूव हुई (उपयोगकर्ता का स्वाइप, प्रोग्राम के हिसाब से स्क्रोल, स्नैप सेटल). इसलिए, खारिज करने के सभी पाथ एक ही कॉलबैक में इकट्ठा होते हैं.
function onDrawerOpened() {
// Mark the rest of the page inert so keyboard and screen-reader
// users cannot tab into content hidden behind the drawer.
document.querySelector('main').inert = true;
openBtn.setAttribute('aria-expanded', 'true');
// Move focus into the drawer for keyboard users.
sheet.focus();
}
function onDrawerClosed() {
// Hide the popover only after the close animation completes,
// so the slide-out is visible to the user.
drawer.hidePopover();
document.querySelector('main').inert = false;
openBtn.setAttribute('aria-expanded', 'false');
}
// Treat "any pixel of the sheet visible inside the popover root" as
// "open enough to count as not closed". This threshold is intentionally
// tiny so the closed callback only fires once the sheet is truly gone.
const visibleThreshold = 1 / window.innerWidth;
const observer = new IntersectionObserver(
(entries) => {
// During programmatic scrolling the observer can deliver multiple
// entries in one batch. Only the most recent describes the
// current state; earlier entries are intermediate positions.
const entry = entries.at(-1);
if (entry.intersectionRatio < visibleThreshold) onDrawerClosed();
if (entry.intersectionRatio === 1) onDrawerOpened();
},
// root: drawer makes the popover element the intersection root,
// so the ratio reflects the sheet's visibility within the popover
// (i.e. how much of it has been swiped on-screen).
{root: drawer, threshold: [visibleThreshold, 1]},
);
observer.observe(sheet);
5. ट्रिगर और खारिज करने वाले हैंडलर को वायर अप करना
// Open trigger.
openBtn.addEventListener('click', openDrawer);
// Light-dismiss: a tap on the dimmed area (anywhere inside the
// popover but outside the sheet) closes the drawer. We implement
// this manually because popover="manual" disables the browser's
// built-in light-dismiss (which would also fire mid-swipe — see step 1).
drawer.addEventListener('click', (event) => {
if (!sheet.contains(event.target)) closeDrawer();
});
// Escape key. Listen on document because focus may be inside the
// drawer when the user presses Escape.
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape') closeDrawer();
});
फ़ॉलबैक रणनीतियां
पॉपओवर एपीआई, स्क्रोल-ड्रिवन ऐनिमेशन, जो बैकड्रॉप को फ़ेड करता है, और scroll-initial-target, परतदार वृद्धि हैं. इनमें आसान फ़ॉलबैक होते हैं. अगर ब्राउज़र के लिए ज़्यादा सहायता की ज़रूरत है, तो इन्हें आसानी से लागू किया जा सकता है.
बैकड्रॉप फ़ेड फ़ॉलबैक (animation-timeline की सुविधा उपलब्ध नहीं है):
CSS.supports('animation-timeline: scroll()') की मदद से पता लगाएं और अगर यह काम न करे, तो scroll इवेंट लिसनर से --drawer-backdrop लिखें. दूसरे चरण में मौजूद सीएसएस @supports ब्लॉक यह पक्का करता है कि कीफ़्रेम, उन ब्राउज़र पर कभी लागू न हों जो काम नहीं करते. इसलिए, JavaScript वैल्यू ही एकमात्र राइटर है.
if (!CSS.supports('animation-timeline: scroll()')) {
scroller.addEventListener('scroll', () => {
// Same mapping as the @keyframes: 0 scroll = 1 (open),
// sheet-width scroll = 0 (closed).
const ratio = 1 - scroller.scrollLeft / sheet.offsetWidth;
drawer.style.setProperty('--drawer-backdrop', ratio);
});
}
शुरुआती स्क्रोल पोज़िशन फ़ॉलबैक (scroll-initial-target के साथ काम नहीं करता):
CSS.supports('scroll-initial-target', 'nearest') और openDrawer() के अंदर मौजूद, स्क्रोलर को showPopover() के तुरंत बाद बंद स्थिति पर जंप-स्क्रोल करें. इसके बिना, ड्रॉअर बिना किसी स्लाइड-इन ऐनिमेशन के तुरंत खुली हुई पोज़िशन में दिखेगा.
async function openDrawer() {
drawer.showPopover();
if (!CSS.supports('scroll-initial-target', 'nearest')) {
// Jump-scroll to the closed stop so the scroll below
// animates the drawer in from off-screen.
scroller.scrollTo({left: scroller.offsetWidth, behavior: 'instant'});
// Wait two animation frames for the jump-scroll to commit.
// A single rAF is not enough — the second `scrollTo` would
// cancel the first before the browser has a chance to apply it.
await new Promise((r) =>
requestAnimationFrame(() => requestAnimationFrame(r))
);
}
scroller.scrollTo({left: 0, behavior: 'auto'});
}
@property फ़ॉलबैक (कोई रजिस्टर्ड कस्टम प्रॉपर्टी नहीं):
@property की ज़रूरत सिर्फ़ इसलिए होती है, क्योंकि स्क्रोल-ड्रिवन ऐनिमेशन, कीफ़्रेम के बीच --drawer-backdrop को इंटरपोलेट करता है. रजिस्ट्रेशन के बिना, प्रॉपर्टी को स्ट्रिंग के तौर पर माना जाएगा और यह बिना फ़ेड हुए 0 और 1 के बीच जंप करेगी. अगर ऊपर बताया गया स्क्रोल-ड्रिवन ऐनिमेशन फ़ॉलबैक मौजूद है, तो JavaScript हर स्क्रोल फ़्रेम पर --drawer-backdrop में एक नई न्यूमेरिक स्ट्रिंग लिखता है और कभी भी इंटरपोलेट नहीं करता. इसलिए, किसी अलग @property फ़ॉलबैक की ज़रूरत नहीं होती, क्योंकि स्क्रोल-ड्रिवन ऐनिमेशन के साथ काम करने वाले सभी ब्राउज़र, @property के साथ भी काम करते हैं.
Popover API फ़ॉलबैक (popover एट्रिब्यूट का इस्तेमाल नहीं किया जा सकता):
यह कॉम्पोनेंट popover="manual" का इस्तेमाल करता है और JavaScript से खारिज करने की सुविधा को पूरी तरह से लागू करता है. इसलिए, यह पॉपओवर एपीआई के तय किए गए व्यवहारों पर निर्भर नहीं करता है. जैसे, लाइट-डिसमिस, popovertarget एट्रिब्यूट, टॉप-लेयर-मैनेज्ड एस्केप हैंडलिंग या फ़ोकस मैनेजमेंट. यह सिर्फ़ पॉपओवर की इन सुविधाओं का इस्तेमाल करता है: टॉप-लेयर प्रमोशन (showPopover() के ज़रिए) और ::backdrop सूडो-एलिमेंट.
अगर आपको ज़्यादा ब्राउज़र पर काम करने वाली सुविधा चाहिए, तो सुविधा की पहचान करने के लिए ब्रांच न बनाएं. इसके बजाय, पॉपओवर का इस्तेमाल ही न करें. popover="manual" एट्रिब्यूट को हटाएं. टॉप-लेयर प्रमोशन को position: fixed और ज़्यादा z-index से बदलें. ::backdrop को एक जैसे स्टाइल वाले सिबलिंग एलिमेंट से बदलें. इसके लिए, एक जैसी --drawer-backdrop कस्टम प्रॉपर्टी का इस्तेमाल करें. साथ ही, showPopover()/hidePopover() के बजाय किसी क्लास से विज़िबिलिटी को टॉगल करें. कॉम्पोनेंट के बाकी हिस्से (स्क्रोल स्नैप, स्क्रोल-ड्रिवन बैकड्रॉप ऐनिमेशन, IntersectionObserver, और खारिज करने वाले हैंडलर) में कोई बदलाव नहीं किया गया है.