-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(theme-classic): toggle code wrap button #7036
Merged
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f37f1b9
feat(theme-classic): toggle code wrap button
lex111 7d483b4
Merge branch 'main' of github.com:facebook/docusaurus into lex111/cod…
lex111 f6bae8b
Add resize listener
lex111 dab2f96
Add support for i18n
lex111 6d57368
Merge branch 'main' of github.com:facebook/docusaurus into lex111/cod…
lex111 97e6922
Fixes
lex111 82966a2
Update packages/docusaurus-theme-translations/locales/fr/theme-common…
lex111 4b19c7a
Revert code-block-tests.mdx
lex111 5a14be5
codeWordWrap toggling can impact scroll => trigger update
slorber 3b43394
Improvements after review
lex111 579b8aa
Merge branch 'main' of github.com:facebook/docusaurus into lex111/cod…
lex111 e8d2841
Remove duplicate code
lex111 0464aa9
return useRef() value directly
slorber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { | ||
Josh-Cena marked this conversation as resolved.
Show resolved
Hide resolved
|
||
color: var(--ifm-color-primary); | ||
} |
62 changes: 62 additions & 0 deletions
62
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,62 @@ | ||
/** | ||
* 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 {MutableRefObject} from 'react'; | ||
import {useState, useCallback, useEffect, useRef} from 'react'; | ||
|
||
export function useCodeWordWrap(): { | ||
readonly codeBlockRef: (node: HTMLPreElement | null) => void; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not needed to use a function here you can return the React ref and assign it directly (like it was before, but instead it's created in the custom hook instead of the component |
||
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 toggle = useCallback(() => { | ||
const codeElement = codeBlock.current.querySelector('code')!; | ||
|
||
if (isEnabled) { | ||
codeElement.removeAttribute('style'); | ||
} else { | ||
codeElement.style.whiteSpace = 'pre-wrap'; | ||
} | ||
|
||
setIsEnabled((value) => !value); | ||
}, [codeBlock, isEnabled]); | ||
|
||
const updateCodeIsScrollable = useCallback(() => { | ||
const {scrollWidth, clientWidth} = codeBlock.current; | ||
const isScrollable = | ||
scrollWidth > clientWidth || | ||
codeBlock.current.querySelector('code')!.hasAttribute('style'); | ||
setIsCodeScrollable(isScrollable); | ||
}, [codeBlock]); | ||
|
||
useEffect(() => { | ||
updateCodeIsScrollable(); | ||
}, [isEnabled, updateCodeIsScrollable]); | ||
|
||
useEffect(() => { | ||
window.addEventListener('resize', updateCodeIsScrollable, { | ||
passive: true, | ||
}); | ||
|
||
return () => { | ||
window.removeEventListener('resize', updateCodeIsScrollable); | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we want to use the same size for all buttongroup icons? and introduce anv env variable to customize?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't work that way, the proportions of both icons are different, so if we use the same sizing for them, the wordWrap button will look smaller than the copyButton icon:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can keep it this way for now, but I guess the wordWrap svg could also be cropped somehow to take full space