-
-
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.
feat(theme-classic): toggle code wrap button (#7036)
Co-authored-by: Sébastien Lorber <[email protected]> Co-authored-by: sebastienlorber <[email protected]>
- Loading branch information
1 parent
c3add31
commit 4e4aa6a
Showing
33 changed files
with
204 additions
and
24 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
49 changes: 49 additions & 0 deletions
49
packages/docusaurus-theme-classic/src/theme/CodeBlock/WordWrapButton/index.tsx
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,49 @@ | ||
/** | ||
* 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 React from 'react'; | ||
import clsx from 'clsx'; | ||
import {translate} from '@docusaurus/Translate'; | ||
import type {Props} from '@theme/CodeBlock/WordWrapButton'; | ||
|
||
import styles from './styles.module.css'; | ||
|
||
export default function WordWrapButton({ | ||
className, | ||
onClick, | ||
isEnabled, | ||
}: Props): JSX.Element | null { | ||
const title = translate({ | ||
id: 'theme.CodeBlock.wordWrapToggle', | ||
message: 'Toggle word wrap', | ||
description: | ||
'The title attribute for toggle word wrapping button of code block lines', | ||
}); | ||
|
||
return ( | ||
<button | ||
type="button" | ||
onClick={onClick} | ||
className={clsx( | ||
'clean-btn', | ||
className, | ||
isEnabled && styles.wordWrapButtonEnabled, | ||
)} | ||
aria-label={title} | ||
title={title}> | ||
<svg | ||
className={styles.wordWrapButtonIcon} | ||
viewBox="0 0 24 24" | ||
aria-hidden="true"> | ||
<path | ||
fill="currentColor" | ||
d="M4 19h6v-2H4v2zM20 5H4v2h16V5zm-3 6H4v2h13.25c1.1 0 2 .9 2 2s-.9 2-2 2H15v-2l-3 3l3 3v-2h2c2.21 0 4-1.79 4-4s-1.79-4-4-4z" | ||
/> | ||
</svg> | ||
</button> | ||
); | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/docusaurus-theme-classic/src/theme/CodeBlock/WordWrapButton/styles.module.css
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,15 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
.wordWrapButtonIcon { | ||
width: 1.2rem; | ||
height: 1.2rem; | ||
} | ||
|
||
.wordWrapButtonEnabled .wordWrapButtonIcon { | ||
color: var(--ifm-color-primary); | ||
} |
56 changes: 56 additions & 0 deletions
56
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,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}; | ||
} |
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
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
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
Oops, something went wrong.