-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create
useIsMacOS
hook for SSR support
- Loading branch information
1 parent
cec4e50
commit 8a37a6f
Showing
7 changed files
with
59 additions
and
29 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {isMacOS as ssrUnsafeIsMacOS} from '@primer/behaviors/utils' | ||
import {useEffect, useState} from 'react' | ||
|
||
/** | ||
* SSR-safe hook for determining if the current platform is MacOS. When rendering | ||
* server-side, will default to non-MacOS and then re-render in an effect if the | ||
* client turns out to be a MacOS device. | ||
*/ | ||
export function useIsMacOS() { | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
const [isMacOS, setIsMacOS] = useState(() => (window !== undefined ? ssrUnsafeIsMacOS() : false)) | ||
|
||
useEffect(() => setIsMacOS(ssrUnsafeIsMacOS()), []) | ||
|
||
return isMacOS | ||
} |