Skip to content

Commit

Permalink
Use translateX instead of changing width
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny-signal committed Nov 11, 2022
1 parent 94a2351 commit 709588a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
3 changes: 1 addition & 2 deletions stylesheets/components/StoryViewer.scss
Expand Up @@ -280,10 +280,9 @@

&--bar {
background: $color-white;
background-size: 200% 100%;
border-radius: 2px;
display: block;
height: 100%;
transform: translateX(-100%);
}
}

Expand Down
61 changes: 41 additions & 20 deletions ts/components/StoryViewer.tsx
Expand Up @@ -284,12 +284,15 @@ export const StoryViewer = ({
);
const target = progressBarRef.current;

const animation = target.animate([{ width: '0%' }, { width: '100%' }], {
id: 'story-progress-bar',
duration: storyDuration,
easing: 'linear',
fill: 'forwards',
});
const animation = target.animate(
[{ transform: 'translateX(-100%)' }, { transform: 'translateX(0%)' }],
{
id: 'story-progress-bar',
duration: storyDuration,
easing: 'linear',
fill: 'forwards',
}
);

animationRef.current = animation;

Expand Down Expand Up @@ -411,27 +414,41 @@ export const StoryViewer = ({
return;
}

let lastMouseMove: number | undefined;
let mouseMoveExpiration: number | undefined;
let timer: NodeJS.Timeout | undefined;

function updateLastMouseMove() {
lastMouseMove = Date.now();
mouseMoveExpiration = Date.now() + MOUSE_IDLE_TIME;

if (timer === undefined) {
checkMouseIdle();
}
}

function checkMouseIdle() {
requestAnimationFrame(() => {
if (lastMouseMove && Date.now() - lastMouseMove > MOUSE_IDLE_TIME) {
setArrowToShow(Arrow.None);
} else {
checkMouseIdle();
}
});
timer = undefined;

if (mouseMoveExpiration === undefined) {
return;
}

const remaining = mouseMoveExpiration - Date.now();
if (remaining <= 0) {
setArrowToShow(Arrow.None);
return;
}

timer = setTimeout(checkMouseIdle, remaining);
}
checkMouseIdle();

document.addEventListener('mousemove', updateLastMouseMove);

return () => {
lastMouseMove = undefined;
if (timer !== undefined) {
clearTimeout(timer);
}
mouseMoveExpiration = undefined;
timer = undefined;
document.removeEventListener('mousemove', updateLastMouseMove);
};
}, [arrowToShow]);
Expand Down Expand Up @@ -723,9 +740,13 @@ export const StoryViewer = ({
) : (
<div
className="StoryViewer__progress--bar"
style={{
width: currentIndex < index ? '0%' : '100%',
}}
style={
currentIndex < index
? {}
: {
transform: 'translateX(0%)',
}
}
/>
)}
</div>
Expand Down

0 comments on commit 709588a

Please sign in to comment.