@watcha-authentic/react-slider 0.0.1 → 0.1.2

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,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 @edge-effect#dark1451
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 ADDED
@@ -0,0 +1,154 @@
1
+ # @watcha-authentic/react-slider
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@watcha-authentic/react-slider)](https://www.npmjs.com/package/@watcha-authentic/react-slider)
4
+
5
+ 리액트기반 슬라이더 컴포넌트입니다. 드래그/스와이프, 키보드 네비게이션을 지원하며 무한 루프 슬라이더를 제공합니다.
6
+
7
+ ## 피어 종속성
8
+
9
+ 이 패키지는 다음 패키지들을 피어 종속성으로 요구합니다.
10
+
11
+ ```bash
12
+ pnpm add react@>=18.0.0 react-dom@>=18.0.0
13
+ ```
14
+
15
+ ## 설치
16
+
17
+ ```bash
18
+ pnpm add @watcha-authentic/react-slider react@>=18.0.0 react-dom@>=18.0.0
19
+ ```
20
+
21
+ ## 사용 예
22
+
23
+ ### 기본 사용
24
+
25
+ ```tsx
26
+ import { Slider } from "@watcha-authentic/react-slider";
27
+ import "@watcha-authentic/react-slider/style.css";
28
+
29
+ const items = [
30
+ { id: 1, title: "Item 1" },
31
+ { id: 2, title: "Item 2" },
32
+ { id: 3, title: "Item 3" },
33
+ ];
34
+
35
+ function App() {
36
+ return (
37
+ <Slider
38
+ items={items}
39
+ onItemKey={(item) => item.id}
40
+ onCreateItemView={(item) => <div>{item.title}</div>}
41
+ onIndexChange={(newIndex, cause) =>
42
+ console.log("Index changed:", newIndex, cause)
43
+ }
44
+ />
45
+ );
46
+ }
47
+ ```
48
+
49
+ ### Ref를 사용한 네비게이션
50
+
51
+ `ref`를 통해 `doNext()`와 `doPrev()` 메서드를 사용하여 슬라이더를 제어할 수 있습니다.
52
+
53
+ ```tsx
54
+ import { Slider, type SliderRef } from "@watcha-authentic/react-slider";
55
+ import { useRef } from "react";
56
+
57
+ function App() {
58
+ const sliderRef = useRef<SliderRef>(null);
59
+
60
+ const handlePrev = () => {
61
+ sliderRef.current?.doPrev();
62
+ };
63
+
64
+ const handleNext = () => {
65
+ sliderRef.current?.doNext();
66
+ };
67
+
68
+ return (
69
+ <>
70
+ <button onClick={handlePrev}>Previous</button>
71
+ <button onClick={handleNext}>Next</button>
72
+ <Slider
73
+ ref={sliderRef}
74
+ items={items}
75
+ onItemKey={(item) => item.id}
76
+ onCreateItemView={(item) => <div>{item.title}</div>}
77
+ defaultIndex={0}
78
+ />
79
+ </>
80
+ );
81
+ }
82
+ ```
83
+
84
+ ### 스타일 import
85
+
86
+ CSS 스타일을 import하여 사용할 수 있습니다:
87
+
88
+ ```tsx
89
+ import "@watcha-authentic/react-slider/style.css";
90
+ ```
91
+
92
+ ### Context 사용
93
+
94
+ 아이템 내부에서 슬라이더 컨텍스트를 사용하여 포커스 상태나 전환 애니메이션을 처리할 수 있습니다.
95
+
96
+ ```tsx
97
+ import { Slider, useSliderContext } from "@watcha-authentic/react-slider";
98
+
99
+ function CustomItem({ item }: { item: { id: number; title: string } }) {
100
+ useSliderContext({
101
+ onFocus: (isAutoSlide) => {
102
+ console.log("Item focused", isAutoSlide);
103
+ },
104
+ onBlur: () => {
105
+ console.log("Item blurred");
106
+ },
107
+ onTransitionChange: (t, immediate) => {
108
+ // t: 0 ~ 1 사이의 값 (0: fade in, 1: fade out)
109
+ // immediate: true면 실시간 값 변경, false면 애니메이션 트리거 가능
110
+ },
111
+ });
112
+
113
+ return <div>{item.title}</div>;
114
+ }
115
+
116
+ function App() {
117
+ return (
118
+ <Slider
119
+ items={items}
120
+ onItemKey={(item) => item.id}
121
+ onCreateItemView={(item) => <CustomItem item={item} />}
122
+ />
123
+ );
124
+ }
125
+ ```
126
+
127
+ ## 주요 Props
128
+
129
+ ### `defaultIndex?: number`
130
+
131
+ - 초기 인덱스를 설정합니다.
132
+ - 기본값은 `0`입니다.
133
+ - **참고**: `index` prop은 제거되었습니다. 슬라이더 제어는 `ref`의 `doNext()`와 `doPrev()` 메서드를 사용하거나 `onIndexChange` 콜백을 통해 처리하세요.
134
+
135
+ ### `ref: React.Ref<SliderRef>`
136
+
137
+ - `SliderRef` 타입의 ref를 전달하면 다음 메서드에 접근할 수 있습니다:
138
+ - `doNext()`: 다음 슬라이드로 이동
139
+ - `doPrev()`: 이전 슬라이드로 이동
140
+ - 또한 `HTMLUListElement`의 모든 속성과 메서드도 사용할 수 있습니다.
141
+
142
+ ### `onIndexChange?: (newIndex: number, cause: SlideTriggerEvent) => void`
143
+
144
+ - 인덱스가 변경될 때 호출되는 콜백입니다.
145
+ - `cause`: 슬라이드 원인 (`'swipe'`: 키보드 네비게이션, `'drag'`: 드래그/스와이프, `'pending'`: 초기 상태)
146
+
147
+ ### 기타 Props
148
+
149
+ - `items`: 슬라이더에 표시할 아이템 배열
150
+ - `onCreateItemView`: 각 아이템을 렌더링하는 함수
151
+ - `onItemKey`: 각 아이템의 고유 키를 반환하는 함수
152
+ - `animationDuration`: 애니메이션 지속 시간 (기본값: 500ms)
153
+ - `enableDrag`: 드래그 기능 활성화 여부 (기본값: true)
154
+ - `visibleCount`: 중앙 기준 좌우로 보여줄 요소 개수 (기본값: 1)
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "SliderItemContextProvider", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return SliderItemContextProvider;
9
+ }
10
+ });
11
+ const _jsxruntime = require("react/jsx-runtime");
12
+ const _slidercontext = require("./slider-context");
13
+ const SliderItemContextProvider = ({ children, immediate, isFocused, itemIndex, slideTriggerEvent, transition })=>{
14
+ const value = {
15
+ immediate,
16
+ isFocused,
17
+ itemIndex,
18
+ slideTriggerEvent,
19
+ transition
20
+ };
21
+ return /*#__PURE__*/ (0, _jsxruntime.jsx)(_slidercontext.SliderItemContext.Provider, {
22
+ value: value,
23
+ children: children
24
+ });
25
+ };
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "SliderItemContext", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return SliderItemContext;
9
+ }
10
+ });
11
+ const _react = /*#__PURE__*/ _interop_require_default(require("react"));
12
+ function _interop_require_default(obj) {
13
+ return obj && obj.__esModule ? obj : {
14
+ default: obj
15
+ };
16
+ }
17
+ const SliderItemContext = /*#__PURE__*/ _react.default.createContext(null);
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "useSliderContext", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return useSliderContext;
9
+ }
10
+ });
11
+ const _reacteventcallback = require("@watcha-authentic/react-event-callback");
12
+ const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
13
+ const _slidercontext = require("../context/slider-context");
14
+ function _getRequireWildcardCache(nodeInterop) {
15
+ if (typeof WeakMap !== "function") return null;
16
+ var cacheBabelInterop = new WeakMap();
17
+ var cacheNodeInterop = new WeakMap();
18
+ return (_getRequireWildcardCache = function(nodeInterop) {
19
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
20
+ })(nodeInterop);
21
+ }
22
+ function _interop_require_wildcard(obj, nodeInterop) {
23
+ if (!nodeInterop && obj && obj.__esModule) {
24
+ return obj;
25
+ }
26
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
27
+ return {
28
+ default: obj
29
+ };
30
+ }
31
+ var cache = _getRequireWildcardCache(nodeInterop);
32
+ if (cache && cache.has(obj)) {
33
+ return cache.get(obj);
34
+ }
35
+ var newObj = {
36
+ __proto__: null
37
+ };
38
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
39
+ for(var key in obj){
40
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
41
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
42
+ if (desc && (desc.get || desc.set)) {
43
+ Object.defineProperty(newObj, key, desc);
44
+ } else {
45
+ newObj[key] = obj[key];
46
+ }
47
+ }
48
+ }
49
+ newObj.default = obj;
50
+ if (cache) {
51
+ cache.set(obj, newObj);
52
+ }
53
+ return newObj;
54
+ }
55
+ const useSliderContext = (callbacks)=>{
56
+ const context = _react.default.useContext(_slidercontext.SliderItemContext);
57
+ if (!context) {
58
+ throw new Error("useSliderContext must be used within SliderItemContextProvider");
59
+ }
60
+ const stableOnBlur = (0, _reacteventcallback.useEventCallback)(callbacks.onBlur);
61
+ const stableOnFocus = (0, _reacteventcallback.useEventCallback)(callbacks.onFocus);
62
+ const stableOnInitialState = (0, _reacteventcallback.useEventCallback)(callbacks.onInitialState);
63
+ const stableOnTransitionChange = (0, _reacteventcallback.useEventCallback)(callbacks.onTransitionChange);
64
+ const prevFocusedRef = (0, _react.useRef)(null);
65
+ /**
66
+ * - 초기 마운트 또는 상태 변경시 콜백 호출
67
+ */ (0, _react.useLayoutEffect)(()=>{
68
+ const isInitialMount = prevFocusedRef.current === null;
69
+ if (prevFocusedRef.current !== context.isFocused) {
70
+ if (isInitialMount) {
71
+ // 초기 마운트 시 onInitialState 호출
72
+ stableOnInitialState(context.isFocused);
73
+ } else {
74
+ // 상태 변경 시 onBlur/onFocus 호출
75
+ if (context.isFocused) {
76
+ stableOnFocus(context.slideTriggerEvent === "swipe");
77
+ } else {
78
+ stableOnBlur();
79
+ }
80
+ }
81
+ prevFocusedRef.current = context.isFocused;
82
+ }
83
+ }, [
84
+ context.isFocused,
85
+ context.slideTriggerEvent,
86
+ stableOnBlur,
87
+ stableOnFocus,
88
+ stableOnInitialState
89
+ ]);
90
+ /**
91
+ * - transition 값 변경 시 콜백 호출
92
+ */ (0, _react.useLayoutEffect)(()=>{
93
+ stableOnTransitionChange(context.transition, context.immediate);
94
+ }, [
95
+ context.transition,
96
+ context.immediate,
97
+ stableOnTransitionChange
98
+ ]);
99
+ };