सुलभता पैटर्न · फ़ॉर्म इनपुट
स्टार रेटिंग
रेटिंग देना पाँच में से एक चुनना है, इसलिए स्टार एक fieldset में रेडियो बटन हैं: ऐरो कुंजियाँ, एक ही Tab पड़ाव और चुनी हुई स्थिति ब्राउज़र से मिलती हैं। औसत दिखाना कोई कंट्रोल है ही नहीं; यह टेक्स्ट है, "5 में से 4.3", और उसके पास बने स्टार सिर्फ़ सजावट हैं।
- डब्ल्यूसीएजी मानदंड
- 7
- कीबोर्ड नियम
- 5
- जाँच का तरीक़ा
- axe, कीबोर्ड और इंस्पेक्टर
लाइव डेमो
आज़माएँ
इसे माउस, कीबोर्ड या स्क्रीन रीडर से इस्तेमाल करें। साथ का इंस्पेक्टर दिखाता है कि आपके इस्तेमाल के दौरान ब्राउज़र सहायक तकनीक को क्या बताता है: फ़ोकस, स्थिति में बदलाव और घोषणाएँ।
जावास्क्रिप्ट के बिना डेमो उतना ही काम करता है जितना उसका एचटीएमएल; इंस्पेक्टर के लिए जावास्क्रिप्ट चाहिए।
कीबोर्ड
जिन कुंजियों पर यह काम करता है
हर क्रिया पॉइंटर के बिना काम करती है।
| कुंजी | यह क्या करती है |
|---|---|
| Tab | स्टार में, चुने हुए स्टार पर (या कोई न चुना हो तो पहले पर), फिर रेटिंग हटाएँ पर ले जाता है। |
| Arrow RightयाArrow Down | एक स्टार और चुनता है, 5 के बाद फिर 1 पर आ जाता है। |
| Arrow LeftयाArrow Up | एक स्टार कम चुनता है, 1 के बाद फिर 5 पर आ जाता है। |
| Space | जब अभी तक कोई न चुना हो, तो फ़ोकस वाला स्टार चुनता है। |
| EnterयाSpace | रेटिंग हटाएँ पर, रेटिंग हटाता है और फ़ोकस वापस पहले स्टार पर लाता है। |
स्क्रीन रीडर
यह क्या घोषित करता है
मार्कअप की भूमिकाओं, नामों और स्थितियों से लिखा गया।
| कब | अपेक्षित घोषणा |
|---|---|
| रेटिंग देने से पहले Tab स्टार पर पहुँचता है | इस प्रोडक्ट को रेटिंग दें, समूह। 1 स्टार, रेडियो बटन, नहीं चुना हुआ, 5 में से 1 |
| Arrow Right 4 स्टार चुनता है | 4 स्टार, रेडियो बटन, चुना हुआ, 5 में से 4 |
| चुनाव एक पल के लिए ठहरता है | रेटिंग सहेजी गई |
| औसत पढ़ना | ग्राहकों की समीक्षाएँ, हेडिंग स्तर 3। 5 में से 4.3। 128 समीक्षाएँ |
| बँटवारा पढ़ना | रेटिंग के हिसाब से समीक्षाएँ, सूची, 5 आइटम। 5 स्टार 74 समीक्षाएँ |
ये अपेक्षित घोषणाएँ हैं, रिकॉर्डिंग नहीं। स्क्रीन रीडर और ब्राउज़र के हिसाब से शब्द और क्रम बदलते हैं।
कोड
कोड कॉपी करें
वही फ़ाइलें जिन पर यह डेमो चलता है। स्टाइल एक प्रीफ़िक्स के साथ परोसी जाती हैं जो इस साइट की अपनी स्टाइल को डेमो से दूर रखता है; आप जो कॉपी करते हैं वह मूल है।
<div class="ap-rating" data-ap-rating>
<form class="ap-rating__card" data-ap-rate>
<div class="ap-rating__product">
<span class="ap-rating__thumb" aria-hidden="true">
<svg viewBox="0 0 24 24" focusable="false"><path d="M8 3h8l3 8H5Z"/><path d="M12 11v7"/><path d="M8 21h8"/><path d="M12 18v3"/></svg>
</span>
<div>
<p class="ap-rating__item">Brass table lamp</p>
<p class="ap-rating__meta">Delivered on 2 October</p>
</div>
</div>
<fieldset class="ap-rating__set">
<legend class="ap-rating__legend">Rate this product</legend>
<div class="ap-rating__stars">
<label class="ap-rating__star">
<input class="ap-rating__radio" type="radio" name="rating-score" value="1" data-word="Poor" />
<svg class="ap-rating__shape" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 2.8l2.8 5.8 6.3.9-4.6 4.4 1.1 6.3L12 17.2l-5.6 3 1.1-6.3L2.9 9.5l6.3-.9Z"/></svg>
<span class="ap-rating__sr">1 star</span>
</label>
<label class="ap-rating__star">
<input class="ap-rating__radio" type="radio" name="rating-score" value="2" data-word="Fair" />
<svg class="ap-rating__shape" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 2.8l2.8 5.8 6.3.9-4.6 4.4 1.1 6.3L12 17.2l-5.6 3 1.1-6.3L2.9 9.5l6.3-.9Z"/></svg>
<span class="ap-rating__sr">2 stars</span>
</label>
<label class="ap-rating__star">
<input class="ap-rating__radio" type="radio" name="rating-score" value="3" data-word="Good" />
<svg class="ap-rating__shape" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 2.8l2.8 5.8 6.3.9-4.6 4.4 1.1 6.3L12 17.2l-5.6 3 1.1-6.3L2.9 9.5l6.3-.9Z"/></svg>
<span class="ap-rating__sr">3 stars</span>
</label>
<label class="ap-rating__star">
<input class="ap-rating__radio" type="radio" name="rating-score" value="4" data-word="Very good" />
<svg class="ap-rating__shape" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 2.8l2.8 5.8 6.3.9-4.6 4.4 1.1 6.3L12 17.2l-5.6 3 1.1-6.3L2.9 9.5l6.3-.9Z"/></svg>
<span class="ap-rating__sr">4 stars</span>
</label>
<label class="ap-rating__star">
<input class="ap-rating__radio" type="radio" name="rating-score" value="5" data-word="Excellent" />
<svg class="ap-rating__shape" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 2.8l2.8 5.8 6.3.9-4.6 4.4 1.1 6.3L12 17.2l-5.6 3 1.1-6.3L2.9 9.5l6.3-.9Z"/></svg>
<span class="ap-rating__sr">5 stars</span>
</label>
<span class="ap-rating__word" aria-hidden="true" data-ap-word></span>
</div>
</fieldset>
<div class="ap-rating__foot">
<p class="ap-rating__chosen" data-ap-chosen>Not rated yet</p>
<button type="button" class="ap-btn ap-btn--ghost ap-rating__clear" data-ap-unrate hidden>Clear rating</button>
</div>
<p class="ap-rating__saved" role="status" data-ap-say></p>
</form>
<section class="ap-rating__card ap-rating__summary" aria-labelledby="rating-avg">
<h3 class="ap-rating__sum" id="rating-avg">Customer reviews</h3>
<div class="ap-rating__avg">
<span class="ap-rating__meter" aria-hidden="true" data-ap-value="4.3" data-ap-of="5">
<span class="ap-rating__row"><svg class="ap-rating__shape" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 2.8l2.8 5.8 6.3.9-4.6 4.4 1.1 6.3L12 17.2l-5.6 3 1.1-6.3L2.9 9.5l6.3-.9Z"/></svg><svg class="ap-rating__shape" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 2.8l2.8 5.8 6.3.9-4.6 4.4 1.1 6.3L12 17.2l-5.6 3 1.1-6.3L2.9 9.5l6.3-.9Z"/></svg><svg class="ap-rating__shape" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 2.8l2.8 5.8 6.3.9-4.6 4.4 1.1 6.3L12 17.2l-5.6 3 1.1-6.3L2.9 9.5l6.3-.9Z"/></svg><svg class="ap-rating__shape" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 2.8l2.8 5.8 6.3.9-4.6 4.4 1.1 6.3L12 17.2l-5.6 3 1.1-6.3L2.9 9.5l6.3-.9Z"/></svg><svg class="ap-rating__shape" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 2.8l2.8 5.8 6.3.9-4.6 4.4 1.1 6.3L12 17.2l-5.6 3 1.1-6.3L2.9 9.5l6.3-.9Z"/></svg></span>
<span class="ap-rating__row ap-rating__row--fill"><svg class="ap-rating__shape" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 2.8l2.8 5.8 6.3.9-4.6 4.4 1.1 6.3L12 17.2l-5.6 3 1.1-6.3L2.9 9.5l6.3-.9Z"/></svg><svg class="ap-rating__shape" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 2.8l2.8 5.8 6.3.9-4.6 4.4 1.1 6.3L12 17.2l-5.6 3 1.1-6.3L2.9 9.5l6.3-.9Z"/></svg><svg class="ap-rating__shape" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 2.8l2.8 5.8 6.3.9-4.6 4.4 1.1 6.3L12 17.2l-5.6 3 1.1-6.3L2.9 9.5l6.3-.9Z"/></svg><svg class="ap-rating__shape" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 2.8l2.8 5.8 6.3.9-4.6 4.4 1.1 6.3L12 17.2l-5.6 3 1.1-6.3L2.9 9.5l6.3-.9Z"/></svg><svg class="ap-rating__shape" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 2.8l2.8 5.8 6.3.9-4.6 4.4 1.1 6.3L12 17.2l-5.6 3 1.1-6.3L2.9 9.5l6.3-.9Z"/></svg></span>
</span>
<p class="ap-rating__score">4.3 out of 5</p>
<p class="ap-rating__count">128 reviews</p>
</div>
<ul class="ap-rating__dist" aria-label="Reviews by rating">
<li class="ap-rating__dist-row"><span class="ap-rating__dist-k">5 stars</span><span class="ap-rating__bar" aria-hidden="true"><span data-ap-share="74"></span></span><span class="ap-rating__dist-n"><span translate="no">74</span><span class="ap-rating__sr"> reviews</span></span></li>
<li class="ap-rating__dist-row"><span class="ap-rating__dist-k">4 stars</span><span class="ap-rating__bar" aria-hidden="true"><span data-ap-share="32"></span></span><span class="ap-rating__dist-n"><span translate="no">32</span><span class="ap-rating__sr"> reviews</span></span></li>
<li class="ap-rating__dist-row"><span class="ap-rating__dist-k">3 stars</span><span class="ap-rating__bar" aria-hidden="true"><span data-ap-share="11"></span></span><span class="ap-rating__dist-n"><span translate="no">11</span><span class="ap-rating__sr"> reviews</span></span></li>
<li class="ap-rating__dist-row"><span class="ap-rating__dist-k">2 stars</span><span class="ap-rating__bar" aria-hidden="true"><span data-ap-share="6"></span></span><span class="ap-rating__dist-n"><span translate="no">6</span><span class="ap-rating__sr"> reviews</span></span></li>
<li class="ap-rating__dist-row"><span class="ap-rating__dist-k">1 star</span><span class="ap-rating__bar" aria-hidden="true"><span data-ap-share="5"></span></span><span class="ap-rating__dist-n"><span translate="no">5</span><span class="ap-rating__sr"> reviews</span></span></li>
</ul>
</section>
</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; }
}
/* Star rating. Uses the --ap-* design tokens and the shared primitives. */
.ap-rating {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 290px), 1fr));
align-items: start;
gap: 16px;
width: min(100%, 700px);
margin-inline: auto;
color: var(--ap-text);
font-family: var(--ap-font);
}
.ap-rating__card {
min-width: 0;
padding: 22px 24px;
border: 1px solid var(--ap-border);
border-radius: var(--ap-radius-lg);
background: var(--ap-surface);
box-shadow: var(--ap-shadow-md);
}
/* The product being rated */
.ap-rating__product {
display: flex;
align-items: center;
gap: 12px;
}
.ap-rating__thumb {
display: grid;
flex-shrink: 0;
place-items: center;
width: 48px;
height: 48px;
border-radius: var(--ap-radius-sm);
background: var(--ap-surface-2);
color: var(--ap-text-2);
}
.ap-rating__thumb svg {
width: 26px;
height: 26px;
fill: none;
stroke: currentColor;
stroke-width: 1.6;
stroke-linecap: round;
stroke-linejoin: round;
}
.ap-rating__item {
margin: 0;
font-weight: 600;
}
.ap-rating__meta {
margin: 2px 0 0;
color: var(--ap-text-3);
font-size: .875rem;
}
/* The question and its five radio buttons */
.ap-rating__set {
min-width: 0;
margin: 18px 0 0;
padding: 16px 0 0;
border: 0;
border-top: 1px solid var(--ap-border);
}
.ap-rating__legend {
float: left;
width: 100%;
margin: 0 0 6px;
padding: 0;
font-size: 1rem;
font-weight: 650;
}
.ap-rating__stars {
clear: both;
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 2px;
margin-left: -6px;
}
.ap-rating__star {
position: relative;
display: grid;
place-items: center;
width: 44px;
height: 44px;
border-radius: var(--ap-radius-sm);
cursor: pointer;
}
.ap-rating__radio {
position: absolute;
inset: 0;
z-index: 1;
width: 100%;
height: 100%;
margin: 0;
opacity: 0;
cursor: pointer;
}
.ap-rating__star:has(.ap-rating__radio:focus-visible) {
outline: 2px solid var(--ap-focus);
outline-offset: 0;
}
.ap-rating__shape {
width: 32px;
height: 32px;
fill: transparent;
stroke: var(--ap-text-3);
stroke-width: 1.6;
stroke-linejoin: round;
transition: fill var(--ap-duration) var(--ap-ease), stroke var(--ap-duration) var(--ap-ease), transform var(--ap-duration) var(--ap-ease);
}
/* Filled: the checked star and every star before it. */
.ap-rating__star:is(:has(:checked), :has(~ .ap-rating__star :checked)) .ap-rating__shape {
fill: var(--ap-warning);
stroke: var(--ap-warning);
}
/* While a pointer is over a star, the preview takes over; nothing is chosen until a click. */
@media (hover: hover) {
.ap-rating__stars:has(.ap-rating__star:hover) .ap-rating__star .ap-rating__shape {
fill: transparent;
stroke: var(--ap-text-3);
}
.ap-rating__stars:has(.ap-rating__star:hover) :is(.ap-rating__star:hover, .ap-rating__star:has(~ .ap-rating__star:hover)) .ap-rating__shape {
fill: var(--ap-warning);
fill-opacity: .45;
stroke: var(--ap-warning);
}
/* Stars both chosen and previewed stay solid; only the extra ones are pale. */
.ap-rating__stars:has(.ap-rating__star:hover) .ap-rating__star:is(:has(:checked), :has(~ .ap-rating__star :checked)):is(:hover, :has(~ .ap-rating__star:hover)) .ap-rating__shape {
fill-opacity: 1;
}
.ap-rating__star:hover .ap-rating__shape {
transform: scale(1.1);
}
}
.ap-rating__word {
margin-left: 10px;
color: var(--ap-text-2);
font-weight: 600;
}
.ap-rating__word.is-preview {
color: var(--ap-text-3);
font-weight: 500;
}
.ap-rating__foot {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: 8px 12px;
min-height: 40px;
margin-top: 12px;
}
.ap-rating__chosen {
margin: 0;
color: var(--ap-text-2);
}
.ap-rating__chosen strong {
color: var(--ap-text);
}
.ap-rating__clear[hidden] {
display: none;
}
.ap-rating__saved {
display: flex;
align-items: center;
gap: 6px;
margin: 6px 0 0;
color: var(--ap-success);
font-size: .875rem;
font-weight: 600;
}
.ap-rating__saved:empty {
margin: 0;
}
.ap-rating__saved svg {
width: 16px;
height: 16px;
fill: none;
stroke: currentColor;
stroke-width: 2.4;
stroke-linecap: round;
stroke-linejoin: round;
}
/* The read-only average */
.ap-rating__sum {
margin: 0;
font-size: 1rem;
font-weight: 650;
}
.ap-rating__avg {
display: grid;
justify-items: start;
gap: 2px;
margin-top: 14px;
}
.ap-rating__meter {
position: relative;
display: inline-block;
margin-bottom: 6px;
}
.ap-rating__row {
display: flex;
gap: 3px;
white-space: nowrap;
}
.ap-rating__row .ap-rating__shape {
flex-shrink: 0;
width: 26px;
height: 26px;
stroke-width: 1.4;
}
.ap-rating__row--fill {
position: absolute;
inset: 0 auto 0 0;
width: var(--fill, 0);
overflow: hidden;
}
.ap-rating__row--fill .ap-rating__shape {
fill: var(--ap-warning);
stroke: var(--ap-warning);
}
.ap-rating__score {
margin: 0;
font-size: 1.375rem;
font-weight: 700;
line-height: 1.3;
}
.ap-rating__count {
margin: 0;
color: var(--ap-text-3);
font-size: .875rem;
}
.ap-rating__dist {
display: grid;
gap: 8px;
margin: 18px 0 0;
padding: 16px 0 0;
border-top: 1px solid var(--ap-border);
list-style: none;
}
.ap-rating__dist-row {
display: grid;
grid-template-columns: auto minmax(0, 1fr) 2.5ch;
align-items: center;
gap: 10px;
font-size: .875rem;
}
.ap-rating__dist-k {
min-width: 52px;
color: var(--ap-text-2);
white-space: nowrap;
}
.ap-rating__bar {
height: 8px;
overflow: hidden;
border-radius: var(--ap-radius-full);
background: var(--ap-surface-3);
}
.ap-rating__bar span {
display: block;
width: var(--share, 0);
height: 100%;
border-radius: var(--ap-radius-full);
background: var(--ap-warning);
}
.ap-rating__dist-n {
color: var(--ap-text-3);
font-variant-numeric: tabular-nums;
text-align: end;
}
.ap-rating__sr {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}
@media (prefers-reduced-motion: reduce) {
.ap-rating__shape { transition: none; }
.ap-rating__star:hover .ap-rating__shape { transform: none; }
}
/**
* Star rating: five native radio buttons drawn as SVG stars, a hover preview
* that never commits, the choice written out and saved once it settles, and a
* read-only average shown as text with the stars as decoration.
*
* Markup: [data-ap-rating] holding a form[data-ap-rate] with a fieldset of
* label.ap-rating__star > input[type=radio][data-word] + svg + span (the
* name), a [data-ap-word] preview, [data-ap-chosen], a [data-ap-unrate]
* button and p[role=status][data-ap-say]; and a summary with
* [data-ap-value][data-ap-of] (the average) and [data-ap-share] bars.
*/
const SVG = "http://www.w3.org/2000/svg";
function span(text) {
const s = document.createElement("span");
s.textContent = text;
return s;
}
function tick() {
const svg = document.createElementNS(SVG, "svg");
svg.setAttribute("viewBox", "0 0 24 24");
svg.setAttribute("aria-hidden", "true");
svg.setAttribute("focusable", "false");
const path = document.createElementNS(SVG, "path");
path.setAttribute("d", "m5 12 5 5L20 7");
svg.append(path);
return svg;
}
export function init(root) {
const radios = [...root.querySelectorAll(".ap-rating__radio")];
const stars = root.querySelector(".ap-rating__stars");
const word = root.querySelector("[data-ap-word]");
const chosen = root.querySelector("[data-ap-chosen]");
const unrate = root.querySelector("[data-ap-unrate]");
const say = root.querySelector("[data-ap-say]");
let saveTimer = 0;
const checked = () => radios.find((r) => r.checked);
const nameOf = (radio) => radio.closest("label").querySelector(".ap-rating__sr").textContent.trim();
function showWord(radio, preview) {
word.textContent = radio ? radio.dataset.word : "";
word.classList.toggle("is-preview", preview);
}
function show() {
const radio = checked();
showWord(radio, false);
unrate.hidden = !radio;
if (!radio) {
chosen.replaceChildren(span("Not rated yet"));
return;
}
const strong = document.createElement("strong");
strong.textContent = nameOf(radio);
chosen.replaceChildren(span("Your rating:"), " ", strong);
}
// Saved once the choice has been still for a moment: arrowing from 1 to 5 is one save.
function onChange(event) {
if (!radios.includes(event.target)) return;
show();
clearTimeout(saveTimer);
say.replaceChildren();
saveTimer = setTimeout(() => say.replaceChildren(tick(), span("Rating saved")), 800);
}
function onUnrate() {
clearTimeout(saveTimer);
for (const r of radios) r.checked = false;
show();
radios[0].focus();
say.replaceChildren();
saveTimer = setTimeout(() => say.replaceChildren(span("Rating removed")), 60);
}
/* The hover preview: a word beside the stars; the fill is done in CSS. */
function onOver(event) {
const radio = event.target.closest(".ap-rating__star")?.querySelector(".ap-rating__radio");
if (radio) showWord(radio, true);
}
function onLeave() {
showWord(checked(), false);
}
stars.addEventListener("pointerover", onOver);
stars.addEventListener("pointerleave", onLeave);
root.addEventListener("change", onChange);
unrate.addEventListener("click", onUnrate);
show();
/* The read-only average: the filled row is clipped to the exact share of five stars. */
const meter = root.querySelector("[data-ap-value]");
if (meter) {
const value = Number(meter.dataset.apValue);
const shapes = meter.querySelectorAll(".ap-rating__row:first-child .ap-rating__shape");
const whole = Math.floor(value);
const at = shapes[Math.min(whole, shapes.length - 1)];
const width = whole >= shapes.length ? meter.offsetWidth : at.getBoundingClientRect().left - meter.getBoundingClientRect().left + (value - whole) * at.getBoundingClientRect().width;
meter.style.setProperty("--fill", `${width}px`);
}
const shares = [...root.querySelectorAll("[data-ap-share]")];
const total = shares.reduce((sum, s) => sum + Number(s.dataset.apShare), 0);
for (const s of shares) s.style.setProperty("--share", `${(Number(s.dataset.apShare) / total) * 100}%`);
return () => {
clearTimeout(saveTimer);
stars.removeEventListener("pointerover", onOver);
stars.removeEventListener("pointerleave", onLeave);
root.removeEventListener("change", onChange);
unrate.removeEventListener("click", onUnrate);
};
}
for (const root of document.querySelectorAll("[data-ap-rating]")) 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.1.1 गैर-टेक्स्ट सामग्री
स्तर A
हर स्टार का एक टेक्स्ट नाम है, 1 स्टार से 5 स्टार तक, और औसत टेक्स्ट में लिखा है, इसलिए कोई भी अर्थ सिर्फ़ चित्रों के भरोसे नहीं है।
-
1.3.1 जानकारी और संबंध
स्तर A
स्टार एक fieldset में रेडियो बटन हैं जिसका legend सवाल पूछता है; औसत एक हेडिंग, टेक्स्ट और एक सूची है।
-
1.4.1 रंग का उपयोग
स्तर A
चुने हुए स्टार भरे जाते हैं, सिर्फ़ उनका रंग नहीं बदलता, और चुनाव उनके नीचे लिखा भी जाता है।
-
1.4.11 गैर-टेक्स्ट कंट्रास्ट
स्तर AA
ख़ाली स्टार की बाहरी रेखा कार्ड के मुकाबले 3:1 या ज़्यादा है, इसलिए कोई भी चुने जाने से पहले पाँचों दिखते हैं।
-
2.1.1 कीबोर्ड
स्तर A
किसी भी रेडियो समूह की तरह ऐरो कुंजियाँ रेटिंग चुनती हैं; रेटिंग हटाएँ एक बटन है।
-
2.4.7 फ़ोकस दिखाई देना
स्तर AA
फ़ोकस वाला स्टार एक घेरा दिखाता है, जो उसके लेबल के चारों ओर बनता है क्योंकि रेडियो ख़ुद पारदर्शी है।
-
4.1.3 स्टेटस मैसेज
स्तर AA
चुनाव ठहरने के बाद सहेजने की पुष्टि एक स्टेटस मैसेज में होती है, हर ऐरो दबाने पर नहीं।
उपयोग
इसे कब इस्तेमाल करें
इसे अपनाएँ
- किसी पैमाने पर जल्दी राय माँगना, जैसे किसी ख़रीद, रेसिपी या सपोर्ट के जवाब को रेटिंग देना।
- किसी प्रोडक्ट, जगह या कोर्स के पास औसत रेटिंग दिखाना।
कुछ और इस्तेमाल करें
- नाम वाले जवाबों वाले सवाल, जैसे संतुष्ट या संतुष्ट नहीं: उन्हीं शब्दों वाला सादा रेडियो समूह इस्तेमाल करें।
- बारीक अंक, जैसे 1 से 10: संख्या इनपुट या स्लाइडर इस्तेमाल करें।
आम विफलताएँ
आमतौर पर यह कहाँ गलत होता है
क्लिक होने वाले span से बने स्टार
क्लिक हैंडलर वाले पाँच आइकन तक कीबोर्ड से नहीं पहुँचा जा सकता, न ही स्क्रीन रीडर उनमें फ़र्क कर पाता है। नेटिव रेडियो बटन दोनों देते हैं।
होवर जो रेटिंग बदल दे
होवर पर चुनाव कर देने से स्टार के ऊपर से गुज़रता पॉइंटर पहले का चुनाव मिटा देता है। यहाँ होवर सिर्फ़ झलक दिखाता है; चुनाव क्लिक या कुंजी से होता है।
सिर्फ़ स्टार से बना औसत
सवा चार पीले स्टार का स्क्रीन रीडर या हाई कंट्रास्ट मोड में कोई मतलब नहीं। यहाँ संख्या लिखी गई है, और चित्र छिपा है।
निष्क्रिय कंट्रोल जैसा औसत
औसत के लिए केवल पढ़ने लायक स्लाइडर या निष्क्रिय रेडियो समूह सिर्फ़ शोर है: लोग Tab से उस पर पहुँचते हैं, या उसे अनुपलब्ध समझकर छोड़ देते हैं। सही एलिमेंट टेक्स्ट है।
बिना रेटिंग पर लौटने का कोई रास्ता नहीं
रेडियो समूह का चुनाव अपने-आप हटाया नहीं जा सकता। ग़लती से क्लिक हो जाने पर रेटिंग हटाएँ लोगों को बाहर निकलने का रास्ता देता है।
नोट्स
इसे बनाना
- पारदर्शी रेडियो इनपुट अपने स्टार को ढकता है, इसलिए पूरा स्टार निशाना है और अपना नेटिव फ़ोकस और कुंजियाँ बनाए रखता है; घेरा :has(:focus-visible) से बनता है।
- भराव :has() से आता है: स्टार तब भरता है जब उसका अपना रेडियो, या उसके बाद वाला कोई, चुना हुआ हो। होवर वाली झलक :hover के साथ यही तरकीब इस्तेमाल करती है।
- औसत में अधूरा स्टार भरे हुए स्टार की एक पंक्ति है, जिसे 4.3 स्टार पर काटा गया है; स्क्रिप्ट इसे data-ap-value से मापती है, और पास का टेक्स्ट यही बात कहता है।
- चुनाव के एक पल ठहरने के बाद रेटिंग सहेजी जाती है, इसलिए ऐरो से 1 से 5 तक जाने पर चार नहीं, एक ही अनुरोध जाता है।
स्रोत: WAI-एआरआईए ऑथरिंग प्रैक्टिसेज़: रेडियो ग्रुप · एचटीएमएल: fieldset एलिमेंट
लाइट और डार्क थीम में, डेस्कटॉप और फ़ोन की चौड़ाई पर axe से और कीबोर्ड से जाँचा गया। अभी हर स्क्रीन रीडर और ब्राउज़र जोड़ी के साथ नहीं परखा गया। सुधार बताएँ