forked from redwoodjs/redwood
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganize router utils (redwoodjs#5659)
- Loading branch information
Showing
5 changed files
with
69 additions
and
75 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,52 @@ | ||
/** | ||
* gets the announcement for the new page. | ||
* called in one of active-route-loader's `useEffect`. | ||
* | ||
* the order of priority is: | ||
* 1. RouteAnnouncement (the most specific one) | ||
* 2. h1 | ||
* 3. document.title | ||
* 4. location.pathname | ||
*/ | ||
export const getAnnouncement = () => { | ||
const routeAnnouncement = global?.document.querySelectorAll( | ||
'[data-redwood-route-announcement]' | ||
)?.[0] | ||
if (routeAnnouncement?.textContent) { | ||
return routeAnnouncement.textContent | ||
} | ||
|
||
const pageHeading = global?.document.querySelector(`h1`) | ||
if (pageHeading?.textContent) { | ||
return pageHeading.textContent | ||
} | ||
|
||
if (global?.document.title) { | ||
return document.title | ||
} | ||
|
||
return `new page at ${global?.location.pathname}` | ||
} | ||
|
||
export const getFocus = () => { | ||
const routeFocus = global?.document.querySelectorAll( | ||
'[data-redwood-route-focus]' | ||
)?.[0] | ||
|
||
if ( | ||
!routeFocus || | ||
!routeFocus.children.length || | ||
(routeFocus.children[0] as HTMLElement).tabIndex < 0 | ||
) { | ||
return null | ||
} | ||
|
||
return routeFocus.children[0] as HTMLElement | ||
} | ||
|
||
// note: tried document.activeElement.blur(), but that didn't reset the focus flow | ||
export const resetFocus = () => { | ||
global?.document.body.setAttribute('tabindex', '-1') | ||
global?.document.body.focus() | ||
global?.document.body.removeAttribute('tabindex') | ||
} |
This file contains 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
This file contains 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