सुलभता पैटर्न · इंटरैक्शन और मोशन
होवर या फ़ोकस पर दिखने वाली सामग्री
होवर या फ़ोकस पर दिखने वाली हर चीज़ को तीन जाँचों पर खरा उतरना होता है: Escape बिना कुछ हिलाए उसे छिपा दे, पॉइंटर उस पर जा सके, और वह तब तक रहे जब तक आप हट न जाएँ या उसे हटा न दें। नीचे के तीनों पॉपअप आज़माएँ, और जाँच साथ-साथ टिक होती जाती है; फिर टूटा हुआ संस्करण चालू करें और उन्हें फ़ेल होते देखें।
- डब्ल्यूसीएजी मानदंड
- 5
- कीबोर्ड नियम
- 3
- जाँच का तरीक़ा
- axe, कीबोर्ड और इंस्पेक्टर
लाइव डेमो
आज़माएँ
इसे माउस, कीबोर्ड या स्क्रीन रीडर से इस्तेमाल करें। साथ का इंस्पेक्टर दिखाता है कि आपके इस्तेमाल के दौरान ब्राउज़र सहायक तकनीक को क्या बताता है: फ़ोकस, स्थिति में बदलाव और घोषणाएँ।
ऑर्डर सारांश
हैंडलूम सूती साड़ी, इंडिगो
विक्रेता: मीरा हैंडलूम्स
₹2,450
- डिलीवरी ₹999 से ज़्यादा के ऑर्डर पर मुफ़्त। बेंगलुरु, मुंबई और दिल्ली में एक्सप्रेस डिलीवरी ₹99 की है और अगले दिन पहुँचती है।
- ₹49
- भुगतान
- कैश ऑन डिलीवरी (COD)कैश ऑन डिलीवरी: पार्सल आने पर नकद या UPI से भुगतान करें।
- कुल
- ₹2,499
तीन शर्तें
-
बंद किया जा सकने वाला
अभी आज़माया नहीं गया
पॉपअप दिखते समय Escape दबाएँ। उसे छिप जाना चाहिए, और फ़ोकस व पॉइंटर जहाँ हैं वहीं रहें।
-
होवर किया जा सकने वाला
अभी आज़माया नहीं गया
पॉइंटर को ट्रिगर से उसके पॉपअप पर ले जाएँ। उसे खुला रहना चाहिए।
-
स्थायी
अभी आज़माया नहीं गया
किसी ट्रिगर पर तीन सेकंड रुकें। पॉपअप तब तक रहना चाहिए जब तक आप हट न जाएँ या Escape न दबाएँ।
जावास्क्रिप्ट के बिना डेमो उतना ही काम करता है जितना उसका एचटीएमएल; इंस्पेक्टर के लिए जावास्क्रिप्ट चाहिए।
कीबोर्ड
जिन कुंजियों पर यह काम करता है
हर क्रिया पॉइंटर के बिना काम करती है।
| कुंजी | यह क्या करती है |
|---|---|
| TabयाShiftTab | किसी ट्रिगर पर जाता है और उसका पॉपअप दिखता है। विक्रेता के नाम से Tab कार्ड के लिंक में जाता है; कार्ड से बाहर जाने पर वह छिप जाता है। |
| Escape | खुला पॉपअप फ़ोकस हिलाए बिना छिपाता है। कार्ड के अंदर से, फ़ोकस विक्रेता के नाम पर लौटता है। जब तक आप हटकर वापस न आएँ, वह छिपा रहता है। |
| Enter | फ़ोकस वाला लिंक खोलता है, या जानकारी बटन से डिलीवरी का प्रीव्यू दिखाता है, जैसे टैप करने पर होता है। |
स्क्रीन रीडर
यह क्या घोषित करता है
मार्कअप की भूमिकाओं, नामों और स्थितियों से लिखा गया।
| कब | अपेक्षित घोषणा |
|---|---|
| फ़ोकस COD पर पहुँचता है | COD, लिंक। कैश ऑन डिलीवरी: पार्सल आने पर नकद या UPI से भुगतान करें। |
| फ़ोकस जानकारी बटन पर पहुँचता है | डिलीवरी शुल्क के बारे में, बटन। ₹999 से ज़्यादा के ऑर्डर पर मुफ़्त। बेंगलुरु, मुंबई और दिल्ली में एक्सप्रेस डिलीवरी ₹99 की है और अगले दिन पहुँचती है। |
| Tab विक्रेता के नाम से कार्ड में जाता है | मीरा हैंडलूम्स, समूह। दुकान पर जाएँ, लिंक |
| Escape पॉपअप छिपाता है | कुछ नहीं; फ़ोकस ट्रिगर पर ही रहता है |
| कोई जाँच बदलती है | होवर किया जा सकने वाला: पास |
ये अपेक्षित घोषणाएँ हैं, रिकॉर्डिंग नहीं। स्क्रीन रीडर और ब्राउज़र के हिसाब से शब्द और क्रम बदलते हैं।
कोड
कोड कॉपी करें
वही फ़ाइलें जिन पर यह डेमो चलता है। स्टाइल एक प्रीफ़िक्स के साथ परोसी जाती हैं जो इस साइट की अपनी स्टाइल को डेमो से दूर रखता है; आप जो कॉपी करते हैं वह मूल है।
<div class="ap-hover" data-ap-hover-content>
<div class="ap-hover__layout">
<section class="ap-hover__order" aria-labelledby="hover-order-name">
<h3 class="ap-hover__name" id="hover-order-name">Order summary</h3>
<div class="ap-hover__item">
<svg class="ap-hover__swatch" viewBox="0 0 48 48" aria-hidden="true" focusable="false"><rect class="ap-hover__cloth" width="48" height="48" rx="8"/><path class="ap-hover__weave" d="M0 34h48M0 39h48"/><path class="ap-hover__motif" d="M12 12l4 6-4 6-4-6zM28 12l4 6-4 6-4-6z"/></svg>
<div class="ap-hover__item-text">
<p class="ap-hover__item-name">Handloom cotton saree, indigo</p>
<p class="ap-hover__seller-line">Sold by <a class="ap-hover__trigger ap-hover__seller" id="hover-seller" href="#hover-shop" data-ap-trigger="hover-card">Meera Handlooms</a></p>
<div class="ap-hover__pop ap-hover__card" id="hover-card" role="group" aria-labelledby="hover-card-name" popover="manual">
<div class="ap-hover__card-top">
<span class="ap-hover__avatar" aria-hidden="true" translate="no">MH</span>
<div>
<p class="ap-hover__card-name" id="hover-card-name">Meera Handlooms</p>
<p class="ap-hover__card-meta">Weavers in Varanasi since 1998</p>
</div>
</div>
<p class="ap-hover__card-stat">Rated 4.8 out of 5 by 1,240 buyers</p>
<div class="ap-hover__card-links">
<a href="#hover-shop">Visit the shop</a>
<a href="#hover-message">Message the seller</a>
</div>
</div>
</div>
<p class="ap-hover__price">₹2,450</p>
</div>
<dl class="ap-hover__lines">
<div class="ap-hover__line">
<dt class="ap-hover__label">Delivery
<button type="button" class="ap-hover__trigger ap-hover__info" id="hover-info" aria-label="About delivery charges" aria-describedby="hover-preview" data-ap-trigger="hover-preview">
<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><circle cx="12" cy="12" r="9"/><path d="M12 11v5M12 8h.01"/></svg>
</button>
<span class="ap-hover__pop ap-hover__tip" id="hover-preview" role="tooltip" popover="manual">Free on orders over ₹999. Express delivery in Bengaluru, Mumbai and Delhi costs ₹99 and arrives the next day.</span>
</dt>
<dd>₹49</dd>
</div>
<div class="ap-hover__line">
<dt class="ap-hover__label">Payment</dt>
<dd><a class="ap-hover__trigger ap-hover__term" id="hover-cod" href="#hover-glossary-cod" aria-describedby="hover-def" data-ap-trigger="hover-def">COD</a><span class="ap-hover__pop ap-hover__tip" id="hover-def" role="tooltip" popover="manual">Cash on delivery: pay in cash or by UPI when the parcel arrives.</span></dd>
</div>
<div class="ap-hover__line ap-hover__line--total">
<dt>Total</dt>
<dd>₹2,499</dd>
</div>
</dl>
<button type="button" class="ap-btn ap-btn--primary ap-hover__place" data-ap-place>Place order</button>
<p class="ap-hover__note" role="status"></p>
</section>
<section class="ap-hover__checks" aria-labelledby="hover-checks-name">
<h3 class="ap-hover__checks-name" id="hover-checks-name">The three conditions</h3>
<ul class="ap-hover__conds">
<li class="ap-hover__cond" data-ap-cond="dismissible" data-state="idle">
<svg class="ap-hover__mark" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><circle class="ap-hover__mark-idle" cx="12" cy="12" r="8"/><path class="ap-hover__mark-pass" d="m5 12.5 4.5 4.5L19 7.5"/><path class="ap-hover__mark-fail" d="M7 7l10 10M17 7 7 17"/></svg>
<span class="ap-hover__cond-name">Dismissible</span>
<span class="ap-hover__verdict" data-ap-verdict>Not tried yet</span>
<p class="ap-hover__how" data-ap-result>Press Escape while a popup shows. It should hide, and focus and the pointer stay where they are.</p>
</li>
<li class="ap-hover__cond" data-ap-cond="hoverable" data-state="idle">
<svg class="ap-hover__mark" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><circle class="ap-hover__mark-idle" cx="12" cy="12" r="8"/><path class="ap-hover__mark-pass" d="m5 12.5 4.5 4.5L19 7.5"/><path class="ap-hover__mark-fail" d="M7 7l10 10M17 7 7 17"/></svg>
<span class="ap-hover__cond-name">Hoverable</span>
<span class="ap-hover__verdict" data-ap-verdict>Not tried yet</span>
<p class="ap-hover__how" data-ap-result>Move the pointer from a trigger onto its popup. It should stay open.</p>
</li>
<li class="ap-hover__cond" data-ap-cond="persistent" data-state="idle">
<svg class="ap-hover__mark" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><circle class="ap-hover__mark-idle" cx="12" cy="12" r="8"/><path class="ap-hover__mark-pass" d="m5 12.5 4.5 4.5L19 7.5"/><path class="ap-hover__mark-fail" d="M7 7l10 10M17 7 7 17"/></svg>
<span class="ap-hover__cond-name">Persistent</span>
<span class="ap-hover__verdict" data-ap-verdict>Not tried yet</span>
<p class="ap-hover__how" data-ap-result>Rest on a trigger for three seconds. The popup should stay until you leave or press Escape.</p>
</li>
</ul>
<p class="ap-hover__said" role="status"></p>
</section>
</div>
</div>
/* Shared primitives: buttons and form fields used across the patterns.
Uses the --ap-* design tokens. */
.ap-btn {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 8px;
min-height: 40px;
padding: 0 16px;
border: 1px solid var(--ap-border-strong);
border-radius: var(--ap-radius-sm);
background: var(--ap-surface);
color: var(--ap-text);
font: inherit;
font-weight: 600;
line-height: 1.2;
text-decoration: none;
white-space: nowrap;
cursor: pointer;
transition: background-color var(--ap-duration) var(--ap-ease), border-color var(--ap-duration) var(--ap-ease), box-shadow var(--ap-duration) var(--ap-ease);
}
.ap-btn:hover {
background: var(--ap-surface-2);
}
.ap-btn:focus-visible {
outline: 2px solid var(--ap-focus);
outline-offset: 2px;
}
.ap-btn:disabled,
.ap-btn[aria-disabled="true"] {
opacity: .55;
cursor: not-allowed;
}
.ap-btn--primary {
border-color: var(--ap-accent);
background: var(--ap-accent);
color: var(--ap-on-accent);
box-shadow: var(--ap-shadow-sm);
}
.ap-btn--primary:hover {
border-color: var(--ap-accent-hover);
background: var(--ap-accent-hover);
}
.ap-btn--danger {
border-color: var(--ap-danger);
background: var(--ap-danger);
color: var(--ap-surface);
}
.ap-btn--danger:hover {
filter: brightness(1.08);
}
.ap-btn--ghost {
border-color: transparent;
background: transparent;
}
.ap-btn--ghost:hover {
background: var(--ap-surface-2);
}
.ap-btn__icon {
width: 18px;
height: 18px;
flex-shrink: 0;
fill: none;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
}
.ap-field {
display: grid;
gap: 6px;
}
.ap-label {
color: var(--ap-text);
font-weight: 600;
font-size: .9375rem;
}
.ap-hint {
margin: 0;
color: var(--ap-text-3);
font-size: .875rem;
line-height: 1.45;
}
.ap-error {
display: flex;
align-items: flex-start;
gap: 6px;
margin: 0;
color: var(--ap-danger);
font-size: .875rem;
font-weight: 600;
line-height: 1.45;
}
.ap-input {
width: 100%;
min-height: 44px;
padding: 10px 12px;
border: 1px solid var(--ap-border-strong);
border-radius: var(--ap-radius-sm);
background: var(--ap-surface);
color: var(--ap-text);
font: inherit;
line-height: 1.4;
transition: border-color var(--ap-duration) var(--ap-ease), box-shadow var(--ap-duration) var(--ap-ease);
}
.ap-input::placeholder {
color: var(--ap-text-3);
}
.ap-input:hover {
border-color: var(--ap-text-2);
}
.ap-input:focus-visible {
outline: 2px solid var(--ap-focus);
outline-offset: 1px;
border-color: var(--ap-focus);
}
.ap-input[aria-invalid="true"] {
border-color: var(--ap-danger);
box-shadow: inset 4px 0 0 var(--ap-danger);
}
textarea.ap-input {
resize: vertical;
min-height: 88px;
}
@media (prefers-reduced-motion: reduce) {
.ap-btn,
.ap-input { transition: none; }
}
/* Content on hover or focus. Uses the --ap-* design tokens and the shared primitives.
data-broken on the root shows the failures 1.4.13 forbids. */
.ap-hover {
width: min(100%, 820px);
margin-inline: auto;
color: var(--ap-text);
container-type: inline-size;
}
.ap-hover__layout {
display: grid;
gap: 16px;
align-items: start;
}
/* ── The order ── */
.ap-hover__order {
display: grid;
gap: 14px;
min-width: 0;
padding: 20px 22px 22px;
border: 1px solid var(--ap-border);
border-radius: var(--ap-radius-lg);
background: var(--ap-surface);
box-shadow: var(--ap-shadow-md);
}
.ap-hover__name {
margin: 0;
font-size: 1.0625rem;
font-weight: 650;
line-height: 1.35;
}
.ap-hover__item {
display: grid;
grid-template-columns: auto minmax(0, 1fr) auto;
align-items: start;
gap: 14px;
padding-bottom: 14px;
border-bottom: 1px solid var(--ap-border);
}
.ap-hover__swatch {
width: 52px;
height: 52px;
}
.ap-hover__cloth { fill: var(--ap-accent); }
.ap-hover__weave { fill: none; stroke: var(--ap-on-accent); stroke-width: 2; }
.ap-hover__motif { fill: var(--ap-warning-soft); }
.ap-hover__item-name,
.ap-hover__seller-line,
.ap-hover__price {
margin: 0;
}
.ap-hover__item-name {
font-weight: 600;
line-height: 1.4;
}
.ap-hover__seller-line {
margin-top: 4px;
color: var(--ap-text-3);
font-size: .875rem;
}
.ap-hover__price {
font-weight: 650;
font-variant-numeric: tabular-nums;
}
.ap-hover__lines {
display: grid;
gap: 2px;
margin: 0;
}
.ap-hover__line {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
min-height: 36px;
}
.ap-hover__line dt {
color: var(--ap-text-2);
}
.ap-hover__line dd {
margin: 0;
font-variant-numeric: tabular-nums;
}
.ap-hover__label {
display: flex;
align-items: center;
gap: 4px;
}
.ap-hover__line--total {
margin-top: 6px;
padding-top: 12px;
border-top: 1px solid var(--ap-border);
font-size: 1.0625rem;
font-weight: 700;
}
.ap-hover__line--total dt {
color: var(--ap-text);
}
.ap-hover__place {
min-height: var(--ap-target);
}
.ap-hover__note {
margin: 0;
color: var(--ap-text-2);
font-size: .875rem;
font-weight: 600;
}
.ap-hover__note:empty {
display: none;
}
/* ── Triggers ── */
.ap-hover__seller,
.ap-hover__term,
.ap-hover__card-links a {
border-radius: 3px;
color: var(--ap-accent-text);
font-weight: 600;
text-decoration: underline;
text-underline-offset: 3px;
}
/* A term with a definition: a dotted underline says "there is more here". */
.ap-hover__term {
text-decoration-style: dotted;
text-decoration-thickness: 2px;
}
.ap-hover__info {
display: grid;
place-items: center;
width: 32px;
height: 32px;
padding: 0;
border: 0;
border-radius: var(--ap-radius-full);
background: transparent;
color: var(--ap-text-2);
cursor: pointer;
}
.ap-hover__info:hover {
background: var(--ap-surface-2);
color: var(--ap-text);
}
.ap-hover__info svg {
width: 18px;
height: 18px;
fill: none;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round;
}
.ap-hover__trigger:focus-visible {
outline: 2px solid var(--ap-focus);
outline-offset: 2px;
}
/* ── Popups: manual popovers in the top layer, placed by the script ── */
.ap-hover__pop {
inset: auto;
margin: 0;
overflow: visible;
font-family: var(--ap-font);
text-align: start;
}
.ap-hover__pop:popover-open {
animation: ap-hover-in var(--ap-duration) var(--ap-ease);
}
.ap-hover__tip {
max-width: min(280px, calc(100vw - 16px));
padding: 9px 12px;
border: 0;
border-radius: var(--ap-radius-sm);
background: var(--ap-text);
color: var(--ap-surface);
font-size: .8125rem;
font-weight: 500;
line-height: 1.5;
box-shadow: var(--ap-shadow-md);
}
.ap-hover__card {
width: min(290px, calc(100vw - 16px));
padding: 16px;
border: 1px solid var(--ap-border);
border-radius: var(--ap-radius);
background: var(--ap-surface);
color: var(--ap-text);
font-size: .9375rem;
box-shadow: var(--ap-shadow-lg);
}
.ap-hover__card-top {
display: flex;
align-items: center;
gap: 12px;
}
.ap-hover__avatar {
display: grid;
place-items: center;
width: 42px;
height: 42px;
flex-shrink: 0;
border-radius: var(--ap-radius-full);
background: var(--ap-accent-soft);
color: var(--ap-accent-soft-text);
font-size: .8125rem;
font-weight: 700;
}
.ap-hover__card-name,
.ap-hover__card-meta,
.ap-hover__card-stat {
margin: 0;
}
.ap-hover__card-name {
font-weight: 650;
}
.ap-hover__card-meta {
color: var(--ap-text-3);
font-size: .8125rem;
}
.ap-hover__card-stat {
margin-top: 12px;
color: var(--ap-text-2);
font-size: .875rem;
}
.ap-hover__card-links {
display: flex;
flex-wrap: wrap;
gap: 4px 16px;
margin-top: 12px;
padding-top: 12px;
border-top: 1px solid var(--ap-border);
font-size: .875rem;
}
.ap-hover__card-links a {
display: inline-flex;
align-items: center;
min-height: 28px;
}
.ap-hover__card-links a:focus-visible {
outline: 2px solid var(--ap-focus);
outline-offset: 2px;
}
/* The bridge: covers the gap toward the trigger, so the pointer can cross it. */
.ap-hover__pop::after {
content: "";
position: absolute;
left: 0;
right: 0;
height: 12px;
}
.ap-hover__pop[data-side="top"]::after { top: 100%; }
.ap-hover__pop[data-side="bottom"]::after { bottom: 100%; }
.ap-hover[data-broken] .ap-hover__pop::after {
display: none;
}
/* ── The checks ── */
.ap-hover__checks {
display: grid;
gap: 12px;
min-width: 0;
padding: 18px 18px 16px;
border: 1px solid var(--ap-border);
border-radius: var(--ap-radius-lg);
background: var(--ap-surface);
box-shadow: var(--ap-shadow-md);
}
.ap-hover__checks-name {
margin: 0;
font-size: 1rem;
font-weight: 650;
}
.ap-hover__conds {
display: grid;
gap: 8px;
margin: 0;
padding: 0;
list-style: none;
}
.ap-hover__cond {
display: grid;
grid-template-columns: auto minmax(0, 1fr) auto;
align-items: center;
gap: 4px 10px;
padding: 12px 14px;
border: 1px solid var(--ap-border);
border-radius: var(--ap-radius-sm);
transition: background-color var(--ap-duration) var(--ap-ease), border-color var(--ap-duration) var(--ap-ease);
}
.ap-hover__mark {
width: 20px;
height: 20px;
fill: none;
stroke: currentColor;
stroke-width: 2.5;
stroke-linecap: round;
stroke-linejoin: round;
}
.ap-hover__mark > * {
display: none;
}
.ap-hover__mark-idle {
stroke-width: 2;
stroke-dasharray: 3 3;
}
.ap-hover__cond[data-state="idle"] .ap-hover__mark-idle,
.ap-hover__cond[data-state="pass"] .ap-hover__mark-pass,
.ap-hover__cond[data-state="fail"] .ap-hover__mark-fail {
display: inline;
}
.ap-hover__cond-name {
font-weight: 650;
}
.ap-hover__verdict {
font-size: .8125rem;
font-weight: 650;
}
.ap-hover__how {
grid-column: 2 / -1;
margin: 0;
color: var(--ap-text-2);
font-size: .8125rem;
line-height: 1.45;
}
.ap-hover__cond[data-state="idle"] {
color: var(--ap-text-3);
}
.ap-hover__cond[data-state="idle"] .ap-hover__cond-name {
color: var(--ap-text);
}
.ap-hover__cond[data-state="pass"] {
border-color: var(--ap-success);
background: var(--ap-success-soft);
color: var(--ap-success);
}
.ap-hover__cond[data-state="fail"] {
border-color: var(--ap-danger);
background: var(--ap-danger-soft);
color: var(--ap-danger);
}
.ap-hover__cond:is([data-state="pass"], [data-state="fail"]) .ap-hover__how {
color: inherit;
}
.ap-hover__said {
margin: 0;
color: var(--ap-text-2);
font-size: .875rem;
}
.ap-hover__said:empty {
display: none;
}
@keyframes ap-hover-in {
from { opacity: 0; transform: translateY(var(--ap-hover-from, 4px)); }
}
.ap-hover__pop[data-side="bottom"] {
--ap-hover-from: -4px;
}
@container (min-width: 640px) {
.ap-hover__layout { grid-template-columns: minmax(0, 1fr) minmax(0, 300px); }
}
@media (forced-colors: active) {
.ap-hover__tip { border: 1px solid CanvasText; }
}
@media (prefers-reduced-motion: reduce) {
.ap-hover__pop:popover-open { animation: none; }
.ap-hover__cond { transition: none; }
}
/**
* Content on hover or focus (WCAG 1.4.13): popups that appear on hover and
* keyboard focus, hide on Escape without moving anything, can be hovered
* across the gap from their trigger, and stay until hover and focus leave.
* A panel of checks watches real events and marks each condition.
*
* Markup: [data-ap-hover-content] holding triggers [data-ap-trigger="<id>"]
* that name a popover=manual element, the checks [data-ap-cond], and two
* role=status paragraphs. Add data-broken to the root for the failures:
* no grace delay or bridge, no Escape, a two-second timer, placed over the
* content below.
*/
const SHOW_HOVER = 250; // long enough not to flash as the pointer passes
const SHOW_FOCUS = 80;
const GRACE = 300; // time to cross from the trigger to the popup
const PERSIST = 3000; // open this long while in use: persistent
const BROKEN_TIMER = 2000; // the broken version closes itself
const GAP = 8;
const BROKEN_GAP = 14;
const EDGE = 8;
const RESULT = {
dismissible: {
pass: ["Escape hid it, and focus and the pointer stayed put.", "Dismissible: pass"],
fail: ["Escape did nothing. The popup still covers the page.", "Dismissible: fail"],
},
hoverable: {
pass: ["The pointer reached the popup, and it stayed open.", "Hoverable: pass"],
fail: ["It closed before the pointer could reach it.", "Hoverable: fail"],
},
persistent: {
pass: ["It stayed open for three seconds, with no timer to close it.", "Persistent: pass"],
fail: ["It closed on a timer while you were still on it.", "Persistent: fail"],
},
};
export function init(root) {
const ac = new AbortController();
const on = (target, type, fn, options = {}) => target.addEventListener(type, fn, { ...options, signal: ac.signal });
const broken = () => root.hasAttribute("data-broken");
const said = root.querySelector(".ap-hover__said");
const note = root.querySelector(".ap-hover__note");
const conds = Object.fromEntries([...root.querySelectorAll("[data-ap-cond]")].map((li) => [li.dataset.apCond, li]));
const timers = new Set();
let ghost = null; // where a broken popup was when it vanished under the pointer's path
const pairs = [...root.querySelectorAll("[data-ap-trigger]")].map((trigger) => ({
trigger,
pop: document.getElementById(trigger.dataset.apTrigger),
hover: false, popHover: false, focus: false, pinned: false,
dismissed: false, byHover: false, returning: false, timer: 0, life: 0,
}));
const isOpen = (p) => p.pop.matches(":popover-open");
const engaged = (p) => p.hover || p.popHover || p.focus || p.pinned;
const later = (fn, ms) => {
const t = setTimeout(() => { timers.delete(t); fn(); }, ms);
timers.add(t);
return t;
};
function say(region, text) {
region.textContent = "";
later(() => { region.textContent = text; }, 60);
}
function mark(key, state) {
const li = conds[key];
const [text, line] = RESULT[key][state];
if (li.dataset.state === state) return;
li.dataset.state = state;
li.querySelector("[data-ap-verdict]").textContent = state === "pass" ? "Pass" : "Fail";
li.querySelector("[data-ap-result]").textContent = text;
say(said, line);
}
function place(p) {
const r = p.trigger.getBoundingClientRect();
const vw = document.documentElement.clientWidth;
const vh = document.documentElement.clientHeight;
const w = p.pop.offsetWidth;
const h = p.pop.offsetHeight;
let side = "bottom";
let gap = BROKEN_GAP;
if (!broken()) {
// Above by default, below when there is no room above: never over the trigger.
gap = GAP;
side = r.top - gap - h >= EDGE || r.bottom + gap + h > vh - EDGE ? "top" : "bottom";
}
const left = Math.min(Math.max(r.left + r.width / 2 - w / 2, EDGE), vw - w - EDGE);
p.pop.dataset.side = side;
p.pop.style.left = `${Math.round(left)}px`;
p.pop.style.top = `${Math.round(side === "top" ? r.top - gap - h : r.bottom + gap)}px`;
}
function show(p) {
clearTimeout(p.timer);
if (p.dismissed || isOpen(p)) return;
for (const o of pairs) if (o !== p) hide(o);
p.pop.showPopover();
place(p);
clearTimeout(p.life);
if (broken()) {
p.life = later(() => {
const inUse = engaged(p);
hide(p);
if (inUse) mark("persistent", "fail");
}, BROKEN_TIMER);
} else {
p.life = later(() => { if (isOpen(p) && engaged(p)) mark("persistent", "pass"); }, PERSIST);
}
}
function hide(p, why = "") {
clearTimeout(p.timer);
clearTimeout(p.life);
if (!isOpen(p)) return;
if (why === "leave" && broken()) {
const b = p.pop.getBoundingClientRect();
ghost = { left: b.left - BROKEN_GAP, top: b.top - BROKEN_GAP, right: b.right + BROKEN_GAP, bottom: b.bottom + BROKEN_GAP, until: performance.now() + 900 };
}
p.pop.hidePopover();
p.byHover = false;
}
function showSoon(p, delay) {
clearTimeout(p.timer);
p.timer = later(() => show(p), broken() ? 0 : delay);
}
// Hidden only once the pointer and focus have both left the trigger and the popup.
function hideSoon(p, delay = GRACE) {
if (engaged(p)) return;
clearTimeout(p.timer);
p.timer = later(() => { if (!engaged(p)) hide(p, "leave"); }, delay);
}
for (const p of pairs) {
const { trigger, pop } = p;
on(trigger, "pointerenter", (event) => {
if (event.pointerType === "touch") return;
p.hover = true;
p.byHover = true;
p.dismissed = false;
showSoon(p, SHOW_HOVER);
});
on(trigger, "pointerleave", (event) => {
if (event.pointerType === "touch") return;
p.hover = false;
if (broken()) hide(p, "leave");
else hideSoon(p);
});
on(pop, "pointerenter", () => {
p.popHover = true;
clearTimeout(p.timer);
if (!broken() && p.byHover) later(() => { if (isOpen(p) && p.popHover) mark("hoverable", "pass"); }, 400);
});
on(pop, "pointerleave", () => {
p.popHover = false;
if (!broken()) hideSoon(p);
});
on(trigger, "focus", () => {
// Keyboard focus: a click already showed it on hover, and a tap uses click.
if (!trigger.matches(":focus-visible")) return;
p.focus = true;
// Focus put back by Escape: the popup stays dismissed.
if (p.returning) {
p.returning = false;
return;
}
p.dismissed = false;
showSoon(p, SHOW_FOCUS);
});
}
// Focus may move from a trigger into its popup (the profile card's links).
function onFocusMove() {
requestAnimationFrame(() => {
const el = document.activeElement;
for (const p of pairs) {
const within = p.pop.contains(el) || (p.trigger === el && p.focus);
if (p.pop.contains(el)) p.focus = true;
if (p.focus && !within) {
p.focus = false;
p.pinned = false;
hideSoon(p, 0);
}
}
});
}
function onKeydown(event) {
if (event.key !== "Escape") return;
const open = pairs.filter(isOpen);
if (!open.length) return;
if (broken()) {
later(() => { if (open.some(isOpen)) mark("dismissible", "fail"); }, 0);
return;
}
// Only this Escape is used up: a dialog around the demo stays open.
event.preventDefault();
for (const p of open) {
const inside = p.pop.contains(document.activeElement);
p.pinned = false;
hide(p);
p.dismissed = true;
// Focus inside a hidden popup would be lost: put it back on the trigger.
if (inside) {
p.returning = true;
p.trigger.focus();
p.returning = false;
}
}
mark("dismissible", "pass");
}
function onPointerMove(event) {
if (!ghost) return;
if (performance.now() > ghost.until) {
ghost = null;
return;
}
const { clientX: x, clientY: y } = event;
if (x >= ghost.left && x <= ghost.right && y >= ghost.top && y <= ghost.bottom) {
ghost = null;
mark("hoverable", "fail");
}
}
// A tap or click elsewhere puts away a popup that a click opened.
function onPointerDown(event) {
for (const p of pairs) {
if (isOpen(p) && !p.trigger.contains(event.target) && !p.pop.contains(event.target)) {
p.pinned = false;
if (!engaged(p)) hide(p);
}
}
}
function onClick(event) {
const t = event.target;
const info = t.closest("button[data-ap-trigger]");
if (info) {
// A tap has no hover and no focus-visible: the click shows the preview.
const p = pairs.find((o) => o.trigger === info);
p.pinned = true;
p.dismissed = false;
show(p);
return;
}
if (t.closest("[data-ap-place]")) {
say(note, "Order placed. A confirmation is on its way by SMS.");
return;
}
const link = t.closest("a[href^='#']");
if (link) {
// The demo's pages do not exist here: stay, and say what would happen.
event.preventDefault();
say(note, "In a real site this link opens that page.");
}
}
const follow = () => { for (const p of pairs) if (isOpen(p)) place(p); };
on(root, "focusin", onFocusMove);
on(root, "focusout", onFocusMove);
on(root, "click", onClick);
on(document, "keydown", onKeydown);
on(document, "pointermove", onPointerMove, { passive: true });
on(document, "pointerdown", onPointerDown);
on(window, "scroll", follow, { capture: true, passive: true });
on(window, "resize", follow, { passive: true });
return () => {
ac.abort();
for (const p of pairs) hide(p);
for (const t of timers) clearTimeout(t);
};
}
for (const root of document.querySelectorAll("[data-ap-hover-content]")) init(root);
/* Design tokens for the pattern components. Light by default, dark when the
system asks for it; set data-theme="dark" on :root to force dark. */
:root {
--ap-radius-sm: 8px;
--ap-radius: 12px;
--ap-radius-lg: 16px;
--ap-radius-full: 999px;
--ap-font: "Instrument Sans", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
--ap-mono: "JetBrains Mono", ui-monospace, "Cascadia Mono", "SF Mono", Consolas, monospace;
--ap-ease: cubic-bezier(.2, .8, .2, 1);
--ap-duration: 180ms;
--ap-target: 44px;
--ap-bg: #F4F4F5;
--ap-surface: #FFFFFF;
--ap-surface-2: #F4F4F5;
--ap-surface-3: #E4E4E7;
--ap-border: #E4E4E7;
--ap-border-strong: #76767F;
--ap-text: #18181B;
--ap-text-2: #3F3F46;
--ap-text-3: #5E5E66;
--ap-accent: #4F46E5;
--ap-accent-hover: #4338CA;
--ap-on-accent: #FFFFFF;
--ap-accent-text: #4338CA;
--ap-accent-soft: #EEF2FF;
--ap-accent-soft-text: #3730A3;
--ap-focus: #4F46E5;
--ap-danger: #B91C1C;
--ap-danger-soft: #FEF2F2;
--ap-success: #15803D;
--ap-success-soft: #F0FDF4;
--ap-warning: #A15C07;
--ap-warning-soft: #FEFCE8;
--ap-info: #1D4ED8;
--ap-info-soft: #EFF6FF;
--ap-scrim: rgb(9 9 11 / .48);
--ap-shadow-sm: 0 1px 2px rgb(9 9 11 / .06);
--ap-shadow-md: 0 1px 2px rgb(9 9 11 / .05), 0 6px 16px -4px rgb(9 9 11 / .1);
--ap-shadow-lg: 0 2px 6px rgb(9 9 11 / .06), 0 20px 40px -12px rgb(9 9 11 / .22);
--ap-light-bg: #F4F4F5;
--ap-light-surface: #FFFFFF;
--ap-light-surface-2: #F4F4F5;
--ap-light-surface-3: #E4E4E7;
--ap-light-border: #E4E4E7;
--ap-light-border-strong: #76767F;
--ap-light-text: #18181B;
--ap-light-text-2: #3F3F46;
--ap-light-text-3: #5E5E66;
--ap-light-accent: #4F46E5;
--ap-light-accent-hover: #4338CA;
--ap-light-on-accent: #FFFFFF;
--ap-light-accent-text: #4338CA;
--ap-light-accent-soft: #EEF2FF;
--ap-light-accent-soft-text: #3730A3;
--ap-light-focus: #4F46E5;
--ap-light-danger: #B91C1C;
--ap-light-danger-soft: #FEF2F2;
--ap-light-success: #15803D;
--ap-light-success-soft: #F0FDF4;
--ap-light-warning: #A15C07;
--ap-light-warning-soft: #FEFCE8;
--ap-light-info: #1D4ED8;
--ap-light-info-soft: #EFF6FF;
--ap-light-scrim: rgb(9 9 11 / .48);
--ap-light-shadow-sm: 0 1px 2px rgb(9 9 11 / .06);
--ap-light-shadow-md: 0 1px 2px rgb(9 9 11 / .05), 0 6px 16px -4px rgb(9 9 11 / .1);
--ap-light-shadow-lg: 0 2px 6px rgb(9 9 11 / .06), 0 20px 40px -12px rgb(9 9 11 / .22);
--ap-dark-bg: #09090B;
--ap-dark-surface: #18181B;
--ap-dark-surface-2: #27272A;
--ap-dark-surface-3: #3F3F46;
--ap-dark-border: #2E2E33;
--ap-dark-border-strong: #8E8E97;
--ap-dark-text: #FAFAFA;
--ap-dark-text-2: #D4D4D8;
--ap-dark-text-3: #A1A1AA;
--ap-dark-accent: #818CF8;
--ap-dark-accent-hover: #A5B4FC;
--ap-dark-on-accent: #0C0A1F;
--ap-dark-accent-text: #A5B4FC;
--ap-dark-accent-soft: #1E1B4B;
--ap-dark-accent-soft-text: #C7D2FE;
--ap-dark-focus: #A5B4FC;
--ap-dark-danger: #F87171;
--ap-dark-danger-soft: #2A1215;
--ap-dark-success: #4ADE80;
--ap-dark-success-soft: #0F2A1A;
--ap-dark-warning: #FACC15;
--ap-dark-warning-soft: #2A2410;
--ap-dark-info: #60A5FA;
--ap-dark-info-soft: #0F1D33;
--ap-dark-scrim: rgb(0 0 0 / .62);
--ap-dark-shadow-sm: 0 1px 2px rgb(0 0 0 / .4);
--ap-dark-shadow-md: 0 1px 2px rgb(0 0 0 / .4), 0 8px 20px -6px rgb(0 0 0 / .5);
--ap-dark-shadow-lg: 0 2px 8px rgb(0 0 0 / .45), 0 24px 48px -12px rgb(0 0 0 / .7);
}
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) {
--ap-bg: #09090B;
--ap-surface: #18181B;
--ap-surface-2: #27272A;
--ap-surface-3: #3F3F46;
--ap-border: #2E2E33;
--ap-border-strong: #8E8E97;
--ap-text: #FAFAFA;
--ap-text-2: #D4D4D8;
--ap-text-3: #A1A1AA;
--ap-accent: #818CF8;
--ap-accent-hover: #A5B4FC;
--ap-on-accent: #0C0A1F;
--ap-accent-text: #A5B4FC;
--ap-accent-soft: #1E1B4B;
--ap-accent-soft-text: #C7D2FE;
--ap-focus: #A5B4FC;
--ap-danger: #F87171;
--ap-danger-soft: #2A1215;
--ap-success: #4ADE80;
--ap-success-soft: #0F2A1A;
--ap-warning: #FACC15;
--ap-warning-soft: #2A2410;
--ap-info: #60A5FA;
--ap-info-soft: #0F1D33;
--ap-scrim: rgb(0 0 0 / .62);
--ap-shadow-sm: 0 1px 2px rgb(0 0 0 / .4);
--ap-shadow-md: 0 1px 2px rgb(0 0 0 / .4), 0 8px 20px -6px rgb(0 0 0 / .5);
--ap-shadow-lg: 0 2px 8px rgb(0 0 0 / .45), 0 24px 48px -12px rgb(0 0 0 / .7);
}
}
:root[data-theme="dark"] {
--ap-bg: #09090B;
--ap-surface: #18181B;
--ap-surface-2: #27272A;
--ap-surface-3: #3F3F46;
--ap-border: #2E2E33;
--ap-border-strong: #8E8E97;
--ap-text: #FAFAFA;
--ap-text-2: #D4D4D8;
--ap-text-3: #A1A1AA;
--ap-accent: #818CF8;
--ap-accent-hover: #A5B4FC;
--ap-on-accent: #0C0A1F;
--ap-accent-text: #A5B4FC;
--ap-accent-soft: #1E1B4B;
--ap-accent-soft-text: #C7D2FE;
--ap-focus: #A5B4FC;
--ap-danger: #F87171;
--ap-danger-soft: #2A1215;
--ap-success: #4ADE80;
--ap-success-soft: #0F2A1A;
--ap-warning: #FACC15;
--ap-warning-soft: #2A2410;
--ap-info: #60A5FA;
--ap-info-soft: #0F1D33;
--ap-scrim: rgb(0 0 0 / .62);
--ap-shadow-sm: 0 1px 2px rgb(0 0 0 / .4);
--ap-shadow-md: 0 1px 2px rgb(0 0 0 / .4), 0 8px 20px -6px rgb(0 0 0 / .5);
--ap-shadow-lg: 0 2px 8px rgb(0 0 0 / .45), 0 24px 48px -12px rgb(0 0 0 / .7);
}
डब्ल्यूसीएजी 2.2
यह किन मानदंडों को पूरा करता है
वे सफलता मानदंड जिनका यह पैटर्न ध्यान रखता है, और कैसे।
-
1.4.13 Hover या फ़ोकस पर सामग्री
स्तर AA
हर पॉपअप को पॉइंटर या फ़ोकस हिलाए बिना Escape से हटाया जा सकता है, ट्रिगर से गैप पार करके उस पर पॉइंटर ले जाया जा सकता है, और वह तब तक रहता है जब तक होवर और फ़ोकस हट न जाएँ या Escape न दबे। जाँच तीनों को लाइव परखती है।
-
2.1.1 कीबोर्ड
स्तर A
हर पॉपअप होवर के साथ कीबोर्ड फ़ोकस पर भी दिखता है, और जानकारी बटन Enter या टैप पर भी अपना प्रीव्यू दिखाता है।
-
2.4.3 फ़ोकस क्रम
स्तर A
डीओएम में प्रोफ़ाइल कार्ड अपने लिंक के ठीक बाद है, इसलिए Tab नाम से सीधे कार्ड के लिंक में और फिर बाकी ऑर्डर पर जाता है।
-
2.4.11 फ़ोकस अवरुद्ध नहीं (न्यूनतम)
स्तर AA
पॉपअप अपने ट्रिगर के बगल में खुलते हैं, उसके ऊपर कभी नहीं, इसलिए फ़ोकस वाला एलिमेंट नज़र में रहता है।
-
4.1.2 नाम, भूमिका, मान
स्तर A
परिभाषा और प्रीव्यू role=tooltip हैं और अपने ट्रिगर का विवरण देते हैं; कार्ड में लिंक हैं, इसलिए वह नाम वाला समूह है।
उपयोग
इसे कब इस्तेमाल करें
इसे अपनाएँ
- छोटी अतिरिक्त जानकारी जो लोग गुज़रते हुए देखना चाहें: कोई परिभाषा, लिंक कहाँ ले जाता है उसका प्रीव्यू, कोई प्रोफ़ाइल कार्ड।
- ऐसी सामग्री जो कहीं और भी मौजूद हो, ताकि होवर न कर पाने वाला कोई भी व्यक्ति ज़रूरी चीज़ से न चूके।
कुछ और इस्तेमाल करें
- काम पूरा करने के लिए ज़रूरी जानकारी, जैसे डिलीवरी शुल्क जो सिर्फ़ टूलटिप में हो: उसे पेज पर रखें।
- लंबी या जटिल सामग्री, या फ़ॉर्म: इसके बजाय क्लिक पर पॉपओवर या डायलॉग खोलें।
- बिना होवर वाली टच-प्रधान स्क्रीन: टैप पर खुलने वाला टॉगलटिप इस्तेमाल करें।
आम विफलताएँ
आमतौर पर यह कहाँ गलत होता है
पहुँचने की कोशिश करते ही ग़ायब
ट्रिगर और पॉपअप के बीच का गैप, बिना किसी मोहलत के, पॉइंटर पार करते ही पॉपअप बंद कर देता है। ज़ूम करके देखने वालों को पढ़ने के लिए उस पर जाना पड़ता है। थोड़ी देरी और एक अदृश्य पुल उसे खुला रखते हैं।
हटाने का कोई तरीका नहीं
जो पॉपअप अगली लाइन ढक ले और Escape को अनदेखा करे, वह लोगों को पॉइंटर हिलाने पर मजबूर करता है, जिससे जो वे पढ़ रहे थे वही छिप सकता है। Escape को बिना कुछ हिलाए उसे छिपाना चाहिए।
टाइमर से बंद होना
दो सेकंड बाद ग़ायब होने वाला पॉपअप धीरे पढ़ने वालों के पढ़ लेने से पहले ही चला जाता है। उसे तब तक रहना चाहिए जब तक पॉइंटर या फ़ोकस न हटे, या Escape न दबे।
सिर्फ़ होवर
जो पॉपअप सिर्फ़ माउस ले जाने पर खुले, उसे कीबोर्ड उपयोगकर्ता कभी नहीं देख पाते, और उसके अंदर के लिंक तक उनका फ़ोकस कभी नहीं पहुँचता। उसे फ़ोकस पर भी दिखाएँ, और डीओएम में उसके ट्रिगर के बगल में रखें।
टूलटिप में लिंक
role=tooltip में इंटरैक्टिव सामग्री नहीं रह सकती। प्रोफ़ाइल कार्ड में लिंक हैं, इसलिए वह टूलटिप नहीं, नाम वाला समूह है जिसमें फ़ोकस जा सकता है।
नोट्स
इसे बनाना
- popover=manual हर पॉपअप को टॉप लेयर में रखता है, इसलिए पैरेंट का overflow उसे काट नहीं सकता, जबकि वह डीओएम और Tab क्रम में अपने ट्रिगर के बगल में रहता है।
- पुल पॉपअप पर बना एक ::after है जो ट्रिगर की ओर का गैप ढकता है, साथ में छिपाने से पहले 300 ms की मोहलत: दोनों ज़रूरी हैं, क्योंकि पॉइंटर सीधी रेखा में नहीं चलते।
- Escape के बाद पॉपअप तब तक हटा रहता है जब तक पॉइंटर और फ़ोकस दोनों उसके ट्रिगर से हटकर वापस न आएँ, ताकि वह रुके हुए पॉइंटर के नीचे फिर न उभरे।
- जाँच असली घटनाओं पर नज़र रखती है: पॉपअप खुला होने पर Escape, पॉइंटर का पॉपअप तक पहुँचना, और पॉपअप का तीन सेकंड खुले रहना या टाइमर से बंद हो जाना।
- डेमो के लिंक #… पर जाते हैं, क्योंकि उनके पेज यहाँ मौजूद नहीं हैं; असली साइट में ये सामान्य लिंक होते हैं।
स्रोत: डब्ल्यूसीएजी 2.2 को समझना: होवर या फ़ोकस पर सामग्री · WAI-एआरआईए ऑथरिंग प्रैक्टिसेज़: टूलटिप · एचटीएमएल: popover एट्रिब्यूट
लाइट और डार्क थीम में, डेस्कटॉप और फ़ोन की चौड़ाई पर axe से और कीबोर्ड से जाँचा गया। अभी हर स्क्रीन रीडर और ब्राउज़र जोड़ी के साथ नहीं परखा गया। सुधार बताएँ