import clsx from "clsx"; import { useCallback, useMemo, useState, type ComponentType, type CSSProperties } from "react"; import "./rds-comp-code-snippet.scss"; import OpenInFullOutlinedIcon from "@mui/icons-material/OpenInFullOutlined"; import CodeOffIcon from '@mui/icons-material/CodeOff'; import KeyboardArrowDownIcon from '@mui/icons-material/KeyboardArrowDown'; import RdsButton from "../../raaghu-elements/rds-button/rds-button"; // @ts-expect-error no declaration file for react-syntax-highlighter subpath import SyntaxHighlighter from 'react-syntax-highlighter/dist/esm/default-highlight'; // @ts-expect-error no declaration file for react-syntax-highlighter styles subpath import { atomOneLight } from 'react-syntax-highlighter/dist/esm/styles/hljs'; import RdsButtonDropdown from "../../raaghu-elements/rds-button-dropdown/rds-button-dropdown"; type HighlighterStyle = Record | string>; const atomOneLightStyle = atomOneLight as HighlighterStyle; const darkStyle: HighlighterStyle = { ...atomOneLightStyle, hljs: { ...(typeof atomOneLightStyle.hljs === 'object' ? atomOneLightStyle.hljs : {}), background: 'var(--rds-code-bg, #0b1220)', color: 'var(--rds-code-color, #e6eef6)', }, }; const Highlighter = SyntaxHighlighter as unknown as ComponentType<{ language: string; style: HighlighterStyle; showLineNumbers?: boolean; wrapLongLines?: boolean; PreTag?: string; className?: string; children: string; }>; const LANGUAGE_OPTIONS = [ { id: 'html', label: 'Html' }, { id: 'css', label: 'CSS' }, { id: 'javascript', label: 'JavaScript' }, { id: 'typescript', label: 'TypeScript' }, { id: 'json', label: 'JSON' }, ]; const COPIED_ICON = ( ); const COPY_ICON = ( ); export interface RdsCompCodeSnippetProps { code?: string; language?: string | boolean; codeLines?: boolean; theme?: "light" | "dark"; type?: "singleLine" | "multiLine"; maxHeight?: string; className?: string; sampleCodeSnippets?: Record; } const RdsCompCodeSnippet = ({ code, language = "html", codeLines = false, theme = "light", type = "multiLine", maxHeight, className = "", sampleCodeSnippets, }: RdsCompCodeSnippetProps) => { const [copied, setCopied] = useState(false); const [selectedLanguage, setSelectedLanguage] = useState( typeof language === 'string' ? language : 'html' ); const currentCode = useMemo((): string => { if (code) return code; if (!sampleCodeSnippets) return ''; return sampleCodeSnippets[selectedLanguage as keyof typeof sampleCodeSnippets] || sampleCodeSnippets.html || ''; }, [code, sampleCodeSnippets, selectedLanguage]); const handleLanguageChange = useCallback((val: string[] | string) => { const next = Array.isArray(val) ? val[0] : val; if (next) setSelectedLanguage(next as string); }, []); const handleCopy = useCallback(async () => { try { await navigator.clipboard.writeText(currentCode); setCopied(true); setTimeout(() => setCopied(false), 2000); } catch (err) { // Failed to copy text } }, [currentCode]); const showLanguage = !!language; const languageLabel = typeof language === 'string' ? language : 'html'; const highlighterStyle = theme === 'dark' ? darkStyle : atomOneLightStyle; return (
{type === "singleLine" ? (
{currentCode.length > 100 ? currentCode.slice(0, 100) + '...' : currentCode}
) : ( <>
{showLanguage && (
} rightIcon={} onChange={handleLanguageChange} showUserAvatar={false} showRadio={false} size="small" />
)}
{currentCode}
)}
); }; RdsCompCodeSnippet.displayName = 'RdsCompCodeSnippet'; export default RdsCompCodeSnippet;