Skip to content
Merged
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
21 changes: 12 additions & 9 deletions packages/@react-aria/landmark/src/useLandmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,12 @@ class LandmarkManager {
this.landmarks.splice(insertPosition, 0, newLandmark);
}

public updateLandmark(landmark: Landmark) {
this.landmarks = this.landmarks.map((prevLandmark) => prevLandmark.ref.current === landmark.ref.current ? {...prevLandmark, ...landmark} : prevLandmark);
this.checkLabels(landmark.role);
public updateLandmark(landmark: Pick<Landmark, 'ref'> & Partial<Landmark>) {
let index = this.landmarks.findIndex(l => l.ref === landmark.ref);
if (index >= 0) {
this.landmarks[index] = {...this.landmarks[index], ...landmark};
this.checkLabels(this.landmarks[index].role);
}
}

public removeLandmark(ref: MutableRefObject<HTMLElement>) {
Expand Down Expand Up @@ -240,12 +243,12 @@ class LandmarkManager {
public focusinHandler(e: FocusEvent) {
let currentLandmark = this.closestLandmark(e.target as HTMLElement);
if (currentLandmark && currentLandmark.ref.current !== e.target) {
this.updateLandmark({...currentLandmark, lastFocused: e.target as HTMLElement});
this.updateLandmark({ref: currentLandmark.ref, lastFocused: e.target as HTMLElement});
}
let previousFocusedElment = e.relatedTarget as HTMLElement;
if (previousFocusedElment) {
let closestPreviousLandmark = this.closestLandmark(previousFocusedElment);
if (closestPreviousLandmark && closestPreviousLandmark.ref.current === previousFocusedElment) {
let previousFocusedElement = e.relatedTarget as HTMLElement;
if (previousFocusedElement) {
let closestPreviousLandmark = this.closestLandmark(previousFocusedElement);
if (closestPreviousLandmark && closestPreviousLandmark.ref.current === previousFocusedElement) {
closestPreviousLandmark.blur();
}
}
Expand Down Expand Up @@ -284,7 +287,7 @@ export function useLandmark(props: AriaLandmarkProps, ref: MutableRefObject<HTML
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
useLayoutEffect(() => {
manager.updateLandmark({ref, label, role, focus, blur});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [label, ref, role]);
Expand Down
50 changes: 48 additions & 2 deletions packages/@react-aria/landmark/test/useLandmark.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,11 @@ describe('LandmarkManager', function () {
});

afterAll(function () {
offsetWidth.mockReset();
offsetWidth.mockRestore();
Copy link
Member

Choose a reason for hiding this comment

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

? did something happen?

Copy link
Member Author

@reidbarber reidbarber Mar 2, 2022

Choose a reason for hiding this comment

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

Question for @LFDanLu

Copy link
Member

Choose a reason for hiding this comment

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

Remnant from my old changes when I had mocked the console.warn, kept the mockRestore change since it feels more accurate than mockReset from what I saw from the docs: https://jestjs.io/docs/mock-function-api#mockfnmockrestore.

offsetHeight.mockReset();
});

afterEach(() => {

act(() => {jest.runAllTimers();});
});

Expand Down Expand Up @@ -1072,4 +1071,51 @@ describe('LandmarkManager', function () {

expect(onKeyDown).toHaveBeenCalled();
});

it('updates the landmark if the label changes', function () {
let spyWarn = jest.spyOn(console, 'warn').mockImplementation(() => {});
let tree = render(
<div>
<Navigation aria-label="nav label 1">
<ul>
<li><a href="/home">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</Navigation>
<Navigation aria-label="nav label 2">
<ul>
<li><a href="/product">Product</a></li>
<li><a href="/support">Support</a></li>
</ul>
</Navigation>
<Main>
<TextField label="First Name" />
</Main>
</div>
);

expect(spyWarn).not.toHaveBeenCalled();
tree.rerender(
<div>
<Navigation aria-label="nav label 1">
<ul>
<li><a href="/home">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</Navigation>
<Navigation aria-label="nav label 1">
<ul>
<li><a href="/product">Product</a></li>
<li><a href="/support">Support</a></li>
</ul>
</Navigation>
<Main>
<TextField label="First Name" />
</Main>
</div>
);
expect(spyWarn).toHaveBeenCalledWith('Page contains more than one landmark with the \'navigation\' role and \'nav label 1\' label. If two or more landmarks on a page share the same role, they must have unique labels.');
});
});