Skip to content

Commit 1375ec7

Browse files
authored
feat: animate in the Carbon Ads Top Bar (#9343)
* feat: animate in the Carbon Ads Top Bar * refactor: use Tailwind animation for the Carbon Ads Top Bar * fix: respect reduced motion for banner animations * fix: animate the Carbon Ads Top Bar to its intrinsic height * fix: collapse surrounding space before the Carbon Ads Top Bar animates in
1 parent 73b1a59 commit 1375ec7

11 files changed

Lines changed: 71 additions & 51 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@node-core/ui-components': patch
3+
---
4+
5+
Skip the `Banner` animation for visitors who prefer reduced motion.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@node-core/ui-components': patch
3+
---
4+
5+
`animate-fade-and-slide-down` now ends at the element's own padding, and also collapses its minimum height and bottom margin.

‎.changeset/main-gap-variable.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': patch
3+
---
4+
5+
Expose the `main` gap as `--main-gap`, and let `animate-fade-and-slide-down` collapse surrounding space via `--fade-and-slide-down-offset`.

‎apps/site/components/CarbonAds/TopBar.tsx‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ import type { FC } from 'react';
77
import styles from './index.module.css';
88

99
const CarbonTopBar: FC = () => {
10-
const { ad, loading, ref } = useCarbonAd('CWBI6KJY');
10+
const { ad, ref } = useCarbonAd('CWBI6KJY');
1111

1212
if (!ad) {
13-
return loading ? (
14-
<aside aria-hidden="true" className={styles.topBarPlaceholder} />
15-
) : null;
13+
return null;
1614
}
1715

1816
return (
@@ -22,21 +20,23 @@ const CarbonTopBar: FC = () => {
2220
style={adColors(ad)}
2321
aria-label="Advertisement"
2422
>
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>
23+
<div className={styles.topBarContent}>
24+
<a
25+
href={ad.statlink}
26+
target="_blank"
27+
rel="sponsored noopener"
28+
className={styles.topBarLink}
29+
>
30+
{/* eslint-disable-next-line @next/next/no-img-element */}
31+
<img src={ad.image} alt={ad.company} className={styles.logo} />
32+
<span>{ad.description}</span>
33+
<span className={styles.cta}>{ad.callToAction}</span>
34+
</a>
35+
36+
<a href={ad.ad_via_link} className={styles.badge}>
37+
Ad
38+
</a>
39+
</div>
4040
</aside>
4141
);
4242
};

‎apps/site/components/CarbonAds/index.module.css‎

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,32 @@
2727
}
2828

2929
.topBar {
30-
@apply mb-4
31-
flex
32-
items-center
33-
gap-3
30+
@apply animate-fade-and-slide-down
31+
mb-4
32+
grid
33+
grid-rows-1
34+
overflow-hidden
3435
rounded-md
3536
px-4
3637
py-2
3738
text-sm
39+
[--fade-and-slide-down-offset:var(--main-gap)]
40+
motion-reduce:animate-none
3841
lg:min-h-14;
39-
}
4042

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;
43+
/* Until it animates in, it also cancels the parent's gap, and the margin it adds to a following heading */
44+
&:first-child:has(+ :is(h1, h2, h3, h4, h5, h6)) {
45+
@apply [--fade-and-slide-down-offset:calc(var(--main-gap)+--spacing(4))];
5146
}
5247
}
5348

49+
.topBarContent {
50+
@apply flex
51+
min-h-0
52+
items-center
53+
gap-3;
54+
}
55+
5456
.topBarLink {
5557
@apply flex
5658
flex-1

‎apps/site/layouts/layouts.module.css‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@
105105

106106
main {
107107
@apply max-w-7xl
108-
gap-4
109108
px-4
110109
py-12
110+
[--main-gap:--spacing(4)]
111111
md:px-14
112112
lg:px-28;
113113
}
@@ -166,8 +166,8 @@
166166
dark:border-l-neutral-900;
167167

168168
main {
169-
@apply gap-4
170-
wrap-anywhere;
169+
@apply wrap-anywhere
170+
[--main-gap:--spacing(4)];
171171
}
172172
}
173173

