Files
blitzkit/packages/website/src/components/Embeds/PreviewWrapper.tsx
T

56 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-10-26 14:33:04 -05:00
import { Box } from '@radix-ui/themes';
import { useEffect, useRef } from 'react';
2024-11-01 10:52:54 -05:00
import { embedPreviews } from '../../constants/embeds';
import { useEmbedStateCurry } from '../../stores/embedState/utilities';
2024-10-26 14:33:04 -05:00
interface PreviewWrapperProps {
2024-10-26 19:46:35 -05:00
name: keyof typeof embedPreviews;
2024-10-28 10:57:42 -05:00
naked?: boolean;
2024-10-26 14:33:04 -05:00
}
2024-10-28 10:57:42 -05:00
export function PreviewWrapper({ name, naked }: PreviewWrapperProps) {
2024-10-28 10:30:53 -05:00
const { useEmbedState } = useEmbedStateCurry();
const width = useEmbedState('width');
const height = useEmbedState('height');
2024-10-26 19:46:35 -05:00
const Preview = embedPreviews[name];
2024-10-26 14:33:04 -05:00
const wrapper = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!wrapper.current) return;
2024-11-01 10:52:54 -05:00
wrapper.current.classList.remove('preview-container-animated');
2024-10-26 14:33:04 -05:00
void wrapper.current.offsetWidth;
2024-11-01 10:52:54 -05:00
wrapper.current.classList.add('preview-container-animated');
2024-10-26 14:33:04 -05:00
}, [width, height]);
2024-10-28 10:57:42 -05:00
if (naked) {
return (
<Box
position="absolute"
top="0"
left="50%"
style={{ transform: 'translate(-50%, 0) scale(0.75)' }}
width={`${width}px`}
height={`${height}px`}
>
<Preview />
</Box>
);
}
2024-10-26 14:33:04 -05:00
return (
<Box
2024-10-28 10:57:42 -05:00
position="absolute"
top="50%"
left="50%"
overflow="hidden"
style={{ transform: naked ? 'scale(0.25)' : 'translate(-50%, -50%)' }}
2024-10-26 14:33:04 -05:00
ref={wrapper}
2024-11-01 10:52:54 -05:00
className="preview-container-animated"
2024-10-26 14:33:04 -05:00
width={`${width}px`}
height={`${height}px`}
>
<Preview />
</Box>
);
}