@react-cupertino-ui/stepper 1.0.0

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,76 @@
1
+ .react-cupertino-ui-stepper {
2
+ display: inline-flex;
3
+ flex-direction: column;
4
+ gap: 0.35rem;
5
+ }
6
+ .react-cupertino-ui-stepper__label {
7
+ font-size: 0.85rem;
8
+ font-weight: 600;
9
+ color: hsl(var(--foreground));
10
+ }
11
+ .react-cupertino-ui-stepper__control {
12
+ display: inline-flex;
13
+ align-items: center;
14
+ background: transparent;
15
+ border-radius: 999px;
16
+ padding: 0.2rem;
17
+ gap: 0.25rem;
18
+ background: rgba(255, 255, 255, 0.6);
19
+ backdrop-filter: blur(28px) saturate(var(--glass-saturation, 180%));
20
+ -webkit-backdrop-filter: blur(28px) saturate(var(--glass-saturation, 180%));
21
+ border: 1px solid var(--glass-border, rgba(255, 255, 255, 0.18));
22
+ box-shadow: var(--glass-shadow, 0 8px 32px rgba(0, 0, 0, 0.12));
23
+ position: relative;
24
+ overflow: hidden;
25
+ }
26
+ .react-cupertino-ui-stepper__control::before {
27
+ content: "";
28
+ position: absolute;
29
+ inset: 0;
30
+ background: var(--glass-highlight, linear-gradient(135deg, rgba(255, 255, 255, 0.4) 0%, transparent 55%));
31
+ border-radius: inherit;
32
+ opacity: var(--glass-highlight-opacity, 0.95);
33
+ pointer-events: none;
34
+ mix-blend-mode: screen;
35
+ }
36
+ .react-cupertino-ui-stepper__button {
37
+ width: 2rem;
38
+ height: 2rem;
39
+ border-radius: 50%;
40
+ border: none;
41
+ background: rgba(255, 255, 255, 0.9);
42
+ color: #0a0a0a;
43
+ font-size: 1.2rem;
44
+ cursor: pointer;
45
+ transition: transform 120ms ease, box-shadow 160ms ease, background 160ms ease;
46
+ box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
47
+ }
48
+ .react-cupertino-ui-stepper__button:disabled {
49
+ opacity: 0.4;
50
+ cursor: not-allowed;
51
+ box-shadow: none;
52
+ }
53
+ .react-cupertino-ui-stepper__button[data-pressing=true] {
54
+ transform: scale(0.9);
55
+ background: rgba(255, 255, 255, 0.65);
56
+ box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.1);
57
+ }
58
+ .react-cupertino-ui-stepper__input {
59
+ width: 3rem;
60
+ border: none;
61
+ text-align: center;
62
+ background: transparent;
63
+ font-weight: 600;
64
+ font-size: 1rem;
65
+ color: hsl(var(--foreground));
66
+ appearance: textfield;
67
+ transition: color 220ms ease, transform 220ms ease;
68
+ }
69
+ .react-cupertino-ui-stepper__input[data-trend=up] {
70
+ color: #0a84ff;
71
+ transform: translateY(-2px);
72
+ }
73
+ .react-cupertino-ui-stepper__input[data-trend=down] {
74
+ color: #ff3b30;
75
+ transform: translateY(2px);
76
+ }
@@ -0,0 +1,15 @@
1
+ import * as React from "react";
2
+ import "./index.scss";
3
+ export interface StepperProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "onChange"> {
4
+ value?: number;
5
+ defaultValue?: number;
6
+ min?: number;
7
+ max?: number;
8
+ step?: number;
9
+ disabled?: boolean;
10
+ onChange?: (value: number) => void;
11
+ label?: string;
12
+ }
13
+ declare const Stepper: React.ForwardRefExoticComponent<StepperProps & React.RefAttributes<HTMLDivElement>>;
14
+ export { Stepper };
15
+ export default Stepper;
package/dist/index.js ADDED
@@ -0,0 +1,136 @@
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 clamp = (value, min, max) => {
6
+ if (min !== undefined && value < min)
7
+ return min;
8
+ if (max !== undefined && value > max)
9
+ return max;
10
+ return value;
11
+ };
12
+ const HOLD_DELAY = 350;
13
+ const HOLD_INTERVAL = 110;
14
+ const Stepper = React.forwardRef(({ className, value, defaultValue = 0, min, max, step = 1, disabled = false, onChange, label, ...props }, ref) => {
15
+ const isControlled = value !== undefined;
16
+ const [internalValue, setInternalValue] = React.useState(clamp(defaultValue, min, max));
17
+ const [heldButton, setHeldButton] = React.useState(null);
18
+ const [valueTrend, setValueTrend] = React.useState(null);
19
+ const currentValue = isControlled ? clamp(value, min, max) : internalValue;
20
+ const valueRef = React.useRef(currentValue);
21
+ valueRef.current = currentValue;
22
+ const holdTimeoutRef = React.useRef(null);
23
+ const holdIntervalRef = React.useRef(null);
24
+ const trendTimeoutRef = React.useRef(null);
25
+ const previousValueRef = React.useRef(currentValue);
26
+ const clearHoldTimers = React.useCallback(() => {
27
+ if (holdTimeoutRef.current) {
28
+ clearTimeout(holdTimeoutRef.current);
29
+ holdTimeoutRef.current = null;
30
+ }
31
+ if (holdIntervalRef.current) {
32
+ clearInterval(holdIntervalRef.current);
33
+ holdIntervalRef.current = null;
34
+ }
35
+ }, []);
36
+ const updateValue = React.useCallback((next) => {
37
+ const resolved = typeof next === "function" ? next(valueRef.current) : next;
38
+ const clamped = clamp(resolved, min, max);
39
+ valueRef.current = clamped;
40
+ if (!isControlled) {
41
+ setInternalValue(clamped);
42
+ }
43
+ onChange?.(clamped);
44
+ }, [isControlled, max, min, onChange]);
45
+ const changeByStep = React.useCallback((direction) => {
46
+ updateValue((prev) => prev + (direction === "increment" ? step : -step));
47
+ }, [step, updateValue]);
48
+ const handleIncrement = () => {
49
+ if (disabled)
50
+ return;
51
+ changeByStep("increment");
52
+ };
53
+ const handleDecrement = () => {
54
+ if (disabled)
55
+ return;
56
+ changeByStep("decrement");
57
+ };
58
+ const handleInputChange = (event) => {
59
+ const next = Number(event.target.value);
60
+ if (Number.isNaN(next)) {
61
+ return;
62
+ }
63
+ updateValue(next);
64
+ };
65
+ const stopHold = React.useCallback(() => {
66
+ clearHoldTimers();
67
+ setHeldButton(null);
68
+ }, [clearHoldTimers]);
69
+ const startHold = React.useCallback((direction) => {
70
+ if (disabled) {
71
+ return;
72
+ }
73
+ setHeldButton(direction);
74
+ clearHoldTimers();
75
+ holdTimeoutRef.current = setTimeout(() => {
76
+ changeByStep(direction);
77
+ holdIntervalRef.current = setInterval(() => {
78
+ changeByStep(direction);
79
+ }, HOLD_INTERVAL);
80
+ }, HOLD_DELAY);
81
+ }, [changeByStep, clearHoldTimers, disabled]);
82
+ React.useEffect(() => {
83
+ return () => {
84
+ clearHoldTimers();
85
+ if (trendTimeoutRef.current) {
86
+ clearTimeout(trendTimeoutRef.current);
87
+ }
88
+ };
89
+ }, [clearHoldTimers]);
90
+ React.useEffect(() => {
91
+ const previous = previousValueRef.current;
92
+ if (previous !== currentValue) {
93
+ const trend = currentValue > previous ? "up" : "down";
94
+ setValueTrend(trend);
95
+ if (trendTimeoutRef.current) {
96
+ clearTimeout(trendTimeoutRef.current);
97
+ }
98
+ trendTimeoutRef.current = setTimeout(() => {
99
+ setValueTrend(null);
100
+ }, 420);
101
+ }
102
+ previousValueRef.current = currentValue;
103
+ }, [currentValue]);
104
+ React.useEffect(() => {
105
+ if (!heldButton) {
106
+ return;
107
+ }
108
+ const handlePointerUp = () => {
109
+ stopHold();
110
+ };
111
+ window.addEventListener("pointerup", handlePointerUp);
112
+ window.addEventListener("pointercancel", handlePointerUp);
113
+ return () => {
114
+ window.removeEventListener("pointerup", handlePointerUp);
115
+ window.removeEventListener("pointercancel", handlePointerUp);
116
+ };
117
+ }, [heldButton, stopHold]);
118
+ const getPointerHandlers = (direction) => {
119
+ return {
120
+ onPointerDown: (event) => {
121
+ if (event.button && event.button !== 0) {
122
+ return;
123
+ }
124
+ event.preventDefault();
125
+ startHold(direction);
126
+ },
127
+ onPointerLeave: stopHold,
128
+ onPointerCancel: stopHold,
129
+ onPointerUp: () => stopHold(),
130
+ };
131
+ };
132
+ return (_jsxs("div", { ref: ref, className: cn("react-cupertino-ui-stepper", className), "data-disabled": disabled ? "true" : undefined, ...props, children: [label && _jsx("span", { className: "react-cupertino-ui-stepper__label", children: label }), _jsxs("div", { className: "react-cupertino-ui-stepper__control", children: [_jsx("button", { type: "button", className: "react-cupertino-ui-stepper__button", onClick: handleDecrement, disabled: disabled || (min !== undefined && currentValue <= min), "aria-label": "Decrease value", "data-pressing": heldButton === "decrement" ? "true" : undefined, ...getPointerHandlers("decrement"), children: "\u2013" }), _jsx("input", { type: "number", className: "react-cupertino-ui-stepper__input", value: currentValue, "data-trend": valueTrend ?? undefined, "aria-live": "polite", onChange: handleInputChange, min: min, max: max, step: step, disabled: disabled }), _jsx("button", { type: "button", className: "react-cupertino-ui-stepper__button", onClick: handleIncrement, disabled: disabled || (max !== undefined && currentValue >= max), "aria-label": "Increase value", "data-pressing": heldButton === "increment" ? "true" : undefined, ...getPointerHandlers("increment"), children: "+" })] })] }));
133
+ });
134
+ Stepper.displayName = "Stepper";
135
+ export { Stepper };
136
+ export default Stepper;
@@ -0,0 +1,70 @@
1
+ @use "@/styles/mixins/glass" as glass;
2
+
3
+ .react-cupertino-ui-stepper {
4
+ display: inline-flex;
5
+ flex-direction: column;
6
+ gap: 0.35rem;
7
+
8
+ &__label {
9
+ font-size: 0.85rem;
10
+ font-weight: 600;
11
+ color: hsl(var(--foreground));
12
+ }
13
+
14
+ &__control {
15
+ display: inline-flex;
16
+ align-items: center;
17
+ background: transparent;
18
+ border-radius: 999px;
19
+ padding: 0.2rem;
20
+ gap: 0.25rem;
21
+ @include glass.glass-panel(light, 28px, 0.6);
22
+ }
23
+
24
+ &__button {
25
+ width: 2rem;
26
+ height: 2rem;
27
+ border-radius: 50%;
28
+ border: none;
29
+ background: rgba(255, 255, 255, 0.9);
30
+ color: #0a0a0a;
31
+ font-size: 1.2rem;
32
+ cursor: pointer;
33
+ transition: transform 120ms ease, box-shadow 160ms ease, background 160ms ease;
34
+ box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
35
+
36
+ &:disabled {
37
+ opacity: 0.4;
38
+ cursor: not-allowed;
39
+ box-shadow: none;
40
+ }
41
+
42
+ &[data-pressing="true"] {
43
+ transform: scale(0.9);
44
+ background: rgba(255, 255, 255, 0.65);
45
+ box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.1);
46
+ }
47
+ }
48
+
49
+ &__input {
50
+ width: 3rem;
51
+ border: none;
52
+ text-align: center;
53
+ background: transparent;
54
+ font-weight: 600;
55
+ font-size: 1rem;
56
+ color: hsl(var(--foreground));
57
+ appearance: textfield;
58
+ transition: color 220ms ease, transform 220ms ease;
59
+
60
+ &[data-trend="up"] {
61
+ color: #0a84ff;
62
+ transform: translateY(-2px);
63
+ }
64
+
65
+ &[data-trend="down"] {
66
+ color: #ff3b30;
67
+ transform: translateY(2px);
68
+ }
69
+ }
70
+ }
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@react-cupertino-ui/stepper",
3
+ "version": "1.0.0",
4
+ "description": "Stepper component from React Cupertino UI",
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
+ }