Skip to content

Commit

Permalink
refactor: adding options
Browse files Browse the repository at this point in the history
  • Loading branch information
anandtiwary committed Mar 31, 2021
1 parent f6dd439 commit 4aaf58a
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions projects/from-resize/src/resize/resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ export const fromResize: (
options?: ResizeOptions
) => Observable<ClientRect> = (
element: Element,
options: ResizeOptions = { emitOnStart: true }
options?: Partial<ResizeOptions>
) =>
new Observable((subscriber) => {
const resolvedOptions = {
...options,
...{ direction: ResizeDirection.All, emitOnStart: true },
};

const resizeObserver = new ResizeObserver((entries) => {
entries.forEach((entry: { target: Element; contentRect: ClientRect }) => {
entries.forEach((entry) => {
if (entry.target === element) {
subscriber.next(entry.contentRect);
}
Expand All @@ -18,7 +23,7 @@ export const fromResize: (

resizeObserver.observe(element);

if (options.emitOnStart) {
if (resolvedOptions.emitOnStart) {
subscriber.next(element.getBoundingClientRect());
}

Expand All @@ -27,4 +32,11 @@ export const fromResize: (

export interface ResizeOptions {
emitOnStart: boolean;
direction: ResizeDirection;
}

export const enum ResizeDirection {
All = "all",
Horizontal = "horizontal",
Vertical = "vertical",
}

0 comments on commit 4aaf58a

Please sign in to comment.