Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/banner-reduced-motion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@node-core/ui-components': patch
---

Skip the `Banner` animation for visitors who prefer reduced motion.
5 changes: 5 additions & 0 deletions .changeset/fade-and-slide-down-spacing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@node-core/ui-components': patch
---

`animate-fade-and-slide-down` now ends at the element's own padding, and also collapses its minimum height and bottom margin.
5 changes: 5 additions & 0 deletions .changeset/main-gap-variable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@node-core/ui-components': patch
---

Expose the `main` gap as `--main-gap`, and let `animate-fade-and-slide-down` collapse surrounding space via `--fade-and-slide-down-offset`.
38 changes: 19 additions & 19 deletions apps/site/components/CarbonAds/TopBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import type { FC } from 'react';
import styles from './index.module.css';

const CarbonTopBar: FC = () => {
const { ad, loading, ref } = useCarbonAd('CWBI6KJY');
const { ad, ref } = useCarbonAd('CWBI6KJY');

if (!ad) {
return loading ? (
<aside aria-hidden="true" className={styles.topBarPlaceholder} />
) : null;
return null;
}

return (
Expand All @@ -22,21 +20,23 @@ const CarbonTopBar: FC = () => {
style={adColors(ad)}
aria-label="Advertisement"
>
<a
href={ad.statlink}
target="_blank"
rel="sponsored noopener"
className={styles.topBarLink}
>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img src={ad.image} alt={ad.company} className={styles.logo} />
<span>{ad.description}</span>
<span className={styles.cta}>{ad.callToAction}</span>
</a>

<a href={ad.ad_via_link} className={styles.badge}>
Ad
</a>
<div className={styles.topBarContent}>
<a
href={ad.statlink}
target="_blank"
rel="sponsored noopener"
className={styles.topBarLink}
>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img src={ad.image} alt={ad.company} className={styles.logo} />
<span>{ad.description}</span>
<span className={styles.cta}>{ad.callToAction}</span>
</a>

<a href={ad.ad_via_link} className={styles.badge}>
Ad
</a>
</div>
</aside>
);
};
Expand Down
32 changes: 17 additions & 15 deletions apps/site/components/CarbonAds/index.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,32 @@
}

.topBar {
@apply mb-4
flex
items-center
gap-3
@apply animate-fade-and-slide-down
mb-4
grid
grid-rows-1
overflow-hidden
rounded-md
px-4
py-2
text-sm
[--fade-and-slide-down-offset:var(--main-gap)]
motion-reduce:animate-none
lg:min-h-14;
}

/* Matches the banner's minimum height, so that it doesn't shift the page when it loads */
.topBarPlaceholder {
@apply mb-4
hidden
lg:block
lg:h-14;

/* While hidden, it still stops the next heading from matching `first:mt-0` in markdown.css */
& + :is(h1, h2, h3, h4, h5, h6) {
@apply max-lg:mt-0;
/* Until it animates in, it also cancels the parent's gap, and the margin it adds to a following heading */
&:first-child:has(+ :is(h1, h2, h3, h4, h5, h6)) {
@apply [--fade-and-slide-down-offset:calc(var(--main-gap)+--spacing(4))];
}
}

.topBarContent {
@apply flex
min-h-0
items-center
gap-3;
}

.topBarLink {
@apply flex
flex-1
Expand Down
6 changes: 3 additions & 3 deletions apps/site/layouts/layouts.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@

main {
@apply max-w-7xl
gap-4
px-4
py-12
[--main-gap:--spacing(4)]
md:px-14
lg:px-28;
}
Expand Down Expand Up @@ -166,8 +166,8 @@
dark:border-l-neutral-900;

main {
@apply gap-4
wrap-anywhere;
@apply wrap-anywhere
[--main-gap:--spacing(4)];
}
}

Expand Down
11 changes: 4 additions & 7 deletions apps/site/tests/e2e/carbon-ads.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ test.describe('Carbon Ads', () => {
'/en/about',
'/en/blog/announcements/diving-into-the-nodejs-website-redesign',
]) {
test(`should not shift ${path} when the Top Bar loads`, async ({
test(`should only render the Top Bar on ${path} once an ad is available`, async ({
page,
}) => {
await mockAds(page);
Expand All @@ -147,17 +147,14 @@ test.describe('Carbon Ads', () => {

await page.goto(path);

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

respond();

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

const after = await heading.boundingBox();

expect(after?.y).toBe(before?.y);
await expect(topBar).toHaveCount(1);
});
}
});
4 changes: 3 additions & 1 deletion docs/carbon-ads.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ Learn (`/learn`) and the API docs (`/api`, `/docs`) are separate applications an
## How it works

- `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.
- `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.
- `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.
- 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`.

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.

## Staging and production

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).
Expand Down
3 changes: 2 additions & 1 deletion packages/ui-components/src/Common/Banner/index.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
grid-rows-1
px-8
py-3
text-sm;
text-sm
motion-reduce:animate-none;

&,
a {
Expand Down
7 changes: 4 additions & 3 deletions packages/ui-components/src/styles/animations.css
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,20 @@
}
}

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

to {
grid-template-rows: 1fr;
opacity: 1;
padding-block: calc(var(--spacing, 0.25rem) * 3);
transform: translateY(0);
}
}
}
6 changes: 4 additions & 2 deletions packages/ui-components/src/styles/markdown.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* The gap is a variable, so that children can account for it */
main {
@apply max-ml:gap-4
@apply max-ml:[--main-gap:--spacing(4)]
flex
w-full
flex-col
gap-6;
gap-(--main-gap)
[--main-gap:--spacing(6)];

hr {
@apply w-full
Expand Down
Loading