index.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import React from "react";
  2. import Script from "next/script";
  3. import { IoMdClose } from "react-icons/io";
  4. import styled from "styled-components";
  5. const StyledCloseBtn = styled.button`
  6. display: none;
  7. width: 3vw;
  8. height: 3vw;
  9. opacity: 0.8;
  10. justify-content: center;
  11. align-items: center;
  12. position: absolute;
  13. top: 0;
  14. right: 0;
  15. border-radius: 0;
  16. background: ${({ theme }) => theme.DANGER};
  17. `;
  18. const StyledWrapper = styled.span<{ editor?: boolean }>`
  19. position: relative;
  20. #carbonads {
  21. width: 100%;
  22. display: flex;
  23. }
  24. ${({ theme, editor }) =>
  25. editor &&
  26. `
  27. #carbonads {
  28. border-radius: 0;
  29. border-top: 1px solid ${theme.BACKGROUND_MODIFIER_ACCENT};
  30. }
  31. #carbonads > span {
  32. max-width: 100%;
  33. width: 100%;
  34. }
  35. `};
  36. &:hover {
  37. ${StyledCloseBtn} {
  38. display: flex;
  39. }
  40. }
  41. @media all and (display-mode: standalone) {
  42. #carbonads {
  43. display: none;
  44. }
  45. }
  46. `;
  47. export const CarbonAds: React.FC<{ editor?: boolean }> = ({ editor = false }) => {
  48. const [isHidden, setIsHidden] = React.useState(false);
  49. if (isHidden) return null;
  50. return (
  51. <StyledWrapper editor={editor} id="carbon-wrapper">
  52. {editor && (
  53. <StyledCloseBtn onClick={() => setIsHidden(true)}>
  54. <IoMdClose color="white" size="15" />
  55. </StyledCloseBtn>
  56. )}
  57. <Script
  58. type="text/javascript"
  59. src="https://cdn.carbonads.com/carbon.js?serve=CE7IPKQL&placement=jsonvisiocom"
  60. id="_carbonads_js"
  61. strategy="lazyOnload"
  62. onLoad={() => {
  63. const init = () => {
  64. const parent = document.getElementById("carbon-wrapper");
  65. const ads = document.getElementById("carbonads");
  66. if (ads === null) return setTimeout(() => init(), 500);
  67. parent?.appendChild(ads);
  68. };
  69. init();
  70. }}
  71. />
  72. </StyledWrapper>
  73. );
  74. };