Skip to content

Commit 54cbb97

Browse files
committed
Feat(core): react optimizations and devx fixes (#12)
1 parent b51c0c0 commit 54cbb97

9 files changed

+184
-113
lines changed

.eslintrc.js .eslintrc.cjs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
root: true,
33
parser: "@typescript-eslint/parser",
44
parserOptions: {
@@ -10,12 +10,9 @@ export default {
1010
},
1111
ignorePatterns: ["**/lib", "**/dist", "**/*.json"],
1212
extends: [
13-
"react-app",
13+
"react-app", // provides @typescript-eslint DO NOT added it twice
1414
"react-app/jest",
15-
"plugin:@typescript-eslint/recommended",
16-
"plugin:@typescript-eslint/recommended-requiring-type-checking",
17-
"plugin:@typescript-eslint/strict",
18-
"plugin:@tanstack/eslint-plugin-query/recommended",
15+
"plugin:jsx-a11y/recommended",
1916
"prettier",
2017
],
2118
// @see https://eslint.org/docs/latest/user-guide/configuring/rules

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ yarn-debug.log*
2020
yarn-error.log*
2121

2222
_crea
23+
tsconfig.tsbuildinfo

bun.lockb

78.7 KB
Binary file not shown.

dist/index.js

+68-38
Original file line numberDiff line numberDiff line change
@@ -9377,12 +9377,15 @@ var FPContainer = ({
93779377
onHide,
93789378
onShow,
93799379
outerStyle = {},
9380+
scrollDebounceMs = 125,
93809381
style = {},
93819382
transitionTiming = 700
93829383
}) => {
9384+
const FPContainerInnerRef = import_react19.useRef(null);
9385+
const scrollTimer = import_react19.useRef(null);
9386+
const scrollY = import_react19.useRef(isSsr ? 0 : window.scrollY);
93839387
const throttled = import_react19.useRef(false);
93849388
const ticking = import_react19.useRef(false);
9385-
const scrollY = import_react19.useRef(isSsr ? 0 : window.scrollY);
93869389
const useOuterStyle = import_react19.useMemo(() => ({
93879390
left: 0,
93889391
position: "fixed",
@@ -9396,8 +9399,8 @@ var FPContainer = ({
93969399
right: 0,
93979400
...style
93989401
}), [style]);
9399-
const FPContainerInnerRef = import_react19.useRef(null);
9400-
const { ReactFPRef, slides } = import_react19.useContext(FPContext);
9402+
const { ReactFPRef, slides, isFullscreen } = import_react19.useContext(FPContext);
9403+
const [, startTransition] = import_react19.useTransition();
94019404
const [pageState, setPageState] = import_react19.useState({
94029405
fullpageHeight: 0,
94039406
offsetHeight: 0,
@@ -9407,32 +9410,41 @@ var FPContainer = ({
94079410
translateY: 0,
94089411
viewportHeight: 0
94099412
});
9410-
const handleScroll = (e) => {
9413+
const handleScroll = () => {
94119414
if (throttled.current || isSsr)
94129415
return;
94139416
throttled.current = true;
9414-
e.stopPropagation();
9415-
setTimeout(() => {
9416-
throttled.current = false;
9417-
}, transitionTiming);
94189417
if (!ticking.current) {
94199418
window.requestAnimationFrame(() => {
94209419
const newScrollY = window.scrollY;
94219420
const prevScrollY = scrollY.current;
9422-
if (prevScrollY < newScrollY)
9421+
if (newScrollY === 0)
9422+
first();
9423+
else if (window.innerHeight + Math.round(newScrollY) >= document.body.offsetHeight)
9424+
last();
9425+
else if (prevScrollY < newScrollY)
94239426
forward();
94249427
else if (prevScrollY > newScrollY)
94259428
back3();
94269429
if (pageState.resetScroll || transitionTiming !== pageState.transitionTiming)
9427-
setPageState((prevState) => ({
9428-
...prevState,
9429-
resetScroll: false,
9430-
transitionTiming
9431-
}));
9430+
startTransition(() => {
9431+
setPageState((prevState) => ({
9432+
...prevState,
9433+
resetScroll: false,
9434+
transitionTiming
9435+
}));
9436+
});
94329437
ticking.current = false;
94339438
});
94349439
ticking.current = true;
94359440
}
9441+
setTimeout(() => {
9442+
throttled.current = false;
9443+
}, transitionTiming);
9444+
};
9445+
const bouncedHandleScroll = () => {
9446+
clearTimeout(scrollTimer.current);
9447+
scrollTimer.current = setTimeout(() => handleScroll(), scrollDebounceMs);
94369448
};
94379449
const handleResize = () => {
94389450
if (!FPContainerInnerRef.current || isSsr)
@@ -9443,11 +9455,13 @@ var FPContainer = ({
94439455
if (!ticking.current) {
94449456
requestAnimationFrame(() => {
94459457
const fullpageHeight = FPContainerInnerRef.current.clientHeight;
9446-
setPageState((prevState) => ({
9447-
...prevState,
9448-
fullpageHeight,
9449-
viewportHeight: Math.max(document.documentElement.clientHeight, window.innerHeight)
9450-
}));
9458+
startTransition(() => {
9459+
setPageState((prevState) => ({
9460+
...prevState,
9461+
fullpageHeight,
9462+
viewportHeight: Math.max(document.documentElement.clientHeight, window.innerHeight)
9463+
}));
9464+
});
94519465
ReactFPRef.current.style.height = `${fullpageHeight}px`;
94529466
ticking.current = false;
94539467
});
@@ -9496,7 +9510,9 @@ var FPContainer = ({
94969510
slideIndex,
94979511
translateY
94989512
};
9499-
setPageState((prevState) => ({ ...prevState, ...newPageState }));
9513+
startTransition(() => {
9514+
setPageState((prevState) => ({ ...prevState, ...newPageState }));
9515+
});
95009516
setTimeout(() => {
95019517
throttled.current = false;
95029518
scrollY.current = window.scrollY;
@@ -9541,18 +9557,19 @@ var FPContainer = ({
95419557
import_react19.useEffect(() => {
95429558
if (isSsr)
95439559
return;
9544-
window.addEventListener("scroll", handleScroll, { passive: true });
9560+
window.addEventListener("scroll", bouncedHandleScroll, { passive: true });
95459561
window.addEventListener("resize", handleResize, { passive: true });
95469562
document.addEventListener("keydown", handleKeys, { passive: true });
95479563
return () => {
9548-
window.removeEventListener("scroll", handleScroll);
9564+
window.removeEventListener("scroll", bouncedHandleScroll);
95499565
window.removeEventListener("resize", handleResize);
95509566
document.removeEventListener("keydown", handleKeys);
95519567
};
95529568
});
9553-
import_react19.useEffect(() => {
9569+
import_react19.useLayoutEffect(() => {
95549570
handleResize();
9555-
});
9571+
handleScroll();
9572+
}, [isFullscreen]);
95569573
return jsx_runtime.jsx("div", {
95579574
style: useOuterStyle,
95589575
children: jsx_runtime.jsx(motion2.div, {
@@ -9573,6 +9590,7 @@ var import_react20 = __toESM(require_react());
95739590
var observeFn = (el) => el;
95749591
var FPContext = import_react20.createContext({
95759592
getIndex: (el) => 0,
9593+
isFullscreen: false,
95769594
ReactFPRef: null,
95779595
slides: [],
95789596
subscribe: observeFn,
@@ -9588,7 +9606,11 @@ var FPItem = ({
95889606
style = {},
95899607
motionProps = {}
95909608
}) => {
9591-
const { subscribe, unsubscribe, getIndex } = import_react21.useContext(FPContext);
9609+
const useStyle2 = import_react21.useMemo(() => ({
9610+
height,
9611+
...style
9612+
}), [style]);
9613+
const { subscribe, unsubscribe } = import_react21.useContext(FPContext);
95929614
const FPItemRef2 = import_react21.useRef(null);
95939615
import_react21.useEffect(() => {
95949616
subscribe(FPItemRef2);
@@ -9598,10 +9620,7 @@ var FPItem = ({
95989620
}, []);
95999621
return jsx_runtime2.jsx(motion2.div, {
96009622
className,
9601-
style: {
9602-
height,
9603-
...style
9604-
},
9623+
style: useStyle2,
96059624
ref: FPItemRef2,
96069625
...motionProps,
96079626
children
@@ -9784,19 +9803,32 @@ var FullScreen = function FullScreen2(_ref) {
97849803

97859804
// src/ReactFP.tsx
97869805
var jsx_runtime4 = __toESM(require_jsx_runtime());
9806+
var ImLoading = () => jsx_runtime4.jsx("div", {
9807+
style: { backgroundColor: "black", height: "100vh" },
9808+
children: "Loading"
9809+
});
97879810
var ReactFP = ({
97889811
children,
97899812
Button = FSButton,
97909813
buttonStyle = {},
97919814
className = "",
9815+
Fallback = ImLoading,
97929816
motionProps = {},
97939817
style = {}
97949818
}) => {
97959819
const useStyle2 = import_react23.useMemo(() => ({
97969820
position: "relative",
97979821
...style
97989822
}), [style]);
9823+
const useButtonStyle = import_react23.useMemo(() => ({
9824+
position: "fixed",
9825+
left: 10,
9826+
top: 10,
9827+
zIndex: 9999,
9828+
...buttonStyle
9829+
}), [buttonStyle]);
97999830
const [slides, setSlides] = import_react23.useState([]);
9831+
const deferredSlides = import_react23.useDeferredValue(slides);
98009832
const fullscreen = import_react23.useRef(false);
98019833
const ReactFPRef = import_react23.useRef(null);
98029834
const getIndex = (slide) => {
@@ -9819,19 +9851,14 @@ var ReactFP = ({
98199851
}
98209852
return void (fullscreen.current = !fullscreen.current);
98219853
},
9822-
style: {
9823-
position: "fixed",
9824-
left: 10,
9825-
top: 10,
9826-
zIndex: 9999,
9827-
...buttonStyle
9828-
}
9854+
style: useButtonStyle
98299855
}),
98309856
jsx_runtime4.jsx(FPContext.Provider, {
98319857
value: {
98329858
getIndex,
9859+
isFullscreen: !!fullscreen.current,
98339860
ReactFPRef,
9834-
slides,
9861+
slides: deferredSlides,
98359862
subscribe,
98369863
unsubscribe
98379864
},
@@ -9840,7 +9867,10 @@ var ReactFP = ({
98409867
ref: ReactFPRef,
98419868
className,
98429869
...motionProps,
9843-
children
9870+
children: jsx_runtime4.jsx(import_react23.Suspense, {
9871+
fallback: jsx_runtime4.jsx(Fallback, {}),
9872+
children
9873+
})
98449874
})
98459875
})
98469876
]

package.json

+9-12
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
"scripts": {
1010
"barrels": "bunx barrelsby --config ./barrels.json",
1111
"build": "rm -rf ./dist/* && bun build src/index.ts --outdir ./dist",
12-
"lint": "bunx eslint src -- --fix",
12+
"lint": "bunx eslint src --fix --resolve-plugins-relative-to .",
1313
"pretty": "bunx prettier --write 'src/**/*.(ts|tsx)'",
14+
"test:types": "bunx tsc",
1415
"upgrade:deps": "bunx ncu -u && bun install"
1516
},
1617
"peerDependencies": {
@@ -22,25 +23,21 @@
2223
"@types/react": "18.2.7",
2324
"@types/react-dom": "18.2.4",
2425
"@types/react-router-dom": "5.3.3",
25-
"@typescript-eslint/eslint-plugin": "5.59.7",
26-
"@typescript-eslint/parser": "5.59.7",
27-
"barrels": "^1.6.6",
26+
"barrels": "1.6.6",
2827
"bun-types": "canary",
2928
"eslint": "8.41.0",
3029
"eslint-config-prettier": "8.8.0",
31-
"eslint-plugin-jsx-a11y": "^6.7.1",
32-
"eslint-plugin-react": "^7.32.2",
33-
"npm-check-updates": "^16.10.12",
34-
"prettier": "^2.8.8",
35-
"react": "^18.2.0",
36-
"react-dom": "^18.2.0",
37-
"framer-motion": "^10.12.16",
30+
"eslint-config-react-app": "7.0.1",
31+
"eslint-plugin-jsx-a11y": "6.7.1",
32+
"eslint-plugin-react": "7.32.2",
33+
"npm-check-updates": "16.10.12",
34+
"prettier": "2.8.8",
3835
"typescript": "beta"
3936
},
4037
"files": [
4138
"dist"
4239
],
4340
"dependencies": {
44-
"react-full-screen": "^1.1.1"
41+
"react-full-screen": "1.1.1"
4542
}
4643
}

0 commit comments

Comments
 (0)