Skip to content

Commit 2b399fc

Browse files
1 parent 44ba772 commit 2b399fc

1 file changed

Lines changed: 44 additions & 4 deletions

File tree

packages/twenty-website-new/src/illustrations/Faq/Background.tsx

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { RoomEnvironment } from 'three/examples/jsm/environments/RoomEnvironment.js';
44
import { theme } from '@/theme';
55
import { styled } from '@linaria/react';
6-
import { useEffect, useRef } from 'react';
6+
import { useEffect, useLayoutEffect, useRef } from 'react';
77
import * as THREE from 'three';
88
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.js';
99
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
@@ -585,6 +585,45 @@ const FaqVisualCanvasMount = styled.div`
585585

586586
export function FaqBackground() {
587587
const mountReference = useRef<HTMLDivElement>(null);
588+
const shellReference = useRef<HTMLDivElement>(null);
589+
590+
useLayoutEffect(() => {
591+
const shell = shellReference.current;
592+
593+
if (!shell) {
594+
return;
595+
}
596+
597+
const initialHeight = shell.clientHeight;
598+
599+
if (initialHeight === 0) {
600+
return;
601+
}
602+
603+
const initialViewportHeight = window.innerHeight || initialHeight;
604+
605+
shell.style.bottom = 'auto';
606+
shell.style.height = `${initialHeight}px`;
607+
608+
let resizeFrameId = 0;
609+
610+
const handleWindowResize = () => {
611+
window.cancelAnimationFrame(resizeFrameId);
612+
resizeFrameId = window.requestAnimationFrame(() => {
613+
const currentViewportHeight =
614+
window.innerHeight || initialViewportHeight;
615+
const ratio = currentViewportHeight / initialViewportHeight;
616+
shell.style.height = `${Math.round(initialHeight * ratio)}px`;
617+
});
618+
};
619+
620+
window.addEventListener('resize', handleWindowResize);
621+
622+
return () => {
623+
window.cancelAnimationFrame(resizeFrameId);
624+
window.removeEventListener('resize', handleWindowResize);
625+
};
626+
}, []);
588627

589628
useEffect(() => {
590629
const container = mountReference.current;
@@ -743,7 +782,7 @@ export function FaqBackground() {
743782
const postScene = new THREE.Scene();
744783
postScene.add(new THREE.Mesh(fullScreenGeometry, halftoneMaterial));
745784

746-
const syncSize = () => {
785+
const applySize = () => {
747786
const width = getWidth();
748787
const height = getHeight();
749788
const virtualWidth = getVirtualWidth();
@@ -766,7 +805,8 @@ export function FaqBackground() {
766805
);
767806
};
768807

769-
const resizeObserver = new ResizeObserver(syncSize);
808+
applySize();
809+
const resizeObserver = new ResizeObserver(applySize);
770810
resizeObserver.observe(container);
771811

772812
loadFaqGeometry(GLB_URL)
@@ -891,7 +931,7 @@ export function FaqBackground() {
891931
}, []);
892932

893933
return (
894-
<FaqVisualShell>
934+
<FaqVisualShell ref={shellReference}>
895935
<FaqVisualCanvasMount aria-hidden ref={mountReference} />
896936
</FaqVisualShell>
897937
);

0 commit comments

Comments
 (0)