Skip to content

Commit

Permalink
Merge fb036a5 into 07ae681
Browse files Browse the repository at this point in the history
  • Loading branch information
daybrush committed Jun 2, 2023
2 parents 07ae681 + fb036a5 commit 96aae84
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/conveyer/src/Conveyer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
ConveyerOptions, FindItemOptions, ConveyerReactiveState,
ScrollIntoViewOptions,
} from "./types";
import { isString } from "./utils";
import { instanceOfElement, isString } from "./utils";


/**
Expand Down Expand Up @@ -360,12 +360,16 @@ class Conveyer extends Component<ConveyerEvents> {

if (isString(scrollArea)) {
el = document.querySelector<HTMLElement>(scrollArea)!;
} else if (scrollArea instanceof Element) {
} else if (instanceOfElement(scrollArea)) {
el = scrollArea;
} else if ("value" in scrollArea || "current" in scrollArea) {
el = scrollArea.value! || scrollArea.current!;
}

if (!el) {
return;
}

this._scrollAreaElement = el;
let isDrag = false;
const scrollAreaElement = this._scrollAreaElement;
Expand Down
5 changes: 5 additions & 0 deletions packages/conveyer/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ export function findIndex<T>(arr: T[], callback: (element: T, index: number) =>
}
return -1;
}


export function instanceOfElement(el: any): el is Element {
return el instanceof Element || ("tagName" in el && "nodeType" in el);
}
18 changes: 18 additions & 0 deletions packages/conveyer/test/unit/Conveyer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,24 @@ describe("test Conveyer", () => {
expect(conveyer.isReachStart).to.be.equal(true);
expect(conveyer.isReachEnd).to.be.equal(true);
});
it("should check if it works even if there is a contaminated Element", () => {

// Given
const items = document.querySelector<HTMLElement>(".items")!;

// When
const Element = window.Element;
(window as any).Element = () => {};

try {
conveyer = new Conveyer(items);
} catch (e) {}

window.Element = Element;

// Thwn
expect(items.style.userSelect).to.be.equals("none");
});
});
describe("Reactvie Properties", () => {
it("should check if isReachStart is true, isReachEnd is false if init", () => {
Expand Down

0 comments on commit 96aae84

Please sign in to comment.