UNPKG

react-commento

Version:
44 lines (43 loc) 1.5 kB
import React, { useEffect } from "react"; const CONTAINER_ID = "commento"; const SCRIPT_ID = "commento-script"; const COMMENTO_URL = "https://cdn.commento.io/js/commento.js"; const insertScript = (src, id, parentElement, dataAttributes) => { const script = window.document.createElement("script"); script.async = true; script.src = src; script.id = id; Object.entries(dataAttributes).forEach(([key, value]) => { if (value === undefined) { return; } script.setAttribute(`data-${key}`, value.toString()); }); parentElement.appendChild(script); }; const removeScript = (id, parentElement) => { const script = window.document.getElementById(id); if (script) { parentElement.removeChild(script); } }; const Commento = ({ id, cssOverride, autoInit, noFonts, hideDeleted, pageId }) => { useEffect(() => { if (!window) { return; } const document = window.document; if (document.getElementById("commento")) { insertScript(COMMENTO_URL, SCRIPT_ID, document.body, { "css-override": cssOverride, "auto-init": autoInit, "no-fonts": noFonts, "hide-deleted": hideDeleted, "page-id": pageId }); } return () => removeScript(SCRIPT_ID, document.body); }, [id]); return React.createElement("div", { key: id, id: CONTAINER_ID }); }; export default Commento;