@pablo-v/rating 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +12 -2
- package/dist/Rating.d.ts +25 -0
- package/dist/Rating.d.ts.map +1 -0
- package/dist/Rating.js +26 -0
- package/dist/Rating.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/rating.css +101 -0
- package/package.json +42 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pablo Viniegra
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @pablo-v/rating
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Star rating input and read-only display
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pnpm add @pablo-v/rating @pablo-v/tokens
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```tsx
|
|
10
|
+
import '@pablo-v/tokens/styles.css';
|
|
11
|
+
import '@pablo-v/rating/style.css';
|
|
12
|
+
import { Rating } from '@pablo-v/rating';
|
|
13
|
+
```
|
package/dist/Rating.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type ComponentPropsWithRef } from 'react';
|
|
2
|
+
export interface RatingProps extends Omit<ComponentPropsWithRef<'div'>, 'onChange' | 'defaultValue'> {
|
|
3
|
+
/** 0 means no rating. Fractions are shown only when `readOnly`. */
|
|
4
|
+
value?: number;
|
|
5
|
+
defaultValue?: number;
|
|
6
|
+
onChange?: (value: number) => void;
|
|
7
|
+
/** @default 5 */
|
|
8
|
+
max?: number;
|
|
9
|
+
/** Shows the value as an image instead of an input. */
|
|
10
|
+
readOnly?: boolean;
|
|
11
|
+
disabled?: boolean;
|
|
12
|
+
/** Picking the current value again resets it to 0. @default true */
|
|
13
|
+
clearable?: boolean;
|
|
14
|
+
/** Form field name for the selected radio. */
|
|
15
|
+
name?: string;
|
|
16
|
+
/** @default 'md' */
|
|
17
|
+
size?: 'md' | 'lg';
|
|
18
|
+
/** Accessible name of each star. @default n => `${n} star(s)` */
|
|
19
|
+
getLabel?: (value: number) => string;
|
|
20
|
+
/** Accessible name in read-only mode. @default (v, max) => `${v} out of ${max} stars` */
|
|
21
|
+
getValueText?: (value: number, max: number) => string;
|
|
22
|
+
}
|
|
23
|
+
/** Star rating on native radios, or a read-only score that can show fractions. */
|
|
24
|
+
export declare function Rating({ value: valueProp, defaultValue, onChange, max, readOnly, disabled, clearable, name, size, getLabel, getValueText, className, ...rest }: RatingProps): import("react").JSX.Element;
|
|
25
|
+
//# sourceMappingURL=Rating.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Rating.d.ts","sourceRoot":"","sources":["../src/Rating.tsx"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,qBAAqB,EAAuC,MAAM,OAAO,CAAC;AAExF,MAAM,WAAW,WACf,SAAQ,IAAI,CAAC,qBAAqB,CAAC,KAAK,CAAC,EAAE,UAAU,GAAG,cAAc,CAAC;IACvE,mEAAmE;IACnE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACnC,iBAAiB;IACjB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,uDAAuD;IACvD,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,oEAAoE;IACpE,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,8CAA8C;IAC9C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,oBAAoB;IACpB,IAAI,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;IACnB,iEAAiE;IACjE,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC;IACrC,yFAAyF;IACzF,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC;CACvD;AAKD,kFAAkF;AAClF,wBAAgB,MAAM,CAAC,EACrB,KAAK,EAAE,SAAS,EAChB,YAAgB,EAChB,QAAQ,EACR,GAAO,EACP,QAAQ,EACR,QAAQ,EACR,SAAgB,EAChB,IAAI,EACJ,IAAW,EACX,QAAuB,EACvB,YAA+B,EAC/B,SAAS,EACT,GAAG,IAAI,EACR,EAAE,WAAW,+BAuEb"}
|
package/dist/Rating.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { clamp, cx, useControllableState } from '@pablo-v/core';
|
|
4
|
+
import { StarIcon } from '@pablo-v/pixel-icons';
|
|
5
|
+
import { useId, useState } from 'react';
|
|
6
|
+
const defaultLabel = (n) => `${n} ${n === 1 ? 'star' : 'stars'}`;
|
|
7
|
+
const defaultValueText = (v, max) => `${v} out of ${max} stars`;
|
|
8
|
+
/** Star rating on native radios, or a read-only score that can show fractions. */
|
|
9
|
+
export function Rating({ value: valueProp, defaultValue = 0, onChange, max = 5, readOnly, disabled, clearable = true, name, size = 'md', getLabel = defaultLabel, getValueText = defaultValueText, className, ...rest }) {
|
|
10
|
+
const [value, setValue] = useControllableState({ value: valueProp, defaultValue, onChange });
|
|
11
|
+
const [hover, setHover] = useState(null);
|
|
12
|
+
const groupName = useId();
|
|
13
|
+
const iconSize = size === 'lg' ? 32 : 16;
|
|
14
|
+
const stars = Array.from({ length: max }, (_, i) => i + 1);
|
|
15
|
+
if (readOnly) {
|
|
16
|
+
return (_jsx("div", { role: "img", "aria-label": getValueText(value, max), "data-size": size, "data-readonly": "", className: cx('pxl-rating', className), ...rest, children: stars.map((n) => (_jsxs("span", { className: "pxl-rating__star", style: { '--_fill': `${clamp(value - n + 1, 0, 1) * 100}%` }, children: [_jsx(StarIcon, { size: iconSize }), _jsx("span", { className: "pxl-rating__fill", children: _jsx(StarIcon, { size: iconSize }) })] }, n))) }));
|
|
17
|
+
}
|
|
18
|
+
const shown = hover ?? Math.round(value);
|
|
19
|
+
return (_jsx("div", { role: "radiogroup", "aria-disabled": disabled || undefined, "data-size": size, className: cx('pxl-rating', className), onPointerLeave: () => setHover(null), ...rest, children: stars.map((n) => (_jsxs("label", { className: "pxl-rating__star", "data-lit": n <= shown || undefined, onPointerEnter: () => !disabled && setHover(n), children: [_jsx("input", { type: "radio", className: "pxl-rating__input", name: name ?? groupName, value: n, checked: Math.round(value) === n, disabled: disabled, "aria-label": getLabel(n), onChange: () => setValue(n), onClick: () => {
|
|
20
|
+
if (clearable && Math.round(value) === n) {
|
|
21
|
+
setValue(0);
|
|
22
|
+
setHover(null);
|
|
23
|
+
}
|
|
24
|
+
} }), _jsx(StarIcon, { size: iconSize })] }, n))) }));
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=Rating.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Rating.js","sourceRoot":"","sources":["../src/Rating.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AAChE,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAkD,KAAK,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAyBxF,MAAM,YAAY,GAAG,CAAC,CAAS,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC;AACzE,MAAM,gBAAgB,GAAG,CAAC,CAAS,EAAE,GAAW,EAAE,EAAE,CAAC,GAAG,CAAC,WAAW,GAAG,QAAQ,CAAC;AAEhF,kFAAkF;AAClF,MAAM,UAAU,MAAM,CAAC,EACrB,KAAK,EAAE,SAAS,EAChB,YAAY,GAAG,CAAC,EAChB,QAAQ,EACR,GAAG,GAAG,CAAC,EACP,QAAQ,EACR,QAAQ,EACR,SAAS,GAAG,IAAI,EAChB,IAAI,EACJ,IAAI,GAAG,IAAI,EACX,QAAQ,GAAG,YAAY,EACvB,YAAY,GAAG,gBAAgB,EAC/B,SAAS,EACT,GAAG,IAAI,EACK;IACZ,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,oBAAoB,CAAC,EA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AI,EACf,SAAS,EAAE,EAAE,CAAC,YAAY,EAAE,SAAS,CAAC,EACtC,cAAc,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,KAChC,IAAI,YAEP,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAChB,iBAEE,SAAS,EAAC,kBAAkB,cAClB,CAAC,IAAI,KAAK,IAAI,SAAS,EACjC,cAAc,EAAE,GAAG,EAAE,CAAC,CAAC,QAAQ,IAAI,QAAQ,CAAC,CAAC,CAAC,aAE9C,gBACE,IAAI,EAAC,OAAO,EACZ,SAAS,EAAC,mBAAmB,EAC7B,IAAI,EAAE,IAAI,IAAI,SAAS,EACvB,KAAK,EAAE,CAAC,EACR,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,EAChC,QAAQ,EAAE,QAAQ,gBACN,QAAQ,CAAC,CAAC,CAAC,EACvB,QAAQ,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,EAC3B,OAAO,EAAE,GAAG,EAAE;wBACZ,IAAI,SAAS,IAAI,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;4BACzC,QAAQ,CAAC,CAAC,CAAC,CAAC;4BACZ,QAAQ,CAAC,IAAI,CAAC,CAAC;wBACjB,CAAC;oBACH,CAAC,GACD,EACF,KAAC,QAAQ,IAAC,IAAI,EAAE,QAAQ,GAAI,KArBvB,CAAC,CAsBA,CACT,CAAC,GACE,CACP,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAoB,MAAM,aAAa,CAAC"}
|
package/dist/rating.css
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
.pxl-rating {
|
|
2
|
+
--_cell: var(--pxl-size-xs);
|
|
3
|
+
--_off: var(--pxl-gray-600);
|
|
4
|
+
--_on: var(--pxl-accent);
|
|
5
|
+
|
|
6
|
+
display: inline-flex;
|
|
7
|
+
align-items: center;
|
|
8
|
+
vertical-align: middle;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
.pxl-rating[data-size="lg"] {
|
|
12
|
+
--_cell: var(--pxl-size-md);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
.pxl-rating[data-readonly] {
|
|
16
|
+
gap: 2px;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
.pxl-rating[data-readonly][data-size="md"] {
|
|
20
|
+
--_cell: 16px;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
.pxl-rating[data-readonly][data-size="lg"] {
|
|
24
|
+
--_cell: 32px;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
.pxl-rating__star {
|
|
28
|
+
position: relative;
|
|
29
|
+
display: grid;
|
|
30
|
+
place-items: center;
|
|
31
|
+
width: var(--_cell);
|
|
32
|
+
height: var(--_cell);
|
|
33
|
+
border-radius: var(--pxl-radius-sm);
|
|
34
|
+
color: var(--_off);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
label.pxl-rating__star {
|
|
38
|
+
cursor: pointer;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
.pxl-rating__star .pxl-icon {
|
|
42
|
+
transition:
|
|
43
|
+
color var(--pxl-dur-fast) linear,
|
|
44
|
+
transform var(--pxl-dur-base) var(--pxl-ease-spring);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.pxl-rating__star[data-lit] {
|
|
48
|
+
color: var(--_on);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
label.pxl-rating__star:hover .pxl-icon {
|
|
52
|
+
transform: scale(1.15);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
label.pxl-rating__star:has(:checked) .pxl-icon {
|
|
56
|
+
animation: pxl-pop-in var(--pxl-dur-base) var(--pxl-ease-out);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
.pxl-rating__star:has(:focus-visible) {
|
|
60
|
+
outline: 2px solid var(--pxl-accent);
|
|
61
|
+
outline-offset: -2px;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
.pxl-rating__input {
|
|
65
|
+
position: absolute;
|
|
66
|
+
inset: 0;
|
|
67
|
+
margin: 0;
|
|
68
|
+
opacity: 0;
|
|
69
|
+
cursor: inherit;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
.pxl-rating[aria-disabled="true"] {
|
|
73
|
+
opacity: var(--pxl-opacity-disabled);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
.pxl-rating[aria-disabled="true"] .pxl-rating__star {
|
|
77
|
+
cursor: not-allowed;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/* Read-only fractions: a lit copy of the star, clipped to --_fill, sits over the unlit one. */
|
|
81
|
+
.pxl-rating__fill {
|
|
82
|
+
position: absolute;
|
|
83
|
+
inset: 0;
|
|
84
|
+
display: grid;
|
|
85
|
+
place-items: center;
|
|
86
|
+
color: var(--_on);
|
|
87
|
+
clip-path: inset(0 calc(100% - var(--_fill)) 0 0);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
@media (prefers-reduced-motion: reduce) {
|
|
91
|
+
label.pxl-rating__star:hover .pxl-icon {
|
|
92
|
+
transform: none;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
@media (forced-colors: active) {
|
|
97
|
+
.pxl-rating__star[data-lit],
|
|
98
|
+
.pxl-rating__fill {
|
|
99
|
+
color: Highlight;
|
|
100
|
+
}
|
|
101
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pablo-v/rating",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Star rating input and read-only display",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/PabloViniegra/pixel-ui.git",
|
|
9
|
+
"directory": "packages/rating"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"sideEffects": [
|
|
13
|
+
"*.css"
|
|
14
|
+
],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"source": "./src/index.ts",
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./style.css": {
|
|
22
|
+
"source": "./src/rating.css",
|
|
23
|
+
"default": "./dist/rating.css"
|
|
24
|
+
},
|
|
25
|
+
"./package.json": "./package.json"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@pablo-v/core": "^0.2.1",
|
|
32
|
+
"@pablo-v/pixel-icons": "^1.3.0"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"react": "^19.0.0",
|
|
36
|
+
"@pablo-v/tokens": "^0.2.4"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsc -b && node ../../scripts/copy-assets.mjs",
|
|
43
|
+
"clean": "rm -rf dist *.tsbuildinfo"
|
|
44
|
+
}
|
|
6
45
|
}
|