@futaba-soft/turnbox-dom 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 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,87 @@
1
- # Temporary Holding Version
1
+ # @futaba-soft/turnbox-dom
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Vanilla JS adapter for TURNBOX.js.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @futaba-soft/turnbox-dom
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```html
14
+ <div id="box">
15
+ <div>Face 1</div>
16
+ <div>Face 2</div>
17
+ <div>Face 3</div>
18
+ <div>Face 4</div>
19
+ </div>
20
+ ```
21
+
22
+ ```js
23
+ import { createTurnBox } from "@futaba-soft/turnbox-dom";
24
+
25
+ const box = createTurnBox(document.getElementById("box"), {
26
+ faces: 4,
27
+ duration: 400,
28
+ });
29
+
30
+ box.next();
31
+ box.prev();
32
+ box.goTo(3); // animated
33
+ box.goTo(2, false); // no animation
34
+ ```
35
+
36
+ ## API
37
+
38
+ ### createTurnBox(container, options)
39
+
40
+ | | Type | Description |
41
+ | --- | --- | --- |
42
+ | `container` | `HTMLElement` | The container element |
43
+ | `options` | `TurnBoxOptions` | See [shared options](../../README.md#options) |
44
+ | `options.reduceAnimation` | `"system setting" \| "never"` | Optional. Default: `"system setting"`. See [`reduceAnimation`](../../README.md#reduceanimation--accessibility-setting) |
45
+
46
+ Returns a `TurnBoxInstance`.
47
+
48
+ ### TurnBoxInstance
49
+
50
+ | Method | Description |
51
+ | --- | --- |
52
+ | `next()` | Go to the next face |
53
+ | `prev()` | Go to the previous face |
54
+ | `goTo(face, animation?)` | Go to the specified face (`animation` defaults to `true`) |
55
+ | `getCurrentFace()` | Current face number (1-based) |
56
+ | `isAnimating()` | Whether an animation is in progress |
57
+ | `destroy()` | Remove all inline styles, classes, and timers |
58
+
59
+ ## CSS Classes
60
+
61
+ ### Container
62
+
63
+ | Class | Description |
64
+ | --- | --- |
65
+ | `turnBoxContainer` | Added on initialization |
66
+ | `turnBoxCurrentFace1` – `turnBoxCurrentFace4` | Reflects the current face |
67
+
68
+ ### Face elements
69
+
70
+ | Class | Description |
71
+ | --- | --- |
72
+ | `turnBoxFace` | Added to all faces |
73
+ | `turnBoxFaceNum1` – `turnBoxFaceNum4` | Identifies each face by number |
74
+ | `turnBoxShow` | Added to the visible face |
75
+
76
+ ```css
77
+ .turnBoxFaceNum1 { background: #ffffff; }
78
+ .turnBoxFaceNum2 { background: #f5f5f5; }
79
+
80
+ .turnBoxContainer.turnBoxCurrentFace2 .turnBoxFaceNum2 {
81
+ box-shadow: 0 0 8px rgba(0, 0, 0, 0.2);
82
+ }
83
+ ```
84
+
85
+ ## License
86
+
87
+ MIT
@@ -0,0 +1,18 @@
1
+ import { TurnBoxOptions, ReduceAnimation } from '@futaba-soft/turnbox-core/internal';
2
+ export { TurnBoxOptions } from '@futaba-soft/turnbox-core';
3
+
4
+ type TurnBoxInstance = {
5
+ goTo(face: number, animation?: boolean): void;
6
+ next(): void;
7
+ prev(): void;
8
+ getCurrentFace(): number;
9
+ isAnimating(): boolean;
10
+ destroy(): void;
11
+ };
12
+ type DomOptions = TurnBoxOptions & {
13
+ ariaLabel?: string;
14
+ reduceAnimation?: ReduceAnimation;
15
+ };
16
+ declare const createTurnBox: (container: HTMLElement, options: DomOptions) => TurnBoxInstance;
17
+
18
+ export { type TurnBoxInstance, createTurnBox };
package/dist/index.js ADDED
@@ -0,0 +1,263 @@
1
+ // src/createTurnBox.ts
2
+ import {
3
+ normalizeOptions,
4
+ calcFaceTransform,
5
+ FOCUSABLE,
6
+ INITIAL_STATE,
7
+ reducer,
8
+ planNavigation,
9
+ buildDrainResult
10
+ } from "@futaba-soft/turnbox-core/internal";
11
+
12
+ // src/css.ts
13
+ var toTransformString = ({ axis, deg, x, y, z }) => `rotate${axis}(${deg}deg) translate3d(${x}px, ${y}px, ${z}px)`;
14
+
15
+ // src/createTurnBox.ts
16
+ var applyFaceTransforms = (faces, currentFace, opts) => {
17
+ faces.forEach((face, i) => {
18
+ const faceNum = i + 1;
19
+ const t = calcFaceTransform(currentFace, faceNum, opts);
20
+ face.style.transform = toTransformString(t);
21
+ face.style.zIndex = String(t.zIndex);
22
+ face.style.transformOrigin = t.transformOrigin;
23
+ });
24
+ };
25
+ var createTurnBox = (container, options) => {
26
+ const rawOpts = normalizeOptions(options);
27
+ const prefersReducedMotion = options.reduceAnimation !== "never" && typeof window !== "undefined" && window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
28
+ const opts = prefersReducedMotion ? { ...rawOpts, duration: 0, delay: 0 } : rawOpts;
29
+ const { geometry } = opts;
30
+ const faces = Array.from(container.children).slice(0, opts.faces);
31
+ const geoOps = geometry.kind === "variable" ? {
32
+ initFace(face, faceNum) {
33
+ const isEven = faceNum % 2 === 0;
34
+ face.style[geometry.axis === "X" ? "height" : "width"] = isEven ? `${geometry.even}px` : `${geometry.length}px`;
35
+ },
36
+ initContainer() {
37
+ container.style[geometry.axis === "X" ? "height" : "left"] = geometry.axis === "X" ? `${geometry.length}px` : "0px";
38
+ },
39
+ onFaceChange(faceNum) {
40
+ const isEven = faceNum % 2 === 0;
41
+ if (geometry.axis === "X") {
42
+ container.style.height = isEven ? `${geometry.even}px` : `${geometry.length}px`;
43
+ } else {
44
+ container.style.left = isEven ? `${(geometry.length - geometry.even) / 2}px` : "0px";
45
+ }
46
+ },
47
+ applyTransition() {
48
+ const prop = geometry.axis === "X" ? "height" : "left";
49
+ container.style.transition = `${prop} ${opts.duration}ms ${opts.easing} ${opts.delay}ms`;
50
+ },
51
+ clearTransition() {
52
+ container.style.transition = "";
53
+ }
54
+ } : {
55
+ initFace(_face, _faceNum) {
56
+ },
57
+ initContainer() {
58
+ },
59
+ onFaceChange(_faceNum) {
60
+ },
61
+ applyTransition() {
62
+ },
63
+ clearTransition() {
64
+ }
65
+ };
66
+ faces.forEach((face, i) => {
67
+ const faceNum = i + 1;
68
+ face.classList.add("turnBoxFace", `turnBoxFaceNum${faceNum}`);
69
+ geoOps.initFace(face, faceNum);
70
+ });
71
+ let state = INITIAL_STATE;
72
+ const dispatch = (action) => {
73
+ state = reducer(state, action);
74
+ };
75
+ let currentFace = 1;
76
+ const pendingTimers = [];
77
+ const schedule = (fn, delay) => {
78
+ pendingTimers.push(setTimeout(fn, delay));
79
+ };
80
+ const setCurrentFace = (n) => {
81
+ if (currentFace === n) return;
82
+ container.classList.remove(`turnBoxCurrentFace${currentFace}`);
83
+ currentFace = n;
84
+ container.classList.add(`turnBoxCurrentFace${n}`);
85
+ geoOps.onFaceChange(n);
86
+ };
87
+ const showFaceVisual = (faceNum) => {
88
+ const face = faces[faceNum - 1];
89
+ if (!face) return;
90
+ if (!face.classList.contains("turnBoxShow")) face.classList.add("turnBoxShow");
91
+ };
92
+ const showFaceA11y = (faceNum) => {
93
+ const face = faces[faceNum - 1];
94
+ if (!face) return;
95
+ face.removeAttribute("aria-hidden");
96
+ face.inert = false;
97
+ };
98
+ const showFace = (faceNum) => {
99
+ showFaceVisual(faceNum);
100
+ showFaceA11y(faceNum);
101
+ };
102
+ const hideFace = (faceNum) => {
103
+ const face = faces[faceNum - 1];
104
+ if (!face) return;
105
+ if (face.classList.contains("turnBoxShow")) face.classList.remove("turnBoxShow");
106
+ if (face.getAttribute("aria-hidden") !== "true") face.setAttribute("aria-hidden", "true");
107
+ if (!face.inert) face.inert = true;
108
+ };
109
+ const syncFaceVisibility = () => {
110
+ faces.forEach((_, i) => {
111
+ const faceNum = i + 1;
112
+ if (state.shownFaces.has(faceNum)) showFace(faceNum);
113
+ else hideFace(faceNum);
114
+ });
115
+ };
116
+ if (options.ariaLabel) {
117
+ container.setAttribute("role", "region");
118
+ container.setAttribute("aria-label", options.ariaLabel);
119
+ }
120
+ container.style.perspective = `${opts.perspective}px`;
121
+ container.classList.add("turnBoxContainer", `turnBoxCurrentFace${currentFace}`);
122
+ geoOps.initContainer();
123
+ applyFaceTransforms(faces, currentFace, opts);
124
+ faces[0]?.classList.add("turnBoxShow");
125
+ for (const face of faces.slice(1)) {
126
+ face.setAttribute("aria-hidden", "true");
127
+ face.inert = true;
128
+ }
129
+ const abortAnimation = () => {
130
+ for (const id of pendingTimers) clearTimeout(id);
131
+ pendingTimers.length = 0;
132
+ faces.forEach((f) => {
133
+ f.classList.remove("turnBoxTransition");
134
+ f.style.transition = "";
135
+ });
136
+ geoOps.clearTransition();
137
+ container.classList.remove("turnBoxAdjust");
138
+ const settleFace = state.displayFace;
139
+ dispatch({ type: "ABORT", displayFace: settleFace });
140
+ setCurrentFace(settleFace);
141
+ applyFaceTransforms(faces, settleFace, opts);
142
+ syncFaceVisibility();
143
+ };
144
+ const drainQueue = (settledFace, queue) => {
145
+ const result = buildDrainResult(settledFace, queue, opts);
146
+ if (result.kind === "empty") return;
147
+ animate(result.nav.face, result.nav.animation);
148
+ if (state.kind !== "idle") {
149
+ for (const item of result.enqueue) dispatch({ type: "ENQUEUE", nav: item });
150
+ }
151
+ };
152
+ const animate = (rawTarget, animationFlag) => {
153
+ let plan = planNavigation(state, rawTarget, opts, animationFlag);
154
+ if (plan.effect === "noop") return;
155
+ if (plan.effect === "enqueue") {
156
+ dispatch(plan.action);
157
+ return;
158
+ }
159
+ if (plan.effect === "abort") {
160
+ abortAnimation();
161
+ plan = planNavigation(state, rawTarget, opts, animationFlag);
162
+ if (plan.effect !== "animate" && plan.effect !== "instant") return;
163
+ }
164
+ const { to } = plan;
165
+ const doAnimate = plan.effect === "animate";
166
+ const time = opts.duration + opts.delay;
167
+ options.onChange?.(to);
168
+ dispatch(plan.action);
169
+ if (doAnimate) {
170
+ faces.forEach((_, i) => {
171
+ const faceNum = i + 1;
172
+ if (state.shownFaces.has(faceNum)) {
173
+ showFaceVisual(faceNum);
174
+ if (faceNum !== to) showFaceA11y(faceNum);
175
+ } else {
176
+ hideFace(faceNum);
177
+ }
178
+ });
179
+ } else {
180
+ syncFaceVisibility();
181
+ }
182
+ const step = () => {
183
+ if (doAnimate) {
184
+ faces.forEach((f) => {
185
+ f.classList.add("turnBoxTransition");
186
+ f.style.transition = `transform ${opts.duration}ms ${opts.easing} ${opts.delay}ms`;
187
+ });
188
+ geoOps.applyTransition();
189
+ }
190
+ setCurrentFace(to);
191
+ applyFaceTransforms(faces, to, opts);
192
+ if (doAnimate) showFaceA11y(to);
193
+ schedule(() => {
194
+ faces.forEach((f) => {
195
+ f.classList.remove("turnBoxTransition");
196
+ f.style.transition = "";
197
+ for (const anim of f.getAnimations?.() ?? []) anim.cancel();
198
+ });
199
+ geoOps.clearTransition();
200
+ const pendingQueue = state.kind !== "idle" ? [...state.queue] : [];
201
+ if (doAnimate) {
202
+ dispatch({ type: "COMPLETE", displayFace: to });
203
+ } else {
204
+ dispatch({ type: "SETTLE" });
205
+ }
206
+ syncFaceVisibility();
207
+ options.onAnimationEnd?.(to);
208
+ faces[currentFace - 1]?.querySelector(FOCUSABLE)?.focus({ preventScroll: true });
209
+ drainQueue(to, pendingQueue);
210
+ }, time);
211
+ };
212
+ schedule(step, 0);
213
+ };
214
+ return {
215
+ goTo(face, animation = true) {
216
+ animate(face, animation);
217
+ },
218
+ next() {
219
+ animate(state.displayFace + 1, true);
220
+ },
221
+ prev() {
222
+ animate(state.displayFace - 1, true);
223
+ },
224
+ // Logical target (the face navigation is heading to), not the DOM-committed face —
225
+ // consistent with next()/prev() stepping from state.displayFace. See current-face-contract.suite.
226
+ getCurrentFace: () => state.displayFace,
227
+ isAnimating: () => state.kind !== "idle",
228
+ destroy() {
229
+ for (const id of pendingTimers) clearTimeout(id);
230
+ pendingTimers.length = 0;
231
+ faces.forEach((face) => {
232
+ face.classList.remove(
233
+ "turnBoxFace",
234
+ "turnBoxShow",
235
+ ...Array.from(face.classList).filter((c) => c.startsWith("turnBoxFaceNum"))
236
+ );
237
+ face.style.transform = "";
238
+ face.style.zIndex = "";
239
+ face.style.transformOrigin = "";
240
+ face.style.transition = "";
241
+ face.style.height = "";
242
+ face.style.width = "";
243
+ face.removeAttribute("aria-hidden");
244
+ face.inert = false;
245
+ });
246
+ container.classList.remove(
247
+ "turnBoxContainer",
248
+ ...Array.from(container.classList).filter((c) => c.startsWith("turnBoxCurrentFace") || c === "turnBoxAdjust")
249
+ );
250
+ container.style.height = "";
251
+ container.style.left = "";
252
+ container.style.transition = "";
253
+ container.style.perspective = "";
254
+ if (options.ariaLabel) {
255
+ container.removeAttribute("role");
256
+ container.removeAttribute("aria-label");
257
+ }
258
+ }
259
+ };
260
+ };
261
+ export {
262
+ createTurnBox
263
+ };
package/package.json CHANGED
@@ -1,6 +1,49 @@
1
1
  {
2
2
  "name": "@futaba-soft/turnbox-dom",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.5.0",
4
+ "description": "Vanilla JS TurnBox — CSS3 3D box flip animation for the browser",
5
+ "keywords": [
6
+ "3d",
7
+ "animation",
8
+ "css3",
9
+ "transform",
10
+ "turnbox",
11
+ "vanilla",
12
+ "dom"
13
+ ],
14
+ "license": "MIT",
15
+ "author": "futaba-soft",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/futaba-soft/turnbox_js.git",
19
+ "directory": "packages/dom"
20
+ },
21
+ "homepage": "https://github.com/futaba-soft/turnbox_js#readme",
22
+ "bugs": {
23
+ "url": "https://github.com/futaba-soft/turnbox_js/issues"
24
+ },
25
+ "type": "module",
26
+ "exports": {
27
+ ".": {
28
+ "import": "./dist/index.js",
29
+ "types": "./dist/index.d.ts"
30
+ }
31
+ },
32
+ "main": "./dist/index.js",
33
+ "types": "./dist/index.d.ts",
34
+ "sideEffects": false,
35
+ "files": [
36
+ "dist",
37
+ "LICENSE"
38
+ ],
39
+ "publishConfig": {
40
+ "access": "public"
41
+ },
42
+ "dependencies": {
43
+ "@futaba-soft/turnbox-core": "0.5.0"
44
+ },
45
+ "scripts": {
46
+ "build": "tsup src/index.ts --format esm --dts --out-dir dist",
47
+ "test": "vitest run"
48
+ }
6
49
  }