Skip to content

Commit a1e7cc2

Browse files
authored
fix: generate scroll animation css from size (#463)
* fix: generate scroll animation css with size * chore: update
1 parent 9a5c415 commit a1e7cc2

File tree

4 files changed

+64
-38
lines changed

4 files changed

+64
-38
lines changed

src/components/pages/schema-migration/hero/hero.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Image from 'next/image';
44
import { useEffect, useRef, useState } from 'react';
55

66
import Pill from '@/components/shared/pill';
7-
import '@/styles/sql-editor-hero.css';
7+
import { generateScrollAnimationCSS } from '@/utils/hero-animate';
88

99
type HeroProps = {
1010
// subjects is an array of comparison subjects.
@@ -51,7 +51,15 @@ const Hero = ({ subjects }: HeroProps) => {
5151
{initialed && isSubjectsScrollable ? (
5252
<>
5353
<div className="absolute z-[1] h-4 w-full bg-white blur-md" />
54-
<div className="scroll-animation flex flex-col items-end justify-center">
54+
<style
55+
// eslint-disable-next-line react/no-danger
56+
dangerouslySetInnerHTML={{
57+
__html: generateScrollAnimationCSS(scrollableSubjects.length),
58+
}}
59+
/>
60+
<div
61+
className={`scroll-animation-${scrollableSubjects.length} flex flex-col items-end justify-center`}
62+
>
5563
{scrollableSubjects.map((subject, index) => (
5664
// eslint-disable-next-line react/no-array-index-key
5765
<mark key={`${subject}-${index}`} className="bg-transparent text-primary-1">

src/components/pages/sql-editor/hero/hero.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Image from 'next/image';
44
import { useEffect, useRef, useState } from 'react';
55

66
import Pill from '@/components/shared/pill';
7-
import '@/styles/sql-editor-hero.css';
7+
import { generateScrollAnimationCSS } from '@/utils/hero-animate';
88

99
type HeroProps = {
1010
// subjects is an array of comparison subjects.
@@ -51,7 +51,15 @@ const Hero = ({ subjects }: HeroProps) => {
5151
{initialed && isSubjectsScrollable ? (
5252
<>
5353
<div className="absolute z-[1] h-4 w-full bg-white blur-md" />
54-
<div className="scroll-animation flex flex-col items-end justify-center">
54+
<style
55+
// eslint-disable-next-line react/no-danger
56+
dangerouslySetInnerHTML={{
57+
__html: generateScrollAnimationCSS(scrollableSubjects.length),
58+
}}
59+
/>
60+
<div
61+
className={`scroll-animation-${scrollableSubjects.length} flex flex-col items-end justify-center`}
62+
>
5563
{scrollableSubjects.map((subject, index) => (
5664
// eslint-disable-next-line react/no-array-index-key
5765
<mark key={`${subject}-${index}`} className="bg-transparent text-primary-1">

src/styles/sql-editor-hero.css

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/utils/hero-animate.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Generates a CSS string for a scroll animation with a specified number of steps.
3+
*
4+
* @param size - The number of steps in the scroll animation.
5+
* @returns A string containing the CSS for the scroll animation.
6+
*
7+
* The generated CSS includes:
8+
* - Keyframes for the scroll animation, where each step translates the element vertically.
9+
* - A CSS class that applies the animation with a duration based on the number of steps.
10+
*/
11+
export const generateScrollAnimationCSS = (size: number): string => {
12+
const keyframes: string[] = [];
13+
const stepPercentage = 100 / (size - 1);
14+
const transformStep = 100 / size;
15+
const animateDuration = 3;
16+
17+
for (let i = 0; i < size; i++) {
18+
const percentage = i * stepPercentage;
19+
const transformValue = -i * transformStep;
20+
21+
keyframes.push(`
22+
${(i - 0.5) * stepPercentage}% {
23+
transform: translateY(${transformValue}%);
24+
}
25+
${percentage}% {
26+
transform: translateY(${transformValue}%);
27+
}
28+
`);
29+
}
30+
31+
const keyframesCSS = `
32+
@keyframes hero-scroll-${size} {
33+
${keyframes.join('')}
34+
}
35+
`;
36+
37+
return `
38+
.scroll-animation-${size} {
39+
animation: hero-scroll-${size} ${animateDuration * (size - 1)}s ease-in-out infinite;
40+
animation-delay: ${animateDuration}s;
41+
}
42+
${keyframesCSS}
43+
`;
44+
};

0 commit comments

Comments
 (0)