Skip to content

Commit

Permalink
return useRef() value directly
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Apr 22, 2022
1 parent e8d2841 commit 0464aa9
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions packages/docusaurus-theme-common/src/hooks/useCodeWordWrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,21 @@
* LICENSE file in the root directory of this source tree.
*/

import type {MutableRefObject} from 'react';
import type {RefObject} from 'react';
import {useState, useCallback, useEffect, useRef} from 'react';

export function useCodeWordWrap(): {
readonly codeBlockRef: (node: HTMLPreElement | null) => void;
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 codeBlock = useRef() as MutableRefObject<HTMLPreElement>;
const codeBlockRef = useCallback((node: HTMLPreElement | null) => {
if (node !== null) {
codeBlock.current = node;
}
}, []);
const codeBlockRef = useRef<HTMLPreElement>(null);

const toggle = useCallback(() => {
const codeElement = codeBlock.current.querySelector('code')!;
const codeElement = codeBlockRef.current!.querySelector('code')!;

if (isEnabled) {
codeElement.removeAttribute('style');
Expand All @@ -33,15 +28,15 @@ export function useCodeWordWrap(): {
}

setIsEnabled((value) => !value);
}, [codeBlock, isEnabled]);
}, [codeBlockRef, isEnabled]);

const updateCodeIsScrollable = useCallback(() => {
const {scrollWidth, clientWidth} = codeBlock.current;
const {scrollWidth, clientWidth} = codeBlockRef.current!;
const isScrollable =
scrollWidth > clientWidth ||
codeBlock.current.querySelector('code')!.hasAttribute('style');
codeBlockRef.current!.querySelector('code')!.hasAttribute('style');
setIsCodeScrollable(isScrollable);
}, [codeBlock]);
}, [codeBlockRef]);

useEffect(() => {
updateCodeIsScrollable();
Expand All @@ -55,8 +50,7 @@ export function useCodeWordWrap(): {
return () => {
window.removeEventListener('resize', updateCodeIsScrollable);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [updateCodeIsScrollable]);

return {codeBlockRef, isEnabled, isCodeScrollable, toggle};
}

0 comments on commit 0464aa9

Please sign in to comment.