UNPKG

nice-ui

Version:

React design system, components, and utilities

60 lines (59 loc) 2.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const React = require("react"); const GenericInlineCode_1 = require("./GenericInlineCode"); const LanguageInlineCode_1 = require("./LanguageInlineCode"); const context_1 = require("../../context"); const Key_1 = require("../../components/Key"); const use_t_1 = require("use-t"); const context_2 = require("../../../7-fullscreen/ToastCardManager/context"); const nano_theme_1 = require("nano-theme"); const getBlockquoteClass = (0, nano_theme_1.drule)({ // pad: '0 0 0 16px !important', pad: '0 0 0 12px !important', mar: '0 !important', // mar: '2px 0 2px 16px !important', trs: 'border 0.2s', ...nano_theme_1.theme.font.mono.mid, }); const copy = require('clipboard-copy'); // eslint-disable-line const { useContext } = React; const InlineCode = ({ idx }) => { const [t] = (0, use_t_1.useT)(); const toasts = (0, context_2.useToasts)(); const { ast } = useContext(context_1.context); const theme = (0, nano_theme_1.useTheme)(); const node = ast.nodes[idx]; let lang = ''; let value = node.value; const hasLanguageSet = node.wrap === '``'; const blockquoteClass = getBlockquoteClass({ bdl: `6px solid ${theme.g(0, 0.08)}`, '&:hover': { bdl: `6px solid ${theme.g(0, 0.16)}`, }, }); if (hasLanguageSet) { const matches = node.value.match(/^([^\s]+)\s+(.+)$/); if (matches) { lang = matches[1]; value = matches[2]; } } if (lang === 'kbd' || lang === 'key') { return React.createElement(Key_1.default, null, value); } const handleClick = () => { copy(value); toasts.add({ title: t('Copied to clipboard!'), message: React.createElement("blockquote", { className: blockquoteClass }, value), duration: 5000, }); }; if (lang) { return React.createElement(LanguageInlineCode_1.default, { value: value, lang: lang, onClick: handleClick }); } return (React.createElement(GenericInlineCode_1.default, { "data-lang": lang, onClick: handleClick }, value)); }; exports.default = InlineCode;