UNPKG

google-publisher-tag

Version:

Type-safe React wrapper for Google Publisher Tag (GPT).

29 lines (28 loc) 1.01 kB
"use client"; import { jsx as _jsx } from "react/jsx-runtime"; import { useEffect, useRef } from "react"; import { useGPT } from "./provider"; import { loadGPT } from "./loader"; export function AdSlot({ id, adUnitPath, sizes, sizeMapping, targeting, style, className, display = true, }) { const { defineSlot } = useGPT(); const handleRef = useRef(null); useEffect(() => { let mounted = true; defineSlot({ id, adUnitPath, sizes, sizeMapping, targeting }).then((h) => { if (!mounted) return; handleRef.current = h; if (display) { loadGPT().then((googletag) => { googletag.cmd.push(() => googletag.display(id)); }); } }); return () => { mounted = false; handleRef.current?.destroy(); handleRef.current = null; }; }, [id, adUnitPath]); return _jsx("div", { id: id, style: style, className: className }); }