Skip to content

Commit 0af0710

Browse files
authored
Merge pull request #1061 from equinor/fix/waves-aspect-ratio
fix/waves-aspect-ratio
2 parents 85d671f + 505a384 commit 0af0710

File tree

4 files changed

+63
-27
lines changed

4 files changed

+63
-27
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@equinor/amplify-component-lib",
3-
"version": "9.8.0",
3+
"version": "9.8.1",
44
"description": "Frontend Typescript components for the Amplify team",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/molecules/Waves/WaveStatic.tsx

+9-18
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
1-
import { FC, useEffect, useState } from 'react';
1+
import { FC } from 'react';
22

33
import { NoiseSvg } from './NoiseSvg'; // Assuming it's another SVG file
44
import { WaveStaticSvg } from './WaveStaticSvg'; // Assuming it's an SVG file
55

66
interface WaveStaticProps {
7+
width: number;
8+
height: number;
79
gradientColors?: string[];
810
}
911

10-
export const WaveStatic: FC<WaveStaticProps> = ({ gradientColors }) => {
11-
const [viewBox, setViewBox] = useState(
12-
`0 0 ${window.innerWidth} ${window.innerHeight - 64}`
13-
);
14-
15-
useEffect(() => {
16-
const resizeHandler = (event: Event) => {
17-
const target = event.target as Window;
18-
setViewBox(`0 0 ${target.innerWidth} ${target.innerHeight - 64}`);
19-
};
20-
21-
window.addEventListener('resize', resizeHandler);
22-
23-
return () => {
24-
window.removeEventListener('resize', resizeHandler);
25-
};
26-
}, []);
12+
export const WaveStatic: FC<WaveStaticProps> = ({
13+
width,
14+
height,
15+
gradientColors,
16+
}) => {
17+
const viewBox = `0 0 ${width} ${height}`;
2718

2819
return (
2920
<svg

src/molecules/Waves/Waves.styles.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { motion } from 'framer-motion';
12
import styled from 'styled-components';
23

3-
export const Container = styled.div`
4+
export const Container = styled(motion.div)`
45
height: calc(100vh - 64px);
56
width: 100%;
67
overflow: hidden;
7-
position: relative;
8+
position: absolute;
89
> svg {
910
position: absolute;
1011
z-index: 0;

src/molecules/Waves/Waves.tsx

+50-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,58 @@
1-
import { FC } from 'react';
1+
import { FC, useEffect, useRef, useState } from 'react';
22

33
import { Container } from './Waves.styles';
44
import { WaveStatic } from './WaveStatic';
55

6+
import { useScroll } from 'framer-motion';
7+
68
export interface WaveProps {
79
gradientColors?: string[];
810
}
911

10-
export const Waves: FC<WaveProps> = ({ gradientColors }) => (
11-
<Container>
12-
<WaveStatic gradientColors={gradientColors} />
13-
</Container>
14-
);
12+
export const Waves: FC<WaveProps> = ({ gradientColors }) => {
13+
const containerRef = useRef<HTMLDivElement | null>(null);
14+
const [width, setWidth] = useState(containerRef.current?.clientWidth ?? 0);
15+
const [height, setHeight] = useState(containerRef.current?.clientHeight ?? 0);
16+
const { scrollY } = useScroll({
17+
container: { current: document.getElementById('content') },
18+
});
19+
20+
const handleSetRef = (element: HTMLDivElement | null) => {
21+
containerRef.current = element;
22+
23+
if (!containerRef.current) return;
24+
25+
setWidth(containerRef.current.clientWidth);
26+
setHeight(containerRef.current.clientHeight);
27+
};
28+
29+
useEffect(() => {
30+
const resizeHandler = () => {
31+
if (!containerRef.current) return;
32+
33+
setWidth(containerRef.current.clientWidth);
34+
setHeight(containerRef.current.clientHeight);
35+
};
36+
37+
window.addEventListener('resize', resizeHandler);
38+
39+
return () => {
40+
window.removeEventListener('resize', resizeHandler);
41+
};
42+
}, []);
43+
44+
return (
45+
<Container
46+
ref={handleSetRef}
47+
style={{
48+
top: scrollY,
49+
}}
50+
>
51+
<WaveStatic
52+
height={height}
53+
width={width}
54+
gradientColors={gradientColors}
55+
/>
56+
</Container>
57+
);
58+
};

0 commit comments

Comments
 (0)