@react-cupertino-ui/picker 0.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 +24 -0
- package/dist/index.css +63 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +54 -0
- package/dist/index.scss +70 -0
- package/package.json +34 -0
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,63 @@
|
|
|
1
|
+
.react-cupertino-ui-picker {
|
|
2
|
+
position: relative;
|
|
3
|
+
width: 100%;
|
|
4
|
+
overflow: hidden;
|
|
5
|
+
background: transparent;
|
|
6
|
+
user-select: none;
|
|
7
|
+
touch-action: pan-y;
|
|
8
|
+
}
|
|
9
|
+
.react-cupertino-ui-picker__scroller {
|
|
10
|
+
width: 100%;
|
|
11
|
+
overflow-y: auto;
|
|
12
|
+
overflow-x: hidden;
|
|
13
|
+
scroll-snap-type: y mandatory;
|
|
14
|
+
scrollbar-width: none;
|
|
15
|
+
}
|
|
16
|
+
.react-cupertino-ui-picker__scroller::-webkit-scrollbar {
|
|
17
|
+
display: none;
|
|
18
|
+
}
|
|
19
|
+
.react-cupertino-ui-picker__item {
|
|
20
|
+
width: 100%;
|
|
21
|
+
text-align: center;
|
|
22
|
+
font-size: 20px;
|
|
23
|
+
color: var(--foreground);
|
|
24
|
+
white-space: nowrap;
|
|
25
|
+
overflow: hidden;
|
|
26
|
+
text-overflow: ellipsis;
|
|
27
|
+
scroll-snap-align: center;
|
|
28
|
+
transform: translateZ(0);
|
|
29
|
+
}
|
|
30
|
+
.react-cupertino-ui-picker__selection-indicator {
|
|
31
|
+
position: absolute;
|
|
32
|
+
top: 50%;
|
|
33
|
+
left: 0;
|
|
34
|
+
right: 0;
|
|
35
|
+
pointer-events: none;
|
|
36
|
+
background: rgba(118, 118, 128, 0.12);
|
|
37
|
+
border-radius: 8px;
|
|
38
|
+
z-index: 1;
|
|
39
|
+
}
|
|
40
|
+
@media (prefers-color-scheme: dark) {
|
|
41
|
+
.react-cupertino-ui-picker__selection-indicator {
|
|
42
|
+
background: rgba(255, 255, 255, 0.12);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
.react-cupertino-ui-picker__mask-top, .react-cupertino-ui-picker__mask-bottom {
|
|
46
|
+
position: absolute;
|
|
47
|
+
left: 0;
|
|
48
|
+
right: 0;
|
|
49
|
+
z-index: 2;
|
|
50
|
+
pointer-events: none;
|
|
51
|
+
}
|
|
52
|
+
.react-cupertino-ui-picker__mask-top {
|
|
53
|
+
top: 0;
|
|
54
|
+
height: 40%;
|
|
55
|
+
background: linear-gradient(to bottom, var(--background) 0%, transparent 100%);
|
|
56
|
+
opacity: 0.9;
|
|
57
|
+
}
|
|
58
|
+
.react-cupertino-ui-picker__mask-bottom {
|
|
59
|
+
bottom: 0;
|
|
60
|
+
height: 40%;
|
|
61
|
+
background: linear-gradient(to top, var(--background) 0%, transparent 100%);
|
|
62
|
+
opacity: 0.9;
|
|
63
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import "./index.scss";
|
|
3
|
+
export interface PickerOption {
|
|
4
|
+
value: string | number;
|
|
5
|
+
label: string;
|
|
6
|
+
}
|
|
7
|
+
export interface PickerProps {
|
|
8
|
+
options: PickerOption[];
|
|
9
|
+
value: string | number;
|
|
10
|
+
onChange: (value: string | number) => void;
|
|
11
|
+
className?: string;
|
|
12
|
+
itemHeight?: number;
|
|
13
|
+
visibleItems?: number;
|
|
14
|
+
}
|
|
15
|
+
declare const Picker: React.ForwardRefExoticComponent<PickerProps & React.RefAttributes<HTMLDivElement>>;
|
|
16
|
+
export { Picker };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
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 Picker = React.forwardRef(({ options, value, onChange, className, itemHeight = 34, visibleItems = 5, ...props }, ref) => {
|
|
6
|
+
const scrollerRef = React.useRef(null);
|
|
7
|
+
const isScrolling = React.useRef(false);
|
|
8
|
+
const scrollTimeout = React.useRef();
|
|
9
|
+
// Initial scroll
|
|
10
|
+
React.useLayoutEffect(() => {
|
|
11
|
+
const index = options.findIndex((o) => o.value === value);
|
|
12
|
+
if (index !== -1 && scrollerRef.current) {
|
|
13
|
+
scrollerRef.current.scrollTop = index * itemHeight;
|
|
14
|
+
}
|
|
15
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
16
|
+
}, []); // Only on mount
|
|
17
|
+
// Update scroll when value changes externally (and not scrolling)
|
|
18
|
+
React.useEffect(() => {
|
|
19
|
+
if (isScrolling.current)
|
|
20
|
+
return;
|
|
21
|
+
const index = options.findIndex((o) => o.value === value);
|
|
22
|
+
if (index !== -1 && scrollerRef.current) {
|
|
23
|
+
// Check if already correct to avoid jitter
|
|
24
|
+
const targetScroll = index * itemHeight;
|
|
25
|
+
if (Math.abs(scrollerRef.current.scrollTop - targetScroll) > 1) {
|
|
26
|
+
scrollerRef.current.scrollTo({ top: targetScroll, behavior: 'smooth' });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}, [value, options, itemHeight]);
|
|
30
|
+
const handleScroll = (e) => {
|
|
31
|
+
isScrolling.current = true;
|
|
32
|
+
clearTimeout(scrollTimeout.current);
|
|
33
|
+
const scrollTop = e.currentTarget.scrollTop;
|
|
34
|
+
scrollTimeout.current = setTimeout(() => {
|
|
35
|
+
isScrolling.current = false;
|
|
36
|
+
// Snap logic handled by CSS mostly, but we need to update value
|
|
37
|
+
const index = Math.round(scrollTop / itemHeight);
|
|
38
|
+
const clampedIndex = Math.min(Math.max(index, 0), options.length - 1);
|
|
39
|
+
const option = options[clampedIndex];
|
|
40
|
+
if (option && option.value !== value) {
|
|
41
|
+
onChange(option.value);
|
|
42
|
+
}
|
|
43
|
+
// Ensure perfect snap
|
|
44
|
+
// e.currentTarget.scrollTo({ top: clampedIndex * itemHeight, behavior: 'smooth' });
|
|
45
|
+
}, 100);
|
|
46
|
+
};
|
|
47
|
+
return (_jsxs("div", { ref: ref, className: cn("react-cupertino-ui-picker", className), style: { height: itemHeight * visibleItems }, ...props, children: [_jsx("div", { className: "react-cupertino-ui-picker__mask-top" }), _jsx("div", { className: "react-cupertino-ui-picker__selection-indicator", style: { height: itemHeight, marginTop: -itemHeight / 2 } }), _jsx("div", { ref: scrollerRef, className: "react-cupertino-ui-picker__scroller", onScroll: handleScroll, style: {
|
|
48
|
+
height: itemHeight * visibleItems,
|
|
49
|
+
paddingTop: (itemHeight * visibleItems) / 2 - itemHeight / 2,
|
|
50
|
+
paddingBottom: (itemHeight * visibleItems) / 2 - itemHeight / 2
|
|
51
|
+
}, children: options.map((option) => (_jsx("div", { className: "react-cupertino-ui-picker__item", style: { height: itemHeight, lineHeight: `${itemHeight}px` }, children: option.label }, option.value))) }), _jsx("div", { className: "react-cupertino-ui-picker__mask-bottom" })] }));
|
|
52
|
+
});
|
|
53
|
+
Picker.displayName = "Picker";
|
|
54
|
+
export { Picker };
|
package/dist/index.scss
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
.react-cupertino-ui-picker {
|
|
2
|
+
position: relative;
|
|
3
|
+
width: 100%;
|
|
4
|
+
overflow: hidden;
|
|
5
|
+
background: transparent;
|
|
6
|
+
user-select: none;
|
|
7
|
+
touch-action: pan-y;
|
|
8
|
+
|
|
9
|
+
&__scroller {
|
|
10
|
+
width: 100%;
|
|
11
|
+
overflow-y: auto;
|
|
12
|
+
overflow-x: hidden;
|
|
13
|
+
scroll-snap-type: y mandatory;
|
|
14
|
+
scrollbar-width: none; // Firefox
|
|
15
|
+
|
|
16
|
+
&::-webkit-scrollbar {
|
|
17
|
+
display: none;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
&__item {
|
|
22
|
+
width: 100%;
|
|
23
|
+
text-align: center;
|
|
24
|
+
font-size: 20px;
|
|
25
|
+
color: var(--foreground);
|
|
26
|
+
white-space: nowrap;
|
|
27
|
+
overflow: hidden;
|
|
28
|
+
text-overflow: ellipsis;
|
|
29
|
+
scroll-snap-align: center;
|
|
30
|
+
transform: translateZ(0); // Hardware accel
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
&__selection-indicator {
|
|
34
|
+
position: absolute;
|
|
35
|
+
top: 50%;
|
|
36
|
+
left: 0;
|
|
37
|
+
right: 0;
|
|
38
|
+
pointer-events: none;
|
|
39
|
+
background: rgba(118, 118, 128, 0.12);
|
|
40
|
+
border-radius: 8px;
|
|
41
|
+
z-index: 1;
|
|
42
|
+
|
|
43
|
+
@media (prefers-color-scheme: dark) {
|
|
44
|
+
background: rgba(255, 255, 255, 0.12);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
&__mask-top,
|
|
49
|
+
&__mask-bottom {
|
|
50
|
+
position: absolute;
|
|
51
|
+
left: 0;
|
|
52
|
+
right: 0;
|
|
53
|
+
z-index: 2;
|
|
54
|
+
pointer-events: none;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
&__mask-top {
|
|
58
|
+
top: 0;
|
|
59
|
+
height: 40%;
|
|
60
|
+
background: linear-gradient(to bottom, var(--background) 0%, transparent 100%);
|
|
61
|
+
opacity: 0.9;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
&__mask-bottom {
|
|
65
|
+
bottom: 0;
|
|
66
|
+
height: 40%;
|
|
67
|
+
background: linear-gradient(to top, var(--background) 0%, transparent 100%);
|
|
68
|
+
opacity: 0.9;
|
|
69
|
+
}
|
|
70
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@react-cupertino-ui/picker",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Picker 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": "0.0.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"typescript": "^5.2.2"
|
|
29
|
+
},
|
|
30
|
+
"gitHead": "a6b07a7a8a0ab6f4c80884b6fb9d5eb16ee6da1d",
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
}
|
|
34
|
+
}
|