Skip to content

Commit

Permalink
fix: fix size NaN (#834)
Browse files Browse the repository at this point in the history
  • Loading branch information
daybrush committed Nov 8, 2023
1 parent bd3a341 commit 6927c3e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,33 +313,36 @@ export const getElementSize = ({
useOffset: boolean;
style: CSSStyleDeclaration;
}): number => {
let size = 0;
if (useFractionalSize) {
const baseSize = parseFloat(horizontal ? style.width : style.height);
const baseSize = parseFloat(horizontal ? style.width : style.height) || 0;
const isBorderBoxSizing = style.boxSizing === "border-box";
const border = horizontal
? parseFloat(style.borderLeftWidth || "0") + parseFloat(style.borderRightWidth || "0")
: parseFloat(style.borderTopWidth || "0") + parseFloat(style.borderBottomWidth || "0");

if (isBorderBoxSizing) {
return useOffset
size = useOffset
? baseSize
: baseSize - border;
} else {
const padding = horizontal
? parseFloat(style.paddingLeft || "0") + parseFloat(style.paddingRight || "0")
: parseFloat(style.paddingTop || "0") + parseFloat(style.paddingBottom || "0");

return useOffset
size = useOffset
? baseSize + padding + border
: baseSize + padding;
}
} else {
const sizeStr = horizontal ? "Width" : "Height";

return useOffset
size = useOffset
? el[`offset${sizeStr}`]
: el[`client${sizeStr}`];
}

return Math.max(size, 0);
};

export const setPrototypeOf = Object.setPrototypeOf || ((obj, proto) => {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/Flicking.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,16 @@ describe("Flicking", () => {
expect(flicking.camera.position).to.equal(flicking.panels[0].position);
expect(flicking.camera.element.style.transform).to.equal("translate(0px)");
});
it("should have zero size with not appended elements", async () => {
const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, { autoInit: false });

flicking.viewport.element.remove();
flicking.init(); // Not awaiting on purpose

expect(flicking.viewport.width).equal(0);
expect(flicking.viewport.height).equal(0);
expect(flicking.panels.every(panel => panel.size === 0)).equal(true);
});

it("should place camera & panels at correct position on circular rendering", async () => {
const flicking = await createFlicking(
Expand Down

0 comments on commit 6927c3e

Please sign in to comment.