import React, { useEffect } from 'react';
import type { Decorator } from '@storybook/react-vite';
import styles from './preventIframeFromScrollingParent.module.css';

/**
 * Prevents story iframes from scrolling main autodocs page.
 */
export const PreventIframeFromScrollingParent: Decorator = (Story, ctx) => {
    useEffect(() => {
        if (ctx.parameters?.preventIframeFromScrollingParent) {
            document.body.classList.add(styles.preventScrollParent);
        }

        return () => document.body.classList.remove(styles.preventScrollParent);
    }, [ctx.parameters?.preventIframeFromScrollingParent]);

    return <Story />;
};
