@spark-ui/components 15.1.0 → 16.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/rating/Rating.d.ts +17 -12
- package/dist/rating/RatingStar.d.ts +12 -3
- package/dist/rating/index.js +1 -1
- package/dist/rating/index.js.map +1 -1
- package/dist/rating/index.mjs +210 -163
- package/dist/rating/index.mjs.map +1 -1
- package/dist/rating/types.d.ts +2 -1
- package/dist/rating/utils.d.ts +1 -2
- package/dist/rating-display/RatingDisplay.d.ts +26 -0
- package/dist/rating-display/RatingDisplayContext.d.ts +12 -0
- package/dist/rating-display/RatingDisplayCount.d.ts +12 -0
- package/dist/rating-display/RatingDisplayStar.d.ts +16 -0
- package/dist/rating-display/RatingDisplayStars.d.ts +22 -0
- package/dist/rating-display/RatingDisplayValue.d.ts +13 -0
- package/dist/rating-display/index.d.mts +14 -0
- package/dist/rating-display/index.d.ts +14 -0
- package/dist/rating-display/index.js +2 -0
- package/dist/rating-display/index.js.map +1 -0
- package/dist/rating-display/index.mjs +172 -0
- package/dist/rating-display/index.mjs.map +1 -0
- package/dist/rating-display/types.d.ts +1 -0
- package/dist/rating-display/utils.d.ts +10 -0
- package/dist/switch/index.js +1 -1
- package/dist/switch/index.js.map +1 -1
- package/dist/switch/index.mjs +1 -1
- package/dist/switch/index.mjs.map +1 -1
- package/package.json +4 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../../src/rating/RatingStar.styles.ts","../../src/rating/RatingStar.tsx","../../src/rating/utils.ts","../../src/rating/Rating.tsx"],"sourcesContent":["import { cva, cx, VariantProps } from 'class-variance-authority'\n\nconst emptyRemainingStarsOnHoverClass = cx('[&_>_div]:peer-hover:w-0!')\n\nconst ratingStarStyles = cva(\n ['peer', 'after:inset-0', 'group', 'relative', 'after:block after:absolute'],\n {\n variants: {\n disabled: {\n true: 'opacity-dim-3',\n false: '',\n },\n readOnly: {\n true: '',\n false: '',\n },\n gap: {\n sm: ['after:w-[calc(100%+(var(--spacing-sm)))]', 'last-of-type:after:content-none'],\n md: ['after:w-[calc(100%+(var(--spacing-md)))]', 'last-of-type:after:content-none'],\n },\n },\n compoundVariants: [\n {\n readOnly: false,\n disabled: false,\n className: cx(emptyRemainingStarsOnHoverClass, 'cursor-pointer'),\n },\n ],\n defaultVariants: {\n disabled: false,\n readOnly: false,\n gap: 'sm',\n },\n }\n)\n\nconst ratingStarIconStyles = cva('', {\n variants: {\n size: {\n sm: 'text-caption-link',\n md: 'text-body-1',\n lg: 'text-display-1',\n },\n design: {\n filled: [\n 'text-main-variant',\n 'group-[[data-part=star][data-hovered]]:text-main-variant-hovered',\n ],\n outlined: ['text-on-surface/dim-3'],\n },\n },\n})\n\ntype RatingStarstylesProps = Omit<VariantProps<typeof ratingStarStyles>, 'gap'>\ntype RatingStarIconStylesProps = Omit<VariantProps<typeof ratingStarIconStyles>, 'design'>\n\nexport { ratingStarStyles, ratingStarIconStyles }\nexport type { RatingStarstylesProps, RatingStarIconStylesProps }\n","import { StarFill } from '@spark-ui/icons/StarFill'\nimport { StarOutline } from '@spark-ui/icons/StarOutline'\nimport { cx } from 'class-variance-authority'\nimport { type MouseEvent, Ref } from 'react'\n\nimport { Icon } from '../icon'\nimport {\n ratingStarIconStyles,\n type RatingStarIconStylesProps,\n ratingStarStyles,\n type RatingStarstylesProps,\n} from './RatingStar.styles'\nimport type { StarValue } from './types'\n\nexport interface RatingStarProps extends RatingStarstylesProps, RatingStarIconStylesProps {\n value: StarValue\n onClick?: (event: MouseEvent<HTMLDivElement>) => void\n onMouseEnter?: (event: MouseEvent<HTMLDivElement>) => void\n ref?: Ref<HTMLDivElement>\n}\n\nexport const RatingStar = ({\n value,\n size,\n disabled,\n readOnly,\n onClick,\n onMouseEnter,\n ref: forwardedRef,\n}: RatingStarProps) => {\n return (\n <div\n data-spark-component=\"rating-star\"\n ref={forwardedRef}\n onMouseEnter={onMouseEnter}\n className={ratingStarStyles({\n gap: size === 'lg' ? 'md' : 'sm',\n disabled,\n readOnly,\n })}\n data-part=\"star\"\n onClick={onClick}\n >\n <div\n className={cx(\n 'z-raised absolute overflow-hidden',\n 'group-[[data-part=star][data-hovered]]:overflow-visible'\n )}\n style={{ width: value * 100 + '%' }}\n >\n <Icon\n className={ratingStarIconStyles({\n size,\n design: 'filled',\n })}\n >\n <StarFill />\n </Icon>\n </div>\n\n <Icon className={ratingStarIconStyles({ size, design: 'outlined' })}>\n <StarOutline />\n </Icon>\n </div>\n )\n}\n","import { type StarValue } from './types'\n\nfunction getNearestHalfDecimal(num: number): number {\n return Math.round(num / 0.5) * 0.5\n}\n\nfunction getStarValue({ value, index }: { value?: number; index: number }): StarValue {\n if (value === undefined) return 0\n\n const starPosition = index + 1\n const formattedValue = getNearestHalfDecimal(value)\n\n if (Math.ceil(formattedValue) < starPosition) return 0\n\n return formattedValue >= starPosition ? 1 : 0.5\n}\n\nfunction splitAt<T>(arr: T[], index: number): [T[], T[]] {\n const prev = arr.slice(0, index)\n const next = arr.slice(index)\n\n return [prev, next]\n}\n\nexport { getNearestHalfDecimal, getStarValue, splitAt }\n","import { useCombinedState } from '@spark-ui/hooks/use-combined-state'\nimport { cx } from 'class-variance-authority'\nimport {\n type ChangeEvent,\n type ComponentPropsWithRef,\n type MouseEvent,\n type PropsWithChildren,\n useCallback,\n useRef,\n} from 'react'\n\nimport { RatingStar, type RatingStarProps } from './RatingStar'\nimport { getNearestHalfDecimal, getStarValue, splitAt } from './utils'\n\nexport interface RatingProps extends PropsWithChildren<ComponentPropsWithRef<'div'>> {\n /**\n * Use the `defaultValue` prop to set the default value of the input, on a from 0 to 5.\n *\n * Use this when you want to use it in an uncontrolled manner\n */\n defaultValue?: number\n /**\n * The value is the number of the rating selected, on a scale from 0 to 5.\n *\n * Use this when you want to use it in a controlled manner,\n * in conjunction with the `onValueChange` prop\n */\n value?: number\n /**\n * Event handler called when the value changes.\n */\n onValueChange?: (value: number) => void\n /**\n * Sets the component as interactive or not.\n * @default undefined\n */\n readOnly?: boolean\n /**\n * When `true`, prevents the user from interacting.\n * @default false\n */\n disabled?: boolean\n /**\n * Sets the size of the stars.\n * @default 'md'\n */\n size?: RatingStarProps['size']\n /**\n * Name of the underlying input.\n * @default undefined\n */\n name?: string\n /**\n * id of the underlying input.\n * @default undefined\n */\n id?: string\n /**\n * aria-label of the underlying input.\n * @default undefined\n */\n 'aria-label': string\n}\n\nexport const Rating = ({\n defaultValue,\n value: propValue,\n onValueChange,\n size = 'md',\n disabled,\n readOnly,\n name,\n id,\n 'aria-label': ariaLabel,\n ref,\n ...rest\n}: RatingProps) => {\n const inputRef = useRef<HTMLInputElement>(null)\n const starRefList = useRef<HTMLDivElement[]>([])\n\n const [value, setRatingValue] = useCombinedState(propValue, defaultValue, onValueChange)\n\n const valueRef = useRef(value)\n const isInteractive = !(disabled || readOnly)\n\n function onStarClick(index: number) {\n if (!inputRef.current) return\n\n setRatingValue(index + 1)\n valueRef.current = index + 1\n\n inputRef.current.focus()\n inputRef.current.setAttribute('data-clicked', '')\n }\n\n function onInputChange(event: ChangeEvent<HTMLInputElement>) {\n // 1. Avoiding unnecessary calls to onValueChange prop if value doesn't change\n // 2. Preventing value to be resetted to 0\n if (valueRef.current === Number(event.target.value) || Number(event.target.value) === 0) {\n return\n }\n valueRef.current = Number(event.target.value)\n\n setRatingValue(Number(event.target.value))\n }\n\n function onStarMouseEnter({ currentTarget }: MouseEvent<HTMLDivElement>) {\n const currentStarIndex = starRefList.current.findIndex(star => star === currentTarget)\n\n const [previousStars, followingStars] = splitAt(starRefList.current, currentStarIndex + 1)\n\n previousStars.forEach(star => star.setAttribute('data-hovered', ''))\n followingStars.forEach(star => star.removeAttribute('data-hovered'))\n }\n\n const handleStarRef = useCallback((elm: HTMLDivElement | null) => {\n if (!elm) return\n starRefList.current.push(elm)\n }, [])\n\n function resetDataPartInputAttr() {\n inputRef.current?.removeAttribute('data-clicked')\n }\n\n function resetDataPartStarAttr() {\n starRefList.current.forEach(star => star.removeAttribute('data-hovered'))\n }\n\n return (\n <div\n className=\"relative inline-flex\"\n ref={ref}\n data-spark-component=\"rating\"\n {...rest}\n onMouseLeave={resetDataPartStarAttr}\n >\n <input\n name={name}\n id={id}\n aria-label={ariaLabel}\n ref={inputRef}\n data-part=\"input\"\n className=\"peer absolute inset-0 opacity-0\"\n type=\"range\"\n min=\"0\"\n max=\"5\"\n step={readOnly ? 0.5 : 1}\n disabled={disabled}\n readOnly={readOnly}\n value={getNearestHalfDecimal(value ?? 0)}\n onChange={event => isInteractive && onInputChange(event)}\n onBlur={resetDataPartInputAttr}\n />\n <div\n className={cx(\n size === 'lg' ? 'gap-x-md' : 'gap-x-sm',\n 'flex',\n 'peer-focus-visible:u-outline peer-[[data-part=input][data-clicked]]:shadow-none'\n )}\n >\n {Array.from({ length: 5 }).map((_, index) => (\n <RatingStar\n disabled={disabled}\n readOnly={readOnly}\n size={size}\n onClick={() => isInteractive && onStarClick(index)}\n onMouseEnter={event => isInteractive && onStarMouseEnter(event)}\n ref={handleStarRef}\n key={index}\n value={getStarValue({ index, value })}\n />\n ))}\n </div>\n </div>\n )\n}\n"],"names":["emptyRemainingStarsOnHoverClass","cx","ratingStarStyles","cva","ratingStarIconStyles","RatingStar","value","size","disabled","readOnly","onClick","onMouseEnter","forwardedRef","jsxs","jsx","Icon","StarFill","StarOutline","getNearestHalfDecimal","num","getStarValue","index","starPosition","formattedValue","splitAt","arr","prev","next","Rating","defaultValue","propValue","onValueChange","name","id","ariaLabel","ref","rest","inputRef","useRef","starRefList","setRatingValue","useCombinedState","valueRef","isInteractive","onStarClick","onInputChange","event","onStarMouseEnter","currentTarget","currentStarIndex","star","previousStars","followingStars","handleStarRef","useCallback","elm","resetDataPartInputAttr","resetDataPartStarAttr","_"],"mappings":";;;;;;;AAEA,MAAMA,IAAkCC,EAAG,2BAA2B,GAEhEC,IAAmBC;AAAA,EACvB,CAAC,QAAQ,iBAAiB,SAAS,YAAY,4BAA4B;AAAA,EAC3E;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ts","../../src/rating/Rating.tsx"],"sourcesContent":["import { cva, cx, VariantProps } from 'class-variance-authority'\n\nconst emptyRemainingStarsOnHoverClass = cx('[&_>_div]:peer-hover:w-0!')\n\nconst ratingStarStyles = cva(['peer after:inset-0 group relative after:block after:absolute'], {\n variants: {\n disabled: {\n true: 'opacity-dim-3',\n false: '',\n },\n readOnly: {\n true: '',\n false: '',\n },\n gap: {\n sm: ['after:w-[calc(100%+(var(--spacing-sm)))]', 'last-of-type:after:content-none'],\n md: ['after:w-[calc(100%+(var(--spacing-md)))]', 'last-of-type:after:content-none'],\n },\n },\n compoundVariants: [\n {\n readOnly: false,\n disabled: false,\n className: cx(\n emptyRemainingStarsOnHoverClass,\n 'cursor-pointer transition-all duration-200 scale-100',\n /* mouseOver / focusIn => scale 150 */\n 'hover:scale-150 focus-visible:scale-150',\n /* mouseOut / focusOut / selection (click) => no scale; mouseMove clears selection => scale again */\n '[&[data-suppress-scale]]:hover:scale-100 [&[data-suppress-scale]]:focus-visible:scale-100'\n ),\n },\n ],\n defaultVariants: {\n disabled: false,\n readOnly: false,\n gap: 'sm',\n },\n})\n\nconst ratingStarIconStyles = cva('', {\n variants: {\n size: {\n sm: 'text-caption-link',\n md: 'text-body-1',\n lg: 'text-display-1',\n },\n design: {\n filled: [\n 'text-main-variant',\n 'group-[[data-part=star][data-hovered]]:text-main-variant-hovered',\n ],\n outlined: ['text-on-surface/dim-3'],\n },\n },\n})\n\ntype RatingStarstylesProps = Omit<VariantProps<typeof ratingStarStyles>, 'gap'>\ntype RatingStarIconStylesProps = Omit<VariantProps<typeof ratingStarIconStyles>, 'design'>\n\nexport { ratingStarStyles, ratingStarIconStyles }\nexport type { RatingStarstylesProps, RatingStarIconStylesProps }\n","import { StarFill } from '@spark-ui/icons/StarFill'\nimport { StarOutline } from '@spark-ui/icons/StarOutline'\nimport { cx } from 'class-variance-authority'\nimport { type KeyboardEvent, type MouseEvent, type PropsWithChildren, Ref, useState } from 'react'\n\nimport { Icon } from '../icon'\nimport {\n ratingStarIconStyles,\n type RatingStarIconStylesProps,\n ratingStarStyles,\n type RatingStarstylesProps,\n} from './RatingStar.styles'\nimport type { StarValue } from './types'\n\nexport interface RatingStarProps\n extends PropsWithChildren<RatingStarstylesProps>,\n RatingStarIconStylesProps {\n value: StarValue\n /** Whether this radio option is selected (for radiogroup pattern). */\n checked?: boolean\n /** Accessible name for the radio (e.g. \"one star\", \"two stars\"). */\n ariaLabel?: string\n /** Accessible ids used to compose the radio name. */\n ariaLabelledBy?: string\n /** Tab index for roving tabindex (0 or -1). */\n tabIndex?: number\n onClick?: (event: MouseEvent<HTMLDivElement>) => void\n onKeyDown?: (event: KeyboardEvent<HTMLDivElement>) => void\n onMouseEnter?: (event: MouseEvent<HTMLDivElement>) => void\n ref?: Ref<HTMLDivElement>\n}\n\nexport const RatingStar = ({\n value,\n size,\n disabled,\n readOnly,\n checked = false,\n ariaLabel,\n ariaLabelledBy,\n tabIndex,\n onClick,\n onKeyDown,\n onMouseEnter,\n children,\n ref: forwardedRef,\n}: RatingStarProps) => {\n const isInteractive = !disabled && !readOnly\n const [justClicked, setJustClicked] = useState(false)\n\n const handleClick = (event: MouseEvent<HTMLDivElement>) => {\n onClick?.(event)\n if (isInteractive) setJustClicked(true)\n }\n\n const clearJustClicked = () => setJustClicked(false)\n\n return (\n <div\n ref={forwardedRef}\n role=\"radio\"\n aria-checked={checked}\n aria-label={ariaLabel}\n aria-labelledby={ariaLabelledBy}\n tabIndex={tabIndex}\n data-spark-component=\"rating-star\"\n data-part=\"star\"\n {...(isInteractive && justClicked && { 'data-suppress-scale': '' })}\n className={ratingStarStyles({\n gap: size === 'lg' ? 'md' : 'sm',\n disabled,\n readOnly,\n })}\n onClick={handleClick}\n onKeyDown={onKeyDown}\n onMouseEnter={onMouseEnter}\n onMouseLeave={clearJustClicked}\n onMouseMove={clearJustClicked}\n >\n <div\n className={cx(\n 'z-raised absolute overflow-hidden',\n 'group-[[data-part=star][data-hovered]]:overflow-visible'\n )}\n style={{ width: value * 100 + '%' }}\n >\n <Icon\n className={ratingStarIconStyles({\n size,\n design: 'filled',\n })}\n >\n <StarFill />\n </Icon>\n </div>\n\n <Icon className={ratingStarIconStyles({ size, design: 'outlined' })}>\n <StarOutline />\n </Icon>\n {children}\n </div>\n )\n}\n","import { type StarValue } from './types'\n\nfunction getStarValue({ value, index }: { value?: number; index: number }): StarValue {\n if (value === undefined) return 0\n\n const starPosition = index + 1\n\n return value >= starPosition ? 1 : 0\n}\n\nfunction splitAt<T>(arr: T[], index: number): [T[], T[]] {\n const prev = arr.slice(0, index)\n const next = arr.slice(index)\n\n return [prev, next]\n}\n\nexport { getStarValue, splitAt }\n","/* eslint-disable max-lines-per-function */\nimport { useCombinedState } from '@spark-ui/hooks/use-combined-state'\nimport { cx } from 'class-variance-authority'\nimport {\n type ComponentPropsWithRef,\n type KeyboardEvent,\n type MouseEvent,\n type PropsWithChildren,\n type RefObject,\n useCallback,\n useId,\n useRef,\n useState,\n} from 'react'\n\nimport { useFormFieldControl } from '../form-field'\nimport { RatingStar } from './RatingStar'\nimport type { RatingValue } from './types'\nimport { getStarValue, splitAt } from './utils'\n\nconst getRatingInteger = (value: number | undefined): RatingValue => {\n if (value === undefined || !Number.isInteger(value) || value < 1) {\n return 0\n }\n\n return Math.min(5, Math.max(1, value)) as RatingValue\n}\n\nfunction createStarKeyDownHandler(\n index: number,\n starRefList: RefObject<(HTMLDivElement | null)[]>,\n setRatingValue: (value: RatingValue) => void,\n isInteractive: boolean\n) {\n return (event: KeyboardEvent<HTMLDivElement>) => {\n if (!isInteractive) return\n\n switch (event.key) {\n case 'ArrowRight':\n case 'ArrowDown':\n event.preventDefault()\n const nextIndex = Math.min(4, index + 1)\n setRatingValue((nextIndex + 1) as RatingValue)\n starRefList.current[nextIndex]?.focus()\n break\n case 'ArrowLeft':\n case 'ArrowUp':\n event.preventDefault()\n const prevIndex = Math.max(0, index - 1)\n setRatingValue((prevIndex + 1) as RatingValue)\n starRefList.current[prevIndex]?.focus()\n break\n case ' ':\n event.preventDefault()\n setRatingValue((index + 1) as RatingValue)\n break\n default:\n break\n }\n }\n}\n\nfunction getStarTabIndex(index: number, ratingValue: RatingValue): number {\n if (ratingValue >= 1) {\n return ratingValue - 1 === index ? 0 : -1\n }\n\n return index === 0 ? 0 : -1\n}\n\nexport interface RatingProps extends PropsWithChildren<ComponentPropsWithRef<'div'>> {\n /**\n * Use the `defaultValue` prop to set the default value of the input, on a from 0 to 5.\n *\n * Use this when you want to use it in an uncontrolled manner\n */\n defaultValue?: RatingValue\n /**\n * The value is the number of the rating selected, on a scale from 0 to 5.\n *\n * Use this when you want to use it in a controlled manner,\n * in conjunction with the `onValueChange` prop\n */\n value?: RatingValue\n /**\n * Event handler called when the value changes.\n */\n onValueChange?: (value: RatingValue) => void\n /**\n * Sets the component as interactive or not.\n * @default undefined\n */\n readOnly?: boolean\n /**\n * When `true`, prevents the user from interacting.\n * @default false\n */\n disabled?: boolean\n /**\n * When true, indicates that the user must select a rating before form submission.\n * @default false\n */\n required?: boolean\n /**\n * Name of the underlying hidden input (for form submission).\n * @default undefined\n */\n name?: string\n /**\n * id of the underlying hidden input.\n * @default undefined\n */\n id?: string\n /**\n * aria-label of the radiogroup.\n * @default undefined\n */\n 'aria-label'?: string\n /**\n * Returns the aria-label applied to each radio star.\n * Defaults to `${aria-label} ${index + 1}`.\n */\n getStarLabel?: (index: number) => string\n}\n\nexport const Rating = ({\n defaultValue,\n value: propValue,\n onValueChange,\n disabled,\n readOnly,\n required: requiredProp,\n name,\n id,\n 'aria-label': ariaLabel,\n getStarLabel,\n ref,\n ...rest\n}: RatingProps) => {\n const {\n labelId,\n isInvalid,\n isRequired,\n description,\n name: formFieldName,\n disabled: formFieldDisabled,\n readOnly: formFieldReadOnly,\n } = useFormFieldControl()\n const starRefList = useRef<(HTMLDivElement | null)[]>([])\n const ratingId = useId()\n const [hoveredStarIndex, setHoveredStarIndex] = useState<number | null>(null)\n const [value, setRatingValue] = useCombinedState(propValue, defaultValue, onValueChange)\n const ratingValue = getRatingInteger(value ?? 0)\n const resolvedDisabled = disabled ?? formFieldDisabled\n const resolvedReadOnly = readOnly ?? formFieldReadOnly\n const required = requiredProp !== undefined ? requiredProp : isRequired\n const groupName = name ?? formFieldName\n const isInteractive = !(resolvedDisabled || resolvedReadOnly)\n const hasExplicitStarLabel = getStarLabel !== undefined || ariaLabel !== undefined\n const displayValue = hoveredStarIndex !== null ? hoveredStarIndex + 1 : ratingValue\n\n function onStarClick(index: number) {\n if (!isInteractive) return\n\n const newValue = (index + 1) as RatingValue\n setRatingValue(newValue)\n starRefList.current[index]?.focus()\n }\n\n const onStarKeyDown = useCallback(\n (index: number) => createStarKeyDownHandler(index, starRefList, setRatingValue, isInteractive),\n [isInteractive, setRatingValue]\n )\n\n function onStarMouseEnter({ currentTarget }: MouseEvent<HTMLDivElement>) {\n const currentStarIndex = starRefList.current.findIndex(star => star === currentTarget)\n setHoveredStarIndex(currentStarIndex >= 0 ? currentStarIndex : null)\n const [previousStars, followingStars] = splitAt(starRefList.current, currentStarIndex + 1)\n previousStars.forEach(star => star?.setAttribute('data-hovered', ''))\n followingStars.forEach(star => star?.removeAttribute('data-hovered'))\n }\n\n const handleStarRef = useCallback(\n (index: number) => (elm: HTMLDivElement | null) => {\n starRefList.current[index] = elm\n },\n []\n )\n\n function resetDataPartStarAttr() {\n setHoveredStarIndex(null)\n starRefList.current.forEach(star => star?.removeAttribute('data-hovered'))\n }\n\n return (\n <div\n ref={ref}\n id={id}\n role=\"radiogroup\"\n aria-label={ariaLabel}\n aria-labelledby={labelId}\n aria-invalid={isInvalid}\n aria-required={required}\n aria-describedby={description}\n className=\"relative inline-flex\"\n data-spark-component=\"rating\"\n {...rest}\n onMouseLeave={resetDataPartStarAttr}\n >\n {groupName !== undefined && (\n <input type=\"hidden\" name={groupName} value={ratingValue} aria-hidden data-part=\"input\" />\n )}\n <div className={cx('gap-x-md', 'flex')}>\n {Array.from({ length: 5 }).map((_, index) => (\n <RatingStar\n ref={handleStarRef(index)}\n key={index}\n disabled={resolvedDisabled}\n readOnly={resolvedReadOnly}\n size=\"lg\"\n value={getStarValue({ index, value: displayValue })}\n checked={ratingValue === index + 1}\n ariaLabel={\n hasExplicitStarLabel\n ? (getStarLabel?.(index) ?? `${ariaLabel} ${index + 1}`)\n : undefined\n }\n ariaLabelledBy={\n !hasExplicitStarLabel && labelId\n ? `${labelId} ${ratingId}-star-${index + 1}`\n : undefined\n }\n tabIndex={isInteractive ? getStarTabIndex(index, ratingValue) : -1}\n onClick={() => onStarClick(index)}\n onKeyDown={onStarKeyDown(index)}\n onMouseEnter={event => isInteractive && onStarMouseEnter(event)}\n >\n {!hasExplicitStarLabel && (\n <span id={`${ratingId}-star-${index + 1}`} className=\"sr-only\">\n {index + 1}\n </span>\n )}\n </RatingStar>\n ))}\n </div>\n </div>\n )\n}\n"],"names":["emptyRemainingStarsOnHoverClass","cx","ratingStarStyles","cva","ratingStarIconStyles","RatingStar","value","size","disabled","readOnly","checked","ariaLabel","ariaLabelledBy","tabIndex","onClick","onKeyDown","onMouseEnter","children","forwardedRef","isInteractive","justClicked","setJustClicked","useState","handleClick","event","clearJustClicked","jsxs","jsx","Icon","StarFill","StarOutline","getStarValue","index","starPosition","splitAt","arr","prev","next","getRatingInteger","createStarKeyDownHandler","starRefList","setRatingValue","nextIndex","prevIndex","getStarTabIndex","ratingValue","Rating","defaultValue","propValue","onValueChange","requiredProp","name","id","getStarLabel","ref","rest","labelId","isInvalid","isRequired","description","formFieldName","formFieldDisabled","formFieldReadOnly","useFormFieldControl","useRef","ratingId","useId","hoveredStarIndex","setHoveredStarIndex","useCombinedState","resolvedDisabled","resolvedReadOnly","required","groupName","hasExplicitStarLabel","displayValue","onStarClick","newValue","onStarKeyDown","useCallback","onStarMouseEnter","currentTarget","currentStarIndex","star","previousStars","followingStars","handleStarRef","elm","resetDataPartStarAttr","_"],"mappings":";;;;;;;;AAEA,MAAMA,KAAkCC,EAAG,2BAA2B,GAEhEC,KAAmB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,IAAerB,CAAK,KAAK,GAAGrB,CAAS,IAAIqB,IAAQ,CAAC,KACnD;AAAA,YAEN,gBACE,CAAC0C,KAAwBlB,IACrB,GAAGA,CAAO,IAAIS,CAAQ,SAASjC,IAAQ,CAAC,KACxC;AAAA,YAEN,UAAUb,IAAgByB,GAAgBZ,GAAOa,CAAW,IAAI;AAAA,YAChE,SAAS,MAAM+B,EAAY5C,CAAK;AAAA,YAChC,WAAW8C,EAAc9C,CAAK;AAAA,YAC9B,cAAc,CAAAR,MAASL,KAAiB6D,EAAiBxD,CAAK;AAAA,YAE7D,UAAA,CAACkD,KACA,gBAAA/C,EAAC,QAAA,EAAK,IAAI,GAAGsC,CAAQ,SAASjC,IAAQ,CAAC,IAAI,WAAU,WAClD,cAAQ,EAAA,CACX;AAAA,UAAA;AAAA,UAxBGA;AAAA,QAAA,CA2BR,EAAA,CACH;AAAA,MAAA;AAAA,IAAA;AAAA,EAAA;AAGN;"}
|
package/dist/rating/types.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export type
|
|
1
|
+
export type RatingValue = 0 | 1 | 2 | 3 | 4 | 5;
|
|
2
|
+
export type StarValue = 0 | 1;
|
package/dist/rating/utils.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { StarValue } from './types';
|
|
2
|
-
declare function getNearestHalfDecimal(num: number): number;
|
|
3
2
|
declare function getStarValue({ value, index }: {
|
|
4
3
|
value?: number;
|
|
5
4
|
index: number;
|
|
6
5
|
}): StarValue;
|
|
7
6
|
declare function splitAt<T>(arr: T[], index: number): [T[], T[]];
|
|
8
|
-
export {
|
|
7
|
+
export { getStarValue, splitAt };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ComponentPropsWithRef, PropsWithChildren } from 'react';
|
|
2
|
+
import { RatingDisplayStarProps } from './RatingDisplayStar';
|
|
3
|
+
export interface RatingDisplayProps extends PropsWithChildren<ComponentPropsWithRef<'div'>> {
|
|
4
|
+
/**
|
|
5
|
+
* The rating value to display, on a scale from 0 to 5.
|
|
6
|
+
*/
|
|
7
|
+
value?: number;
|
|
8
|
+
/**
|
|
9
|
+
* Sets the size of the stars.
|
|
10
|
+
* @default 'md'
|
|
11
|
+
*/
|
|
12
|
+
size?: RatingDisplayStarProps['size'];
|
|
13
|
+
/**
|
|
14
|
+
* Optional count value available to `RatingDisplay.Count`.
|
|
15
|
+
*/
|
|
16
|
+
count?: number;
|
|
17
|
+
/**
|
|
18
|
+
* Accessible description of the rating content.
|
|
19
|
+
*/
|
|
20
|
+
'aria-label': string;
|
|
21
|
+
}
|
|
22
|
+
export type RatingDisplayRootProps = RatingDisplayProps;
|
|
23
|
+
export declare const RatingDisplay: {
|
|
24
|
+
({ value, size, count, ref, children, ...rest }: RatingDisplayProps): import("react/jsx-runtime").JSX.Element;
|
|
25
|
+
displayName: string;
|
|
26
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { PropsWithChildren } from 'react';
|
|
2
|
+
import { RatingDisplayStarProps } from './RatingDisplayStar';
|
|
3
|
+
interface RatingDisplayContextValue {
|
|
4
|
+
value: number;
|
|
5
|
+
size: RatingDisplayStarProps['size'];
|
|
6
|
+
count?: number;
|
|
7
|
+
}
|
|
8
|
+
interface RatingDisplayProviderProps extends PropsWithChildren<RatingDisplayContextValue> {
|
|
9
|
+
}
|
|
10
|
+
export declare const RatingDisplayProvider: ({ value, size, count, children, }: RatingDisplayProviderProps) => import("react/jsx-runtime").JSX.Element;
|
|
11
|
+
export declare const useRatingDisplay: () => RatingDisplayContextValue;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ComponentPropsWithRef, ReactNode } from 'react';
|
|
2
|
+
export interface RatingDisplayCountProps extends Omit<ComponentPropsWithRef<'span'>, 'children'> {
|
|
3
|
+
/**
|
|
4
|
+
* Custom count content.
|
|
5
|
+
* Pass a render function to receive the count value and return the content to render.
|
|
6
|
+
*/
|
|
7
|
+
children?: ReactNode | ((count: number) => ReactNode);
|
|
8
|
+
}
|
|
9
|
+
export declare const RatingDisplayCount: {
|
|
10
|
+
({ className, children, ...rest }: RatingDisplayCountProps): import("react/jsx-runtime").JSX.Element | null;
|
|
11
|
+
displayName: string;
|
|
12
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { VariantProps } from 'class-variance-authority';
|
|
2
|
+
import { StarValue } from './types';
|
|
3
|
+
declare const ratingDisplayStarStyles: (props?: ({
|
|
4
|
+
gap?: "sm" | "md" | null | undefined;
|
|
5
|
+
} & import('class-variance-authority/types').ClassProp) | undefined) => string;
|
|
6
|
+
declare const ratingDisplayStarIconStyles: (props?: ({
|
|
7
|
+
size?: "sm" | "md" | "lg" | null | undefined;
|
|
8
|
+
design?: "filled" | "outlined" | null | undefined;
|
|
9
|
+
} & import('class-variance-authority/types').ClassProp) | undefined) => string;
|
|
10
|
+
type RatingDisplayStarstylesProps = Omit<VariantProps<typeof ratingDisplayStarStyles>, never>;
|
|
11
|
+
type RatingDisplayStarIconStylesProps = Omit<VariantProps<typeof ratingDisplayStarIconStyles>, 'design'>;
|
|
12
|
+
export interface RatingDisplayStarProps extends RatingDisplayStarstylesProps, RatingDisplayStarIconStylesProps {
|
|
13
|
+
value: StarValue;
|
|
14
|
+
}
|
|
15
|
+
export declare const RatingDisplayStar: ({ value, size }: RatingDisplayStarProps) => import("react/jsx-runtime").JSX.Element;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { RatingDisplayStarProps } from './RatingDisplayStar';
|
|
2
|
+
import { StarValue } from './types';
|
|
3
|
+
export interface RatingDisplayStarsProps {
|
|
4
|
+
size?: RatingDisplayStarProps['size'];
|
|
5
|
+
/**
|
|
6
|
+
* Sets the rendering mode for stars.
|
|
7
|
+
* @default 'default'
|
|
8
|
+
*/
|
|
9
|
+
variant?: 'default' | 'single-star';
|
|
10
|
+
/**
|
|
11
|
+
* Custom fill algorithm for each star.
|
|
12
|
+
* By default, stars are rounded to the nearest 0.5.
|
|
13
|
+
*/
|
|
14
|
+
getFillMode?: ({ value, index }: {
|
|
15
|
+
value?: number;
|
|
16
|
+
index: number;
|
|
17
|
+
}) => StarValue;
|
|
18
|
+
}
|
|
19
|
+
export declare const RatingDisplayStars: {
|
|
20
|
+
({ size, variant, getFillMode, }: RatingDisplayStarsProps): import("react/jsx-runtime").JSX.Element;
|
|
21
|
+
displayName: string;
|
|
22
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ComponentPropsWithRef, ReactNode } from 'react';
|
|
2
|
+
export interface RatingDisplayValueProps extends Omit<ComponentPropsWithRef<'span'>, 'children'> {
|
|
3
|
+
/**
|
|
4
|
+
* Custom value content.
|
|
5
|
+
* Pass a render function to receive the formatted value (first arg) and raw value (second arg),
|
|
6
|
+
* then return the content to render.
|
|
7
|
+
*/
|
|
8
|
+
children?: ReactNode | ((formattedValue: string, value: number) => ReactNode);
|
|
9
|
+
}
|
|
10
|
+
export declare const RatingDisplayValue: {
|
|
11
|
+
({ className, children, ...rest }: RatingDisplayValueProps): import("react/jsx-runtime").JSX.Element;
|
|
12
|
+
displayName: string;
|
|
13
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { RatingDisplay as Root } from './RatingDisplay';
|
|
2
|
+
import { RatingDisplayCount as Count } from './RatingDisplayCount';
|
|
3
|
+
import { RatingDisplayStars as Stars } from './RatingDisplayStars';
|
|
4
|
+
import { RatingDisplayValue as Value } from './RatingDisplayValue';
|
|
5
|
+
export declare const RatingDisplay: typeof Root & {
|
|
6
|
+
Stars: typeof Stars;
|
|
7
|
+
Value: typeof Value;
|
|
8
|
+
Count: typeof Count;
|
|
9
|
+
};
|
|
10
|
+
export { type RatingDisplayProps, type RatingDisplayRootProps } from './RatingDisplay';
|
|
11
|
+
export { type RatingDisplayStarsProps } from './RatingDisplayStars';
|
|
12
|
+
export { type RatingDisplayValueProps } from './RatingDisplayValue';
|
|
13
|
+
export { type RatingDisplayCountProps } from './RatingDisplayCount';
|
|
14
|
+
export type { StarValue } from './types';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { RatingDisplay as Root } from './RatingDisplay';
|
|
2
|
+
import { RatingDisplayCount as Count } from './RatingDisplayCount';
|
|
3
|
+
import { RatingDisplayStars as Stars } from './RatingDisplayStars';
|
|
4
|
+
import { RatingDisplayValue as Value } from './RatingDisplayValue';
|
|
5
|
+
export declare const RatingDisplay: typeof Root & {
|
|
6
|
+
Stars: typeof Stars;
|
|
7
|
+
Value: typeof Value;
|
|
8
|
+
Count: typeof Count;
|
|
9
|
+
};
|
|
10
|
+
export { type RatingDisplayProps, type RatingDisplayRootProps } from './RatingDisplay';
|
|
11
|
+
export { type RatingDisplayStarsProps } from './RatingDisplayStars';
|
|
12
|
+
export { type RatingDisplayValueProps } from './RatingDisplayValue';
|
|
13
|
+
export { type RatingDisplayCountProps } from './RatingDisplayCount';
|
|
14
|
+
export type { StarValue } from './types';
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("react/jsx-runtime"),x=require("react"),o=require("class-variance-authority"),N=require("@spark-ui/icons/StarFill"),V=require("@spark-ui/icons/StarOutline"),y=require("../Icon-CF0W0LKr.js"),D=x.createContext(null),j=({value:t,size:a,count:s,children:e})=>n.jsx(D.Provider,{value:{value:t,size:a,count:s},children:e}),d=()=>{const t=x.useContext(D);if(!t)throw new Error("RatingDisplay compound components must be used within RatingDisplay.");return t},v=({value:t=0,size:a="md",count:s,ref:e,children:r,...i})=>{const l=t??0;return n.jsx(j,{value:l,size:a,count:s,children:n.jsx("div",{ref:e,className:"gap-x-sm relative inline-flex items-center","data-spark-component":"rating-display",...i,children:r})})};v.displayName="RatingDisplay";const b=o.cva("text-on-surface/dim-1",{variants:{size:{sm:"text-caption",md:"text-body-2",lg:"text-display-3"}},defaultVariants:{size:"md"}}),p=({className:t,children:a,...s})=>{const{count:e,size:r}=d();if(e===void 0)return null;const i=typeof a=="function"?a(e):a??e;return n.jsxs("span",{className:b({size:r??"md",className:t}),...s,children:["(",i,")"]})};p.displayName="RatingDisplay.Count";const h=o.cva(["relative block after:absolute after:block after:inset-0"],{variants:{gap:{sm:["after:w-[calc(100%+(var(--spacing-sm)))]","last-of-type:after:content-none"],md:["after:w-[calc(100%+(var(--spacing-md)))]","last-of-type:after:content-none"]}},defaultVariants:{gap:"sm"}}),f=o.cva("",{variants:{size:{sm:"text-caption-link",md:"text-body-1",lg:"text-display-3"},design:{filled:["text-main-variant"],outlined:["text-on-surface/dim-3"]}}}),z=({value:t,size:a})=>n.jsxs("div",{"data-spark-component":"rating-display-star","data-part":"star",className:h({gap:a==="lg"?"md":"sm"}),children:[n.jsx("div",{className:o.cx("z-raised absolute overflow-hidden"),style:{width:t*100+"%"},children:n.jsx(y.Icon,{className:f({size:a,design:"filled"}),children:n.jsx(N.StarFill,{})})}),n.jsx(y.Icon,{className:f({size:a,design:"outlined"}),children:n.jsx(V.StarOutline,{})})]});function C(t){return Math.round(t/.5)*.5}function w(t){const a=Intl.DateTimeFormat().resolvedOptions().locale;return new Intl.NumberFormat(a,{minimumFractionDigits:0,maximumFractionDigits:1}).format(t)}function k({value:t,index:a}){if(t===void 0)return 0;const s=a+1,e=C(t);return Math.ceil(e)<s?0:e>=s?1:.5}function q(t){return t===void 0||t<1?0:t<4?.5:1}const m=({size:t,variant:a="default",getFillMode:s})=>{const{value:e,size:r}=d(),i=t??r,l=c=>s?s({index:c,value:e}):a==="single-star"?q(e):k({index:c,value:e}),S=a==="single-star"?[l(0)]:Array.from({length:5},(c,u)=>l(u));return n.jsx("div",{"data-spark-component":"rating-display-stars",className:o.cx(i==="lg"?"gap-x-md":"gap-x-sm","flex"),children:S.map((c,u)=>n.jsx(z,{size:i,value:c},u))})};m.displayName="RatingDisplay.Stars";const I=o.cva("text-on-surface font-bold",{variants:{size:{sm:"text-caption",md:"text-body-2",lg:"text-display-3"}},defaultVariants:{size:"md"}}),g=({className:t,children:a,...s})=>{const{value:e,size:r}=d(),i=w(e),l=typeof a=="function"?a(i,e):a??i;return n.jsx("span",{"data-spark-component":"rating-display-value",className:I({size:r??"md",className:t}),...s,children:l})};g.displayName="RatingDisplay.Value";const R=Object.assign(v,{Stars:m,Value:g,Count:p});R.displayName="RatingDisplay";m.displayName="RatingDisplay.Stars";g.displayName="RatingDisplay.Value";p.displayName="RatingDisplay.Count";exports.RatingDisplay=R;
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/rating-display/RatingDisplayContext.tsx","../../src/rating-display/RatingDisplay.tsx","../../src/rating-display/RatingDisplayCount.tsx","../../src/rating-display/RatingDisplayStar.tsx","../../src/rating-display/utils.ts","../../src/rating-display/RatingDisplayStars.tsx","../../src/rating-display/RatingDisplayValue.tsx","../../src/rating-display/index.ts"],"sourcesContent":["import { createContext, type PropsWithChildren, useContext } from 'react'\n\nimport type { RatingDisplayStarProps } from './RatingDisplayStar'\n\ninterface RatingDisplayContextValue {\n value: number\n size: RatingDisplayStarProps['size']\n count?: number\n}\n\nconst RatingDisplayContext = createContext<RatingDisplayContextValue | null>(null)\n\ninterface RatingDisplayProviderProps extends PropsWithChildren<RatingDisplayContextValue> {}\n\nexport const RatingDisplayProvider = ({\n value,\n size,\n count,\n children,\n}: RatingDisplayProviderProps) => {\n return (\n <RatingDisplayContext.Provider value={{ value, size, count }}>\n {children}\n </RatingDisplayContext.Provider>\n )\n}\n\nexport const useRatingDisplay = () => {\n const context = useContext(RatingDisplayContext)\n if (!context) {\n throw new Error('RatingDisplay compound components must be used within RatingDisplay.')\n }\n\n return context\n}\n","import { type ComponentPropsWithRef, type PropsWithChildren } from 'react'\n\nimport { RatingDisplayProvider } from './RatingDisplayContext'\nimport type { RatingDisplayStarProps } from './RatingDisplayStar'\n\nexport interface RatingDisplayProps extends PropsWithChildren<ComponentPropsWithRef<'div'>> {\n /**\n * The rating value to display, on a scale from 0 to 5.\n */\n value?: number\n /**\n * Sets the size of the stars.\n * @default 'md'\n */\n size?: RatingDisplayStarProps['size']\n /**\n * Optional count value available to `RatingDisplay.Count`.\n */\n count?: number\n /**\n * Accessible description of the rating content.\n */\n 'aria-label': string\n}\n\nexport type RatingDisplayRootProps = RatingDisplayProps\n\nexport const RatingDisplay = ({\n value = 0,\n size = 'md',\n count,\n ref,\n children,\n ...rest\n}: RatingDisplayProps) => {\n const ratingValue = value ?? 0\n\n return (\n <RatingDisplayProvider value={ratingValue} size={size} count={count}>\n <div\n ref={ref}\n className=\"gap-x-sm relative inline-flex items-center\"\n data-spark-component=\"rating-display\"\n {...rest}\n >\n {children}\n </div>\n </RatingDisplayProvider>\n )\n}\n\nRatingDisplay.displayName = 'RatingDisplay'\n","import { cva } from 'class-variance-authority'\nimport { type ComponentPropsWithRef, type ReactNode } from 'react'\n\nimport { useRatingDisplay } from './RatingDisplayContext'\n\nexport interface RatingDisplayCountProps extends Omit<ComponentPropsWithRef<'span'>, 'children'> {\n /**\n * Custom count content.\n * Pass a render function to receive the count value and return the content to render.\n */\n children?: ReactNode | ((count: number) => ReactNode)\n}\n\nconst ratingDisplayCountStyles = cva('text-on-surface/dim-1', {\n variants: {\n size: {\n sm: 'text-caption',\n md: 'text-body-2',\n lg: 'text-display-3',\n },\n },\n defaultVariants: {\n size: 'md',\n },\n})\n\nexport const RatingDisplayCount = ({ className, children, ...rest }: RatingDisplayCountProps) => {\n const { count, size } = useRatingDisplay()\n if (count === undefined) return null\n const renderedCount = typeof children === 'function' ? children(count) : (children ?? count)\n\n return (\n <span className={ratingDisplayCountStyles({ size: size ?? 'md', className })} {...rest}>\n ({renderedCount})\n </span>\n )\n}\n\nRatingDisplayCount.displayName = 'RatingDisplay.Count'\n","import { StarFill } from '@spark-ui/icons/StarFill'\nimport { StarOutline } from '@spark-ui/icons/StarOutline'\nimport { cva, cx, type VariantProps } from 'class-variance-authority'\n\nimport { Icon } from '../icon'\nimport type { StarValue } from './types'\n\nconst ratingDisplayStarStyles = cva(['relative block after:absolute after:block after:inset-0'], {\n variants: {\n gap: {\n sm: ['after:w-[calc(100%+(var(--spacing-sm)))]', 'last-of-type:after:content-none'],\n md: ['after:w-[calc(100%+(var(--spacing-md)))]', 'last-of-type:after:content-none'],\n },\n },\n defaultVariants: {\n gap: 'sm',\n },\n})\n\nconst ratingDisplayStarIconStyles = cva('', {\n variants: {\n size: {\n sm: 'text-caption-link',\n md: 'text-body-1',\n lg: 'text-display-3',\n },\n design: {\n filled: ['text-main-variant'],\n outlined: ['text-on-surface/dim-3'],\n },\n },\n})\n\ntype RatingDisplayStarstylesProps = Omit<VariantProps<typeof ratingDisplayStarStyles>, never>\ntype RatingDisplayStarIconStylesProps = Omit<\n VariantProps<typeof ratingDisplayStarIconStyles>,\n 'design'\n>\n\nexport interface RatingDisplayStarProps\n extends RatingDisplayStarstylesProps,\n RatingDisplayStarIconStylesProps {\n value: StarValue\n}\n\nexport const RatingDisplayStar = ({ value, size }: RatingDisplayStarProps) => {\n return (\n <div\n data-spark-component=\"rating-display-star\"\n data-part=\"star\"\n className={ratingDisplayStarStyles({\n gap: size === 'lg' ? 'md' : 'sm',\n })}\n >\n <div className={cx('z-raised absolute overflow-hidden')} style={{ width: value * 100 + '%' }}>\n <Icon\n className={ratingDisplayStarIconStyles({\n size,\n design: 'filled',\n })}\n >\n <StarFill />\n </Icon>\n </div>\n\n <Icon className={ratingDisplayStarIconStyles({ size, design: 'outlined' })}>\n <StarOutline />\n </Icon>\n </div>\n )\n}\n","import { type StarValue } from './types'\n\nfunction getNearestHalfDecimal(num: number): number {\n return Math.round(num / 0.5) * 0.5\n}\n\nfunction formatRatingValue(value: number): string {\n const locale = Intl.DateTimeFormat().resolvedOptions().locale\n\n return new Intl.NumberFormat(locale, {\n minimumFractionDigits: 0,\n maximumFractionDigits: 1,\n }).format(value)\n}\n\nfunction getStarValue({ value, index }: { value?: number; index: number }): StarValue {\n if (value === undefined) return 0\n\n const starPosition = index + 1\n const formattedValue = getNearestHalfDecimal(value)\n\n if (Math.ceil(formattedValue) < starPosition) return 0\n\n return formattedValue >= starPosition ? 1 : 0.5\n}\n\nfunction getSingleStarValue(value?: number): StarValue {\n if (value === undefined) return 0\n if (value < 1) return 0\n if (value < 4) return 0.5\n return 1\n}\n\nfunction splitAt<T>(arr: T[], index: number): [T[], T[]] {\n const prev = arr.slice(0, index)\n const next = arr.slice(index)\n\n return [prev, next]\n}\n\nexport { formatRatingValue, getNearestHalfDecimal, getSingleStarValue, getStarValue, splitAt }\n","import { cx } from 'class-variance-authority'\n\nimport { useRatingDisplay } from './RatingDisplayContext'\nimport { RatingDisplayStar, type RatingDisplayStarProps } from './RatingDisplayStar'\nimport type { StarValue } from './types'\nimport { getSingleStarValue, getStarValue } from './utils'\n\nexport interface RatingDisplayStarsProps {\n size?: RatingDisplayStarProps['size']\n /**\n * Sets the rendering mode for stars.\n * @default 'default'\n */\n variant?: 'default' | 'single-star'\n /**\n * Custom fill algorithm for each star.\n * By default, stars are rounded to the nearest 0.5.\n */\n getFillMode?: ({ value, index }: { value?: number; index: number }) => StarValue\n}\n\nexport const RatingDisplayStars = ({\n size,\n variant = 'default',\n getFillMode,\n}: RatingDisplayStarsProps) => {\n const { value, size: contextSize } = useRatingDisplay()\n const resolvedSize = size ?? contextSize\n const getDisplayValue = (index: number) => {\n if (getFillMode) {\n return getFillMode({ index, value })\n }\n\n return variant === 'single-star' ? getSingleStarValue(value) : getStarValue({ index, value })\n }\n\n const stars =\n variant === 'single-star'\n ? [getDisplayValue(0)]\n : Array.from({ length: 5 }, (_, index) => getDisplayValue(index))\n\n return (\n <div\n data-spark-component=\"rating-display-stars\"\n className={cx(resolvedSize === 'lg' ? 'gap-x-md' : 'gap-x-sm', 'flex')}\n >\n {stars.map((starValue, index) => (\n <RatingDisplayStar key={index} size={resolvedSize} value={starValue} />\n ))}\n </div>\n )\n}\n\nRatingDisplayStars.displayName = 'RatingDisplay.Stars'\n","import { cva } from 'class-variance-authority'\nimport { type ComponentPropsWithRef, type ReactNode } from 'react'\n\nimport { useRatingDisplay } from './RatingDisplayContext'\nimport { formatRatingValue } from './utils'\n\nexport interface RatingDisplayValueProps extends Omit<ComponentPropsWithRef<'span'>, 'children'> {\n /**\n * Custom value content.\n * Pass a render function to receive the formatted value (first arg) and raw value (second arg),\n * then return the content to render.\n */\n children?: ReactNode | ((formattedValue: string, value: number) => ReactNode)\n}\n\nconst ratingDisplayValueStyles = cva('text-on-surface font-bold', {\n variants: {\n size: {\n sm: 'text-caption',\n md: 'text-body-2',\n lg: 'text-display-3',\n },\n },\n defaultVariants: {\n size: 'md',\n },\n})\n\nexport const RatingDisplayValue = ({ className, children, ...rest }: RatingDisplayValueProps) => {\n const { value, size } = useRatingDisplay()\n const formattedValue = formatRatingValue(value)\n const renderedValue =\n typeof children === 'function' ? children(formattedValue, value) : (children ?? formattedValue)\n\n return (\n <span\n data-spark-component=\"rating-display-value\"\n className={ratingDisplayValueStyles({ size: size ?? 'md', className })}\n {...rest}\n >\n {renderedValue}\n </span>\n )\n}\n\nRatingDisplayValue.displayName = 'RatingDisplay.Value'\n","import { RatingDisplay as Root } from './RatingDisplay'\nimport { RatingDisplayCount as Count } from './RatingDisplayCount'\nimport { RatingDisplayStars as Stars } from './RatingDisplayStars'\nimport { RatingDisplayValue as Value } from './RatingDisplayValue'\n\nexport const RatingDisplay: typeof Root & {\n Stars: typeof Stars\n Value: typeof Value\n Count: typeof Count\n} = Object.assign(Root, {\n Stars,\n Value,\n Count,\n})\n\nRatingDisplay.displayName = 'RatingDisplay'\nStars.displayName = 'RatingDisplay.Stars'\nValue.displayName = 'RatingDisplay.Value'\nCount.displayName = 'RatingDisplay.Count'\n\nexport { type RatingDisplayProps, type RatingDisplayRootProps } from './RatingDisplay'\nexport { type RatingDisplayStarsProps } from './RatingDisplayStars'\nexport { type RatingDisplayValueProps } from './RatingDisplayValue'\nexport { type RatingDisplayCountProps } from './RatingDisplayCount'\nexport type { StarValue } from './types'\n"],"names":["RatingDisplayContext","createContext","RatingDisplayProvider","value","size","count","children","jsx","useRatingDisplay","context","useContext","RatingDisplay","ref","rest","ratingValue","ratingDisplayCountStyles","cva","RatingDisplayCount","className","renderedCount","jsxs","ratingDisplayStarStyles","ratingDisplayStarIconStyles","RatingDisplayStar","cx","Icon","StarFill","StarOutline","getNearestHalfDecimal","num","formatRatingValue","locale","getStarValue","index","starPosition","formattedValue","getSingleStarValue","RatingDisplayStars","variant","getFillMode","contextSize","resolvedSize","getDisplayValue","stars","_","starValue","ratingDisplayValueStyles","RatingDisplayValue","renderedValue","Root","Stars","Value","Count"],"mappings":"8RAUMA,EAAuBC,EAAAA,cAAgD,IAAI,EAIpEC,EAAwB,CAAC,CACpC,MAAAC,EACA,KAAAC,EACA,MAAAC,EACA,SAAAC,CACF,IAEIC,MAACP,EAAqB,SAArB,CAA8B,MAAO,CAAE,MAAAG,EAAO,KAAAC,EAAM,MAAAC,GAClD,SAAAC,EACH,EAISE,EAAmB,IAAM,CACpC,MAAMC,EAAUC,EAAAA,WAAWV,CAAoB,EAC/C,GAAI,CAACS,EACH,MAAM,IAAI,MAAM,sEAAsE,EAGxF,OAAOA,CACT,ECPaE,EAAgB,CAAC,CAC5B,MAAAR,EAAQ,EACR,KAAAC,EAAO,KACP,MAAAC,EACA,IAAAO,EACA,SAAAN,EACA,GAAGO,CACL,IAA0B,CACxB,MAAMC,EAAcX,GAAS,EAE7B,OACEI,EAAAA,IAACL,EAAA,CAAsB,MAAOY,EAAa,KAAAV,EAAY,MAAAC,EACrD,SAAAE,EAAAA,IAAC,MAAA,CACC,IAAAK,EACA,UAAU,6CACV,uBAAqB,iBACpB,GAAGC,EAEH,SAAAP,CAAA,CAAA,EAEL,CAEJ,EAEAK,EAAc,YAAc,gBCtC5B,MAAMI,EAA2BC,EAAAA,IAAI,wBAAyB,CAC5D,SAAU,CACR,KAAM,CACJ,GAAI,eACJ,GAAI,cACJ,GAAI,gBAAA,C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CAAE,OAAQ,GAAK,CAACE,EAAGX,IAAUS,EAAgBT,CAAK,CAAC,EAEpE,OACE1B,EAAAA,IAAC,MAAA,CACC,uBAAqB,uBACrB,UAAWiB,EAAAA,GAAGiB,IAAiB,KAAO,WAAa,WAAY,MAAM,EAEpE,SAAAE,EAAM,IAAI,CAACE,EAAWZ,IACrB1B,EAAAA,IAACgB,EAAA,CAA8B,KAAMkB,EAAc,MAAOI,CAAA,EAAlCZ,CAA6C,CACtE,CAAA,CAAA,CAGP,EAEAI,EAAmB,YAAc,sBCtCjC,MAAMS,EAA2B9B,EAAAA,IAAI,4BAA6B,CAChE,SAAU,CACR,KAAM,CACJ,GAAI,eACJ,GAAI,cACJ,GAAI,gBAAA,CACN,EAEF,gBAAiB,CACf,KAAM,IAAA,CAEV,CAAC,EAEY+B,EAAqB,CAAC,CAAE,UAAA7B,EAAW,SAAAZ,EAAU,GAAGO,KAAoC,CAC/F,KAAM,CAAE,MAAAV,EAAO,KAAAC,CAAA,EAASI,EAAA,EAClB2B,EAAiBL,EAAkB3B,CAAK,EACxC6C,EACJ,OAAO1C,GAAa,WAAaA,EAAS6B,EAAgBhC,CAAK,EAAKG,GAAY6B,EAElF,OACE5B,EAAAA,IAAC,OAAA,CACC,uBAAqB,uBACrB,UAAWuC,EAAyB,CAAE,KAAM1C,GAAQ,KAAM,UAAAc,EAAW,EACpE,GAAGL,EAEH,SAAAmC,CAAA,CAAA,CAGP,EAEAD,EAAmB,YAAc,sBCxC1B,MAAMpC,EAIT,OAAO,OAAOsC,EAAM,CAAA,MACtBC,EAAA,MACAC,EAAA,MACAC,CACF,CAAC,EAEDzC,EAAc,YAAc,gBAC5BuC,EAAM,YAAc,sBACpBC,EAAM,YAAc,sBACpBC,EAAM,YAAc"}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { jsx as i, jsxs as D } from "react/jsx-runtime";
|
|
2
|
+
import { createContext as S, useContext as V } from "react";
|
|
3
|
+
import { cva as c, cx as x } from "class-variance-authority";
|
|
4
|
+
import { StarFill as z } from "@spark-ui/icons/StarFill";
|
|
5
|
+
import { StarOutline as b } from "@spark-ui/icons/StarOutline";
|
|
6
|
+
import { I as y } from "../Icon-Ck-dhfLd.mjs";
|
|
7
|
+
const v = S(null), h = ({
|
|
8
|
+
value: t,
|
|
9
|
+
size: a,
|
|
10
|
+
count: n,
|
|
11
|
+
children: e
|
|
12
|
+
}) => /* @__PURE__ */ i(v.Provider, { value: { value: t, size: a, count: n }, children: e }), p = () => {
|
|
13
|
+
const t = V(v);
|
|
14
|
+
if (!t)
|
|
15
|
+
throw new Error("RatingDisplay compound components must be used within RatingDisplay.");
|
|
16
|
+
return t;
|
|
17
|
+
}, R = ({
|
|
18
|
+
value: t = 0,
|
|
19
|
+
size: a = "md",
|
|
20
|
+
count: n,
|
|
21
|
+
ref: e,
|
|
22
|
+
children: r,
|
|
23
|
+
...s
|
|
24
|
+
}) => /* @__PURE__ */ i(h, { value: t ?? 0, size: a, count: n, children: /* @__PURE__ */ i(
|
|
25
|
+
"div",
|
|
26
|
+
{
|
|
27
|
+
ref: e,
|
|
28
|
+
className: "gap-x-sm relative inline-flex items-center",
|
|
29
|
+
"data-spark-component": "rating-display",
|
|
30
|
+
...s,
|
|
31
|
+
children: r
|
|
32
|
+
}
|
|
33
|
+
) });
|
|
34
|
+
R.displayName = "RatingDisplay";
|
|
35
|
+
const C = c("text-on-surface/dim-1", {
|
|
36
|
+
variants: {
|
|
37
|
+
size: {
|
|
38
|
+
sm: "text-caption",
|
|
39
|
+
md: "text-body-2",
|
|
40
|
+
lg: "text-display-3"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
defaultVariants: {
|
|
44
|
+
size: "md"
|
|
45
|
+
}
|
|
46
|
+
}), d = ({ className: t, children: a, ...n }) => {
|
|
47
|
+
const { count: e, size: r } = p();
|
|
48
|
+
if (e === void 0) return null;
|
|
49
|
+
const s = typeof a == "function" ? a(e) : a ?? e;
|
|
50
|
+
return /* @__PURE__ */ D("span", { className: C({ size: r ?? "md", className: t }), ...n, children: [
|
|
51
|
+
"(",
|
|
52
|
+
s,
|
|
53
|
+
")"
|
|
54
|
+
] });
|
|
55
|
+
};
|
|
56
|
+
d.displayName = "RatingDisplay.Count";
|
|
57
|
+
const w = c(["relative block after:absolute after:block after:inset-0"], {
|
|
58
|
+
variants: {
|
|
59
|
+
gap: {
|
|
60
|
+
sm: ["after:w-[calc(100%+(var(--spacing-sm)))]", "last-of-type:after:content-none"],
|
|
61
|
+
md: ["after:w-[calc(100%+(var(--spacing-md)))]", "last-of-type:after:content-none"]
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
defaultVariants: {
|
|
65
|
+
gap: "sm"
|
|
66
|
+
}
|
|
67
|
+
}), f = c("", {
|
|
68
|
+
variants: {
|
|
69
|
+
size: {
|
|
70
|
+
sm: "text-caption-link",
|
|
71
|
+
md: "text-body-1",
|
|
72
|
+
lg: "text-display-3"
|
|
73
|
+
},
|
|
74
|
+
design: {
|
|
75
|
+
filled: ["text-main-variant"],
|
|
76
|
+
outlined: ["text-on-surface/dim-3"]
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}), k = ({ value: t, size: a }) => /* @__PURE__ */ D(
|
|
80
|
+
"div",
|
|
81
|
+
{
|
|
82
|
+
"data-spark-component": "rating-display-star",
|
|
83
|
+
"data-part": "star",
|
|
84
|
+
className: w({
|
|
85
|
+
gap: a === "lg" ? "md" : "sm"
|
|
86
|
+
}),
|
|
87
|
+
children: [
|
|
88
|
+
/* @__PURE__ */ i("div", { className: x("z-raised absolute overflow-hidden"), style: { width: t * 100 + "%" }, children: /* @__PURE__ */ i(
|
|
89
|
+
y,
|
|
90
|
+
{
|
|
91
|
+
className: f({
|
|
92
|
+
size: a,
|
|
93
|
+
design: "filled"
|
|
94
|
+
}),
|
|
95
|
+
children: /* @__PURE__ */ i(z, {})
|
|
96
|
+
}
|
|
97
|
+
) }),
|
|
98
|
+
/* @__PURE__ */ i(y, { className: f({ size: a, design: "outlined" }), children: /* @__PURE__ */ i(b, {}) })
|
|
99
|
+
]
|
|
100
|
+
}
|
|
101
|
+
);
|
|
102
|
+
function I(t) {
|
|
103
|
+
return Math.round(t / 0.5) * 0.5;
|
|
104
|
+
}
|
|
105
|
+
function F(t) {
|
|
106
|
+
const a = Intl.DateTimeFormat().resolvedOptions().locale;
|
|
107
|
+
return new Intl.NumberFormat(a, {
|
|
108
|
+
minimumFractionDigits: 0,
|
|
109
|
+
maximumFractionDigits: 1
|
|
110
|
+
}).format(t);
|
|
111
|
+
}
|
|
112
|
+
function j({ value: t, index: a }) {
|
|
113
|
+
if (t === void 0) return 0;
|
|
114
|
+
const n = a + 1, e = I(t);
|
|
115
|
+
return Math.ceil(e) < n ? 0 : e >= n ? 1 : 0.5;
|
|
116
|
+
}
|
|
117
|
+
function O(t) {
|
|
118
|
+
return t === void 0 || t < 1 ? 0 : t < 4 ? 0.5 : 1;
|
|
119
|
+
}
|
|
120
|
+
const u = ({
|
|
121
|
+
size: t,
|
|
122
|
+
variant: a = "default",
|
|
123
|
+
getFillMode: n
|
|
124
|
+
}) => {
|
|
125
|
+
const { value: e, size: r } = p(), s = t ?? r, l = (o) => n ? n({ index: o, value: e }) : a === "single-star" ? O(e) : j({ index: o, value: e }), N = a === "single-star" ? [l(0)] : Array.from({ length: 5 }, (o, m) => l(m));
|
|
126
|
+
return /* @__PURE__ */ i(
|
|
127
|
+
"div",
|
|
128
|
+
{
|
|
129
|
+
"data-spark-component": "rating-display-stars",
|
|
130
|
+
className: x(s === "lg" ? "gap-x-md" : "gap-x-sm", "flex"),
|
|
131
|
+
children: N.map((o, m) => /* @__PURE__ */ i(k, { size: s, value: o }, m))
|
|
132
|
+
}
|
|
133
|
+
);
|
|
134
|
+
};
|
|
135
|
+
u.displayName = "RatingDisplay.Stars";
|
|
136
|
+
const P = c("text-on-surface font-bold", {
|
|
137
|
+
variants: {
|
|
138
|
+
size: {
|
|
139
|
+
sm: "text-caption",
|
|
140
|
+
md: "text-body-2",
|
|
141
|
+
lg: "text-display-3"
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
defaultVariants: {
|
|
145
|
+
size: "md"
|
|
146
|
+
}
|
|
147
|
+
}), g = ({ className: t, children: a, ...n }) => {
|
|
148
|
+
const { value: e, size: r } = p(), s = F(e), l = typeof a == "function" ? a(s, e) : a ?? s;
|
|
149
|
+
return /* @__PURE__ */ i(
|
|
150
|
+
"span",
|
|
151
|
+
{
|
|
152
|
+
"data-spark-component": "rating-display-value",
|
|
153
|
+
className: P({ size: r ?? "md", className: t }),
|
|
154
|
+
...n,
|
|
155
|
+
children: l
|
|
156
|
+
}
|
|
157
|
+
);
|
|
158
|
+
};
|
|
159
|
+
g.displayName = "RatingDisplay.Value";
|
|
160
|
+
const A = Object.assign(R, {
|
|
161
|
+
Stars: u,
|
|
162
|
+
Value: g,
|
|
163
|
+
Count: d
|
|
164
|
+
});
|
|
165
|
+
A.displayName = "RatingDisplay";
|
|
166
|
+
u.displayName = "RatingDisplay.Stars";
|
|
167
|
+
g.displayName = "RatingDisplay.Value";
|
|
168
|
+
d.displayName = "RatingDisplay.Count";
|
|
169
|
+
export {
|
|
170
|
+
A as RatingDisplay
|
|
171
|
+
};
|
|
172
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../../src/rating-display/RatingDisplayContext.tsx","../../src/rating-display/RatingDisplay.tsx","../../src/rating-display/RatingDisplayCount.tsx","../../src/rating-display/RatingDisplayStar.tsx","../../src/rating-display/utils.ts","../../src/rating-display/RatingDisplayStars.tsx","../../src/rating-display/RatingDisplayValue.tsx","../../src/rating-display/index.ts"],"sourcesContent":["import { createContext, type PropsWithChildren, useContext } from 'react'\n\nimport type { RatingDisplayStarProps } from './RatingDisplayStar'\n\ninterface RatingDisplayContextValue {\n value: number\n size: RatingDisplayStarProps['size']\n count?: number\n}\n\nconst RatingDisplayContext = createContext<RatingDisplayContextValue | null>(null)\n\ninterface RatingDisplayProviderProps extends PropsWithChildren<RatingDisplayContextValue> {}\n\nexport const RatingDisplayProvider = ({\n value,\n size,\n count,\n children,\n}: RatingDisplayProviderProps) => {\n return (\n <RatingDisplayContext.Provider value={{ value, size, count }}>\n {children}\n </RatingDisplayContext.Provider>\n )\n}\n\nexport const useRatingDisplay = () => {\n const context = useContext(RatingDisplayContext)\n if (!context) {\n throw new Error('RatingDisplay compound components must be used within RatingDisplay.')\n }\n\n return context\n}\n","import { type ComponentPropsWithRef, type PropsWithChildren } from 'react'\n\nimport { RatingDisplayProvider } from './RatingDisplayContext'\nimport type { RatingDisplayStarProps } from './RatingDisplayStar'\n\nexport interface RatingDisplayProps extends PropsWithChildren<ComponentPropsWithRef<'div'>> {\n /**\n * The rating value to display, on a scale from 0 to 5.\n */\n value?: number\n /**\n * Sets the size of the stars.\n * @default 'md'\n */\n size?: RatingDisplayStarProps['size']\n /**\n * Optional count value available to `RatingDisplay.Count`.\n */\n count?: number\n /**\n * Accessible description of the rating content.\n */\n 'aria-label': string\n}\n\nexport type RatingDisplayRootProps = RatingDisplayProps\n\nexport const RatingDisplay = ({\n value = 0,\n size = 'md',\n count,\n ref,\n children,\n ...rest\n}: RatingDisplayProps) => {\n const ratingValue = value ?? 0\n\n return (\n <RatingDisplayProvider value={ratingValue} size={size} count={count}>\n <div\n ref={ref}\n className=\"gap-x-sm relative inline-flex items-center\"\n data-spark-component=\"rating-display\"\n {...rest}\n >\n {children}\n </div>\n </RatingDisplayProvider>\n )\n}\n\nRatingDisplay.displayName = 'RatingDisplay'\n","import { cva } from 'class-variance-authority'\nimport { type ComponentPropsWithRef, type ReactNode } from 'react'\n\nimport { useRatingDisplay } from './RatingDisplayContext'\n\nexport interface RatingDisplayCountProps extends Omit<ComponentPropsWithRef<'span'>, 'children'> {\n /**\n * Custom count content.\n * Pass a render function to receive the count value and return the content to render.\n */\n children?: ReactNode | ((count: number) => ReactNode)\n}\n\nconst ratingDisplayCountStyles = cva('text-on-surface/dim-1', {\n variants: {\n size: {\n sm: 'text-caption',\n md: 'text-body-2',\n lg: 'text-display-3',\n },\n },\n defaultVariants: {\n size: 'md',\n },\n})\n\nexport const RatingDisplayCount = ({ className, children, ...rest }: RatingDisplayCountProps) => {\n const { count, size } = useRatingDisplay()\n if (count === undefined) return null\n const renderedCount = typeof children === 'function' ? children(count) : (children ?? count)\n\n return (\n <span className={ratingDisplayCountStyles({ size: size ?? 'md', className })} {...rest}>\n ({renderedCount})\n </span>\n )\n}\n\nRatingDisplayCount.displayName = 'RatingDisplay.Count'\n","import { StarFill } from '@spark-ui/icons/StarFill'\nimport { StarOutline } from '@spark-ui/icons/StarOutline'\nimport { cva, cx, type VariantProps } from 'class-variance-authority'\n\nimport { Icon } from '../icon'\nimport type { StarValue } from './types'\n\nconst ratingDisplayStarStyles = cva(['relative block after:absolute after:block after:inset-0'], {\n variants: {\n gap: {\n sm: ['after:w-[calc(100%+(var(--spacing-sm)))]', 'last-of-type:after:content-none'],\n md: ['after:w-[calc(100%+(var(--spacing-md)))]', 'last-of-type:after:content-none'],\n },\n },\n defaultVariants: {\n gap: 'sm',\n },\n})\n\nconst ratingDisplayStarIconStyles = cva('', {\n variants: {\n size: {\n sm: 'text-caption-link',\n md: 'text-body-1',\n lg: 'text-display-3',\n },\n design: {\n filled: ['text-main-variant'],\n outlined: ['text-on-surface/dim-3'],\n },\n },\n})\n\ntype RatingDisplayStarstylesProps = Omit<VariantProps<typeof ratingDisplayStarStyles>, never>\ntype RatingDisplayStarIconStylesProps = Omit<\n VariantProps<typeof ratingDisplayStarIconStyles>,\n 'design'\n>\n\nexport interface RatingDisplayStarProps\n extends RatingDisplayStarstylesProps,\n RatingDisplayStarIconStylesProps {\n value: StarValue\n}\n\nexport const RatingDisplayStar = ({ value, size }: RatingDisplayStarProps) => {\n return (\n <div\n data-spark-component=\"rating-display-star\"\n data-part=\"star\"\n className={ratingDisplayStarStyles({\n gap: size === 'lg' ? 'md' : 'sm',\n })}\n >\n <div className={cx('z-raised absolute overflow-hidden')} style={{ width: value * 100 + '%' }}>\n <Icon\n className={ratingDisplayStarIconStyles({\n size,\n design: 'filled',\n })}\n >\n <StarFill />\n </Icon>\n </div>\n\n <Icon className={ratingDisplayStarIconStyles({ size, design: 'outlined' })}>\n <StarOutline />\n </Icon>\n </div>\n )\n}\n","import { type StarValue } from './types'\n\nfunction getNearestHalfDecimal(num: number): number {\n return Math.round(num / 0.5) * 0.5\n}\n\nfunction formatRatingValue(value: number): string {\n const locale = Intl.DateTimeFormat().resolvedOptions().locale\n\n return new Intl.NumberFormat(locale, {\n minimumFractionDigits: 0,\n maximumFractionDigits: 1,\n }).format(value)\n}\n\nfunction getStarValue({ value, index }: { value?: number; index: number }): StarValue {\n if (value === undefined) return 0\n\n const starPosition = index + 1\n const formattedValue = getNearestHalfDecimal(value)\n\n if (Math.ceil(formattedValue) < starPosition) return 0\n\n return formattedValue >= starPosition ? 1 : 0.5\n}\n\nfunction getSingleStarValue(value?: number): StarValue {\n if (value === undefined) return 0\n if (value < 1) return 0\n if (value < 4) return 0.5\n return 1\n}\n\nfunction splitAt<T>(arr: T[], index: number): [T[], T[]] {\n const prev = arr.slice(0, index)\n const next = arr.slice(index)\n\n return [prev, next]\n}\n\nexport { formatRatingValue, getNearestHalfDecimal, getSingleStarValue, getStarValue, splitAt }\n","import { cx } from 'class-variance-authority'\n\nimport { useRatingDisplay } from './RatingDisplayContext'\nimport { RatingDisplayStar, type RatingDisplayStarProps } from './RatingDisplayStar'\nimport type { StarValue } from './types'\nimport { getSingleStarValue, getStarValue } from './utils'\n\nexport interface RatingDisplayStarsProps {\n size?: RatingDisplayStarProps['size']\n /**\n * Sets the rendering mode for stars.\n * @default 'default'\n */\n variant?: 'default' | 'single-star'\n /**\n * Custom fill algorithm for each star.\n * By default, stars are rounded to the nearest 0.5.\n */\n getFillMode?: ({ value, index }: { value?: number; index: number }) => StarValue\n}\n\nexport const RatingDisplayStars = ({\n size,\n variant = 'default',\n getFillMode,\n}: RatingDisplayStarsProps) => {\n const { value, size: contextSize } = useRatingDisplay()\n const resolvedSize = size ?? contextSize\n const getDisplayValue = (index: number) => {\n if (getFillMode) {\n return getFillMode({ index, value })\n }\n\n return variant === 'single-star' ? getSingleStarValue(value) : getStarValue({ index, value })\n }\n\n const stars =\n variant === 'single-star'\n ? [getDisplayValue(0)]\n : Array.from({ length: 5 }, (_, index) => getDisplayValue(index))\n\n return (\n <div\n data-spark-component=\"rating-display-stars\"\n className={cx(resolvedSize === 'lg' ? 'gap-x-md' : 'gap-x-sm', 'flex')}\n >\n {stars.map((starValue, index) => (\n <RatingDisplayStar key={index} size={resolvedSize} value={starValue} />\n ))}\n </div>\n )\n}\n\nRatingDisplayStars.displayName = 'RatingDisplay.Stars'\n","import { cva } from 'class-variance-authority'\nimport { type ComponentPropsWithRef, type ReactNode } from 'react'\n\nimport { useRatingDisplay } from './RatingDisplayContext'\nimport { formatRatingValue } from './utils'\n\nexport interface RatingDisplayValueProps extends Omit<ComponentPropsWithRef<'span'>, 'children'> {\n /**\n * Custom value content.\n * Pass a render function to receive the formatted value (first arg) and raw value (second arg),\n * then return the content to render.\n */\n children?: ReactNode | ((formattedValue: string, value: number) => ReactNode)\n}\n\nconst ratingDisplayValueStyles = cva('text-on-surface font-bold', {\n variants: {\n size: {\n sm: 'text-caption',\n md: 'text-body-2',\n lg: 'text-display-3',\n },\n },\n defaultVariants: {\n size: 'md',\n },\n})\n\nexport const RatingDisplayValue = ({ className, children, ...rest }: RatingDisplayValueProps) => {\n const { value, size } = useRatingDisplay()\n const formattedValue = formatRatingValue(value)\n const renderedValue =\n typeof children === 'function' ? children(formattedValue, value) : (children ?? formattedValue)\n\n return (\n <span\n data-spark-component=\"rating-display-value\"\n className={ratingDisplayValueStyles({ size: size ?? 'md', className })}\n {...rest}\n >\n {renderedValue}\n </span>\n )\n}\n\nRatingDisplayValue.displayName = 'RatingDisplay.Value'\n","import { RatingDisplay as Root } from './RatingDisplay'\nimport { RatingDisplayCount as Count } from './RatingDisplayCount'\nimport { RatingDisplayStars as Stars } from './RatingDisplayStars'\nimport { RatingDisplayValue as Value } from './RatingDisplayValue'\n\nexport const RatingDisplay: typeof Root & {\n Stars: typeof Stars\n Value: typeof Value\n Count: typeof Count\n} = Object.assign(Root, {\n Stars,\n Value,\n Count,\n})\n\nRatingDisplay.displayName = 'RatingDisplay'\nStars.displayName = 'RatingDisplay.Stars'\nValue.displayName = 'RatingDisplay.Value'\nCount.displayName = 'RatingDisplay.Count'\n\nexport { type RatingDisplayProps, type RatingDisplayRootProps } from './RatingDisplay'\nexport { type RatingDisplayStarsProps } from './RatingDisplayStars'\nexport { type RatingDisplayValueProps } from './RatingDisplayValue'\nexport { type RatingDisplayCountProps } from './RatingDisplayCount'\nexport type { StarValue } from './types'\n"],"names":["RatingDisplayContext","createContext","RatingDisplayProvider","value","size","count","children","jsx","useRatingDisplay","context","useContext","RatingDisplay","ref","rest","ratingDisplayCountStyles","cva","RatingDisplayCount","className","renderedCount","jsxs","ratingDisplayStarStyles","ratingDisplayStarIconStyles","RatingDisplayStar","cx","Icon","StarFill","StarOutline","getNearestHalfDecimal","num","formatRatingValue","locale","getStarValue","index","starPosition","formattedValue","getSingleStarValue","RatingDisplayStars","variant","getFillMode","contextSize","resolvedSize","getDisplayValue","stars","_","starValue","ratingDisplayValueStyles","RatingDisplayValue","renderedValue","Root","Stars","Value","Count"],"mappings":";;;;;;AAUA,MAAMA,IAAuBC,EAAgD,IAAI,GAIpEC,IAAwB,CAAC;AAAA,EACpC,OAAAC;AAAA,EACA,MAAAC;AAAA,EACA,OAAAC;AAAA,EACA,UAAAC;AACF,MAEI,gBAAAC,EAACP,EAAqB,UAArB,EAA8B,OAAO,EAAE,OAAAG,GAAO,MAAAC,GAAM,OAAAC,KAClD,UAAAC,GACH,GAISE,IAAmB,MAAM;AACpC,QAAMC,IAAUC,EAAWV,CAAoB;AAC/C,MAAI,CAACS;AACH,UAAM,IAAI,MAAM,sEAAsE;AAGxF,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B9B,EAAI,6BAA6B;AAAA,EAChE,UAAU;AAAA,IACR,MAAM;AAAA,MACJ,IAAI;AAAA,MACJ,IAAI;AAAA,MACJ,IAAI;AAAA,IAAA;AAAA,EACN;AAAA,EAEF,iBAAiB;AAAA,IACf,MAAM;AAAA,EAAA;AAEV,CAAC,GAEY+B,IAAqB,CAAC,EAAE,WAAA7B,GAAW,UAAAX,GAAU,GAAGO,QAAoC;AAC/F,QAAM,EAAE,OAAAV,GAAO,MAAAC,EAAA,IAASI,EAAA,GAClB0B,IAAiBL,EAAkB1B,CAAK,GACxC4C,IACJ,OAAOzC,KAAa,aAAaA,EAAS4B,GAAgB/B,CAAK,IAAKG,KAAY4B;AAElF,SACE,gBAAA3B;AAAA,IAAC;AAAA,IAAA;AAAA,MACC,wBAAqB;AAAA,MACrB,WAAWsC,EAAyB,EAAE,MAAMzC,KAAQ,MAAM,WAAAa,GAAW;AAAA,MACpE,GAAGJ;AAAA,MAEH,UAAAkC;AAAA,IAAA;AAAA,EAAA;AAGP;AAEAD,EAAmB,cAAc;ACxC1B,MAAMnC,IAIT,OAAO,OAAOqC,GAAM;AAAA,EAAA,OACtBC;AAAAA,EAAA,OACAC;AAAAA,EAAA,OACAC;AACF,CAAC;AAEDxC,EAAc,cAAc;AAC5BsC,EAAM,cAAc;AACpBC,EAAM,cAAc;AACpBC,EAAM,cAAc;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type StarValue = 0 | 0.5 | 1;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { StarValue } from './types';
|
|
2
|
+
declare function getNearestHalfDecimal(num: number): number;
|
|
3
|
+
declare function formatRatingValue(value: number): string;
|
|
4
|
+
declare function getStarValue({ value, index }: {
|
|
5
|
+
value?: number;
|
|
6
|
+
index: number;
|
|
7
|
+
}): StarValue;
|
|
8
|
+
declare function getSingleStarValue(value?: number): StarValue;
|
|
9
|
+
declare function splitAt<T>(arr: T[], index: number): [T[], T[]];
|
|
10
|
+
export { formatRatingValue, getNearestHalfDecimal, getSingleStarValue, getStarValue, splitAt };
|
package/dist/switch/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react/jsx-runtime"),y=require("@spark-ui/components/form-field"),c=require("class-variance-authority"),v=require("react"),D=require("@spark-ui/hooks/use-combined-state"),$=require("@spark-ui/icons/Check"),L=require("@spark-ui/icons/Close"),w=require("radix-ui"),k=require("../Slot-DQ8z2zsy.js"),a=require("@spark-ui/internal-utils"),P=require("../label/index.js"),T=c.cva(a.tw(["relative shrink-0 self-baseline","cursor-pointer","rounded-full border-transparent","hover:ring-4","transition-colors duration-200 ease-in-out","disabled:hover:ring-transparent disabled:cursor-not-allowed disabled:opacity-dim-3","focus-visible:u-outline","data-[state=unchecked]:bg-on-surface/dim-3","u-shadow-border-transition","overflow-x-hidden"]),{variants:{size:a.makeVariants({sm:a.tw(["h-sz-24","w-sz-40","border-md"]),md:a.tw(["h-sz-32","w-sz-56","border-[4px]"])}),intent:a.makeVariants({main:["data-[state=checked]:bg-main","hover:ring-main-container","text-main"],support:["data-[state=checked]:bg-support","hover:ring-support-container","text-support"],accent:["data-[state=checked]:bg-accent","hover:ring-accent-container","text-accent"],basic:["data-[state=checked]:bg-basic","hover:ring-basic-container","text-basic"],success:["data-[state=checked]:bg-success","hover:ring-success-container","text-success"],alert:["data-[state=checked]:bg-alert","hover:ring-alert-container","text-alert"],error:["data-[state=checked]:bg-error","hover:ring-error-container","text-error"],info:["data-[state=checked]:bg-info","hover:ring-info-container","text-info"],neutral:["data-[state=checked]:bg-neutral","hover:ring-neutral-container","text-neutral"]})},defaultVariants:{intent:"basic",size:"sm"}}),U=c.cva(["pointer-events-none absolute inset-0 flex items-center","transition-all duration-200 ease-in-out"],{variants:{checked:{true:"translate-x-full",false:"translate-x-0"}}}),A=c.cva(["absolute left-0 top-0 flex items-center justify-center","bg-surface","rounded-full","ring-0","transition-all duration-200 ease-in-out"],{variants:{size:a.makeVariants({sm:["h-sz-20","w-sz-20"],md:["h-sz-24","w-sz-24"]}),checked:{true:"-translate-x-full",false:"translate-x-0 text-on-surface/dim-2"}},defaultVariants:{size:"sm",checked:!1}}),S=c.cva(["transition-opacity duration-200"],{variants:{size:a.makeVariants({sm:["h-sz-10 w-sz-10"],md:["h-sz-12 w-sz-12"]})},defaultVariants:{size:"sm"}}),z=({checked:n,checkedIcon:t=e.jsx($.Check,{}),defaultChecked:r,intent:m,uncheckedIcon:o=e.jsx(L.Close,{}),size:i="md",value:b="on",onCheckedChange:x,className:l,required:d,ref:p,...u})=>{const[s,h]=D.useCombinedState(n,r),{name:f,description:q,state:I,isRequired:F,isInvalid:N}=y.useFormFieldControl(),V=I??m,R=g=>{h(g),x?.(g)};return e.jsx(w.Switch.Root,{"data-spark-component":"switch-input",ref:p,className:T({intent:V,size:i,className:l}),value:b,checked:n,defaultChecked:r,onCheckedChange:R,name:f,required:d||F,"aria-invalid":N,"aria-describedby":q,...u,children:e.jsx("span",{className:U({checked:s}),children:e.jsxs(w.Switch.Thumb,{className:A({size:i,checked:s}),children:[s&&t&&e.jsx(k.Slot,{className:S({size:i}),children:t}),!s&&o&&e.jsx(k.Slot,{className:S({size:i}),children:o})]})})})};z.displayName="SwitchInput";const E=c.cva("",{variants:{disabled:{true:["text-neutral/dim-2","cursor-not-allowed"],false:["cursor-pointer"]}},defaultVariants:{disabled:!1}}),G=({className:n,disabled:t,...r})=>e.jsx(P.Label,{"data-spark-component":"switch-label",className:E({disabled:t,className:n}),...r}),j=":switch",C=({size:n="md",children:t,className:r,id:m,disabled:o,reverse:i=!1,ref:b,...x})=>{const l=y.useFormFieldControl(),d=`${j}-label-${v.useId()}`,p=`${j}-input-${v.useId()}`,u=l.id||m||p,s=t&&e.jsx(G,{disabled:o,htmlFor:u,id:d,children:t}),h=e.jsx(z,{ref:b,size:n,id:u,disabled:o,"aria-labelledby":t?d:l.labelId,...x}),f=i?e.jsxs(e.Fragment,{children:[s,h]}):e.jsxs(e.Fragment,{children:[h,s]});return e.jsx("div",{"data-spark-component":"switch",className:c.cx("gap-md text-body-1 flex items-center",r),children:f})};C.displayName="Switch";exports.Switch=C;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react/jsx-runtime"),y=require("@spark-ui/components/form-field"),c=require("class-variance-authority"),v=require("react"),D=require("@spark-ui/hooks/use-combined-state"),$=require("@spark-ui/icons/Check"),L=require("@spark-ui/icons/Close"),w=require("radix-ui"),k=require("../Slot-DQ8z2zsy.js"),a=require("@spark-ui/internal-utils"),P=require("../label/index.js"),T=c.cva(a.tw(["relative shrink-0 self-baseline","cursor-pointer","rounded-full border-transparent","hover:ring-4","transition-colors duration-200 ease-in-out","disabled:hover:ring-transparent disabled:cursor-not-allowed disabled:opacity-dim-3","focus-visible:u-outline","data-[state=unchecked]:bg-on-surface/dim-3","u-shadow-border-transition","overflow-x-hidden"]),{variants:{size:a.makeVariants({sm:a.tw(["h-sz-24","w-sz-40","border-md"]),md:a.tw(["h-sz-32","w-sz-56","border-[4px]"])}),intent:a.makeVariants({main:["data-[state=checked]:bg-main","hover:ring-main-container","text-main"],support:["data-[state=checked]:bg-support","hover:ring-support-container","text-support"],accent:["data-[state=checked]:bg-accent","hover:ring-accent-container","text-accent"],basic:["data-[state=checked]:bg-basic","hover:ring-basic-container","text-basic"],success:["data-[state=checked]:bg-success","hover:ring-success-container","text-success"],alert:["data-[state=checked]:bg-alert","hover:ring-alert-container","text-alert"],error:["data-[state=checked]:bg-error","hover:ring-error-container","text-error"],info:["data-[state=checked]:bg-info","hover:ring-info-container","text-info"],neutral:["data-[state=checked]:bg-neutral","hover:ring-neutral-container","text-neutral"]})},defaultVariants:{intent:"basic",size:"sm"}}),U=c.cva(["pointer-events-none absolute inset-0 flex items-center","transition-all duration-200 ease-in-out"],{variants:{checked:{true:"translate-x-full",false:"translate-x-0"}}}),A=c.cva(["absolute left-0 top-0 flex items-center justify-center","bg-surface","rounded-full","ring-0","transition-all duration-200 ease-in-out"],{variants:{size:a.makeVariants({sm:["h-sz-20","w-sz-20"],md:["h-sz-24","w-sz-24"]}),checked:{true:"-translate-x-full",false:"translate-x-0 text-on-surface/dim-2"}},defaultVariants:{size:"sm",checked:!1}}),S=c.cva(["transition-opacity duration-200"],{variants:{size:a.makeVariants({sm:["h-sz-10 w-sz-10"],md:["h-sz-12 w-sz-12"]})},defaultVariants:{size:"sm"}}),z=({checked:n,checkedIcon:t=e.jsx($.Check,{}),defaultChecked:r,intent:m,uncheckedIcon:o=e.jsx(L.Close,{}),size:i="md",value:b="on",onCheckedChange:x,className:l,required:d,ref:p,...u})=>{const[s,h]=D.useCombinedState(n,r),{name:f,description:q,state:I,isRequired:F,isInvalid:N}=y.useFormFieldControl(),V=I??m,R=g=>{h(g),x?.(g)};return e.jsx(w.Switch.Root,{"data-spark-component":"switch-input",ref:p,className:T({intent:V,size:i,className:l}),value:b,checked:n,defaultChecked:r,onCheckedChange:R,name:f,required:d||F,"aria-invalid":N,"aria-describedby":q,...u,children:e.jsx("span",{className:U({checked:s}),children:e.jsxs(w.Switch.Thumb,{className:A({size:i,checked:s}),children:[s&&t&&e.jsx(k.Slot,{className:S({size:i}),children:t}),!s&&o&&e.jsx(k.Slot,{className:S({size:i}),children:o})]})})})};z.displayName="SwitchInput";const E=c.cva("default:text-on-surface",{variants:{disabled:{true:["text-neutral/dim-2","cursor-not-allowed"],false:["cursor-pointer"]}},defaultVariants:{disabled:!1}}),G=({className:n,disabled:t,...r})=>e.jsx(P.Label,{"data-spark-component":"switch-label",className:E({disabled:t,className:n}),...r}),j=":switch",C=({size:n="md",children:t,className:r,id:m,disabled:o,reverse:i=!1,ref:b,...x})=>{const l=y.useFormFieldControl(),d=`${j}-label-${v.useId()}`,p=`${j}-input-${v.useId()}`,u=l.id||m||p,s=t&&e.jsx(G,{disabled:o,htmlFor:u,id:d,children:t}),h=e.jsx(z,{ref:b,size:n,id:u,disabled:o,"aria-labelledby":t?d:l.labelId,...x}),f=i?e.jsxs(e.Fragment,{children:[s,h]}):e.jsxs(e.Fragment,{children:[h,s]});return e.jsx("div",{"data-spark-component":"switch",className:c.cx("gap-md text-body-1 flex items-center",r),children:f})};C.displayName="Switch";exports.Switch=C;
|
|
2
2
|
//# sourceMappingURL=index.js.map
|