-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
feat: inline copy feedback for code blocks #8458
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,10 @@ | ||
| 'use client'; | ||
|
|
||
| import { CodeBracketIcon } from '@heroicons/react/24/outline'; | ||
| import { | ||
| DocumentDuplicateIcon, | ||
| CodeBracketIcon, | ||
| } from '@heroicons/react/24/outline'; | ||
| import BaseCodeBox from '@node-core/ui-components/Common/BaseCodeBox'; | ||
| import styles from '@node-core/ui-components/Common/BaseCodeBox/index.module.css'; | ||
| import { useNotification } from '@node-core/ui-components/Providers/NotificationProvider'; | ||
| import { useTranslations } from 'next-intl'; | ||
|
|
||
| import Link from '#site/components/Link'; | ||
|
|
@@ -14,34 +15,30 @@ import type { FC, PropsWithChildren } from 'react'; | |
| type CodeBoxProps = { | ||
| language: string; | ||
| className?: string; | ||
| showCopyButton?: boolean; | ||
| }; | ||
|
|
||
| const CodeBox: FC<PropsWithChildren<CodeBoxProps>> = props => { | ||
| const [, copyToClipboard] = useCopyToClipboard(); | ||
| const notify = useNotification(); | ||
| const [copied, copyToClipboard] = useCopyToClipboard(); | ||
| const t = useTranslations(); | ||
|
|
||
| const onCopy = (text: string) => { | ||
| copyToClipboard(text); | ||
|
|
||
| notify({ | ||
| duration: 800, | ||
| message: ( | ||
| <div className="flex items-center gap-3"> | ||
| <CodeBracketIcon className={styles.icon} /> | ||
| {t('components.common.codebox.copied')} | ||
| </div> | ||
| ), | ||
| }); | ||
| }; | ||
|
|
||
| return ( | ||
| <BaseCodeBox | ||
| as={Link} | ||
| onCopy={onCopy} | ||
| onCopy={copyToClipboard} | ||
| buttonContent={ | ||
| copied ? ( | ||
| <> | ||
| <DocumentDuplicateIcon className="size-4" /> | ||
| {t('components.common.codebox.copied')} | ||
| </> | ||
| ) : ( | ||
| <> | ||
| <CodeBracketIcon className="size-4" /> | ||
|
Member
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. What is this icon? Also nit: (Cleared and less code) const ButtonIcon = copied ? DocumentDuplicateIcon : CodeBracketIcon;
...
buttonContext={
<>
<ButtonIcon className="size-4" />
{t(copied ?
'components.common.codebox.copied' :
'components.common.codebox.copy'
)}
</>
}
Member
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.
What do you mean? This is the icon we used previously, is it not?
Member
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. Yeah, I realized that afterwards. |
||
| {t('components.common.codebox.copy')} | ||
| </> | ||
| ) | ||
| } | ||
| {...props} | ||
| buttonText={t('components.common.codebox.copy')} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,13 @@ | ||
| 'use client'; | ||
|
|
||
| import { NotificationProvider } from '@node-core/ui-components/Providers/NotificationProvider'; | ||
|
|
||
| import { NavigationStateProvider } from '#site/providers/navigationStateProvider'; | ||
|
|
||
| import type { FC, PropsWithChildren } from 'react'; | ||
|
|
||
| import styles from './layouts.module.css'; | ||
|
|
||
| const BaseLayout: FC<PropsWithChildren> = ({ children }) => ( | ||
| <NotificationProvider> | ||
| <NavigationStateProvider> | ||
| <div className={styles.baseLayout}>{children}</div> | ||
| </NavigationStateProvider> | ||
| </NotificationProvider> | ||
| <NavigationStateProvider> | ||
| <div className={styles.baseLayout}>{children}</div> | ||
| </NavigationStateProvider> | ||
| ); | ||
|
|
||
| export default BaseLayout; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -181,15 +181,6 @@ export default async function rehypeShikiji(options) { | |
| codeLanguage | ||
| ); | ||
|
|
||
| // Adds a Copy Button to the CodeBox if requested as an additional parameter | ||
|
Member
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. 🤔 |
||
| // And avoids setting the property (overriding) if undefined or invalid value | ||
| if ( | ||
| meta.showCopyButton && | ||
| ['true', 'false'].includes(meta.showCopyButton) | ||
| ) { | ||
| children[0].properties.showCopyButton = meta.showCopyButton; | ||
| } | ||
|
|
||
| // Replaces the <pre> element with the updated one | ||
| parent.children.splice(index, 1, ...children); | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,7 +78,3 @@ | |
| } | ||
| } | ||
| } | ||
|
|
||
| .icon { | ||
| @apply size-4; | ||
| } | ||
This file was deleted.
This file was deleted.
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.