Repository navigation
feat(ui-components): close ChangeHistory dropdown on outside click #9299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hrithiksaini99
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
hrithiksaini99:feat-change-history-click-outside
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
packages/ui-components/src/Common/ChangeHistory/Details.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| 'use client'; | ||
|
|
||
| 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> | ||
| ); | ||
| }; | ||
72 changes: 72 additions & 0 deletions
72
packages/ui-components/src/Common/ChangeHistory/__tests__/index.test.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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 aschildrenvia the RSC island pattern. Only this outerChangeHistoryDetailswrapper uses'use client'to attach the documentpointerdownoutside-click listener.That said, if we want to avoid introducing a client component here altogether:
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.Would you prefer using the native Popover API or an alternative pattern here?