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
40 changes: 40 additions & 0 deletions packages/ui-components/src/Common/ChangeHistory/Details.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use client';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it a client component, which isn't ideal...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @avivkeller, thanks for the review!

Just to clarify the architecture here: (index.tsx) remains a pure Server Component without 'use client'—all the version history entries, links, icons, and summaries are pre-rendered entirely on the server and passed through as children via the RSC island pattern. Only this outer ChangeHistoryDetails wrapper uses 'use client' to attach the document pointerdown outside-click listener.

That said, if we want to avoid introducing a client component here altogether:

  1. HTML Popover API (popover="auto"): Modern browsers natively support light-dismiss / click-outside on popovers in pure declarative HTML without any React client component or JavaScript event listeners.
  2. Pure CSS backdrop: A fixed transparent backdrop element rendered when the menu opens (e.g. via pseudo-element or sibling overlay in CSS) that dismisses or closes the focus.

Would you prefer using the native Popover API or an alternative pattern here?


import {
useRef,
useEffect,
type FC,
type ComponentProps,
type ReactNode,
} from 'react';

type ChangeHistoryDetailsProps = ComponentProps<'details'> & {
children?: ReactNode;
};

export const ChangeHistoryDetails: FC<ChangeHistoryDetailsProps> = ({
children,
...props
}) => {
const detailsRef = useRef<HTMLDetailsElement | null>(null);

useEffect(() => {
const handlePointerDown = (e: PointerEvent) => {
const details = detailsRef.current;
if (details && details.open && !details.contains(e.target as Node)) {
details.removeAttribute('open');
}
};

document.addEventListener('pointerdown', handlePointerDown);
return () => {
document.removeEventListener('pointerdown', handlePointerDown);
};
}, []);

return (
<details ref={detailsRef} {...props}>
{children}
</details>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';

import { render, screen, fireEvent } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import ChangeHistory from '../index';

const sampleChanges = [
{
label: 'v18.0.0',
versions: ['v18.0.0'],
content: 'Initial implementation',
url: 'https://github.com/nodejs/node/pull/1',
},
{
label: 'v16.0.0',
versions: ['v16.0.0'],
content: 'Deprecated feature',
},
];

describe('ChangeHistory', () => {
it('renders summary label and changes properly', () => {
render(<ChangeHistory label="History" changes={sampleChanges} />);

assert.ok(screen.getByText('History'));
assert.ok(screen.getByText('Initial implementation'));
assert.ok(screen.getByText('Deprecated feature'));
});

it('closes dropdown when clicking outside', async () => {
const { container } = render(
<div>
<button type="button">Outside Button</button>
<ChangeHistory label="History" changes={sampleChanges} />
</div>
);

const details = container.querySelector('details');
assert.ok(details);

// Open details
details.setAttribute('open', '');
assert.equal(details.hasAttribute('open'), true);

// Click outside
const outsideButton = screen.getByRole('button', { name: 'Outside Button' });
fireEvent.pointerDown(outsideButton);

assert.equal(details.hasAttribute('open'), false);
});

it('does not close when clicking inside the details dropdown', async () => {
const { container } = render(
<ChangeHistory label="History" changes={sampleChanges} />
);

const details = container.querySelector('details');
assert.ok(details);

// Open details
details.setAttribute('open', '');
assert.equal(details.hasAttribute('open'), true);

// Click inside
const insideItem = screen.getByText('Initial implementation');
fireEvent.pointerDown(insideItem);

assert.equal(details.hasAttribute('open'), true);
});
});
6 changes: 4 additions & 2 deletions packages/ui-components/src/Common/ChangeHistory/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import classNames from 'classnames';
import type { LinkLike } from '#ui/types';
import type { FC, ComponentProps, ReactNode } from 'react';

import { ChangeHistoryDetails } from './Details';

import styles from './index.module.css';

export type HistoryChange = {
Expand All @@ -28,7 +30,7 @@ const ChangeHistory: FC<ChangeHistoryProps> = ({
...props
}) => (
<div className={classNames('relative', 'inline-block', className)} {...props}>
<details className="group">
<ChangeHistoryDetails className="group">
<summary className={styles.summary} role="button" aria-haspopup="menu">
<ClockIcon className="size-4" />
<span>{label}</span>
Expand Down Expand Up @@ -63,7 +65,7 @@ const ChangeHistory: FC<ChangeHistoryProps> = ({
})}
</div>
</div>
</details>
</ChangeHistoryDetails>
</div>
);

Expand Down