mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-20 11:37:52 +02:00
feat(theme-classic): toggle code wrap button (#7036)
Co-authored-by: Sébastien Lorber <slorber@users.noreply.github.com> Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
This commit is contained in:
parent
c3add31ebf
commit
4e4aa6add7
33 changed files with 204 additions and 24 deletions
|
@ -0,0 +1,56 @@
|
|||
/**
|
||||
* 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 type {RefObject} from 'react';
|
||||
import {useState, useCallback, useEffect, useRef} from 'react';
|
||||
|
||||
export function useCodeWordWrap(): {
|
||||
readonly codeBlockRef: RefObject<HTMLPreElement>;
|
||||
readonly isEnabled: boolean;
|
||||
readonly isCodeScrollable: boolean;
|
||||
readonly toggle: () => void;
|
||||
} {
|
||||
const [isEnabled, setIsEnabled] = useState(false);
|
||||
const [isCodeScrollable, setIsCodeScrollable] = useState<boolean>(false);
|
||||
const codeBlockRef = useRef<HTMLPreElement>(null);
|
||||
|
||||
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();
|
||||
}, [isEnabled, updateCodeIsScrollable]);
|
||||
|
||||
useEffect(() => {
|
||||
window.addEventListener('resize', updateCodeIsScrollable, {
|
||||
passive: true,
|
||||
});
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('resize', updateCodeIsScrollable);
|
||||
};
|
||||
}, [updateCodeIsScrollable]);
|
||||
|
||||
return {codeBlockRef, isEnabled, isCodeScrollable, toggle};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue