Skip to content

Commit fe759ea

Browse files
committed
chore: refactor interface
1 parent f83359a commit fe759ea

File tree

4 files changed

+33
-77
lines changed

4 files changed

+33
-77
lines changed

debug/state-api/index.html

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
<button class="navigation-btn" id="prev">Previous</button>
9494
<div class="flicking-viewport">
9595
<div class="flicking-camera">
96-
<div class="flicking-panel" style="width: 500px;">x0</div>
96+
<div class="flicking-panel" style="width: 500px">x0</div>
9797
<div class="flicking-panel">x1</div>
9898
<div class="flicking-panel">x2</div>
9999
<div class="flicking-panel">x3</div>
@@ -125,7 +125,10 @@
125125
<script src="../../dist/flicking.pkgd.js"></script>
126126
<script>
127127
document.addEventListener("DOMContentLoaded", () => {
128-
const flicking = new Flicking(".flicking-viewport", {renderOnlyVisible: true});
128+
const flicking = new Flicking(".flicking-viewport", {
129+
renderOnlyVisible: true,
130+
// circular: true
131+
});
129132
const { connectFlickingStateApi } = Flicking;
130133
const reactiveObj = connectFlickingStateApi(flicking);
131134
const {
@@ -237,13 +240,10 @@
237240
});
238241
});
239242

240-
reactiveObj.subscribe("indexProgress", (nextValue) => {
241-
progressFillElement.style.width = `${nextValue}%`;
242-
});
243-
244-
245-
const scrollProgressFillElement = document.getElementById("scroll-progress-fill");
246-
reactiveObj.subscribe("scrollProgress", (nextValue) => {
243+
const scrollProgressFillElement = document.getElementById(
244+
"scroll-progress-fill"
245+
);
246+
reactiveObj.subscribe("progress", (nextValue) => {
247247
scrollProgressFillElement.style.width = `${nextValue}%`;
248248
});
249249
});

packages/react-flicking/src/demo/StatePage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import LinearProgress from "@mui/material/LinearProgress";
1313
const DebugPage: React.FC = () => {
1414
const flickingRef = useRef<Flicking>(null);
1515

16-
const {isReachStart, isReachEnd, indexProgress, currentPanelIndex, totalPanelCount, moveTo} = useFlickingStateApi(flickingRef);
16+
const {isReachStart, isReachEnd, progress, currentPanelIndex, totalPanelCount, moveTo} = useFlickingStateApi(flickingRef);
1717

1818
const onClickPrev = () => {
1919
flickingRef.current?.prev();
@@ -90,7 +90,7 @@ const DebugPage: React.FC = () => {
9090
<Pagination count={totalPanelCount} page={currentPanelIndex+1} onChange={(e, v) => {
9191
moveTo(v-1);
9292
}}/>
93-
<LinearProgress variant="determinate" value={indexProgress}/>
93+
<LinearProgress variant="determinate" value={progress}/>
9494
{/* <LinearProgress variant="determinate" value={scrollProgress}/> */}
9595
</Stack>
9696
</div>

src/state-api/index.ts

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,17 @@ import {
77

88
import Flicking from "../Flicking";
99

10-
const getIsReachStart = (flicking: Flicking) => flicking.index === 0;
11-
const getIsReachEnd = (flicking: Flicking) =>
12-
flicking.index === flicking.panelCount - 1;
10+
const getIsReachStart = (flicking: Flicking) => !flicking.circular && flicking.index === 0;
11+
12+
const getIsReachEnd = (flicking: Flicking) => !flicking.circular && flicking.index === flicking.panelCount - 1;
13+
1314

1415
const getTotalPanelCount = (flicking: Flicking) => flicking.panelCount;
1516
const getCurrentPanelIndex = (flicking: Flicking) => flicking.index;
1617

17-
// Returns the progress percentage based on the current panel index.
18-
const getProgressByPanelIndex = (flicking: Flicking) =>
19-
flicking.panelCount > 1
20-
? (flicking.index / (flicking.panelCount - 1)) * 100
21-
: 100;
2218

2319
// Returns the progress percentage based on the current scroll position.
24-
const getProgressByScrollPos = (flicking: Flicking) => {
20+
const getProgress = (flicking: Flicking) => {
2521
const cam = flicking.camera;
2622

2723
const progressRatio = (cam.position - cam.range.min) / (cam.range.max - cam.range.min);
@@ -31,37 +27,33 @@ const getProgressByScrollPos = (flicking: Flicking) => {
3127
return percent;
3228
};
3329

34-
export type FlickingStateApi = ReactiveObject<{
35-
isReachStart: boolean;
36-
isReachEnd: boolean;
37-
totalPanelCount: number;
38-
currentPanelIndex: number;
39-
indexProgress: number;
40-
scrollProgress: number;
41-
moveTo: (i: number) => Promise<void> | undefined;
42-
}>;
30+
export type FlickingReactiveStateApi = ReactiveObject<FlickingReactiveState & FlickingReactiveMethod>;
4331

4432
export interface FlickingReactiveState {
4533
isReachStart: boolean;
4634
isReachEnd: boolean;
4735
totalPanelCount: number;
4836
currentPanelIndex: number;
49-
indexProgress: number;
50-
scrollProgress: number;
37+
progress: number;
38+
}
39+
40+
export interface FlickingReactiveMethod {
41+
moveTo: (i: number) => Promise<void>;
5142
}
5243

53-
const flickingStateApiAdapter: ReactiveSetupAdapter<FlickingStateApi, FlickingReactiveState, "moveTo"> = ({ onInit, onDestroy }) => {
44+
const flickingStateApiAdapter: ReactiveSetupAdapter<FlickingReactiveStateApi, FlickingReactiveState, "moveTo"> = ({ onInit, onDestroy }) => {
5445
let flicking: Flicking | null = null;
5546

56-
const moveTo = (i: number) => flicking?.moveTo(i);
47+
const moveTo = (i: number) => {
48+
return flicking ? flicking.moveTo(i) : Promise.reject(new Error("Flicking instance is not available"));
49+
};
5750

5851
const reactiveObj = reactive({
5952
isReachStart: false,
6053
isReachEnd: false,
6154
totalPanelCount: 0,
6255
currentPanelIndex: 0,
63-
indexProgress: 0,
64-
scrollProgress: 0,
56+
progress: 0,
6557
moveTo
6658
});
6759

@@ -71,7 +63,6 @@ const flickingStateApiAdapter: ReactiveSetupAdapter<FlickingStateApi, FlickingRe
7163
reactiveObj.isReachStart = getIsReachStart(flicking);
7264
reactiveObj.isReachEnd = getIsReachEnd(flicking);
7365
reactiveObj.currentPanelIndex = getCurrentPanelIndex(flicking);
74-
reactiveObj.indexProgress = getProgressByPanelIndex(flicking);
7566
};
7667

7768
const onPanelChange = () => {
@@ -84,7 +75,7 @@ const flickingStateApiAdapter: ReactiveSetupAdapter<FlickingStateApi, FlickingRe
8475
const onMove = () => {
8576
if (flicking === null) return;
8677

87-
reactiveObj.scrollProgress = getProgressByScrollPos(flicking);
78+
reactiveObj.progress = getProgress(flicking);
8879
};
8980

9081
onInit((inst, data) => {
@@ -94,8 +85,7 @@ const flickingStateApiAdapter: ReactiveSetupAdapter<FlickingStateApi, FlickingRe
9485
reactiveObj.isReachStart = getIsReachStart(flicking);
9586
reactiveObj.isReachEnd = getIsReachEnd(flicking);
9687
reactiveObj.currentPanelIndex = getCurrentPanelIndex(flicking);
97-
reactiveObj.indexProgress = getProgressByPanelIndex(flicking);
98-
reactiveObj.scrollProgress = getProgressByScrollPos(flicking);
88+
reactiveObj.progress = getProgress(flicking);
9989

10090
reactiveObj.totalPanelCount = getTotalPanelCount(flicking);
10191

test/unit/state-api/state-api.spec.ts

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ describe("State API", () => {
3131
expect(stateApi.isReachEnd).to.be.false;
3232
expect(stateApi.totalPanelCount).to.equal(3);
3333
expect(stateApi.currentPanelIndex).to.equal(0);
34-
expect(stateApi.indexProgress).to.equal(0);
35-
expect(stateApi.scrollProgress).to.equal(0);
34+
expect(stateApi.progress).to.equal(0);
3635
});
3736
});
3837

@@ -45,8 +44,7 @@ describe("State API", () => {
4544
expect(stateApi.isReachStart).to.be.false;
4645
expect(stateApi.isReachEnd).to.be.false;
4746
expect(stateApi.currentPanelIndex).to.equal(1);
48-
expect(stateApi.indexProgress).to.equal(50);
49-
expect(stateApi.scrollProgress).to.equal(50);
47+
expect(stateApi.progress).to.equal(50);
5048
});
5149

5250
it("should update state when moving to last panel", async () => {
@@ -57,8 +55,7 @@ describe("State API", () => {
5755
expect(stateApi.isReachStart).to.be.false;
5856
expect(stateApi.isReachEnd).to.be.true;
5957
expect(stateApi.currentPanelIndex).to.equal(2);
60-
expect(stateApi.indexProgress).to.equal(100);
61-
expect(stateApi.scrollProgress).to.equal(100);
58+
expect(stateApi.progress).to.equal(100);
6259
});
6360

6461
it("should update state when adding panels", async () => {
@@ -83,38 +80,7 @@ describe("State API", () => {
8380
await promise;
8481

8582
expect(stateApi.currentPanelIndex).to.equal(1);
86-
expect(stateApi.indexProgress).to.equal(50);
87-
expect(stateApi.scrollProgress).to.equal(50);
88-
});
89-
});
90-
91-
describe("Progress", () => {
92-
it("should show difference between indexProgress and scrollProgress with different panel sizes", async () => {
93-
// Create flicking with panels of different sizes
94-
const customFlicking = await createFlicking(
95-
El.viewport("1000px", "400px").add(
96-
El.camera().add(
97-
El.panel("200px", "100%"), // 200px wide
98-
El.panel("400px", "100%"), // 400px wide
99-
El.panel("600px", "100%") // 600px wide
100-
)
101-
)
102-
);
103-
const customStateApi = connectFlickingStateApi(customFlicking);
104-
105-
// Move to second panel
106-
const promise = customFlicking.moveTo(1);
107-
tick(1000);
108-
await promise;
109-
110-
// indexProgress is based on panel index
111-
// scrollProgress is based on actual scroll position
112-
// They should be different when panels have different sizes
113-
expect(customStateApi.indexProgress).to.not.equal(
114-
customStateApi.scrollProgress
115-
);
116-
117-
await customStateApi.unsubscribe();
83+
expect(stateApi.progress).to.equal(50);
11884
});
11985
});
12086
});

0 commit comments

Comments
 (0)