@futaba-soft/turnbox-react 0.0.0-stage → 0.5.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 +22 -0
- package/README.md +119 -2
- package/dist/index.d.ts +55 -0
- package/dist/index.js +412 -0
- package/package.json +56 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 futaba-soft
|
|
4
|
+
Based on turnBox.js (https://github.com/nohtcoltd/turnbox_js) by NOHT CO.,LTD., MIT licensed.
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,120 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @futaba-soft/turnbox-react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React adapter for TURNBOX.js. Compound component and `useTurnBox` hook.
|
|
4
|
+
|
|
5
|
+
Requires React 18 or later.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @futaba-soft/turnbox-react
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Compound Component
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { TurnBox } from "@futaba-soft/turnbox-react";
|
|
17
|
+
|
|
18
|
+
const FlipCard = () => (
|
|
19
|
+
<TurnBox.Root faces={2} duration={400}>
|
|
20
|
+
<TurnBox.Face>
|
|
21
|
+
Front<TurnBox.Button>Flip</TurnBox.Button>
|
|
22
|
+
</TurnBox.Face>
|
|
23
|
+
<TurnBox.Face>
|
|
24
|
+
<TurnBox.Button direction="prev">Flip back</TurnBox.Button>Back
|
|
25
|
+
</TurnBox.Face>
|
|
26
|
+
</TurnBox.Root>
|
|
27
|
+
);
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### External control via ref
|
|
31
|
+
|
|
32
|
+
```tsx
|
|
33
|
+
import { useRef } from "react";
|
|
34
|
+
import { TurnBox, type TurnBoxRootHandle } from "@futaba-soft/turnbox-react";
|
|
35
|
+
|
|
36
|
+
const FlipCard = () => {
|
|
37
|
+
const ref = useRef<TurnBoxRootHandle>(null);
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<>
|
|
41
|
+
<TurnBox.Root faces={4} duration={400} ref={ref}>
|
|
42
|
+
<TurnBox.Face>Face 1</TurnBox.Face>
|
|
43
|
+
<TurnBox.Face>Face 2</TurnBox.Face>
|
|
44
|
+
<TurnBox.Face>Face 3</TurnBox.Face>
|
|
45
|
+
<TurnBox.Face>Face 4</TurnBox.Face>
|
|
46
|
+
</TurnBox.Root>
|
|
47
|
+
<button onClick={() => ref.current?.next()}>Next</button>
|
|
48
|
+
<button onClick={() => ref.current?.prev()}>Back</button>
|
|
49
|
+
</>
|
|
50
|
+
);
|
|
51
|
+
};
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### TurnBoxRootHandle
|
|
55
|
+
|
|
56
|
+
| Method | Description |
|
|
57
|
+
| --- | --- |
|
|
58
|
+
| `next()` | Go to the next face |
|
|
59
|
+
| `prev()` | Go to the previous face |
|
|
60
|
+
| `goTo(face, animation?)` | Go to the specified face (`animation` defaults to `true`) |
|
|
61
|
+
| `getCurrentFace()` | Current face number (1-based) |
|
|
62
|
+
| `isAnimating()` | Whether an animation is in progress |
|
|
63
|
+
|
|
64
|
+
### TurnBox.Button props
|
|
65
|
+
|
|
66
|
+
| Prop | Type | Default | Description |
|
|
67
|
+
| --- | --- | --- | --- |
|
|
68
|
+
| `direction` | `"next" \| "prev"` | `"next"` | Go to the next or previous face |
|
|
69
|
+
| `to` | `number` | | Jump to a specific face (takes priority over `direction`) |
|
|
70
|
+
|
|
71
|
+
Forwards standard props (`className`, `style`, etc.) to the native `<button>`.
|
|
72
|
+
|
|
73
|
+
## useTurnBox hook
|
|
74
|
+
|
|
75
|
+
```tsx
|
|
76
|
+
import { useTurnBox } from "@futaba-soft/turnbox-react";
|
|
77
|
+
|
|
78
|
+
const FlipCard = () => {
|
|
79
|
+
const { containerRef, currentFace, isAnimating, next, prev, goTo } = useTurnBox({
|
|
80
|
+
faces: 2,
|
|
81
|
+
duration: 400,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
return (
|
|
85
|
+
<div ref={containerRef}>
|
|
86
|
+
<div>Front</div>
|
|
87
|
+
<div>Back</div>
|
|
88
|
+
</div>
|
|
89
|
+
);
|
|
90
|
+
};
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### UseTurnBoxReturn
|
|
94
|
+
|
|
95
|
+
| Field | Type | Description |
|
|
96
|
+
| --- | --- | --- |
|
|
97
|
+
| `containerRef` | `RefObject<HTMLElement>` | Attach to the container element |
|
|
98
|
+
| `currentFace` | `number` | Current face number (1-based) |
|
|
99
|
+
| `isAnimating` | `boolean` | Whether an animation is in progress |
|
|
100
|
+
| `next()` | `() => void` | Go to the next face |
|
|
101
|
+
| `prev()` | `() => void` | Go to the previous face |
|
|
102
|
+
| `goTo(face, animation?)` | `(face: number, animation?: boolean) => void` | Go to the specified face |
|
|
103
|
+
|
|
104
|
+
## TurnBox.Provider
|
|
105
|
+
|
|
106
|
+
Use `TurnBox.Provider` to override [`reduceAnimation`](../../README.md#reduceanimation--accessibility-setting) for a subtree. Without a Provider the default is `"system setting"`.
|
|
107
|
+
|
|
108
|
+
```tsx
|
|
109
|
+
<TurnBox.Provider reduceAnimation="never">
|
|
110
|
+
<App />
|
|
111
|
+
</TurnBox.Provider>
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Options
|
|
115
|
+
|
|
116
|
+
See [shared options](../../README.md#options).
|
|
117
|
+
|
|
118
|
+
## License
|
|
119
|
+
|
|
120
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import * as _futaba_soft_turnbox_core from '@futaba-soft/turnbox-core';
|
|
2
|
+
import { TurnBoxOptions } from '@futaba-soft/turnbox-core';
|
|
3
|
+
export { TurnBoxOptions } from '@futaba-soft/turnbox-core';
|
|
4
|
+
import * as React$1 from 'react';
|
|
5
|
+
import React__default from 'react';
|
|
6
|
+
|
|
7
|
+
type UseTurnBoxReturn = {
|
|
8
|
+
containerRef: React.RefObject<HTMLElement | null>;
|
|
9
|
+
currentFace: number;
|
|
10
|
+
isAnimating: boolean;
|
|
11
|
+
goTo(face: number, animation?: boolean): void;
|
|
12
|
+
next(): void;
|
|
13
|
+
prev(): void;
|
|
14
|
+
};
|
|
15
|
+
declare const useTurnBox: (options: TurnBoxOptions) => UseTurnBoxReturn;
|
|
16
|
+
|
|
17
|
+
type ButtonProps = {
|
|
18
|
+
children?: React.ReactNode;
|
|
19
|
+
direction?: "next" | "prev";
|
|
20
|
+
to?: number;
|
|
21
|
+
} & Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "onClick" | "type">;
|
|
22
|
+
|
|
23
|
+
type FaceProps = React__default.HTMLAttributes<HTMLDivElement>;
|
|
24
|
+
|
|
25
|
+
type TurnBoxRootHandle = {
|
|
26
|
+
goTo(face: number, animation?: boolean): void;
|
|
27
|
+
getCurrentFace(): number;
|
|
28
|
+
isAnimating(): boolean;
|
|
29
|
+
next(): void;
|
|
30
|
+
prev(): void;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
declare const TurnBox: {
|
|
34
|
+
Root: React$1.ForwardRefExoticComponent<_futaba_soft_turnbox_core.TurnBoxOptions & React$1.AriaAttributes & {
|
|
35
|
+
children?: React.ReactNode;
|
|
36
|
+
className?: string;
|
|
37
|
+
style?: React.CSSProperties;
|
|
38
|
+
} & React$1.RefAttributes<TurnBoxRootHandle>>;
|
|
39
|
+
Face: {
|
|
40
|
+
({ children, className, style, _faceIndex, ...rest }: FaceProps & {
|
|
41
|
+
_faceIndex?: number;
|
|
42
|
+
}): React$1.JSX.Element;
|
|
43
|
+
displayName: string;
|
|
44
|
+
};
|
|
45
|
+
Button: {
|
|
46
|
+
({ direction, to, children, ...rest }: ButtonProps): React$1.JSX.Element;
|
|
47
|
+
displayName: string;
|
|
48
|
+
};
|
|
49
|
+
Provider: ({ reduceAnimation, children }: {
|
|
50
|
+
reduceAnimation?: _futaba_soft_turnbox_core.ReduceAnimation;
|
|
51
|
+
children?: React.ReactNode;
|
|
52
|
+
}) => React.ReactElement;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export { type ButtonProps, type FaceProps, TurnBox, type TurnBoxRootHandle, type UseTurnBoxReturn, useTurnBox };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
// src/useTurnBox.ts
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
3
|
+
import { createTurnBox } from "@futaba-soft/turnbox-dom";
|
|
4
|
+
|
|
5
|
+
// src/TurnBox/ConfigContext.ts
|
|
6
|
+
import { createContext, useContext } from "react";
|
|
7
|
+
var DEFAULT_CONFIG = { reduceAnimation: "system setting" };
|
|
8
|
+
var TurnBoxConfigContext = createContext(DEFAULT_CONFIG);
|
|
9
|
+
var useTurnBoxConfig = () => useContext(TurnBoxConfigContext);
|
|
10
|
+
|
|
11
|
+
// src/useTurnBox.ts
|
|
12
|
+
var useTurnBox = (options) => {
|
|
13
|
+
const config = useTurnBoxConfig();
|
|
14
|
+
const { faces, axis, direction, type, duration, delay, easing, perspective, width, height, even } = options;
|
|
15
|
+
const containerRef = useRef(null);
|
|
16
|
+
const instanceRef = useRef(null);
|
|
17
|
+
const [currentFace, setCurrentFace] = useState(1);
|
|
18
|
+
const [isAnimating, setIsAnimating] = useState(false);
|
|
19
|
+
const onChangeRef = useRef(options.onChange);
|
|
20
|
+
const onAnimationEndRef = useRef(options.onAnimationEnd);
|
|
21
|
+
onChangeRef.current = options.onChange;
|
|
22
|
+
onAnimationEndRef.current = options.onAnimationEnd;
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
const el = containerRef.current;
|
|
25
|
+
if (!el) return;
|
|
26
|
+
const instance = createTurnBox(el, {
|
|
27
|
+
faces,
|
|
28
|
+
axis,
|
|
29
|
+
direction,
|
|
30
|
+
type,
|
|
31
|
+
duration,
|
|
32
|
+
delay,
|
|
33
|
+
easing,
|
|
34
|
+
perspective,
|
|
35
|
+
width,
|
|
36
|
+
height,
|
|
37
|
+
even,
|
|
38
|
+
reduceAnimation: config.reduceAnimation,
|
|
39
|
+
onChange: (face) => {
|
|
40
|
+
setCurrentFace(face);
|
|
41
|
+
setIsAnimating(true);
|
|
42
|
+
onChangeRef.current?.(face);
|
|
43
|
+
},
|
|
44
|
+
onAnimationEnd: (face) => {
|
|
45
|
+
setIsAnimating(false);
|
|
46
|
+
onAnimationEndRef.current?.(face);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
instanceRef.current = instance;
|
|
50
|
+
setCurrentFace(instance.getCurrentFace());
|
|
51
|
+
return () => {
|
|
52
|
+
instance.destroy();
|
|
53
|
+
instanceRef.current = null;
|
|
54
|
+
};
|
|
55
|
+
}, [faces, axis, direction, type, duration, delay, easing, perspective, width, height, even, config.reduceAnimation]);
|
|
56
|
+
const goTo = useCallback((face, animation = true) => {
|
|
57
|
+
instanceRef.current?.goTo(face, animation);
|
|
58
|
+
}, []);
|
|
59
|
+
const next = useCallback(() => {
|
|
60
|
+
instanceRef.current?.next();
|
|
61
|
+
}, []);
|
|
62
|
+
const prev = useCallback(() => {
|
|
63
|
+
instanceRef.current?.prev();
|
|
64
|
+
}, []);
|
|
65
|
+
return { containerRef, currentFace, isAnimating, goTo, next, prev };
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// src/TurnBox/Root.tsx
|
|
69
|
+
import React, { useCallback as useCallback3, useEffect as useEffect2, useImperativeHandle, useMemo, useReducer, useRef as useRef2, useState as useState2 } from "react";
|
|
70
|
+
import { normalizeOptions, DEFAULT_SIZE, DEFAULT_HEIGHT } from "@futaba-soft/turnbox-core";
|
|
71
|
+
import { FOCUSABLE, planNavigation, buildDrainResult } from "@futaba-soft/turnbox-core/internal";
|
|
72
|
+
|
|
73
|
+
// src/TurnBox/context.ts
|
|
74
|
+
import { createContext as createContext2, useContext as useContext2 } from "react";
|
|
75
|
+
var TurnBoxContext = createContext2(null);
|
|
76
|
+
var useTurnBoxContext = () => {
|
|
77
|
+
const ctx = useContext2(TurnBoxContext);
|
|
78
|
+
if (!ctx) throw new Error("TurnBox components must be used within <TurnBox.Root>");
|
|
79
|
+
return ctx;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// src/TurnBox/Face.tsx
|
|
83
|
+
import { useCallback as useCallback2 } from "react";
|
|
84
|
+
import { calcFaceTransform } from "@futaba-soft/turnbox-core";
|
|
85
|
+
|
|
86
|
+
// src/TurnBox/utils.ts
|
|
87
|
+
var toTransformString = (ft) => `rotate${ft.axis}(${ft.deg}deg) translate3d(${ft.x}px, ${ft.y}px, ${ft.z}px)`;
|
|
88
|
+
|
|
89
|
+
// src/TurnBox/Face.tsx
|
|
90
|
+
import { jsx } from "react/jsx-runtime";
|
|
91
|
+
var faceDimStyle = (faceIndex, opts) => {
|
|
92
|
+
const { geometry } = opts;
|
|
93
|
+
if (geometry.kind !== "variable") return { inset: 0 };
|
|
94
|
+
const isEven = faceIndex % 2 === 0;
|
|
95
|
+
if (geometry.axis === "X") {
|
|
96
|
+
return { width: "100%", height: isEven ? geometry.even : geometry.length };
|
|
97
|
+
}
|
|
98
|
+
return { width: isEven ? geometry.even : geometry.length, height: "100%" };
|
|
99
|
+
};
|
|
100
|
+
var hasTransition = (phase) => phase.kind === "animating";
|
|
101
|
+
var Face = ({ children, className, style, _faceIndex = 0, ...rest }) => {
|
|
102
|
+
const { opts, displayFace, phase, shownFaces } = useTurnBoxContext();
|
|
103
|
+
const ft = calcFaceTransform(displayFace, _faceIndex, opts);
|
|
104
|
+
const transformStr = toTransformString(ft);
|
|
105
|
+
const faceStyle = {
|
|
106
|
+
...style,
|
|
107
|
+
position: "absolute",
|
|
108
|
+
...faceDimStyle(_faceIndex, opts),
|
|
109
|
+
transform: transformStr,
|
|
110
|
+
transformOrigin: ft?.transformOrigin ?? "center",
|
|
111
|
+
zIndex: ft?.zIndex ?? 0,
|
|
112
|
+
opacity: shownFaces.has(_faceIndex) ? 1 : 0,
|
|
113
|
+
backfaceVisibility: "hidden",
|
|
114
|
+
transition: hasTransition(phase) ? `transform ${opts.duration}ms ${opts.easing} ${opts.delay}ms` : void 0
|
|
115
|
+
};
|
|
116
|
+
const isShown = shownFaces.has(_faceIndex);
|
|
117
|
+
const inertRef = useCallback2(
|
|
118
|
+
(el) => {
|
|
119
|
+
if (el) el.inert = !isShown;
|
|
120
|
+
},
|
|
121
|
+
[isShown]
|
|
122
|
+
);
|
|
123
|
+
return /* @__PURE__ */ jsx(
|
|
124
|
+
"div",
|
|
125
|
+
{
|
|
126
|
+
...rest,
|
|
127
|
+
ref: inertRef,
|
|
128
|
+
"data-face-index": _faceIndex,
|
|
129
|
+
className,
|
|
130
|
+
style: faceStyle,
|
|
131
|
+
"aria-hidden": isShown ? void 0 : true,
|
|
132
|
+
children
|
|
133
|
+
}
|
|
134
|
+
);
|
|
135
|
+
};
|
|
136
|
+
Face.displayName = "TurnBox.Face";
|
|
137
|
+
|
|
138
|
+
// src/TurnBox/stateMachine.ts
|
|
139
|
+
import {
|
|
140
|
+
INITIAL_STATE,
|
|
141
|
+
reducer,
|
|
142
|
+
toPhase,
|
|
143
|
+
buildGoStepAction,
|
|
144
|
+
buildGoInstantAction
|
|
145
|
+
} from "@futaba-soft/turnbox-core/internal";
|
|
146
|
+
|
|
147
|
+
// src/TurnBox/Root.tsx
|
|
148
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
149
|
+
var calcContainerDynStyle = (state, opts) => {
|
|
150
|
+
const { geometry } = opts;
|
|
151
|
+
if (geometry.kind !== "variable") return {};
|
|
152
|
+
const isEven = state.displayFace % 2 === 0;
|
|
153
|
+
const isAnimating = state.kind === "animating";
|
|
154
|
+
const transition = isAnimating ? `${geometry.axis === "X" ? "height" : "left"} ${opts.duration}ms ${opts.easing} ${opts.delay}ms` : void 0;
|
|
155
|
+
return geometry.axis === "X" ? { height: isEven ? geometry.even : geometry.length, transition } : { left: isEven ? (geometry.length - geometry.even) / 2 : 0, transition };
|
|
156
|
+
};
|
|
157
|
+
var buildIndexedChildren = (children, maxFaces) => {
|
|
158
|
+
let faceCount = 0;
|
|
159
|
+
return React.Children.map(children, (child) => {
|
|
160
|
+
if (React.isValidElement(child) && child.type === Face) {
|
|
161
|
+
faceCount++;
|
|
162
|
+
if (faceCount > maxFaces) return null;
|
|
163
|
+
return React.cloneElement(child, {
|
|
164
|
+
_faceIndex: faceCount
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
return child;
|
|
168
|
+
});
|
|
169
|
+
};
|
|
170
|
+
var Root = React.forwardRef(
|
|
171
|
+
({
|
|
172
|
+
faces,
|
|
173
|
+
axis,
|
|
174
|
+
direction,
|
|
175
|
+
type,
|
|
176
|
+
duration,
|
|
177
|
+
delay,
|
|
178
|
+
easing,
|
|
179
|
+
perspective,
|
|
180
|
+
width,
|
|
181
|
+
height,
|
|
182
|
+
even,
|
|
183
|
+
onChange,
|
|
184
|
+
onAnimationEnd,
|
|
185
|
+
children,
|
|
186
|
+
className,
|
|
187
|
+
style,
|
|
188
|
+
"aria-label": ariaLabel,
|
|
189
|
+
...ariaRest
|
|
190
|
+
}, ref) => {
|
|
191
|
+
const config = useTurnBoxConfig();
|
|
192
|
+
const effectiveReduceAnimation = config.reduceAnimation;
|
|
193
|
+
const [state, dispatch] = useReducer(reducer, INITIAL_STATE);
|
|
194
|
+
const stateRef = useRef2(state);
|
|
195
|
+
stateRef.current = state;
|
|
196
|
+
const pendingTimers = useRef2([]);
|
|
197
|
+
const onChangeRef = useRef2(onChange);
|
|
198
|
+
const onAnimationEndRef = useRef2(onAnimationEnd);
|
|
199
|
+
onChangeRef.current = onChange;
|
|
200
|
+
onAnimationEndRef.current = onAnimationEnd;
|
|
201
|
+
const boxRef = useRef2(null);
|
|
202
|
+
const dispatchAndSync = useCallback3((action) => {
|
|
203
|
+
stateRef.current = reducer(stateRef.current, action);
|
|
204
|
+
dispatch(action);
|
|
205
|
+
}, []);
|
|
206
|
+
const cancelFaceAnimations = useCallback3(() => {
|
|
207
|
+
const box = boxRef.current;
|
|
208
|
+
if (!box) return;
|
|
209
|
+
for (const el of box.querySelectorAll("[data-face-index]")) {
|
|
210
|
+
for (const anim of el.getAnimations?.() ?? []) anim.cancel();
|
|
211
|
+
}
|
|
212
|
+
}, []);
|
|
213
|
+
const opts = useMemo(() => {
|
|
214
|
+
const base = normalizeOptions({
|
|
215
|
+
faces,
|
|
216
|
+
axis,
|
|
217
|
+
direction,
|
|
218
|
+
type,
|
|
219
|
+
duration,
|
|
220
|
+
delay,
|
|
221
|
+
easing,
|
|
222
|
+
perspective,
|
|
223
|
+
width,
|
|
224
|
+
height,
|
|
225
|
+
even
|
|
226
|
+
});
|
|
227
|
+
const prefersReducedMotion = effectiveReduceAnimation !== "never" && typeof window !== "undefined" && window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
|
|
228
|
+
if (prefersReducedMotion) {
|
|
229
|
+
return { ...base, duration: 0, delay: 0 };
|
|
230
|
+
}
|
|
231
|
+
return base;
|
|
232
|
+
}, [
|
|
233
|
+
faces,
|
|
234
|
+
axis,
|
|
235
|
+
direction,
|
|
236
|
+
type,
|
|
237
|
+
duration,
|
|
238
|
+
delay,
|
|
239
|
+
easing,
|
|
240
|
+
perspective,
|
|
241
|
+
width,
|
|
242
|
+
height,
|
|
243
|
+
even,
|
|
244
|
+
effectiveReduceAnimation
|
|
245
|
+
]);
|
|
246
|
+
const addTimeout = useCallback3((fn, ms) => {
|
|
247
|
+
pendingTimers.current.push(setTimeout(fn, ms));
|
|
248
|
+
}, []);
|
|
249
|
+
useEffect2(
|
|
250
|
+
() => () => {
|
|
251
|
+
for (const id of pendingTimers.current) clearTimeout(id);
|
|
252
|
+
},
|
|
253
|
+
[]
|
|
254
|
+
);
|
|
255
|
+
const [prevKind, setPrevKind] = useState2(state.kind);
|
|
256
|
+
const [prevFace, setPrevFace] = useState2(state.displayFace);
|
|
257
|
+
const [focusRequest, setFocusRequest] = useState2(null);
|
|
258
|
+
if (state.kind !== prevKind || state.displayFace !== prevFace) {
|
|
259
|
+
setPrevKind(state.kind);
|
|
260
|
+
setPrevFace(state.displayFace);
|
|
261
|
+
if (state.kind === "idle" && prevKind === "animating" || state.kind === "settling") {
|
|
262
|
+
setFocusRequest((prev2) => ({ face: state.displayFace, seq: (prev2?.seq ?? 0) + 1 }));
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
useEffect2(() => {
|
|
266
|
+
if (!focusRequest) return;
|
|
267
|
+
boxRef.current?.querySelector(`[data-face-index="${focusRequest.face}"]`)?.querySelector(FOCUSABLE)?.focus({ preventScroll: true });
|
|
268
|
+
}, [focusRequest]);
|
|
269
|
+
const resolveCurrentFace = useCallback3(() => stateRef.current.displayFace, []);
|
|
270
|
+
const goTo = useCallback3(
|
|
271
|
+
(rawTarget, animation = true) => {
|
|
272
|
+
const drainQueue = (settledFace, queue) => {
|
|
273
|
+
const result = buildDrainResult(settledFace, queue, opts);
|
|
274
|
+
if (result.kind === "empty") return;
|
|
275
|
+
goTo(result.nav.face, result.nav.animation);
|
|
276
|
+
if (stateRef.current.kind !== "idle") {
|
|
277
|
+
for (const item of result.enqueue) dispatchAndSync({ type: "ENQUEUE", nav: item });
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
let plan = planNavigation(stateRef.current, rawTarget, opts, animation);
|
|
281
|
+
if (plan.effect === "noop") return;
|
|
282
|
+
if (plan.effect === "enqueue") {
|
|
283
|
+
dispatchAndSync(plan.action);
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (plan.effect === "abort") {
|
|
287
|
+
for (const id of pendingTimers.current) clearTimeout(id);
|
|
288
|
+
pendingTimers.current = [];
|
|
289
|
+
dispatchAndSync({ type: "ABORT", displayFace: stateRef.current.displayFace });
|
|
290
|
+
plan = planNavigation(stateRef.current, rawTarget, opts, animation);
|
|
291
|
+
if (plan.effect !== "animate" && plan.effect !== "instant") return;
|
|
292
|
+
}
|
|
293
|
+
const { to } = plan;
|
|
294
|
+
const time = opts.duration + opts.delay;
|
|
295
|
+
onChangeRef.current?.(to);
|
|
296
|
+
if (plan.effect === "instant") {
|
|
297
|
+
dispatchAndSync(plan.action);
|
|
298
|
+
addTimeout(() => {
|
|
299
|
+
const currentState = stateRef.current;
|
|
300
|
+
const pendingQueue = currentState.kind === "settling" ? [...currentState.queue] : [];
|
|
301
|
+
dispatchAndSync({ type: "SETTLE" });
|
|
302
|
+
onAnimationEndRef.current?.(to);
|
|
303
|
+
drainQueue(to, pendingQueue);
|
|
304
|
+
}, time);
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
dispatchAndSync(plan.action);
|
|
308
|
+
addTimeout(() => {
|
|
309
|
+
cancelFaceAnimations();
|
|
310
|
+
const currentState = stateRef.current;
|
|
311
|
+
const pendingQueue = currentState.kind === "animating" ? [...currentState.queue] : [];
|
|
312
|
+
dispatchAndSync({ type: "COMPLETE", displayFace: to });
|
|
313
|
+
onAnimationEndRef.current?.(to);
|
|
314
|
+
drainQueue(to, pendingQueue);
|
|
315
|
+
}, time);
|
|
316
|
+
},
|
|
317
|
+
[opts, addTimeout, cancelFaceAnimations, dispatchAndSync]
|
|
318
|
+
);
|
|
319
|
+
const next = useCallback3(() => goTo(resolveCurrentFace() + 1, true), [goTo, resolveCurrentFace]);
|
|
320
|
+
const prev = useCallback3(() => goTo(resolveCurrentFace() - 1, true), [goTo, resolveCurrentFace]);
|
|
321
|
+
useImperativeHandle(
|
|
322
|
+
ref,
|
|
323
|
+
() => ({
|
|
324
|
+
goTo,
|
|
325
|
+
getCurrentFace: () => state.displayFace,
|
|
326
|
+
isAnimating: () => stateRef.current.kind !== "idle",
|
|
327
|
+
next,
|
|
328
|
+
prev
|
|
329
|
+
}),
|
|
330
|
+
[goTo, next, prev, state.displayFace]
|
|
331
|
+
);
|
|
332
|
+
const ctx = useMemo(
|
|
333
|
+
() => ({
|
|
334
|
+
opts,
|
|
335
|
+
displayFace: state.displayFace,
|
|
336
|
+
phase: toPhase(state),
|
|
337
|
+
shownFaces: state.shownFaces,
|
|
338
|
+
goTo,
|
|
339
|
+
next,
|
|
340
|
+
prev
|
|
341
|
+
}),
|
|
342
|
+
[opts, state, goTo, next, prev]
|
|
343
|
+
);
|
|
344
|
+
const boxWidth = width ?? DEFAULT_SIZE;
|
|
345
|
+
const boxHeight = height ?? DEFAULT_HEIGHT;
|
|
346
|
+
const containerDynStyle = calcContainerDynStyle(state, opts);
|
|
347
|
+
return (
|
|
348
|
+
// biome-ignore lint/a11y/useAriaPropsSupportedByRole: role="region" is set conditionally alongside aria-label; Biome cannot track the dynamic pairing
|
|
349
|
+
/* @__PURE__ */ jsx2(
|
|
350
|
+
"div",
|
|
351
|
+
{
|
|
352
|
+
...ariaRest,
|
|
353
|
+
role: ariaLabel ? "region" : void 0,
|
|
354
|
+
"aria-label": ariaLabel,
|
|
355
|
+
className,
|
|
356
|
+
style: {
|
|
357
|
+
perspective: `${opts.perspective}px`,
|
|
358
|
+
width: boxWidth,
|
|
359
|
+
height: boxHeight,
|
|
360
|
+
position: "relative",
|
|
361
|
+
...style
|
|
362
|
+
},
|
|
363
|
+
children: /* @__PURE__ */ jsx2(
|
|
364
|
+
"div",
|
|
365
|
+
{
|
|
366
|
+
ref: boxRef,
|
|
367
|
+
"data-turnbox-box": true,
|
|
368
|
+
style: {
|
|
369
|
+
width: boxWidth,
|
|
370
|
+
position: "absolute",
|
|
371
|
+
top: 0,
|
|
372
|
+
bottom: 0,
|
|
373
|
+
transformStyle: "preserve-3d",
|
|
374
|
+
...containerDynStyle
|
|
375
|
+
},
|
|
376
|
+
children: /* @__PURE__ */ jsx2(TurnBoxContext.Provider, { value: ctx, children: buildIndexedChildren(children, opts.faces) })
|
|
377
|
+
}
|
|
378
|
+
)
|
|
379
|
+
}
|
|
380
|
+
)
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
);
|
|
384
|
+
Root.displayName = "TurnBox.Root";
|
|
385
|
+
|
|
386
|
+
// src/TurnBox/Button.tsx
|
|
387
|
+
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
388
|
+
var Button = ({ direction = "next", to, children, ...rest }) => {
|
|
389
|
+
const { goTo, next, prev } = useTurnBoxContext();
|
|
390
|
+
const handleClick = () => {
|
|
391
|
+
if (to !== void 0) {
|
|
392
|
+
goTo(to);
|
|
393
|
+
} else if (direction === "prev") {
|
|
394
|
+
prev();
|
|
395
|
+
} else {
|
|
396
|
+
next();
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
return /* @__PURE__ */ jsx3("button", { type: "button", ...rest, onClick: handleClick, children });
|
|
400
|
+
};
|
|
401
|
+
Button.displayName = "TurnBox.Button";
|
|
402
|
+
|
|
403
|
+
// src/TurnBox/Provider.tsx
|
|
404
|
+
import { jsx as jsx4 } from "react/jsx-runtime";
|
|
405
|
+
var Provider = ({ reduceAnimation = "system setting", children }) => /* @__PURE__ */ jsx4(TurnBoxConfigContext.Provider, { value: { reduceAnimation }, children });
|
|
406
|
+
|
|
407
|
+
// src/TurnBox/index.ts
|
|
408
|
+
var TurnBox = { Root, Face, Button, Provider };
|
|
409
|
+
export {
|
|
410
|
+
TurnBox,
|
|
411
|
+
useTurnBox
|
|
412
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@futaba-soft/turnbox-react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "React TurnBox — CSS3 3D box flip animation compound component and hook for React 18+",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"3d",
|
|
7
|
+
"animation",
|
|
8
|
+
"css3",
|
|
9
|
+
"transform",
|
|
10
|
+
"turnbox",
|
|
11
|
+
"react",
|
|
12
|
+
"hook",
|
|
13
|
+
"component"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "futaba-soft",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/futaba-soft/turnbox_js.git",
|
|
20
|
+
"directory": "packages/react"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/futaba-soft/turnbox_js#readme",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/futaba-soft/turnbox_js/issues"
|
|
25
|
+
},
|
|
26
|
+
"type": "module",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"import": "./dist/index.js",
|
|
30
|
+
"types": "./dist/index.d.ts"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"main": "./dist/index.js",
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"sideEffects": false,
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"LICENSE"
|
|
39
|
+
],
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"react": ">=18"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@futaba-soft/turnbox-core": "^0.5.0",
|
|
48
|
+
"@futaba-soft/turnbox-dom": "0.5.0"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/react": "^19.2.17",
|
|
52
|
+
"react": "^19.2.7",
|
|
53
|
+
"react-dom": "^19.2.7"
|
|
54
|
+
},
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "tsup src/index.ts --format esm --dts --out-dir dist",
|
|
57
|
+
"test": "vitest run"
|
|
58
|
+
}
|
|
6
59
|
}
|