Skip to content

Commit

Permalink
MARKET-2350 [Animate on Scroll] Events from one triggered by another …
Browse files Browse the repository at this point in the history
…component. (#908)

* MARKET-2350 [Animate on Scroll] Events from one triggered by another component.

* fix event listener deletion

---------

Co-authored-by: serhiy.melnychuk <[email protected]>
  • Loading branch information
serhmel and serhmel authored Feb 7, 2024
1 parent c286f59 commit 1496bc9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 26 deletions.
7 changes: 3 additions & 4 deletions components/bl-animate-on-scroll/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ The component is based on external [Animate on Scroll](https://github.com/michal

## Events

| Name | Triggers | Context Blocks |
|--------------------|-----------------------------------|-----------------------|
| On Animation Enter | whenever any element animates in | Detail: `HTMLElement` |
| On Animation Out | whenever any element animates out | Detail: `HTMLElement` |
| Name | Triggers | Context Blocks |
|--------------------|--------------------------------|-----------------------|
| On Animation Event | whenever any element animates | Detail: `HTMLElement` |

## Usage

Expand Down
17 changes: 3 additions & 14 deletions components/bl-animate-on-scroll/component.json
Original file line number Diff line number Diff line change
Expand Up @@ -228,26 +228,15 @@
],
"eventHandlers": [
{
"name": "onAnimationEnter",
"label": "On Animation Enter",
"name": "onAnimation",
"label": "On Animation Event",
"contextBlocks": [
{
"id": "detail",
"label": "Detail"
}
],
"handlerDescription": "The event is fired whenever any element animates in."
},
{
"name": "onAnimationOut",
"label": "On Animation Out",
"contextBlocks": [
{
"id": "detail",
"label": "Detail"
}
],
"handlerDescription": "The event is fired whenever any element animates out."
"handlerDescription": "The event is fired whenever any element animates."
}
],
"actions": [],
Expand Down
15 changes: 7 additions & 8 deletions components/bl-animate-on-scroll/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,24 @@ const DevicesMap = {
none : false,
};

export default function AnimateOnScroll({ component, eventHandlers, settings, pods, elRef }) {
export default function AnimateOnScroll(props) {
const { component, eventHandlers: { onAnimation }, settings: { disableFor }, pods, elRef, instanceId } = props;
const {
display, classList, style, animationType, easing, side,
duration, offset, delay, mirror, once, anchor,
} = component;
const { disableFor } = settings;
const { onAnimationEnter, onAnimationOut } = eventHandlers;

const elementPod = pods['animationPod'];

const animationName = useMemo(() => animationType + (side === 'none' ? '' : `-${ side }`), [animationType, side]);

useEffect(() => {
Aos.init({ disable: DevicesMap[disableFor] });

document.addEventListener('aos:in', ({ detail }) => onAnimationEnter({ detail }));
document.addEventListener('aos:out', ({ detail }) => onAnimationOut({ detail }));
document.addEventListener(`aos:in:${ instanceId }`, ({ detail }) => onAnimation({ detail }));

return () => {
document.addEventListener('aos:in', ({ detail }) => onAnimationEnter({ detail }));
document.addEventListener('aos:out', ({ detail }) => onAnimationOut({ detail }));
document.removeEventListener(`aos:in:${ instanceId }`, ({ detail }) => onAnimation({ detail }));
};
}, []);

Expand All @@ -55,7 +53,8 @@ export default function AnimateOnScroll({ component, eventHandlers, settings, po
data-aos-mirror={ mirror }
data-aos-once={ once }
data-aos-anchor={ anchor ? '.' + anchor : null }
>
data-aos-id={ instanceId }>

{ elementPod.render() }
</div>
);
Expand Down

0 comments on commit 1496bc9

Please sign in to comment.