-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
95 additions
and
58 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
48 changes: 48 additions & 0 deletions
48
packages/docusaurus-theme-common/src/hooks/useCodeWordWrap.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,48 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {useState, useCallback, useEffect} from 'react'; | ||
|
||
export function useCodeWordWrap(codeBlockRef: React.RefObject<HTMLPreElement>) { | ||
const [isEnabled, setIsEnabled] = useState(false); | ||
const [isCodeScrollable, setIsCodeScrollable] = useState<boolean>(false); | ||
|
||
const toggle = useCallback(() => { | ||
const codeElement = codeBlockRef.current!.querySelector('code')!; | ||
|
||
if (isEnabled) { | ||
codeElement.removeAttribute('style'); | ||
} else { | ||
codeElement.style.whiteSpace = 'pre-wrap'; | ||
} | ||
|
||
setIsEnabled((value) => !value); | ||
}, [codeBlockRef, isEnabled]); | ||
|
||
const updateCodeIsScrollable = useCallback(() => { | ||
const {scrollWidth, clientWidth} = codeBlockRef.current!; | ||
const isScrollable = | ||
scrollWidth > clientWidth || | ||
codeBlockRef.current!.querySelector('code')!.hasAttribute('style'); | ||
setIsCodeScrollable(isScrollable); | ||
}, [codeBlockRef]); | ||
|
||
useEffect(() => { | ||
updateCodeIsScrollable(); | ||
|
||
window.addEventListener('resize', updateCodeIsScrollable, { | ||
passive: true, | ||
}); | ||
|
||
return () => { | ||
window.removeEventListener('resize', updateCodeIsScrollable); | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
return {isEnabled, isCodeScrollable, toggle}; | ||
} |
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