@neveranyart/weaver 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 +674 -0
- package/README.md +18 -0
- package/dist/hooks.d.mts +83 -0
- package/dist/hooks.d.ts +83 -0
- package/dist/hooks.js +358 -0
- package/dist/hooks.js.map +1 -0
- package/dist/hooks.mjs +320 -0
- package/dist/hooks.mjs.map +1 -0
- package/dist/index.d.mts +14 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +44 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +17 -0
- package/dist/index.mjs.map +1 -0
- package/dist/routing.d.mts +88 -0
- package/dist/routing.d.ts +88 -0
- package/dist/routing.js +206 -0
- package/dist/routing.js.map +1 -0
- package/dist/routing.mjs +167 -0
- package/dist/routing.mjs.map +1 -0
- package/dist/scene.d.mts +158 -0
- package/dist/scene.d.ts +158 -0
- package/dist/scene.js +325 -0
- package/dist/scene.js.map +1 -0
- package/dist/scene.mjs +297 -0
- package/dist/scene.mjs.map +1 -0
- package/package.json +79 -0
package/dist/routing.js
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/routing/index.ts
|
|
31
|
+
var routing_exports = {};
|
|
32
|
+
__export(routing_exports, {
|
|
33
|
+
DelayedOutlet: () => DelayedOutlet,
|
|
34
|
+
Pipeline: () => Pipeline,
|
|
35
|
+
useWeaverState: () => useWeaverState
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(routing_exports);
|
|
38
|
+
|
|
39
|
+
// src/routing/context.ts
|
|
40
|
+
var import_react = require("zustand/react");
|
|
41
|
+
var useWeaverContext = (0, import_react.create)()((set) => ({
|
|
42
|
+
activeParent: "",
|
|
43
|
+
setActiveParent: (activeParent) => set({ activeParent }),
|
|
44
|
+
activePipeline: "",
|
|
45
|
+
setActivePipeline: (activePipeline) => set({ activePipeline }),
|
|
46
|
+
navigating: true,
|
|
47
|
+
setNavigating: (navigating) => set({ navigating }),
|
|
48
|
+
pageRendered: false,
|
|
49
|
+
setPageRendered: (pageRendered) => set({ pageRendered })
|
|
50
|
+
}));
|
|
51
|
+
|
|
52
|
+
// src/routing/DelayedOutlet.tsx
|
|
53
|
+
var import_react3 = require("react");
|
|
54
|
+
var import_react_router2 = require("react-router");
|
|
55
|
+
|
|
56
|
+
// src/hooks/routeNormalizer.ts
|
|
57
|
+
var import_react2 = require("react");
|
|
58
|
+
var import_react_router = require("react-router");
|
|
59
|
+
function useRouteNormalizer(options) {
|
|
60
|
+
const { pathname } = (0, import_react_router.useLocation)();
|
|
61
|
+
const navigate = (0, import_react_router.useNavigate)();
|
|
62
|
+
const normalizer = (0, import_react2.useCallback)(
|
|
63
|
+
(input) => input.replace(/\/+\//g, () => "/"),
|
|
64
|
+
[]
|
|
65
|
+
);
|
|
66
|
+
(0, import_react2.useLayoutEffect)(() => {
|
|
67
|
+
const result = normalizer(pathname);
|
|
68
|
+
if (options.autoReplace && result !== pathname) {
|
|
69
|
+
window.location.replace(result);
|
|
70
|
+
}
|
|
71
|
+
}, [options.autoReplace, pathname, navigate, normalizer]);
|
|
72
|
+
return {
|
|
73
|
+
pathname,
|
|
74
|
+
normalizer
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// src/utils/getParentRoute.ts
|
|
79
|
+
function getParentRoute(pathname) {
|
|
80
|
+
let slashes = 0;
|
|
81
|
+
let sliceAt = 0;
|
|
82
|
+
for (sliceAt; sliceAt < pathname.length; sliceAt++) {
|
|
83
|
+
if (pathname[sliceAt] == "/") {
|
|
84
|
+
slashes++;
|
|
85
|
+
}
|
|
86
|
+
if (slashes === 2) {
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return pathname.slice(0, sliceAt);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/routing/DelayedOutlet.tsx
|
|
94
|
+
function DelayedOutlet(props) {
|
|
95
|
+
useRouteNormalizer({ autoReplace: true });
|
|
96
|
+
const activePipeline = useWeaverContext((state) => state.activePipeline);
|
|
97
|
+
const setNavigating = useWeaverContext((state) => state.setNavigating);
|
|
98
|
+
const setPageRendered = useWeaverContext((state) => state.setPageRendered);
|
|
99
|
+
const renderPageTask = (0, import_react3.useRef)(setTimeout(() => {
|
|
100
|
+
}));
|
|
101
|
+
const routerOutlet = (0, import_react_router2.useOutlet)();
|
|
102
|
+
const [renderer, setRenderer] = (0, import_react3.useState)(null);
|
|
103
|
+
(0, import_react3.useLayoutEffect)(() => {
|
|
104
|
+
if ("scrollRestoration" in history) {
|
|
105
|
+
history.scrollRestoration = "manual";
|
|
106
|
+
}
|
|
107
|
+
if (getParentRoute(window.location.pathname) === activePipeline) {
|
|
108
|
+
setNavigating(false);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
setNavigating(true);
|
|
112
|
+
setPageRendered(false);
|
|
113
|
+
clearTimeout(renderPageTask.current);
|
|
114
|
+
renderPageTask.current = setTimeout(() => {
|
|
115
|
+
setRenderer(routerOutlet);
|
|
116
|
+
setNavigating(false);
|
|
117
|
+
}, props.delay);
|
|
118
|
+
return () => {
|
|
119
|
+
clearInterval(renderPageTask.current);
|
|
120
|
+
};
|
|
121
|
+
}, [
|
|
122
|
+
activePipeline,
|
|
123
|
+
props.delay,
|
|
124
|
+
routerOutlet,
|
|
125
|
+
setNavigating,
|
|
126
|
+
setPageRendered
|
|
127
|
+
]);
|
|
128
|
+
return renderer;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// src/routing/Pipeline.tsx
|
|
132
|
+
var import_react5 = __toESM(require("react"));
|
|
133
|
+
|
|
134
|
+
// src/index.ts
|
|
135
|
+
var lenisInstance = void 0;
|
|
136
|
+
|
|
137
|
+
// src/hooks/effectOnce.ts
|
|
138
|
+
var import_react4 = require("react");
|
|
139
|
+
function useLayoutEffectOnce(callback) {
|
|
140
|
+
(0, import_react4.useLayoutEffect)(callback, []);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// src/routing/Pipeline.tsx
|
|
144
|
+
function Pipeline(props) {
|
|
145
|
+
const { children, ...restOfProps } = props;
|
|
146
|
+
return /* @__PURE__ */ import_react5.default.createElement(import_react5.default.Fragment, null, /* @__PURE__ */ import_react5.default.createElement(RouteHandler, { ...restOfProps }), children);
|
|
147
|
+
}
|
|
148
|
+
function RouteHandler(props) {
|
|
149
|
+
const navigating = useWeaverContext((state) => state.navigating);
|
|
150
|
+
const pageRendered = useWeaverContext((state) => state.pageRendered);
|
|
151
|
+
const activeParent = useWeaverContext((state) => state.activeParent);
|
|
152
|
+
const setActivePipeline = useWeaverContext(
|
|
153
|
+
(state) => state.setActivePipeline
|
|
154
|
+
);
|
|
155
|
+
const setPageRendered = useWeaverContext((state) => state.setPageRendered);
|
|
156
|
+
const setActiveParent = useWeaverContext((state) => state.setActiveParent);
|
|
157
|
+
(0, import_react5.useLayoutEffect)(() => {
|
|
158
|
+
document.title = props.title;
|
|
159
|
+
setActivePipeline(props.parentPath);
|
|
160
|
+
if (props.contentReady !== void 0 && !props.contentReady) {
|
|
161
|
+
console.log(`[${props.debugName}] Renderer status: Loading scene`);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
if (!navigating && (!pageRendered || activeParent !== props.parentPath)) {
|
|
165
|
+
if (props.autoStartLenis === void 0 || props.autoStartLenis) {
|
|
166
|
+
lenisInstance.start();
|
|
167
|
+
}
|
|
168
|
+
setPageRendered(true);
|
|
169
|
+
setActiveParent(props.parentPath);
|
|
170
|
+
console.log(`[${props.debugName}] Renderer status: Mounted`);
|
|
171
|
+
}
|
|
172
|
+
}, [
|
|
173
|
+
activeParent,
|
|
174
|
+
setActivePipeline,
|
|
175
|
+
navigating,
|
|
176
|
+
pageRendered,
|
|
177
|
+
props.autoStartLenis,
|
|
178
|
+
props.contentReady,
|
|
179
|
+
props.debugName,
|
|
180
|
+
props.parentPath,
|
|
181
|
+
props.title,
|
|
182
|
+
setActiveParent,
|
|
183
|
+
setPageRendered
|
|
184
|
+
]);
|
|
185
|
+
useLayoutEffectOnce(() => {
|
|
186
|
+
return () => {
|
|
187
|
+
lenisInstance.stop();
|
|
188
|
+
lenisInstance.scrollTo(0, { immediate: true, force: true });
|
|
189
|
+
console.log(`[${props.debugName}] Renderer status: Unmounted`);
|
|
190
|
+
};
|
|
191
|
+
});
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// src/routing/index.ts
|
|
196
|
+
var useWeaverState = (givenState) => {
|
|
197
|
+
const state = useWeaverContext((state2) => state2[givenState]);
|
|
198
|
+
return state;
|
|
199
|
+
};
|
|
200
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
201
|
+
0 && (module.exports = {
|
|
202
|
+
DelayedOutlet,
|
|
203
|
+
Pipeline,
|
|
204
|
+
useWeaverState
|
|
205
|
+
});
|
|
206
|
+
//# sourceMappingURL=routing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/routing/index.ts","../src/routing/context.ts","../src/routing/DelayedOutlet.tsx","../src/hooks/routeNormalizer.ts","../src/utils/getParentRoute.ts","../src/routing/Pipeline.tsx","../src/index.ts","../src/hooks/effectOnce.ts"],"sourcesContent":["import { useWeaverContext, WeaverContextGetter } from './context';\nimport DelayedOutlet from './DelayedOutlet';\nimport Pipeline from './Pipeline';\n\n/// A read-only state for reacting with changes reflected by weaver.\nexport const useWeaverState = (givenState: keyof WeaverContextGetter) => {\n const state = useWeaverContext((state) => state[givenState]);\n return state;\n};\n\nexport { DelayedOutlet, Pipeline };\n","import { create } from 'zustand/react';\n\nexport interface WeaverContextGetter {\n /**\n * Current active parent, **AFTER** Pipeline has finished rendering, so the `activeParent`\n * will be delayed. Example: \"/\", \"/works\",...\n */\n activeParent: string;\n\n /**\n * Current active Pipeline. Example: \"/\", \"/works\",...\n *\n * This is **not based on URL**, it's based on which `Pipeline` has access to the the site.\n */\n activePipeline: string;\n\n /**\n * `DelayedOutlet` special variable, indicating if the route is current transitioning to a new route or not.\n */\n navigating: boolean;\n\n /**\n * When the page is rendered, it will turns this to true,\n * any parent page navigation will causes this to goes false, set to false in `DelayedOulet`.\n *\n * It's set to `false` right after `navigating` is set to `true`. `true` statement will be handled by `Pipeline`.\n */\n pageRendered: boolean;\n}\n\ninterface WeaverContextSetter {\n setActiveParent: (activeParent: string) => void;\n setActivePipeline: (activePipeline: string) => void;\n setNavigating: (navigating: boolean) => void;\n setPageRendered: (pageRendered: boolean) => void;\n}\n\nexport const useWeaverContext = create<\n WeaverContextSetter & WeaverContextGetter\n>()((set) => ({\n activeParent: '',\n setActiveParent: (activeParent) => set({ activeParent }),\n\n activePipeline: '',\n setActivePipeline: (activePipeline) => set({ activePipeline }),\n\n navigating: true,\n setNavigating: (navigating) => set({ navigating }),\n\n pageRendered: false,\n setPageRendered: (pageRendered) => set({ pageRendered }),\n}));\n","import { type ReactNode, useLayoutEffect, useRef, useState } from 'react';\nimport { useOutlet } from 'react-router';\nimport { useRouteNormalizer } from '../hooks/routeNormalizer';\nimport { getParentRoute } from '../utils/getParentRoute';\nimport { useWeaverContext } from './context';\n\n/**\n * A core part of an in-house tool called `weaver`.\n *\n * Delaying the routing process from `react-router`, handles gracefully between `Pipeline`s\n * while allowing any loading fallback component to listen and react with event changes.\n */\nexport default function DelayedOutlet(props: { delay: number }) {\n // Middleware to detect and replace invalid url.\n useRouteNormalizer({ autoReplace: true });\n\n const activePipeline = useWeaverContext((state) => state.activePipeline);\n const setNavigating = useWeaverContext((state) => state.setNavigating);\n const setPageRendered = useWeaverContext((state) => state.setPageRendered);\n\n const renderPageTask = useRef(setTimeout(() => {}));\n const routerOutlet = useOutlet();\n\n const [renderer, setRenderer] = useState<ReactNode>(null);\n\n // Handle and update new parent page.\n useLayoutEffect(() => {\n /**\n * `pageRendered` should be outside of `renderPageTask`,\n * we alrady have `Pipeline` to correct errors.\n */\n if ('scrollRestoration' in history) {\n history.scrollRestoration = 'manual';\n }\n\n if (getParentRoute(window.location.pathname) === activePipeline) {\n // Avoid while changing page, cancelling before new page pushed in\n // cause `activePipeline` to not change, making `navigating` softlock.\n setNavigating(false);\n return;\n }\n\n setNavigating(true);\n setPageRendered(false);\n\n clearTimeout(renderPageTask.current);\n renderPageTask.current = setTimeout(() => {\n setRenderer(routerOutlet!);\n setNavigating(false);\n }, props.delay);\n\n return () => {\n /**\n * Clear invalid task that were scheduled last effect.\n */\n clearInterval(renderPageTask.current);\n };\n }, [\n activePipeline,\n props.delay,\n routerOutlet,\n setNavigating,\n setPageRendered,\n ]);\n\n return renderer;\n}\n","import { useCallback, useLayoutEffect } from 'react';\nimport { useLocation, useNavigate } from 'react-router';\n\nexport function useRouteNormalizer(options: { autoReplace: boolean }) {\n const { pathname } = useLocation();\n const navigate = useNavigate();\n\n const normalizer = useCallback(\n (input: string) => input.replace(/\\/+\\//g, () => '/'),\n []\n );\n\n useLayoutEffect(() => {\n const result = normalizer(pathname);\n if (options.autoReplace && result !== pathname) {\n window.location.replace(result);\n }\n }, [options.autoReplace, pathname, navigate, normalizer]);\n\n return {\n pathname,\n normalizer,\n };\n}\n","export function getParentRoute(pathname: string) {\n let slashes = 0;\n let sliceAt = 0;\n for (sliceAt; sliceAt < pathname.length; sliceAt++) {\n if (pathname[sliceAt] == '/') {\n slashes++;\n }\n if (slashes === 2) {\n break;\n }\n }\n\n return pathname.slice(0, sliceAt);\n}\n","import React, { useLayoutEffect, type ReactNode } from 'react';\nimport { lenisInstance } from '..';\nimport { useLayoutEffectOnce } from '../hooks/effectOnce';\nimport { useWeaverContext } from './context';\n\ninterface PipelineProps {\n children?: ReactNode;\n\n /**\n * A state switch to notifies `Pipeline` that the content and elements of the page is ready to be displayed.\n *\n * Usually, you will need to preload some other external sources, or initialize 3D scene, this state\n * make sure that the loading fallback doesn't mess up and show initializing stuff.\n *\n * Using `BakeScene`, you can ensure that the scene is loaded via its callback, you can then pass the state value that\n * `BakeScene` changes to this variable to hide all the lags behind loading screen.\n */\n contentReady?: boolean;\n\n /**\n * Title for the page.\n */\n title: string;\n\n /**\n * By default, `Pipeline` will log the current phase to console.\n */\n debugName: string;\n\n /**\n * This is crucial for different `Pipeline`s to differentiate each other and avoiding conflict.\n *\n * Example for parent path: `/`, `/about`, `/projects`,...\n */\n parentPath: string;\n\n /**\n * By default, this value is `true`\n *\n * When navigating off of a previous `Pipeline`. It will stop lenis as a clean up.\n *\n * So when a new `Pipeline` takes in, it will enable lenis immediately when `contentReady` is switched to `true`.\n *\n * Set to `false` to gain manual control over when lenis will start. This does not affect to stop mechanism,\n * you don't have to clean up yourself.\n */\n autoStartLenis?: boolean;\n}\n\n/**\n * A core part of an in-house tool called `weaver`.\n *\n * `Pipeline`: Notifies & reflect changes to/from `LoadingFallback`\n * and `DelayedOutlet` about its page loading status.\n *\n * All parent routes must have `Pipeline` in order to work and sync with `DelayedOutlet`.\n */\nexport default function Pipeline(props: PipelineProps) {\n const { children, ...restOfProps } = props;\n return (\n <>\n <RouteHandler {...restOfProps} />\n {children}\n </>\n );\n}\n\nfunction RouteHandler(props: Omit<PipelineProps, 'children'>) {\n const navigating = useWeaverContext((state) => state.navigating);\n const pageRendered = useWeaverContext((state) => state.pageRendered);\n const activeParent = useWeaverContext((state) => state.activeParent);\n\n const setActivePipeline = useWeaverContext(\n (state) => state.setActivePipeline\n );\n const setPageRendered = useWeaverContext((state) => state.setPageRendered);\n const setActiveParent = useWeaverContext((state) => state.setActiveParent);\n\n useLayoutEffect(() => {\n document.title = props.title;\n setActivePipeline(props.parentPath);\n\n if (props.contentReady !== undefined && !props.contentReady) {\n console.log(`[${props.debugName}] Renderer status: Loading scene`);\n return;\n }\n\n if (!navigating && (!pageRendered || activeParent !== props.parentPath)) {\n if (props.autoStartLenis === undefined || props.autoStartLenis) {\n lenisInstance!.start();\n }\n\n setPageRendered(true);\n setActiveParent(props.parentPath);\n\n console.log(`[${props.debugName}] Renderer status: Mounted`);\n }\n }, [\n activeParent,\n setActivePipeline,\n navigating,\n pageRendered,\n props.autoStartLenis,\n props.contentReady,\n props.debugName,\n props.parentPath,\n props.title,\n setActiveParent,\n setPageRendered,\n ]);\n\n useLayoutEffectOnce(() => {\n return () => {\n /**\n * When unmounted, stop lenis to pass control to another Pipline instance.\n *\n * The unmount process only happens when the `<LoadingFallback />` kicks in,\n * so any visual glitches can happen in here, we can now set the scroll position to 0.\n */\n lenisInstance!.stop();\n lenisInstance!.scrollTo(0, { immediate: true, force: true });\n\n console.log(`[${props.debugName}] Renderer status: Unmounted`);\n };\n });\n\n return null;\n}\n","import Lenis from 'lenis';\nimport { ReactNode } from 'react';\n\nexport let lenisInstance: Lenis | undefined = undefined;\n\nexport type BasicTunnelIn = ({ children }: { children: ReactNode }) => null;\nexport let Default3DTunnelIn: BasicTunnelIn | undefined = undefined;\n\nexport const weaverSetup = {\n setLenisInstance(instance: Lenis) {\n lenisInstance = instance;\n },\n set3DTunnel(tunnelIn: BasicTunnelIn) {\n Default3DTunnelIn = tunnelIn;\n },\n};\n","import { useEffect, useLayoutEffect } from 'react';\n\nexport function useEffectOnce(callback: React.EffectCallback) {\n // eslint-disable-next-line react-hooks/exhaustive-deps\n useEffect(callback, []);\n}\n\nexport function useLayoutEffectOnce(callback: React.EffectCallback) {\n // eslint-disable-next-line react-hooks/exhaustive-deps\n useLayoutEffect(callback, []);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAuB;AAqChB,IAAM,uBAAmB,qBAE9B,EAAE,CAAC,SAAS;AAAA,EACZ,cAAc;AAAA,EACd,iBAAiB,CAAC,iBAAiB,IAAI,EAAE,aAAa,CAAC;AAAA,EAEvD,gBAAgB;AAAA,EAChB,mBAAmB,CAAC,mBAAmB,IAAI,EAAE,eAAe,CAAC;AAAA,EAE7D,YAAY;AAAA,EACZ,eAAe,CAAC,eAAe,IAAI,EAAE,WAAW,CAAC;AAAA,EAEjD,cAAc;AAAA,EACd,iBAAiB,CAAC,iBAAiB,IAAI,EAAE,aAAa,CAAC;AACzD,EAAE;;;ACnDF,IAAAA,gBAAkE;AAClE,IAAAC,uBAA0B;;;ACD1B,IAAAC,gBAA6C;AAC7C,0BAAyC;AAElC,SAAS,mBAAmB,SAAmC;AACpE,QAAM,EAAE,SAA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,MAAM,gBAAgB;AAC9D,sBAAe,MAAM;AAAA,MACvB;AAEA,sBAAgB,IAAI;AACpB,sBAAgB,MAAM,UAAU;AAEhC,cAAQ,IAAI,IAAI,MAAM,SAAS,4BAA4B;AAAA,IAC7D;AAAA,EACF,GAAG;AAAA,IACD;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,MAAM;AAAA,IACN,MAAM;AAAA,IACN,MAAM;AAAA,IACN,MAAM;AAAA,IACN,MAAM;AAAA,IACN;AAAA,IACA;AAAA,EACF,CAAC;AAED,sBAAoB,MAAM;AACxB,WAAO,MAAM;AAOX,oBAAe,KAAK;AACpB,oBAAe,SAAS,GAAG,EAAE,WAAW,MAAM,OAAO,KAAK,CAAC;AAE3D,cAAQ,IAAI,IAAI,MAAM,SAAS,8BAA8B;AAAA,IAC/D;AAAA,EACF,CAAC;AAED,SAAO;AACT;;;AL1HO,IAAM,iBAAiB,CAAC,eAA0C;AACvE,QAAM,QAAQ,iBAAiB,CAACC,WAAUA,OAAM,UAAU,CAAC;AAC3D,SAAO;AACT;","names":["import_react","import_react_router","import_react","import_react","import_react","React","state"]}
|
package/dist/routing.mjs
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// src/routing/context.ts
|
|
2
|
+
import { create } from "zustand/react";
|
|
3
|
+
var useWeaverContext = create()((set) => ({
|
|
4
|
+
activeParent: "",
|
|
5
|
+
setActiveParent: (activeParent) => set({ activeParent }),
|
|
6
|
+
activePipeline: "",
|
|
7
|
+
setActivePipeline: (activePipeline) => set({ activePipeline }),
|
|
8
|
+
navigating: true,
|
|
9
|
+
setNavigating: (navigating) => set({ navigating }),
|
|
10
|
+
pageRendered: false,
|
|
11
|
+
setPageRendered: (pageRendered) => set({ pageRendered })
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
// src/routing/DelayedOutlet.tsx
|
|
15
|
+
import { useLayoutEffect as useLayoutEffect2, useRef, useState } from "react";
|
|
16
|
+
import { useOutlet } from "react-router";
|
|
17
|
+
|
|
18
|
+
// src/hooks/routeNormalizer.ts
|
|
19
|
+
import { useCallback, useLayoutEffect } from "react";
|
|
20
|
+
import { useLocation, useNavigate } from "react-router";
|
|
21
|
+
function useRouteNormalizer(options) {
|
|
22
|
+
const { pathname } = useLocation();
|
|
23
|
+
const navigate = useNavigate();
|
|
24
|
+
const normalizer = useCallback(
|
|
25
|
+
(input) => input.replace(/\/+\//g, () => "/"),
|
|
26
|
+
[]
|
|
27
|
+
);
|
|
28
|
+
useLayoutEffect(() => {
|
|
29
|
+
const result = normalizer(pathname);
|
|
30
|
+
if (options.autoReplace && result !== pathname) {
|
|
31
|
+
window.location.replace(result);
|
|
32
|
+
}
|
|
33
|
+
}, [options.autoReplace, pathname, navigate, normalizer]);
|
|
34
|
+
return {
|
|
35
|
+
pathname,
|
|
36
|
+
normalizer
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// src/utils/getParentRoute.ts
|
|
41
|
+
function getParentRoute(pathname) {
|
|
42
|
+
let slashes = 0;
|
|
43
|
+
let sliceAt = 0;
|
|
44
|
+
for (sliceAt; sliceAt < pathname.length; sliceAt++) {
|
|
45
|
+
if (pathname[sliceAt] == "/") {
|
|
46
|
+
slashes++;
|
|
47
|
+
}
|
|
48
|
+
if (slashes === 2) {
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return pathname.slice(0, sliceAt);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// src/routing/DelayedOutlet.tsx
|
|
56
|
+
function DelayedOutlet(props) {
|
|
57
|
+
useRouteNormalizer({ autoReplace: true });
|
|
58
|
+
const activePipeline = useWeaverContext((state) => state.activePipeline);
|
|
59
|
+
const setNavigating = useWeaverContext((state) => state.setNavigating);
|
|
60
|
+
const setPageRendered = useWeaverContext((state) => state.setPageRendered);
|
|
61
|
+
const renderPageTask = useRef(setTimeout(() => {
|
|
62
|
+
}));
|
|
63
|
+
const routerOutlet = useOutlet();
|
|
64
|
+
const [renderer, setRenderer] = useState(null);
|
|
65
|
+
useLayoutEffect2(() => {
|
|
66
|
+
if ("scrollRestoration" in history) {
|
|
67
|
+
history.scrollRestoration = "manual";
|
|
68
|
+
}
|
|
69
|
+
if (getParentRoute(window.location.pathname) === activePipeline) {
|
|
70
|
+
setNavigating(false);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
setNavigating(true);
|
|
74
|
+
setPageRendered(false);
|
|
75
|
+
clearTimeout(renderPageTask.current);
|
|
76
|
+
renderPageTask.current = setTimeout(() => {
|
|
77
|
+
setRenderer(routerOutlet);
|
|
78
|
+
setNavigating(false);
|
|
79
|
+
}, props.delay);
|
|
80
|
+
return () => {
|
|
81
|
+
clearInterval(renderPageTask.current);
|
|
82
|
+
};
|
|
83
|
+
}, [
|
|
84
|
+
activePipeline,
|
|
85
|
+
props.delay,
|
|
86
|
+
routerOutlet,
|
|
87
|
+
setNavigating,
|
|
88
|
+
setPageRendered
|
|
89
|
+
]);
|
|
90
|
+
return renderer;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/routing/Pipeline.tsx
|
|
94
|
+
import React, { useLayoutEffect as useLayoutEffect4 } from "react";
|
|
95
|
+
|
|
96
|
+
// src/index.ts
|
|
97
|
+
var lenisInstance = void 0;
|
|
98
|
+
|
|
99
|
+
// src/hooks/effectOnce.ts
|
|
100
|
+
import { useEffect, useLayoutEffect as useLayoutEffect3 } from "react";
|
|
101
|
+
function useLayoutEffectOnce(callback) {
|
|
102
|
+
useLayoutEffect3(callback, []);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/routing/Pipeline.tsx
|
|
106
|
+
function Pipeline(props) {
|
|
107
|
+
const { children, ...restOfProps } = props;
|
|
108
|
+
return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(RouteHandler, { ...restOfProps }), children);
|
|
109
|
+
}
|
|
110
|
+
function RouteHandler(props) {
|
|
111
|
+
const navigating = useWeaverContext((state) => state.navigating);
|
|
112
|
+
const pageRendered = useWeaverContext((state) => state.pageRendered);
|
|
113
|
+
const activeParent = useWeaverContext((state) => state.activeParent);
|
|
114
|
+
const setActivePipeline = useWeaverContext(
|
|
115
|
+
(state) => state.setActivePipeline
|
|
116
|
+
);
|
|
117
|
+
const setPageRendered = useWeaverContext((state) => state.setPageRendered);
|
|
118
|
+
const setActiveParent = useWeaverContext((state) => state.setActiveParent);
|
|
119
|
+
useLayoutEffect4(() => {
|
|
120
|
+
document.title = props.title;
|
|
121
|
+
setActivePipeline(props.parentPath);
|
|
122
|
+
if (props.contentReady !== void 0 && !props.contentReady) {
|
|
123
|
+
console.log(`[${props.debugName}] Renderer status: Loading scene`);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (!navigating && (!pageRendered || activeParent !== props.parentPath)) {
|
|
127
|
+
if (props.autoStartLenis === void 0 || props.autoStartLenis) {
|
|
128
|
+
lenisInstance.start();
|
|
129
|
+
}
|
|
130
|
+
setPageRendered(true);
|
|
131
|
+
setActiveParent(props.parentPath);
|
|
132
|
+
console.log(`[${props.debugName}] Renderer status: Mounted`);
|
|
133
|
+
}
|
|
134
|
+
}, [
|
|
135
|
+
activeParent,
|
|
136
|
+
setActivePipeline,
|
|
137
|
+
navigating,
|
|
138
|
+
pageRendered,
|
|
139
|
+
props.autoStartLenis,
|
|
140
|
+
props.contentReady,
|
|
141
|
+
props.debugName,
|
|
142
|
+
props.parentPath,
|
|
143
|
+
props.title,
|
|
144
|
+
setActiveParent,
|
|
145
|
+
setPageRendered
|
|
146
|
+
]);
|
|
147
|
+
useLayoutEffectOnce(() => {
|
|
148
|
+
return () => {
|
|
149
|
+
lenisInstance.stop();
|
|
150
|
+
lenisInstance.scrollTo(0, { immediate: true, force: true });
|
|
151
|
+
console.log(`[${props.debugName}] Renderer status: Unmounted`);
|
|
152
|
+
};
|
|
153
|
+
});
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// src/routing/index.ts
|
|
158
|
+
var useWeaverState = (givenState) => {
|
|
159
|
+
const state = useWeaverContext((state2) => state2[givenState]);
|
|
160
|
+
return state;
|
|
161
|
+
};
|
|
162
|
+
export {
|
|
163
|
+
DelayedOutlet,
|
|
164
|
+
Pipeline,
|
|
165
|
+
useWeaverState
|
|
166
|
+
};
|
|
167
|
+
//# sourceMappingURL=routing.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/routing/context.ts","../src/routing/DelayedOutlet.tsx","../src/hooks/routeNormalizer.ts","../src/utils/getParentRoute.ts","../src/routing/Pipeline.tsx","../src/index.ts","../src/hooks/effectOnce.ts","../src/routing/index.ts"],"sourcesContent":["import { create } from 'zustand/react';\n\nexport interface WeaverContextGetter {\n /**\n * Current active parent, **AFTER** Pipeline has finished rendering, so the `activeParent`\n * will be delayed. Example: \"/\", \"/works\",...\n */\n activeParent: string;\n\n /**\n * Current active Pipeline. Example: \"/\", \"/works\",...\n *\n * This is **not based on URL**, it's based on which `Pipeline` has access to the the site.\n */\n activePipeline: string;\n\n /**\n * `DelayedOutlet` special variable, indicating if the route is current transitioning to a new route or not.\n */\n navigating: boolean;\n\n /**\n * When the page is rendered, it will turns this to true,\n * any parent page navigation will causes this to goes false, set to false in `DelayedOulet`.\n *\n * It's set to `false` right after `navigating` is set to `true`. `true` statement will be handled by `Pipeline`.\n */\n pageRendered: boolean;\n}\n\ninterface WeaverContextSetter {\n setActiveParent: (activeParent: string) => void;\n setActivePipeline: (activePipeline: string) => void;\n setNavigating: (navigating: boolean) => void;\n setPageRendered: (pageRendered: boolean) => void;\n}\n\nexport const useWeaverContext = create<\n WeaverContextSetter & WeaverContextGetter\n>()((set) => ({\n activeParent: '',\n setActiveParent: (activeParent) => set({ activeParent }),\n\n activePipeline: '',\n setActivePipeline: (activePipeline) => set({ activePipeline }),\n\n navigating: true,\n setNavigating: (navigating) => set({ navigating }),\n\n pageRendered: false,\n setPageRendered: (pageRendered) => set({ pageRendered }),\n}));\n","import { type ReactNode, useLayoutEffect, useRef, useState } from 'react';\nimport { useOutlet } from 'react-router';\nimport { useRouteNormalizer } from '../hooks/routeNormalizer';\nimport { getParentRoute } from '../utils/getParentRoute';\nimport { useWeaverContext } from './context';\n\n/**\n * A core part of an in-house tool called `weaver`.\n *\n * Delaying the routing process from `react-router`, handles gracefully between `Pipeline`s\n * while allowing any loading fallback component to listen and react with event changes.\n */\nexport default function DelayedOutlet(props: { delay: number }) {\n // Middleware to detect and replace invalid url.\n useRouteNormalizer({ autoReplace: true });\n\n const activePipeline = useWeaverContext((state) => state.activePipeline);\n const setNavigating = useWeaverContext((state) => state.setNavigating);\n const setPageRendered = useWeaverContext((state) => state.setPageRendered);\n\n const renderPageTask = useRef(setTimeout(() => {}));\n const routerOutlet = useOutlet();\n\n const [renderer, setRenderer] = useState<ReactNode>(null);\n\n // Handle and update new parent page.\n useLayoutEffect(() => {\n /**\n * `pageRendered` should be outside of `renderPageTask`,\n * we alrady have `Pipeline` to correct errors.\n */\n if ('scrollRestoration' in history) {\n history.scrollRestoration = 'manual';\n }\n\n if (getParentRoute(window.location.pathname) === activePipeline) {\n // Avoid while changing page, cancelling before new page pushed in\n // cause `activePipeline` to not change, making `navigating` softlock.\n setNavigating(false);\n return;\n }\n\n setNavigating(true);\n setPageRendered(false);\n\n clearTimeout(renderPageTask.current);\n renderPageTask.current = setTimeout(() => {\n setRenderer(routerOutlet!);\n setNavigating(false);\n }, props.delay);\n\n return () => {\n /**\n * Clear invalid task that were scheduled last effect.\n */\n clearInterval(renderPageTask.current);\n };\n }, [\n activePipeline,\n props.delay,\n routerOutlet,\n setNavigating,\n setPageRendered,\n ]);\n\n return renderer;\n}\n","import { useCallback, useLayoutEffect } from 'react';\nimport { useLocation, useNavigate } from 'react-router';\n\nexport function useRouteNormalizer(options: { autoReplace: boolean }) {\n const { pathname } = useLocation();\n const navigate = useNavigate();\n\n const normalizer = useCallback(\n (input: string) => input.replace(/\\/+\\//g, () => '/'),\n []\n );\n\n useLayoutEffect(() => {\n const result = normalizer(pathname);\n if (options.autoReplace && result !== pathname) {\n window.location.replace(result);\n }\n }, [options.autoReplace, pathname, navigate, normalizer]);\n\n return {\n pathname,\n normalizer,\n };\n}\n","export function getParentRoute(pathname: string) {\n let slashes = 0;\n let sliceAt = 0;\n for (sliceAt; sliceAt < pathname.length; sliceAt++) {\n if (pathname[sliceAt] == '/') {\n slashes++;\n }\n if (slashes === 2) {\n break;\n }\n }\n\n return pathname.slice(0, sliceAt);\n}\n","import React, { useLayoutEffect, type ReactNode } from 'react';\nimport { lenisInstance } from '..';\nimport { useLayoutEffectOnce } from '../hooks/effectOnce';\nimport { useWeaverContext } from './context';\n\ninterface PipelineProps {\n children?: ReactNode;\n\n /**\n * A state switch to notifies `Pipeline` that the content and elements of the page is ready to be displayed.\n *\n * Usually, you will need to preload some other external sources, or initialize 3D scene, this state\n * make sure that the loading fallback doesn't mess up and show initializing stuff.\n *\n * Using `BakeScene`, you can ensure that the scene is loaded via its callback, you can then pass the state value that\n * `BakeScene` changes to this variable to hide all the lags behind loading screen.\n */\n contentReady?: boolean;\n\n /**\n * Title for the page.\n */\n title: string;\n\n /**\n * By default, `Pipeline` will log the current phase to console.\n */\n debugName: string;\n\n /**\n * This is crucial for different `Pipeline`s to differentiate each other and avoiding conflict.\n *\n * Example for parent path: `/`, `/about`, `/projects`,...\n */\n parentPath: string;\n\n /**\n * By default, this value is `true`\n *\n * When navigating off of a previous `Pipeline`. It will stop lenis as a clean up.\n *\n * So when a new `Pipeline` takes in, it will enable lenis immediately when `contentReady` is switched to `true`.\n *\n * Set to `false` to gain manual control over when lenis will start. This does not affect to stop mechanism,\n * you don't have to clean up yourself.\n */\n autoStartLenis?: boolean;\n}\n\n/**\n * A core part of an in-house tool called `weaver`.\n *\n * `Pipeline`: Notifies & reflect changes to/from `LoadingFallback`\n * and `DelayedOutlet` about its page loading status.\n *\n * All parent routes must have `Pipeline` in order to work and sync with `DelayedOutlet`.\n */\nexport default function Pipeline(props: PipelineProps) {\n const { children, ...restOfProps } = props;\n return (\n <>\n <RouteHandler {...restOfProps} />\n {children}\n </>\n );\n}\n\nfunction RouteHandler(props: Omit<PipelineProps, 'children'>) {\n const navigating = useWeaverContext((state) => state.navigating);\n const pageRendered = useWeaverContext((state) => state.pageRendered);\n const activeParent = useWeaverContext((state) => state.activeParent);\n\n const setActivePipeline = useWeaverContext(\n (state) => state.setActivePipeline\n );\n const setPageRendered = useWeaverContext((state) => state.setPageRendered);\n const setActiveParent = useWeaverContext((state) => state.setActiveParent);\n\n useLayoutEffect(() => {\n document.title = props.title;\n setActivePipeline(props.parentPath);\n\n if (props.contentReady !== undefined && !props.contentReady) {\n console.log(`[${props.debugName}] Renderer status: Loading scene`);\n return;\n }\n\n if (!navigating && (!pageRendered || activeParent !== props.parentPath)) {\n if (props.autoStartLenis === undefined || props.autoStartLenis) {\n lenisInstance!.start();\n }\n\n setPageRendered(true);\n setActiveParent(props.parentPath);\n\n console.log(`[${props.debugName}] Renderer status: Mounted`);\n }\n }, [\n activeParent,\n setActivePipeline,\n navigating,\n pageRendered,\n props.autoStartLenis,\n props.contentReady,\n props.debugName,\n props.parentPath,\n props.title,\n setActiveParent,\n setPageRendered,\n ]);\n\n useLayoutEffectOnce(() => {\n return () => {\n /**\n * When unmounted, stop lenis to pass control to another Pipline instance.\n *\n * The unmount process only happens when the `<LoadingFallback />` kicks in,\n * so any visual glitches can happen in here, we can now set the scroll position to 0.\n */\n lenisInstance!.stop();\n lenisInstance!.scrollTo(0, { immediate: true, force: true });\n\n console.log(`[${props.debugName}] Renderer status: Unmounted`);\n };\n });\n\n return null;\n}\n","import Lenis from 'lenis';\nimport { ReactNode } from 'react';\n\nexport let lenisInstance: Lenis | undefined = undefined;\n\nexport type BasicTunnelIn = ({ children }: { children: ReactNode }) => null;\nexport let Default3DTunnelIn: BasicTunnelIn | undefined = undefined;\n\nexport const weaverSetup = {\n setLenisInstance(instance: Lenis) {\n lenisInstance = instance;\n },\n set3DTunnel(tunnelIn: BasicTunnelIn) {\n Default3DTunnelIn = tunnelIn;\n },\n};\n","import { useEffect, useLayoutEffect } from 'react';\n\nexport function useEffectOnce(callback: React.EffectCallback) {\n // eslint-disable-next-line react-hooks/exhaustive-deps\n useEffect(callback, []);\n}\n\nexport function useLayoutEffectOnce(callback: React.EffectCallback) {\n // eslint-disable-next-line react-hooks/exhaustive-deps\n useLayoutEffect(callback, []);\n}\n","import { useWeaverContext, WeaverContextGetter } from './context';\nimport DelayedOutlet from './DelayedOutlet';\nimport Pipeline from './Pipeline';\n\n/// A read-only state for reacting with changes reflected by weaver.\nexport const useWeaverState = (givenState: keyof WeaverContextGetter) => {\n const state = useWeaverContext((state) => state[givenState]);\n return state;\n};\n\nexport { DelayedOutlet, Pipeline };\n"],"mappings":";AAAA,SAAS,cAAc;AAqChB,IAAM,mBAAmB,OAE9B,EAAE,CAAC,SAAS;AAAA,EACZ,cAAc;AAAA,EACd,iBAAiB,CAAC,iBAAiB,IAAI,EAAE,aAAa,CAAC;AAAA,EAEvD,gBAAgB;AAAA,EAChB,mBAAmB,CAAC,mBAAmB,IAAI,EAAE,eAAe,CAAC;AAAA,EAE7D,YAAY;AAAA,EACZ,eAAe,CAAC,eAAe,IAAI,EAAE,WAAW,CAAC;AAAA,EAEjD,cAAc;AAAA,EACd,iBAAiB,CAAC,iBAAiB,IAAI,EAAE,aAAa,CAAC;AACzD,EAAE;;;ACnDF,SAAyB,mBAAAA,kBAAiB,QAAQ,gBAAgB;AAClE,SAAS,iBAAiB;;;ACD1B,SAAS,aAAa,uBAAuB;AAC7C,SAAS,aAAa,mBAAmB;AAElC,SAAS,mBAAmB,SAAmC;AACpE,QAAM,EAAE,SAAS,IAAI,YAAY;AACjC,QAAM,WAAW,YAAY;AAE7B,QAAM,aAAa;AAAA,IACjB,CAAC,UAAkB,MAAM,QAAQ,UAAU,MAAM,GAAG;AAAA,IACpD,CAAC;AAAA,EACH;AAEA,kBAAgB,MAA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F,GAAG;AAAA,IACD;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,MAAM;AAAA,IACN,MAAM;AAAA,IACN,MAAM;AAAA,IACN,MAAM;AAAA,IACN,MAAM;AAAA,IACN;AAAA,IACA;AAAA,EACF,CAAC;AAED,sBAAoB,MAAM;AACxB,WAAO,MAAM;AAOX,oBAAe,KAAK;AACpB,oBAAe,SAAS,GAAG,EAAE,WAAW,MAAM,OAAO,KAAK,CAAC;AAE3D,cAAQ,IAAI,IAAI,MAAM,SAAS,8BAA8B;AAAA,IAC/D;AAAA,EACF,CAAC;AAED,SAAO;AACT;;;AG1HO,IAAM,iBAAiB,CAAC,eAA0C;AACvE,QAAM,QAAQ,iBAAiB,CAACC,WAAUA,OAAM,UAAU,CAAC;AAC3D,SAAO;AACT;","names":["useLayoutEffect","useLayoutEffect","useLayoutEffect","useLayoutEffect","useLayoutEffect","useLayoutEffect","state"]}
|
package/dist/scene.d.mts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import React, { ReactNode, Dispatch, SetStateAction, RefObject } from 'react';
|
|
2
|
+
import { BasicTunnelIn } from './index.mjs';
|
|
3
|
+
import 'lenis';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A core part of an in-house tool called `weaver`.
|
|
7
|
+
*
|
|
8
|
+
* `BakeScene`: This component will notifiy when the global 3D scene is ready.
|
|
9
|
+
*
|
|
10
|
+
* It works by tune in the `useFrame` from `@react-three/fiber`. When the scene is loaded, `useFreame` will fire
|
|
11
|
+
* with ease, the component takes advantage of that, and because `useLoader` is unreliable.
|
|
12
|
+
*
|
|
13
|
+
* This component also accepts 3D elements `children` to be rendered directly to the canvas with some camera options.
|
|
14
|
+
* But you don't have to put every 3D components inside the baker, for example, `SceneSync`s in the page are also
|
|
15
|
+
* being watched by this component.
|
|
16
|
+
*
|
|
17
|
+
* The route renderer **CAN'T** detect if the page has 3D elements or not, so if a page uses any sort of 3D rendering,
|
|
18
|
+
* this component **MUST** be a children iniside `Pipeline` (`index.tsx`), then pass the state value that bake changes
|
|
19
|
+
* to `Pipeline`'s `contentReady` in order for the `BakeScene` to work behind loading fallback screen.
|
|
20
|
+
*/
|
|
21
|
+
declare function BakeScene(props: {
|
|
22
|
+
children?: ReactNode;
|
|
23
|
+
sceneReady: boolean;
|
|
24
|
+
tunnelIn: BasicTunnelIn;
|
|
25
|
+
loadTaskDelay?: number;
|
|
26
|
+
setSceneReady: Dispatch<SetStateAction<boolean>>;
|
|
27
|
+
onReady?: () => void;
|
|
28
|
+
}): React.JSX.Element;
|
|
29
|
+
|
|
30
|
+
type Basic3DTransforms = {
|
|
31
|
+
scale: {
|
|
32
|
+
set: (x: number, y: number, z: number) => void;
|
|
33
|
+
};
|
|
34
|
+
position: {
|
|
35
|
+
x: number;
|
|
36
|
+
y: number;
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
interface SyncProps {
|
|
40
|
+
/**
|
|
41
|
+
* HTML element ref that `<SceneSync />` will use to sync with the scene.
|
|
42
|
+
*
|
|
43
|
+
* ```tsx
|
|
44
|
+
* <div ref={container} />
|
|
45
|
+
* <SceneSync attach={container}>
|
|
46
|
+
* <group />
|
|
47
|
+
* </SceneSync>
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
attach: RefObject<HTMLElement | null>;
|
|
51
|
+
/**
|
|
52
|
+
* This variable allows fine-grain control over your scene when passed to `<SceneSync />`.
|
|
53
|
+
*
|
|
54
|
+
* `<SceneSync />` will use its own ref and group when creating your scene to control its scale and position.
|
|
55
|
+
* Setting this variable will disable the internal ref, and you can decide on which object gets controlled.
|
|
56
|
+
*
|
|
57
|
+
* This variable is needed for `hud` if you wanted to add a custom camera.
|
|
58
|
+
*
|
|
59
|
+
* For listening to change details, use `onLayoutChange` instead.
|
|
60
|
+
*/
|
|
61
|
+
control?: RefObject<Basic3DTransforms | null>;
|
|
62
|
+
/**
|
|
63
|
+
* When this variable is set, `<SceneSync />` will send updates when the scene update its positions.
|
|
64
|
+
*
|
|
65
|
+
* The function return the calculated DOM rect, with dimension and position in 3D measurements.
|
|
66
|
+
*/
|
|
67
|
+
onLayoutUpdate?: (rect: DOMRect, dimension: {
|
|
68
|
+
w: number;
|
|
69
|
+
h: number;
|
|
70
|
+
}, position: {
|
|
71
|
+
x: number;
|
|
72
|
+
y: number;
|
|
73
|
+
}) => void;
|
|
74
|
+
/**
|
|
75
|
+
* Use `Hud` for this scene or not.
|
|
76
|
+
*
|
|
77
|
+
* This is useful when you want to apply custom camera for this scene, or renders multiple scenes on each other.
|
|
78
|
+
*
|
|
79
|
+
* NOTE: When setting a custom camera, the `control` variable must also be set and mount to your scene, not related to the camera
|
|
80
|
+
* to avoid unwanted behavior.
|
|
81
|
+
*
|
|
82
|
+
* `<SceneSync />` groups children passed to it by default, so the camera is also in the group, when syncer updates the group,
|
|
83
|
+
* the camera is also change, ruining the effect.
|
|
84
|
+
*/
|
|
85
|
+
hud?: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Control the scene's scaling when positioning.
|
|
88
|
+
*/
|
|
89
|
+
scaleFactor?: number;
|
|
90
|
+
/**
|
|
91
|
+
* `<SceneSync />` avoid stretching the object by default by using the smallest dimension of the DOM element.
|
|
92
|
+
*
|
|
93
|
+
* This variable will tell `<SceneSync />` to stretch it anyways.
|
|
94
|
+
*/
|
|
95
|
+
stretch?: boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Disable automatic scaling on the object.
|
|
98
|
+
*
|
|
99
|
+
* This variable will also disable any scaling settings like `stretch` and `scaleFactor`.
|
|
100
|
+
*/
|
|
101
|
+
disableScaling?: boolean;
|
|
102
|
+
/**
|
|
103
|
+
* `<SceneSync />` will depend on this variable to adjust how it should update.
|
|
104
|
+
*
|
|
105
|
+
* There are 3 modes: `relaxed`, `balanced` and `aggressive`.
|
|
106
|
+
*
|
|
107
|
+
* For each mode, there will be some very distinct trade-offs
|
|
108
|
+
*
|
|
109
|
+
* - `relaxed`: Uses IntersectionObserver paired with a scroll hook, together with ResizeObserver.
|
|
110
|
+
* - (+): Minimal update calls, best performance.
|
|
111
|
+
* - (-): The scene get desynced the moment DOM element moves without changing its sizes.
|
|
112
|
+
* When the scene bleeds out of the DOM element too much, if IntersectionObserver reported that the DOM element
|
|
113
|
+
* is out of view, the scene that did not fully moved out of view will stay there, as scroll hook will be disabled.
|
|
114
|
+
* - `balanced`: Uses IntersectionObserver paired with frame-based update, together with ResizeObserver.
|
|
115
|
+
* - (+): Just enough update calls to allow the DOM element to move freely while maintain aceptable performance.
|
|
116
|
+
* - (-): It will update on every frame when the object gets into view as reported by IntersectionObserver. And the same
|
|
117
|
+
* problem with `relaxed` mode when the scene bleeds out too much.
|
|
118
|
+
* - `aggressive`: Frame-based update only. This mode is like how `<View />` from `@react-three/drei` kepts track of DOM elements.
|
|
119
|
+
* - (+): Designed for precise element <-> scene updates. Can't be desynced, if desynced, that's a bug.
|
|
120
|
+
* - (-): This is frame-based. It will fire updates as long as the scene is still mounted. Too many scenes with this
|
|
121
|
+
* mode enabled is not a good idea. Acceptable amount would be 3 scenes with this mode.
|
|
122
|
+
*
|
|
123
|
+
* Best of both worlds is `balanced` mode, for simpler scenes that doesn't change its position, `relaxed` should be used.
|
|
124
|
+
*/
|
|
125
|
+
trackingMode: 'relaxed' | 'balanced' | 'aggressive';
|
|
126
|
+
/**
|
|
127
|
+
* Set a custom tunnel for `<SceneSync />` send the components to for this scene only.
|
|
128
|
+
*
|
|
129
|
+
* Which is useful for example, put the objects inside a container in the scene.
|
|
130
|
+
*
|
|
131
|
+
* To set a default tunnel, pass it to `setDefaulTunnel` before use.
|
|
132
|
+
*/
|
|
133
|
+
tunnelIn?: BasicTunnelIn;
|
|
134
|
+
children: ReactNode;
|
|
135
|
+
}
|
|
136
|
+
interface HudProps extends SyncProps {
|
|
137
|
+
hud: true;
|
|
138
|
+
/**
|
|
139
|
+
* Set the `renderPriority` to render things for `Hud`.
|
|
140
|
+
*
|
|
141
|
+
* This variable is ignored when `hud` is not `true`.
|
|
142
|
+
*/
|
|
143
|
+
hudRenderPriority: number;
|
|
144
|
+
}
|
|
145
|
+
interface NormalProps extends SyncProps {
|
|
146
|
+
hud?: false;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* A core part of an in-house tool called `weaver`.
|
|
150
|
+
*
|
|
151
|
+
* A component to allow three objects to track and sync with DOM element.
|
|
152
|
+
*
|
|
153
|
+
* The component uses `<Hud />` under the "hud", so if you want to use more than one `<SceneSync />`,
|
|
154
|
+
* you must set `renderPriority`. If not, the component will render the last scene pushed through React.
|
|
155
|
+
*/
|
|
156
|
+
declare function SceneSync(props: NormalProps | HudProps): React.JSX.Element;
|
|
157
|
+
|
|
158
|
+
export { BakeScene, SceneSync };
|