-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(apps): create new
CodeBlock
(#3321)
* docs(apps): create new `CodeBlock` * remove code snippet imports * add bg to inline code * add supported langs * plaintext white color * literal * comment * get bash support * comment out await
- Loading branch information
Showing
25 changed files
with
322 additions
and
601 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
.codeBlock { | ||
max-width: 100%; | ||
overflow-x: auto; | ||
width: auto; | ||
padding: var(--ds-size-5); | ||
border-top-left-radius: var(--ds-border-radius-md); | ||
border-top-left-radius: var(--ds-border-radius-md); | ||
height: fit-content; | ||
font-size: min(0.8em, 14px); | ||
background-color: var(--ds-color-neutral-background-tinted); | ||
font-family: monospace; | ||
margin-bottom: 0; | ||
|
||
& [class~='plain-text'] { | ||
color: var(--ds-color-neutral-text-default); | ||
} | ||
} | ||
|
||
.codeBlockWrapper { | ||
position: relative; | ||
} | ||
|
||
.copyButton { | ||
border-radius: 0; | ||
margin-left: auto; | ||
border-bottom-right-radius: var(--ds-border-radius-md); | ||
} | ||
|
||
.toolbar { | ||
display: flex; | ||
justify-content: flex-end; | ||
margin-bottom: var(--ds-size-4); | ||
background-color: var(--ds-color-neutral-background-tinted); | ||
border-top: 1px solid var(--ds-color-neutral-border-subtle); | ||
border-bottom-right-radius: var(--ds-border-radius-md); | ||
border-bottom-left-radius: var(--ds-border-radius-md); | ||
} |
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,92 @@ | ||
'use client'; | ||
import { Button } from '@digdir/designsystemet-react'; | ||
import cl from 'clsx/lite'; | ||
import { useState } from 'react'; | ||
import { CodeBlock as ReactCodeBlock } from 'react-code-block'; | ||
import classes from './CodeBlock.module.css'; | ||
|
||
/* (typeof global !== 'undefined' ? global : window).Prism = Prism; */ | ||
/* @ts-ignore - TODO: fix this */ | ||
/* await import('prismjs/components/prism-bash'); */ | ||
|
||
export type CodeBlockProps = { | ||
children: string; | ||
className?: string; | ||
/** | ||
* Language for syntax highlighting. | ||
* Only some languages give syntax highlighting, if the passed language is not supported | ||
* it will default to 'text'. | ||
* | ||
* @todo Extend the language list with prismjs. | ||
*/ | ||
language?: | ||
| 'markup' | ||
| 'jsx' | ||
| 'tsx' | ||
| 'swift' | ||
| 'kotlin' | ||
| 'objectivec' | ||
| 'js-extras' | ||
| 'reason' | ||
| 'rust' | ||
| 'graphql' | ||
| 'yaml' | ||
| 'go' | ||
| 'cpp' | ||
| 'markdown' | ||
| 'python' | ||
| 'json' | ||
| 'text' | ||
| 'bash' | ||
| string; | ||
}; | ||
|
||
export const CodeBlock = ({ | ||
children, | ||
className, | ||
language = 'text', | ||
}: CodeBlockProps) => { | ||
return ( | ||
<ReactCodeBlock code={children} language={language}> | ||
<div className={classes.codeBlockWrapper}> | ||
<ReactCodeBlock.Code | ||
data-color-scheme='dark' | ||
className={cl(classes.codeBlock, className)} | ||
> | ||
<ReactCodeBlock.LineContent> | ||
<ReactCodeBlock.Token /> | ||
</ReactCodeBlock.LineContent> | ||
</ReactCodeBlock.Code> | ||
<div data-color-scheme='dark' className={classes.toolbar}> | ||
<CopyButton text={children} /> | ||
</div> | ||
</div> | ||
</ReactCodeBlock> | ||
); | ||
}; | ||
|
||
const CopyButton = ({ text }: { text: string }) => { | ||
const [toolTipText, setToolTipText] = useState('Kopier'); | ||
|
||
const onButtonClick = () => { | ||
setToolTipText('Kopiert!'); | ||
navigator.clipboard.writeText(text).catch((reason) => { | ||
throw Error(String(reason)); | ||
}); | ||
}; | ||
return ( | ||
<> | ||
{/* @ts-ignore */} | ||
<Button | ||
onMouseEnter={() => setToolTipText('Kopier')} | ||
onClick={() => onButtonClick()} | ||
className={classes.copyButton} | ||
aria-label='Kopier kodesnutt' | ||
data-color='neutral' | ||
data-size='2xs' | ||
> | ||
{toolTipText} | ||
</Button> | ||
</> | ||
); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
1 change: 0 additions & 1 deletion
1
apps/storefront/app/bloggen/2025/generering-av-fargar-i-fargesystemet/page.mdx
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.