Skip to content

Commit

Permalink
Hoist styles for Route Announcer 📢 (#39331)
Browse files Browse the repository at this point in the history
Hi,

- Remove inlined CSS style which have minor performance loss. And hoist it to top of the component.
> Each time you inline an object, React re-creates a new reference to this object on every render. This causes components that receive this object to treat it as a referentially different one which have some performance pitfalls.


_Extremely sorry if I made any mistakes :(_
  • Loading branch information
sanjaiyan-dev committed Aug 5, 2022
1 parent 869a2fe commit d88fd14
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions packages/next/client/route-announcer.tsx
@@ -1,7 +1,22 @@
import React from 'react'
import { useRouter } from './router'

export function RouteAnnouncer() {
const nextjsRouteAnnouncerStyles: React.CSSProperties = {
border: 0,
clip: 'rect(0 0 0 0)',
height: '1px',
margin: '-1px',
overflow: 'hidden',
padding: 0,
position: 'absolute',
width: '1px',

// https://medium.com/@jessebeach/beware-smushed-off-screen-accessible-text-5952a4c2cbfe
whiteSpace: 'nowrap',
wordWrap: 'normal',
}

export const RouteAnnouncer = () => {
const { asPath } = useRouter()
const [routeAnnouncement, setRouteAnnouncement] = React.useState('')

Expand Down Expand Up @@ -39,20 +54,7 @@ export function RouteAnnouncer() {
aria-live="assertive" // Make the announcement immediately.
id="__next-route-announcer__"
role="alert"
style={{
border: 0,
clip: 'rect(0 0 0 0)',
height: '1px',
margin: '-1px',
overflow: 'hidden',
padding: 0,
position: 'absolute',
width: '1px',

// https://medium.com/@jessebeach/beware-smushed-off-screen-accessible-text-5952a4c2cbfe
whiteSpace: 'nowrap',
wordWrap: 'normal',
}}
style={nextjsRouteAnnouncerStyles}
>
{routeAnnouncement}
</p>
Expand Down

0 comments on commit d88fd14

Please sign in to comment.