דילוג לתוכן
uiify

Rating

שדה לבחירת דירוג מתוך שורת כוכבים.

דפדפנים
Chrome 123+Firefox 121+Safari 17.5+
שכבה
0
גודל חבילה דחוסה
1.61 kB
מגבלת פריסה כפויה
0 ms
מגבלת Axe
0
תואם SSR
כן
מבוסס על
elements/choice
חוזה מפורסם
סביבת בדיקות

מגרש ניסויים

Rating

ברירת מחדל

import { Rating } from "@gunkin/uiify";

export default function Example() {
  return (
    <Rating
      className=""
      defaultValue={3}
      id="movie-rating"
      slotProps={{
        input: { className: "" },
        label: { className: "" },
      }}
    />
  );
}
/* Rating — a native radio group of stars. Inputs are visually hidden;
   flex-direction: row-reverse restores left-to-right visual order over
   reversed DOM order. Each input+label pair sits in its own [data-part="item"]
   wrapper (ChoiceGroup's shape), so `~` alone can't reach across pairs —
   `:has()` promotes a checked/hovered item onto its later (= more-preferred,
   thanks to row-reverse) sibling wrappers. This keeps the uncontrolled rating functional without
   JavaScript. */

@layer components {
  [data-uiify-rating] {
    display: inline-flex;
    flex-direction: row-reverse;
    gap: var(--space-xs);
  }

  [data-uiify-rating] > [data-part="item"] > input {
    position: absolute;
    inline-size: 1px;
    block-size: 1px;
    margin: -1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

  [data-uiify-rating] > [data-part="item"] > label {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: var(--input);
    font-size: 1.5rem;
    line-height: 1;
    cursor: pointer;
    transition: var(--transition-colors);
  }

  [data-uiify-rating] > [data-part="item"]:has(> input:checked) > label,
  [data-uiify-rating] > [data-part="item"]:has(> input:checked) ~ [data-part="item"] > label {
    color: var(--primary);
  }

  [data-uiify-rating]:not([data-disabled], [data-readonly])
    > [data-part="item"]:has(> label:hover)
    > label,
  [data-uiify-rating]:not([data-disabled], [data-readonly])
    > [data-part="item"]:has(> label:hover)
    ~ [data-part="item"]
    > label {
    color: var(--primary);
  }

  [data-uiify-rating] > [data-part="item"] > input:focus-visible ~ label {
    outline: none;
    box-shadow: 0 0 0 var(--ring-width) color-mix(in oklch, var(--ring) 20%, transparent);
    border-radius: var(--radius-xs);
  }

  [data-uiify-rating][data-readonly] > [data-part="item"] > label {
    cursor: default;
  }

  [data-uiify-rating][data-disabled] > [data-part="item"] > label {
    cursor: default;
    opacity: 0.5;
  }
}
import { Rating } from "@gunkin/uiify";

export default function Example() {
  return (
    <Rating
      className="
        group/rating inline-flex [flex-direction:row-reverse] gap-xs
        [&>[data-part=item]:has(>input:checked)>label]:text-primary
        [&>[data-part=item]:has(>input:checked)~[data-part=item]>label]:text-primary
        [&:not([data-disabled],[data-readonly])>[data-part=item]:has(>label:hover)>label]:text-primary
        [&:not([data-disabled],[data-readonly])>[data-part=item]:has(>label:hover)~[data-part=item]>label]:text-primary
      "
      defaultValue={3}
      id="movie-rating"
      slotProps={{
        input: {
          className:
            "peer absolute [inline-size:1px] [block-size:1px] [margin:-1px] overflow-hidden " +
            "[clip-path:inset(50%)] whitespace-nowrap",
        },
        label: {
          className:
            "inline-flex items-center justify-center text-input [font-size:1.5rem] " +
            "leading-none cursor-pointer [transition:var(--transition-colors)] " +
            "peer-checked:text-primary peer-focus-visible:[outline:none] " +
            "peer-focus-visible:[box-shadow:0_0_0_var(--ring-width)_color-mix(in_oklch,_var(--ring)_20%,_transparent)] " +
            "peer-focus-visible:rounded-xs group-data-[readonly]/rating:cursor-default " +
            "group-data-[disabled]/rating:cursor-default " +
            "group-data-[disabled]/rating:opacity-50",
        },
      }}
    />
  );
}

API

Rating

מאפייןטיפוסברירת מחדלתיאור
id*string—קידומת מזהה לקבוצה ולכל קלט רדיו שנוצר.
defaultValuenumber0הדירוג הנבחר בהצגה הראשונה; הבחירה והתצוגה המקדימה בריחוף מבוססות על קלטי רדיו מובנים ו-CSS, ולכן פועלות ללא JavaScript.
maxnumber5מספר פריטי הדירוג המוצגים.
readOnlybooleanfalseמציג את הדירוג בלי לאפשר למשתמש לשנותו.
disabledbooleanfalseמשבית את כל קלטי הרדיו בדירוג.
classNamestring—המחלקה המוחלת על אלמנט הדירוג.
aria-labelstring—שם נגיש לקבוצת הדירוג.
slotPropsRatingSlotProps—מאפיינים מובנים לאלמנטי הפריט, הקלט והתווית שנוצרים; ניתן למקד משבצות חוזרות בפריט לפי ערכו.

מקלדת

מקשפעולה
Tabהעברת המיקוד אל הפקד או ממנו
Enter / Spaceהפעלה כשהפקד ממוקד (פקדים דמויי כפתור)

נגישות

משתמש ב-HTML סמנטי ככל האפשר. בדקו את הסימון המרונדר במגרש הניסויים — ARIA נוסף רק כאשר האלמנטים המובנים אינם מספיקים.

נקודות חיבור לעיצוב

data-disabled
דף זה תורגם אוטומטית וטרם נבדק.