Skip to content

Commit

Permalink
💸 Add Carbon Ads on the documentation website (#527)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliencrn authored Mar 5, 2024
1 parent 9ad8e61 commit 4cd934b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
8 changes: 8 additions & 0 deletions apps/www/src/app/(docs)/introduction/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CarbonAds } from '@/components/carbon-ads'
import { CommandCopy } from '@/components/command-copy'
import { DocsPageHeader } from '@/components/docs-page-header'
import { components } from '@/components/ui/components'
Expand Down Expand Up @@ -46,6 +47,13 @@ export default async function IntroductionPage() {
}}
/>
</div>
<aside className="hidden text-sm xl:block">
<div className="sticky top-16 -mt-10 max-h-[calc(var(--vh)-4rem)] overflow-y-auto pt-10">
<div className="my-10">
<CarbonAds />
</div>
</div>
</aside>
</main>
)
}
9 changes: 7 additions & 2 deletions apps/www/src/app/(docs)/react-hook/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Metadata } from 'next'
import { notFound } from 'next/navigation'

import { CarbonAds } from '@/components/carbon-ads'
import { DocsPageHeader } from '@/components/docs-page-header'
import { DocsPager } from '@/components/paper'
import { Mdx } from '@/components/remote-mdx'
Expand Down Expand Up @@ -83,11 +84,15 @@ const PostLayout = ({ params }: { params: { slug: string } }) => {
<hr className="my-4 md:my-6" />
<DocsPager slug={post.slug} />
</div>
<div className="hidden text-sm xl:block">
<aside className="hidden text-sm xl:block">
<div className="sticky top-16 -mt-10 max-h-[calc(var(--vh)-4rem)] overflow-y-auto pt-10">
<DashboardTableOfContents toc={toc} />

<div className="my-10">
<CarbonAds />
</div>
</div>
</div>
</aside>
</main>
)
}
Expand Down
14 changes: 14 additions & 0 deletions apps/www/src/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,18 @@
'rlig' 1,
'calt' 1;
}

/* Override the default styles for the Carbon Ads block */
.carbon-wrap * {
--carbon-bg-primary: hsl(var(--background));
--carbon-bg-secondary: hsl(var(--border));
--carbon-text-color: hsl(var(--foreground));

.carbon-responsive-wrap {
@apply !rounded-md;
}
.carbon-poweredby {
@apply !opacity-100 !text-muted-foreground;
}
}
}
46 changes: 46 additions & 0 deletions apps/www/src/components/carbon-ads.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use client'

import { useEffect, useRef } from 'react'

// TODO: We can't use usehooks-ts's useScript because it mounts the script in the document.body, maybe provide a way to specify the mount point
const useScript = (scriptUrl: string, scriptId: string) => {
const ref = useRef<HTMLDivElement>(null)

useEffect(() => {
const existingScript = document.getElementById(scriptId)

if (!existingScript) {
const script = document.createElement('script')

script.setAttribute('async', '')
script.setAttribute('type', 'text/javascript')
script.setAttribute('src', scriptUrl)
script.setAttribute('id', scriptId)

ref.current?.appendChild(script)
}

return () => {
if (existingScript) {
existingScript.remove()
}
}
}, [scriptUrl, scriptId])

useEffect(() => {
if (ref.current) {
ref.current.style.setProperty('--carbon-bg-primary', 'red', 'important')
}
}, [])

return ref
}

export function CarbonAds() {
const ref = useScript(
'//cdn.carbonads.com/carbon.js?serve=CWYIEKJU&placement=usehooks-tscom&format=cover',
'_carbonads_js',
)

return <div className="carbon-wrap" ref={ref} />
}

0 comments on commit 4cd934b

Please sign in to comment.