|
| 1 | +import * as RadioGroupPrimitive from '@radix-ui/react-radio-group'; |
| 2 | +import { clsx } from 'clsx'; |
| 3 | +import * as React from 'react'; |
| 4 | + |
| 5 | +import { FieldError } from '@/vibes/soul/form/field-error'; |
| 6 | +import { Label } from '@/vibes/soul/form/label'; |
| 7 | +import { Star } from '@/vibes/soul/primitives/rating'; |
| 8 | + |
| 9 | +/** |
| 10 | + * This component supports various CSS variables for theming. Here's a comprehensive list, along |
| 11 | + * with their default values: |
| 12 | + * |
| 13 | + * ```css |
| 14 | + * :root { |
| 15 | + * --rating-radio-group-focus: hsl(var(--primary)); |
| 16 | + * --rating-radio-group-light-star-empty: hsl(var(--contrast-200)); |
| 17 | + * --rating-radio-group-light-star-filled: hsl(var(--foreground)); |
| 18 | + * --rating-radio-group-light-star-hover: hsl(var(--contrast-300)); |
| 19 | + * --rating-radio-group-dark-star-empty: hsl(var(--contrast-400)); |
| 20 | + * --rating-radio-group-dark-star-filled: hsl(var(--background)); |
| 21 | + * --rating-radio-group-dark-star-hover: hsl(var(--contrast-300)); |
| 22 | + * } |
| 23 | + * ``` |
| 24 | + */ |
| 25 | +export const RatingRadioGroup = React.forwardRef< |
| 26 | + React.ComponentRef<typeof RadioGroupPrimitive.Root>, |
| 27 | + React.ComponentPropsWithoutRef<typeof RadioGroupPrimitive.Root> & { |
| 28 | + label: string; |
| 29 | + max?: number; |
| 30 | + errors?: string[]; |
| 31 | + onOptionMouseEnter?: (value: string) => void; |
| 32 | + onOptionMouseLeave?: () => void; |
| 33 | + colorScheme?: 'light' | 'dark'; |
| 34 | + } |
| 35 | +>( |
| 36 | + ( |
| 37 | + { |
| 38 | + label, |
| 39 | + max = 5, |
| 40 | + errors, |
| 41 | + className, |
| 42 | + onOptionMouseEnter, |
| 43 | + onOptionMouseLeave, |
| 44 | + colorScheme = 'light', |
| 45 | + ...rest |
| 46 | + }, |
| 47 | + ref, |
| 48 | + ) => { |
| 49 | + const groupId = React.useId(); |
| 50 | + const [previewValue, setPreviewValue] = React.useState<string | null>(null); |
| 51 | + const isMouseDownRef = React.useRef(false); |
| 52 | + |
| 53 | + const currentValue = rest.value?.toString() ?? '0'; |
| 54 | + const displayRating = parseInt(previewValue ?? currentValue, 10) || 0; |
| 55 | + |
| 56 | + const handleMouseLeave = () => { |
| 57 | + setPreviewValue(null); |
| 58 | + onOptionMouseLeave?.(); |
| 59 | + }; |
| 60 | + |
| 61 | + const handleMouseDown = () => { |
| 62 | + isMouseDownRef.current = true; |
| 63 | + }; |
| 64 | + |
| 65 | + const handleMouseUp = () => { |
| 66 | + isMouseDownRef.current = false; |
| 67 | + }; |
| 68 | + |
| 69 | + const handleBlur = () => { |
| 70 | + if (!isMouseDownRef.current) { |
| 71 | + setPreviewValue(null); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + return ( |
| 76 | + <div className={clsx('rating-radio-group space-y-2', className)}> |
| 77 | + <Label colorScheme={colorScheme} id={groupId}> |
| 78 | + {label} |
| 79 | + </Label> |
| 80 | + |
| 81 | + <RadioGroupPrimitive.Root |
| 82 | + {...rest} |
| 83 | + aria-labelledby={groupId} |
| 84 | + className="flex items-center gap-1" |
| 85 | + onMouseDown={handleMouseDown} |
| 86 | + onMouseLeave={handleMouseLeave} |
| 87 | + onMouseUp={handleMouseUp} |
| 88 | + ref={ref} |
| 89 | + > |
| 90 | + <div className="flex items-center gap-1"> |
| 91 | + {Array.from({ length: max }, (_, i) => { |
| 92 | + const ratingValue = i + 1; |
| 93 | + const filled = displayRating >= ratingValue; |
| 94 | + const valueStr = ratingValue.toString(); |
| 95 | + const itemId = `${groupId}-${ratingValue}`; |
| 96 | + |
| 97 | + return ( |
| 98 | + <div className="relative" key={ratingValue}> |
| 99 | + <RadioGroupPrimitive.Item |
| 100 | + className={clsx( |
| 101 | + 'peer sr-only', |
| 102 | + 'data-disabled:pointer-events-none data-disabled:opacity-50 transition-colors focus-visible:outline-0 focus-visible:ring-2', |
| 103 | + { |
| 104 | + light: |
| 105 | + 'focus-visible:ring-[var(--rating-radio-group-focus,hsl(var(--primary)))]', |
| 106 | + dark: 'focus-visible:ring-[var(--rating-radio-group-focus,hsl(var(--primary)))]', |
| 107 | + }[colorScheme], |
| 108 | + )} |
| 109 | + id={itemId} |
| 110 | + onBlur={handleBlur} |
| 111 | + value={valueStr} |
| 112 | + /> |
| 113 | + <label |
| 114 | + aria-label={`${ratingValue} ${ratingValue === 1 ? 'star' : 'stars'}`} |
| 115 | + className="flex shrink-0 cursor-pointer rounded-full transition-colors focus-visible:outline-0 focus-visible:ring-2 peer-focus-visible:ring-2 peer-focus-visible:ring-[var(--rating-radio-group-focus,hsl(var(--primary)))]" |
| 116 | + htmlFor={itemId} |
| 117 | + onMouseEnter={() => { |
| 118 | + setPreviewValue(valueStr); |
| 119 | + onOptionMouseEnter?.(valueStr); |
| 120 | + }} |
| 121 | + > |
| 122 | + <Star type={filled ? 'full' : 'empty'} /> |
| 123 | + </label> |
| 124 | + </div> |
| 125 | + ); |
| 126 | + })} |
| 127 | + </div> |
| 128 | + </RadioGroupPrimitive.Root> |
| 129 | + {errors?.map((error) => ( |
| 130 | + <FieldError key={error}>{error}</FieldError> |
| 131 | + ))} |
| 132 | + </div> |
| 133 | + ); |
| 134 | + }, |
| 135 | +); |
| 136 | + |
| 137 | +RatingRadioGroup.displayName = 'RatingRadioGroup'; |
0 commit comments