Skip to content

Commit ed83104

Browse files
mcollinaavivkellerovflowdbmuenzenmeyer
authored
feat: add Carbon Ads Top Bar and Sidebar placements (#9250)
* chore: carbon ads Co-authored-by: Claudio Wunder <cwunder@gnome.org> Co-authored-by: Brian Muenzenmeyer <brian.muenzenmeyer@gmail.com> Co-authored-by: Matteo Collina <hello@matteocollina.com> * fixup! * chore: re-add docs * chore: selectively re-add tests * chore: add sidebar ad on blog posts addresses https://github.com/nodejs/nodejs.org/pull/9250\#discussion_r4449949953 * fix: attempt to preserve space while loading to avoid CLS * fix: light validation of ad responses to as to not crash the page --------- Co-authored-by: avivkeller <me@aviv.sh> Co-authored-by: Claudio Wunder <cwunder@gnome.org> Co-authored-by: Brian Muenzenmeyer <brian.muenzenmeyer@gmail.com>
1 parent 827e51d commit ed83104

17 files changed

Lines changed: 842 additions & 1 deletion

File tree

‎.changeset/smart-lilies-invent.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@node-core/ui-components': minor
3+
---
4+
5+
Adds a footer slot to the metabar
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use client';
2+
3+
import classNames from 'classnames';
4+
5+
import useCarbonAd, { adColors } from '#site/hooks/useCarbonAd';
6+
import useClientContext from '#site/hooks/useClientContext';
7+
8+
import type { FC } from 'react';
9+
10+
import styles from './index.module.css';
11+
12+
const CarbonSidebar: FC = () => {
13+
const { frontmatter } = useClientContext();
14+
15+
// Must match where each layout's grid gives the MetaBar a right-hand column
16+
const media =
17+
frontmatter.layout === 'blog-post'
18+
? '(min-width: 896px)'
19+
: '(min-width: 1280px)';
20+
21+
const { ad, ref } = useCarbonAd('CWBI6KJU', media);
22+
23+
if (!ad) {
24+
return null;
25+
}
26+
27+
return (
28+
<aside
29+
ref={ref}
30+
className={styles.sideBar}
31+
style={adColors(ad)}
32+
aria-label="Advertisement"
33+
>
34+
<a
35+
href={ad.statlink}
36+
target="_blank"
37+
rel="sponsored noopener"
38+
className={styles.sideBarLink}
39+
>
40+
{/* eslint-disable-next-line @next/next/no-img-element */}
41+
<img src={ad.largeImage} alt={ad.company} className={styles.image} />
42+
<span className={styles.sideBarBody}>
43+
{ad.title && <strong>{ad.title}</strong>}
44+
<span>{ad.description}</span>
45+
<span className={styles.sideBarCta}>{ad.callToAction}</span>
46+
</span>
47+
</a>
48+
49+
<a
50+
href={ad.ad_via_link}
51+
className={classNames(styles.badge, styles.overlay)}
52+
>
53+
Ad
54+
</a>
55+
</aside>
56+
);
57+
};
58+
59+
export default CarbonSidebar;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use client';
2+
3+
import useCarbonAd, { adColors } from '#site/hooks/useCarbonAd';
4+
5+
import type { FC } from 'react';
6+
7+
import styles from './index.module.css';
8+
9+
const CarbonTopBar: FC = () => {
10+
const { ad, loading, ref } = useCarbonAd('CWBI6KJY');
11+
12+
if (!ad) {
13+
return loading ? (
14+
<aside aria-hidden="true" className={styles.topBarPlaceholder} />
15+
) : null;
16+
}
17+
18+
return (
19+
<aside
20+
ref={ref}
21+
className={styles.topBar}
22+
style={adColors(ad)}
23+
aria-label="Advertisement"
24+
>
25+
<a
26+
href={ad.statlink}
27+
target="_blank"
28+
rel="sponsored noopener"
29+
className={styles.topBarLink}
30+
>
31+
{/* eslint-disable-next-line @next/next/no-img-element */}
32+
<img src={ad.image} alt={ad.company} className={styles.logo} />
33+
<span>{ad.description}</span>
34+
<span className={styles.cta}>{ad.callToAction}</span>
35+
</a>
36+
37+
<a href={ad.ad_via_link} className={styles.badge}>
38+
Ad
39+
</a>
40+
</aside>
41+
);
42+
};
43+
44+
export default CarbonTopBar;
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
@reference "../../styles/index.css";
2+
3+
.topBar,
4+
.sideBar {
5+
@apply relative
6+
bg-(--ad-bg)
7+
text-(color:--ad-text)
8+
dark:bg-(--ad-bg-dark)
9+
dark:text-(color:--ad-text-dark);
10+
11+
& a {
12+
@apply text-inherit
13+
no-underline;
14+
}
15+
}
16+
17+
.badge {
18+
@apply shrink-0
19+
rounded-sm
20+
border
21+
border-current
22+
px-1.5
23+
text-xs
24+
leading-4
25+
opacity-70
26+
hover:opacity-100;
27+
}
28+
29+
.topBar {
30+
@apply mb-4
31+
flex
32+
items-center
33+
gap-3
34+
rounded-md
35+
px-4
36+
py-2
37+
text-sm
38+
lg:min-h-14;
39+
}
40+
41+
/* Matches the banner's minimum height, so that it doesn't shift the page when it loads */
42+
.topBarPlaceholder {
43+
@apply mb-4
44+
hidden
45+
lg:block
46+
lg:h-14;
47+
48+
/* While hidden, it still stops the next heading from matching `first:mt-0` in markdown.css */
49+
& + :is(h1, h2, h3, h4, h5, h6) {
50+
@apply max-lg:mt-0;
51+
}
52+
}
53+
54+
.topBarLink {
55+
@apply flex
56+
flex-1
57+
items-center
58+
gap-3;
59+
}
60+
61+
.logo {
62+
@apply size-8
63+
shrink-0
64+
rounded-sm;
65+
}
66+
67+
.cta {
68+
@apply hidden
69+
shrink-0
70+
rounded-md
71+
bg-(--ad-cta-bg)
72+
px-3
73+
py-1
74+
text-xs
75+
font-semibold
76+
whitespace-nowrap
77+
text-(color:--ad-cta-text)
78+
sm:block
79+
dark:bg-(--ad-cta-bg-dark)
80+
dark:text-(color:--ad-cta-text-dark);
81+
}
82+
83+
.sideBar {
84+
@apply w-44
85+
shrink-0
86+
overflow-hidden
87+
rounded-lg;
88+
}
89+
90+
.sideBarLink {
91+
@apply flex
92+
flex-col;
93+
}
94+
95+
.image {
96+
@apply aspect-square
97+
w-full
98+
object-cover;
99+
}
100+
101+
.sideBarBody {
102+
@apply flex
103+
flex-col
104+
items-start
105+
gap-1
106+
p-3.5
107+
text-xs;
108+
}
109+
110+
.sideBarCta {
111+
@apply mt-2
112+
rounded-full
113+
border
114+
border-current
115+
px-3
116+
py-1
117+
font-semibold;
118+
}
119+
120+
.overlay {
121+
@apply absolute
122+
top-2
123+
right-2
124+
border-transparent
125+
bg-neutral-950/50
126+
text-white;
127+
}

‎apps/site/components/MetaBar.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { defaultLocale } from '@node-core/website-i18n';
66
import { useFormatter, useLocale, useTranslations } from 'next-intl';
77

88
import AvatarGroup from '#site/components/AvatarGroup';
9+
import CarbonSidebar from '#site/components/CarbonAds/Sidebar';
910
import Link from '#site/components/Link';
1011
import useClientContext from '#site/hooks/useClientContext';
1112
import useMediaQuery from '#site/hooks/useMediaQuery';
@@ -74,6 +75,7 @@ const MetaBar: FC = () => {
7475
),
7576
}}
7677
headings={{ items: headings }}
78+
footer={<CarbonSidebar />}
7779
/>
7880
);
7981
};

0 commit comments

Comments
 (0)