‎apps/site/tests/e2e/carbon-ads.spec.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ test.describe('Carbon Ads', () => {
129129
'/en/about',
130130
'/en/blog/announcements/diving-into-the-nodejs-website-redesign',
131131
]) {
132-
test(`should not shift ${path} when the Top Bar loads`, async ({
132+
test(`should only render the Top Bar on ${path} once an ad is available`, async ({
133133
page,
134134
}) => {
135135
await mockAds(page);
@@ -147,17 +147,14 @@ test.describe('Carbon Ads', () => {
147147

148148
await page.goto(path);
149149

150-
// Only the position matters: the heading's size can change as fonts load
151-
const heading = page.locator('main h1').first();
152-
const before = await heading.boundingBox();
150+
const topBar = page.locator('main > aside');
151+
await expect(topBar).toHaveCount(0);
153152

154153
respond();
155154

156155
await expect(adLinks(page)).toHaveCount(2);
157156

158-
const after = await heading.boundingBox();
159-
160-
expect(after?.y).toBe(before?.y);
157+
await expect(topBar).toHaveCount(1);
161158
});
162159
}
163160
});

‎docs/carbon-ads.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ Learn (`/learn`) and the API docs (`/api`, `/docs`) are separate applications an
2828
## How it works
2929

3030
- `apps/site/hooks/useCarbonAd.ts` requests the ad for the current page view and sends its view beacon. A page view changes with the pathname or locale (not with hashes or query strings), so a revisited page always gets a fresh ad.
31-
- `apps/site/components/CarbonAds/TopBar.tsx` and `apps/site/components/CarbonAds/Sidebar.tsx` render each format. From the `lg` breakpoint, the Top Bar reserves its minimum height while loading, so one- and two-line creatives don't shift the page; longer ones can still shift it slightly. Narrower screens reserve no space, because the height varies too much with the creative's length, and neither does the Sidebar, because it is the last item in its column. When there is no ad (empty response, invalid data, network error or ad blocker) nothing is shown, and any reserved space collapses.
31+
- `apps/site/components/CarbonAds/TopBar.tsx` and `apps/site/components/CarbonAds/Sidebar.tsx` render each format. Neither reserves space while loading. When there is no ad (empty response, invalid data, network error or ad blocker) nothing is shown.
3232
- The ad request itself records the impression. The `statview` beacon is sent once per page view after at least 50% of the ad was visible for one continuous second. Clicks go through `statlink`.
3333

34+
Once an ad is available, the Top Bar uses the announcement banner's `animate-fade-and-slide-down` animation to expand, fade and slide in. It takes up no space until then, including the parent's gap (from the `--main-gap` variable on `main`) and the margin it adds to a following heading. Visitors who prefer reduced motion see it immediately, without the animation.
35+
3436
## Staging and production
3537

3638
By default, every request is sent with `ignore=yes`, which returns a real ad **without counting the impression**. This applies to local development, tests and all preview deployments (which are production builds too).

‎packages/ui-components/src/Common/Banner/index.module.css‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
grid-rows-1
99
px-8
1010
py-3
11-
text-sm;
11+
text-sm
12+
motion-reduce:animate-none;
1213

1314
&,
1415
a {

‎packages/ui-components/src/styles/animations.css‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,20 @@
8989
}
9090
}
9191

92+
/* Expands a grid with a single `min-h-0` row from nothing, ending at the element's own spacing.
93+
* `--fade-and-slide-down-offset` also collapses any space around it, such as the parent's gap */
9294
@keyframes fade-and-slide-down {
9395
from {
9496
grid-template-rows: 0fr;
97+
margin-bottom: calc(-1 * var(--fade-and-slide-down-offset, 0px));
98+
min-height: 0;
9599
opacity: 0;
96100
padding-block: 0;
97101
transform: translateY(-100%);
98102
}
99103

100104
to {
101105
grid-template-rows: 1fr;
102-
opacity: 1;
103-
padding-block: calc(var(--spacing, 0.25rem) * 3);
104-
transform: translateY(0);
105106
}
106107
}
107108
}

0 commit comments

Comments
 (0)