@react-cupertino-ui/rating 1.0.3

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 ADDED
@@ -0,0 +1,24 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Anderson Lima
4
+
5
+ This project is inspired by Apple's design principles but is not affiliated with, endorsed, or sponsored by Apple Inc.
6
+
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ of this software and associated documentation files (the "Software"), to deal
10
+ in the Software without restriction, including without limitation the rights
11
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ copies of the Software, and to permit persons to whom the Software is
13
+ furnished to do so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in all
16
+ copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ SOFTWARE.
package/dist/index.css ADDED
@@ -0,0 +1,88 @@
1
+ .react-cupertino-ui-rating {
2
+ display: inline-flex;
3
+ flex-direction: column;
4
+ gap: 0.35rem;
5
+ color: var(--foreground, #05060a);
6
+ }
7
+ .react-cupertino-ui-rating__stars {
8
+ display: inline-flex;
9
+ gap: 0.35rem;
10
+ padding: 0.25rem 0.5rem;
11
+ border-radius: 999px;
12
+ background: rgba(255, 255, 255, 0.65);
13
+ background: rgba(255, 255, 255, 0.7);
14
+ backdrop-filter: blur(var(--glass-blur, 24px)) saturate(var(--glass-saturation, 180%));
15
+ -webkit-backdrop-filter: blur(var(--glass-blur, 24px)) saturate(var(--glass-saturation, 180%));
16
+ border: 1px solid var(--glass-border, rgba(255, 255, 255, 0.18));
17
+ box-shadow: var(--glass-shadow, 0 8px 32px rgba(0, 0, 0, 0.12));
18
+ position: relative;
19
+ overflow: hidden;
20
+ }
21
+ .react-cupertino-ui-rating__stars::before {
22
+ content: "";
23
+ position: absolute;
24
+ inset: 0;
25
+ background: var(--glass-highlight, linear-gradient(135deg, rgba(255, 255, 255, 0.4) 0%, transparent 55%));
26
+ border-radius: inherit;
27
+ opacity: var(--glass-highlight-opacity, 0.95);
28
+ pointer-events: none;
29
+ mix-blend-mode: screen;
30
+ }
31
+ .react-cupertino-ui-rating__stars {
32
+ box-shadow: 0 18px 45px rgba(15, 15, 15, 0.18);
33
+ }
34
+ .react-cupertino-ui-rating__slot {
35
+ border: none;
36
+ background: transparent;
37
+ padding: 0;
38
+ cursor: pointer;
39
+ display: inline-flex;
40
+ align-items: center;
41
+ justify-content: center;
42
+ width: var(--rating-size, 32px);
43
+ height: var(--rating-size, 32px);
44
+ border-radius: 999px;
45
+ transition: transform 160ms ease, filter 160ms ease;
46
+ }
47
+ .react-cupertino-ui-rating__slot[data-active=true] {
48
+ filter: drop-shadow(0 6px 12px rgba(10, 132, 255, 0.35));
49
+ }
50
+ .react-cupertino-ui-rating__slot:focus-visible {
51
+ outline: none;
52
+ box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.35);
53
+ }
54
+ .react-cupertino-ui-rating[data-readonly=true] .react-cupertino-ui-rating__slot {
55
+ cursor: default;
56
+ }
57
+ .react-cupertino-ui-rating__star {
58
+ width: 100%;
59
+ height: 100%;
60
+ }
61
+ .react-cupertino-ui-rating__star svg {
62
+ width: 100%;
63
+ height: 100%;
64
+ }
65
+ .react-cupertino-ui-rating__helper {
66
+ font-size: 0.82rem;
67
+ color: color-mix(in srgb, currentColor, transparent 35%);
68
+ margin: 0;
69
+ }
70
+ .react-cupertino-ui-rating.size-sm {
71
+ --rating-size: 22px;
72
+ }
73
+ .react-cupertino-ui-rating.size-md {
74
+ --rating-size: 30px;
75
+ }
76
+ .react-cupertino-ui-rating.size-lg {
77
+ --rating-size: 38px;
78
+ }
79
+
80
+ @media (prefers-color-scheme: dark) {
81
+ .react-cupertino-ui-rating {
82
+ color: rgba(248, 250, 252, 0.95);
83
+ }
84
+ .react-cupertino-ui-rating__stars {
85
+ background: rgba(15, 15, 20, 0.68);
86
+ box-shadow: 0 20px 50px rgba(0, 0, 0, 0.45);
87
+ }
88
+ }
@@ -0,0 +1,17 @@
1
+ import * as React from "react";
2
+ import type { BaseProps } from "@react-cupertino-ui/shared/lib/interfaces/BaseProps";
3
+ import "./index.scss";
4
+ export type RatingSize = "sm" | "md" | "lg";
5
+ export interface RatingProps extends Omit<BaseProps<HTMLDivElement>, "children" | "size"> {
6
+ value?: number;
7
+ defaultValue?: number;
8
+ max?: number;
9
+ readOnly?: boolean;
10
+ allowHalf?: boolean;
11
+ onValueChange?: (value: number) => void;
12
+ size?: RatingSize;
13
+ labels?: string[];
14
+ helperText?: string;
15
+ }
16
+ declare const Rating: React.ForwardRefExoticComponent<RatingProps & React.RefAttributes<HTMLDivElement>>;
17
+ export { Rating };
package/dist/index.js ADDED
@@ -0,0 +1,85 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import * as React from "react";
3
+ import { cn } from "@react-cupertino-ui/shared/lib/utils";
4
+ import "./index.scss";
5
+ const clampValue = (value, max, allowHalf) => {
6
+ const step = allowHalf ? 0.5 : 1;
7
+ const clamped = Math.max(0, Math.min(value, max));
8
+ return Math.round(clamped / step) * step;
9
+ };
10
+ const StarIcon = ({ percentage }) => {
11
+ const pct = Math.max(0, Math.min(percentage, 100));
12
+ return (_jsx("span", { className: "react-cupertino-ui-rating__star", children: _jsxs("svg", { viewBox: "0 0 24 24", "aria-hidden": "true", focusable: "false", children: [_jsx("defs", { children: _jsxs("linearGradient", { id: "rating-fill", x1: "0%", y1: "0%", x2: "100%", y2: "0%", children: [_jsx("stop", { offset: `${pct}%`, stopColor: "currentColor" }), _jsx("stop", { offset: `${pct}%`, stopColor: "rgba(255,255,255,0.2)" })] }) }), _jsx("path", { d: "M12 2.6l2.7 5.46 6.03.88-4.37 4.24 1.03 6.02L12 16.9l-5.37 2.8 1.03-6.02L3.3 8.94l6.03-.88L12 2.6z", fill: "url(#rating-fill)", stroke: "rgba(255,255,255,0.5)", strokeWidth: "0.6" })] }) }));
13
+ };
14
+ const Rating = React.forwardRef((props, ref) => {
15
+ const { className, value, defaultValue = 0, max = 5, allowHalf = true, readOnly = false, onValueChange, size = "md", labels, helperText, ...rest } = props;
16
+ const isControlled = value !== undefined;
17
+ const [internalValue, setInternalValue] = React.useState(() => clampValue(defaultValue, max, allowHalf));
18
+ const currentValue = isControlled ? clampValue(value, max, allowHalf) : internalValue;
19
+ const handleChange = (next) => {
20
+ const resolved = clampValue(next, max, allowHalf);
21
+ if (!isControlled) {
22
+ setInternalValue(resolved);
23
+ }
24
+ onValueChange?.(resolved);
25
+ };
26
+ const handleKeyDown = (event, index) => {
27
+ if (readOnly) {
28
+ return;
29
+ }
30
+ const step = allowHalf ? 0.5 : 1;
31
+ if (event.key === "ArrowRight" || event.key === "ArrowUp") {
32
+ event.preventDefault();
33
+ handleChange(currentValue + step);
34
+ }
35
+ else if (event.key === "ArrowLeft" || event.key === "ArrowDown") {
36
+ event.preventDefault();
37
+ handleChange(currentValue - step);
38
+ }
39
+ else if (event.key === "Home") {
40
+ event.preventDefault();
41
+ handleChange(0);
42
+ }
43
+ else if (event.key === "End") {
44
+ event.preventDefault();
45
+ handleChange(max);
46
+ }
47
+ else if (event.key === "Enter" || event.key === " ") {
48
+ event.preventDefault();
49
+ handleChange(index + 1);
50
+ }
51
+ };
52
+ const getFillPercentage = (index) => {
53
+ const starValue = index + 1;
54
+ if (currentValue >= starValue) {
55
+ return 100;
56
+ }
57
+ if (currentValue + (allowHalf ? 0.5 : 1) > starValue && allowHalf) {
58
+ return 50;
59
+ }
60
+ return Math.max(0, (currentValue - index) * 100);
61
+ };
62
+ return (_jsxs("div", { ref: ref, className: cn("react-cupertino-ui-rating", `size-${size}`, className), "data-readonly": readOnly ? "true" : undefined, ...rest, children: [_jsx("div", { className: "react-cupertino-ui-rating__stars", role: "group", "aria-label": "Rating", children: Array.from({ length: max }).map((_, index) => {
63
+ const percentage = getFillPercentage(index);
64
+ const label = labels?.[index];
65
+ const buttonValue = index + 1;
66
+ if (readOnly) {
67
+ return (_jsx("div", { className: "react-cupertino-ui-rating__slot", "aria-hidden": "true", title: label, children: _jsx(StarIcon, { percentage: percentage }) }, buttonValue));
68
+ }
69
+ return (_jsx("button", { type: "button", className: "react-cupertino-ui-rating__slot", "data-active": currentValue >= buttonValue ? "true" : undefined, onClick: (event) => {
70
+ if (readOnly) {
71
+ return;
72
+ }
73
+ if (allowHalf) {
74
+ const rect = event.currentTarget.getBoundingClientRect();
75
+ const clickOffset = event.clientX - rect.left;
76
+ const isLeftHalf = clickOffset <= rect.width / 2;
77
+ handleChange(isLeftHalf ? buttonValue - 0.5 : buttonValue);
78
+ return;
79
+ }
80
+ handleChange(buttonValue);
81
+ }, onKeyDown: (event) => handleKeyDown(event, index), "aria-label": label ?? `${buttonValue} star${buttonValue === 1 ? "" : "s"}`, children: _jsx(StarIcon, { percentage: percentage }) }, buttonValue));
82
+ }) }), helperText ? _jsx("p", { className: "react-cupertino-ui-rating__helper", children: helperText }) : null] }));
83
+ });
84
+ Rating.displayName = "Rating";
85
+ export { Rating };
@@ -0,0 +1,84 @@
1
+ @use "@/styles/mixins/glass" as glass;
2
+
3
+ .react-cupertino-ui-rating {
4
+ display: inline-flex;
5
+ flex-direction: column;
6
+ gap: 0.35rem;
7
+ color: var(--foreground, #05060a);
8
+
9
+ &__stars {
10
+ display: inline-flex;
11
+ gap: 0.35rem;
12
+ padding: 0.25rem 0.5rem;
13
+ border-radius: 999px;
14
+ background: rgba(255, 255, 255, 0.65);
15
+ @include glass.glass-panel(light, var(--glass-blur, 24px), 0.7);
16
+ box-shadow: 0 18px 45px rgba(15, 15, 15, 0.18);
17
+ }
18
+
19
+ &__slot {
20
+ border: none;
21
+ background: transparent;
22
+ padding: 0;
23
+ cursor: pointer;
24
+ display: inline-flex;
25
+ align-items: center;
26
+ justify-content: center;
27
+ width: var(--rating-size, 32px);
28
+ height: var(--rating-size, 32px);
29
+ border-radius: 999px;
30
+ transition: transform 160ms ease, filter 160ms ease;
31
+
32
+ &[data-active="true"] {
33
+ filter: drop-shadow(0 6px 12px rgba(10, 132, 255, 0.35));
34
+ }
35
+
36
+ &:focus-visible {
37
+ outline: none;
38
+ box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.35);
39
+ }
40
+ }
41
+
42
+ &[data-readonly="true"] &__slot {
43
+ cursor: default;
44
+ }
45
+
46
+ &__star {
47
+ width: 100%;
48
+ height: 100%;
49
+
50
+ svg {
51
+ width: 100%;
52
+ height: 100%;
53
+ }
54
+ }
55
+
56
+ &__helper {
57
+ font-size: 0.82rem;
58
+ color: color-mix(in srgb, currentColor, transparent 35%);
59
+ margin: 0;
60
+ }
61
+
62
+ &.size-sm {
63
+ --rating-size: 22px;
64
+ }
65
+
66
+ &.size-md {
67
+ --rating-size: 30px;
68
+ }
69
+
70
+ &.size-lg {
71
+ --rating-size: 38px;
72
+ }
73
+ }
74
+
75
+ @media (prefers-color-scheme: dark) {
76
+ .react-cupertino-ui-rating {
77
+ color: rgba(248, 250, 252, 0.95);
78
+
79
+ &__stars {
80
+ background: rgba(15, 15, 20, 0.68);
81
+ box-shadow: 0 20px 50px rgba(0, 0, 0, 0.45);
82
+ }
83
+ }
84
+ }
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@react-cupertino-ui/rating",
3
+ "version": "1.0.3",
4
+ "description": "Liquid Glass rating component",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "sideEffects": [
13
+ "./dist/**/*.css",
14
+ "./dist/**/*.scss"
15
+ ],
16
+ "scripts": {
17
+ "build": "tsc -p tsconfig.build.json && npm run build:styles",
18
+ "build:styles": "node ../../../scripts/build-styles.mjs"
19
+ },
20
+ "peerDependencies": {
21
+ "react": "^18.3.1",
22
+ "react-dom": "^18.3.1"
23
+ },
24
+ "dependencies": {
25
+ "@react-cupertino-ui/shared": "workspace:*"
26
+ },
27
+ "devDependencies": {
28
+ "typescript": "^5.2.2"
29
+ },
30
+ "gitHead": "3f53987bdb936a331665691b517c2ba9984777f8",
31
+ "publishConfig": {
32
+ "access": "public"
33
+ }
34
+ }