-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* delay initialization of Dialog We were using a useLayoutEffect, now let's use a useEffect instead. It still moves focus to the correct element, but that process is now a bit delayed. This means that users will less-likely be urged to "hack" around the issue by using fake focusable elements which will result in worse accessibility. * add hook to deal with server handoff This will allow us to delay certain features. For example we can delay the focus trapping until it is fully hydrated. We can also delay rendering the Portal to ensure hydration works correctly. * use server handoff complete hook * update changelog
- Loading branch information
1 parent
ab92811
commit c13e6b7
Showing
8 changed files
with
47 additions
and
18 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
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
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 |
---|---|---|
@@ -1,28 +1,25 @@ | ||
import { useState, useEffect } from 'react' | ||
import { useState } from 'react' | ||
import { useIsoMorphicEffect } from './use-iso-morphic-effect' | ||
import { useServerHandoffComplete } from './use-server-handoff-complete' | ||
|
||
// We used a "simple" approach first which worked for SSR and rehydration on the client. However we | ||
// didn't take care of the Suspense case. To fix this we used the approach the @reach-ui/auto-id | ||
// uses. | ||
// | ||
// Credits: https://github.com/reach/reach-ui/blob/develop/packages/auto-id/src/index.tsx | ||
|
||
let state = { serverHandoffComplete: false } | ||
let id = 0 | ||
function generateId() { | ||
return ++id | ||
} | ||
|
||
export function useId() { | ||
let [id, setId] = useState(state.serverHandoffComplete ? generateId : null) | ||
let ready = useServerHandoffComplete() | ||
let [id, setId] = useState(ready ? generateId : null) | ||
|
||
useIsoMorphicEffect(() => { | ||
if (id === null) setId(generateId()) | ||
}, [id]) | ||
|
||
useEffect(() => { | ||
if (state.serverHandoffComplete === false) state.serverHandoffComplete = true | ||
}, []) | ||
|
||
return id != null ? '' + id : undefined | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/@headlessui-react/src/hooks/use-server-handoff-complete.ts
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,19 @@ | ||
import { useState, useEffect } from 'react' | ||
|
||
let state = { serverHandoffComplete: false } | ||
|
||
export function useServerHandoffComplete() { | ||
let [serverHandoffComplete, setServerHandoffComplete] = useState(state.serverHandoffComplete) | ||
|
||
useEffect(() => { | ||
if (serverHandoffComplete === true) return | ||
|
||
setServerHandoffComplete(true) | ||
}, [serverHandoffComplete]) | ||
|
||
useEffect(() => { | ||
if (state.serverHandoffComplete === false) state.serverHandoffComplete = true | ||
}, []) | ||
|
||
return serverHandoffComplete | ||
} |