1
0
Fork 0
freesewing/packages/react/components/Highlight/index.mjs

57 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-12-21 14:25:49 +01:00
import React from 'react'
import { CopyToClipboard } from '@freesewing/react/components/CopyToClipboard'
const defaultTitles = {
2024-12-21 14:25:49 +01:00
js: 'Javascript',
bash: 'Bash commands',
sh: 'Shell commands',
2024-12-21 14:25:49 +01:00
json: 'JSON',
yaml: 'YAML',
2024-12-21 14:25:49 +01:00
}
/**
* A React component to highlight code
*
* @params {object} props - All React props
* @params {string} language - The language to highlight
* @params {object} children - The React children
* @params {bool} raw - Set this to true to not escape tags
* @params {string} title - Title for the highlight
* @params {string} copy - Content to copy to clipboard
*/
export const Highlight = ({
language = 'txt',
children,
raw = false,
title = false,
copy = false,
}) => {
if (children?.props?.className) {
language = children.props.className.split('-').pop()
2024-12-21 14:25:49 +01:00
}
const preProps = {
className: `language-${language} hljs text-base lg:text-lg whitespace-break-spaces overflow-scroll pr-4`,
}
if (raw) preProps.dangerouslySetInnerHTML = { __html: raw }
const label = title ? title : defaultTitles[language] ? defaultTitles[language] : language
2024-12-21 14:25:49 +01:00
return (
<div className="hljs my-4">
<div
className={`
flex flex-row justify-between items-center
text-xs font-medium text-warning
mt-1 border-b border-neutral-content border-opacity-25
px-4 py-1 mb-2 lg:text-sm
`}
>
<span>{label}</span>
<CopyToClipboard content={copy ? copy : children} label={label} />
2024-12-21 14:25:49 +01:00
</div>
<pre {...preProps}>{children}</pre>
2024-12-21 14:25:49 +01:00
</div>
)
}