Skip to content

Commit

Permalink
🐛 no triggerOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
provok-me committed Sep 10, 2021
1 parent 0feb063 commit f32e037
Showing 1 changed file with 37 additions and 20 deletions.
57 changes: 37 additions & 20 deletions packages/alice/src/utils/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,34 @@ export const getTriggerOffset = (
{ inputOptions: { triggerOffset } }: TweenObject,
boundings: Boundings
): TriggerOffsets => {
const inputOffset = !Array.isArray(triggerOffset)
? [triggerOffset, triggerOffset]
: triggerOffset;
const defaultValues: TriggerOffsets = [0, 0];

const {
top: triggerOffsetTop,
bottom: triggerOffsetBottom,
} = inputOffset.reduce(
(acc, offset, index) => {
let parsedOffset = 0;
const key = index === 0 ? 'top' : 'bottom';
if (!triggerOffset) {
return defaultValues;
}

let inputOffset = [triggerOffset, triggerOffset];

try {
if (Array.isArray(triggerOffset)) {
// Throw if more than two values.
if (triggerOffset.length !== 2) {
throw new Error(
'One of your triggerOffset option contains an array with less or more than two values.'
);
}

inputOffset = triggerOffset;
}

const {
top: triggerOffsetTop,
bottom: triggerOffsetBottom,
} = inputOffset.reduce(
(acc, offset, index) => {
let parsedOffset = 0;
const key = index === 0 ? 'top' : 'bottom';

try {
if (typeof offset === 'number' && !isNaN(offset)) {
parsedOffset = offset;
} else if (
Expand All @@ -65,18 +80,20 @@ export const getTriggerOffset = (
parseInt(offset.replace('%', ''), 10) * (boundings.height / 100);
} else {
throw new Error(
'There is a problem with the syntax of one of your triggerOffset option.'
`There is a problem with the syntax of one of your triggerOffset option: ${triggerOffset}`
);
}
} catch (error) {
console.error(error);
}
return { ...acc, [key]: parsedOffset };
},
{ top: 0, bottom: 0 }
);
return { ...acc, [key]: parsedOffset };
},
{ top: 0, bottom: 0 }
);

return [triggerOffsetTop, triggerOffsetBottom];
} catch (error) {
console.error(error);
}

return [triggerOffsetTop, triggerOffsetBottom];
return defaultValues;
};

export const isInView = (
Expand Down

0 comments on commit f32e037

Please sign in to comment.