mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-30 15:00:09 +02:00
refactor(core): replace useDocusaurusContext().isClient by useIsBrowser() (#5349)
* extract separate useIsClient() hook * for consistency, rename to `useIsBrowser` * useless return * improve doc for BrowserOnly * update snapshot * polish
This commit is contained in:
parent
69b11a8546
commit
295e77cc09
20 changed files with 213 additions and 90 deletions
|
@ -6,7 +6,7 @@
|
|||
*/
|
||||
|
||||
import React, {ComponentProps, ReactElement, useRef, useState} from 'react';
|
||||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
||||
import useIsBrowser from '@docusaurus/useIsBrowser';
|
||||
import clsx from 'clsx';
|
||||
import {useCollapsible, Collapsible} from '../Collapsible';
|
||||
import styles from './styles.module.css';
|
||||
|
@ -30,7 +30,7 @@ export type DetailsProps = {
|
|||
} & ComponentProps<'details'>;
|
||||
|
||||
const Details = ({summary, children, ...props}: DetailsProps): JSX.Element => {
|
||||
const {isClient} = useDocusaurusContext();
|
||||
const isBrowser = useIsBrowser();
|
||||
const detailsRef = useRef<HTMLDetailsElement>(null);
|
||||
|
||||
const {collapsed, setCollapsed} = useCollapsible({
|
||||
|
@ -48,7 +48,7 @@ const Details = ({summary, children, ...props}: DetailsProps): JSX.Element => {
|
|||
data-collapsed={collapsed}
|
||||
className={clsx(
|
||||
styles.details,
|
||||
{[styles.isClient]: isClient},
|
||||
{[styles.isBrowser]: isBrowser},
|
||||
props.className,
|
||||
)}>
|
||||
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events */}
|
||||
|
|
|
@ -46,9 +46,9 @@ CSS variables, meant to be overriden by final theme
|
|||
}
|
||||
|
||||
/* When JS disabled/failed to load: we use the open property for arrow animation: */
|
||||
.details[open]:not(.isClient) > summary:before,
|
||||
.details[open]:not(.isBrowser) > summary:before,
|
||||
/* When JS works: we use the data-attribute for arrow animation */
|
||||
.details[data-collapsed='false'].isClient > summary:before {
|
||||
.details[data-collapsed='false'].isBrowser > summary:before {
|
||||
transform: rotate(90deg);
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ import React, {
|
|||
useContext,
|
||||
createContext,
|
||||
} from 'react';
|
||||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
||||
import useIsBrowser from '@docusaurus/useIsBrowser';
|
||||
import {createStorageSlot} from './storageUtils';
|
||||
import {useThemeConfig} from './useThemeConfig';
|
||||
|
||||
|
@ -39,10 +39,10 @@ type AnnouncementBarAPI = {
|
|||
|
||||
const useAnnouncementBarContextValue = (): AnnouncementBarAPI => {
|
||||
const {announcementBar} = useThemeConfig();
|
||||
const {isClient} = useDocusaurusContext();
|
||||
const isBrowser = useIsBrowser();
|
||||
|
||||
const [isClosed, setClosed] = useState(() => {
|
||||
return isClient
|
||||
return isBrowser
|
||||
? // On client navigation: init with localstorage value
|
||||
isDismissedInStorage()
|
||||
: // On server/hydration: always visible to prevent layout shifts (will be hidden with css if needed)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue