Skip to content

Commit

Permalink
fix: top fade should be calculated on first render too
Browse files Browse the repository at this point in the history
if page had been scrolled and refreshed the wrong initial value was used
  • Loading branch information
peritpatrio committed May 31, 2024
1 parent ba41f50 commit 93eba92
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
1 change: 0 additions & 1 deletion components/Nav.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use client';

import Image from 'next/image';
import Link, { LinkProps } from 'next/link';
import Wrapper from './Wrapper';
import { useEffect, useRef, useState } from 'react';
Expand Down
18 changes: 9 additions & 9 deletions components/TopFade.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

import { useEffect, useState } from 'react';

const calculatedBrightnessValue = () => {
if (window.scrollY > 200) return 0.5;
const amountToDecrease = (window.scrollY / 200) * 0.5;
return 1 - amountToDecrease;
};

export default function TopFade() {
const [brightness, setBrightness] = useState(1);
const [brightness, setBrightness] = useState(calculatedBrightnessValue());

const handleScroll = (event: Event) => {
if (window.scrollY > 200) {
if (brightness === 0.5) return;
setBrightness(0.5);
return;
}

const amountToDecrease = (window.scrollY / 200) * 0.5;
setBrightness(1 - amountToDecrease);
if (window.scrollY > 200 && brightness === 0.5) return;
setBrightness(calculatedBrightnessValue());
};

useEffect(() => {
Expand Down

0 comments on commit 93eba92

Please sign in to comment.