@leafygreen-ui/code
leafyGreen UI Kit Code Blocks
Last updated 5 days ago by brookescarlett .
Apache-2.0 · Original npm · Tarball · package.json
$ cnpm install @leafygreen-ui/code 
SYNC missed versions from official npm registry.

Code

npm (scoped)

View on Storybook

Installation

Yarn

yarn add @leafygreen-ui/code

NPM

npm install @leafygreen-ui/code

Source Code Pro Font

In addition to using a limited amount of our standard brand font, this component uses the open-source monospace font "Source Code Pro". For this component to display correctly, loading the following weights / styles of "Source Code Pro" is necessary:

  • 400 / normal
  • 400 / italic
  • 600 / normal

You can find instructions for using Google's CDN for font delivery, and download "Source Code Pro" on Google Fonts.

We recommend using Google's CDN for loading this font to optimize for caching the assets, speeding up loading times for webpages as a result. We use the following HTML in our <head> tags to load the necessary variants of "Source Code Pro":

<link rel="preconnect" href="https://fonts.gstatic.com" />
<link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro:ital,wght@0,400;0,600;1,400&display=swap" rel="stylesheet" />

Example

import Code from '@leafygreen-ui/code';

const codeSnippet = `
function greeting(entity) {
  return \`Hello, \${entity}!\`;
}

console.log(greeting('World'));
`;

const SomeComponent = () => <Code>{codeSnippet}</Code>;

Output HTML

<div class="leafygreen-ui-1xban8j">
  <div class="leafygreen-ui-1q8jsgx">
    <pre class="leafygreen-ui-5u6rr1">
			<code class="lg-highlight-hljs-light leafygreen-ui-16k3j4z">
				<span class="lg-highlight-function">
					<span class="lg-highlight-keyword">function</span>
					<span class="lg-highlight-title">greeting</span>
					(
					<span class="lg-highlight-params">entity</span>
					)
				</span>
				{
				<span class="lg-highlight-keyword">return</span>
				<span class="lg-highlight-string">
					`Hello, <span class="lg-highlight-subst">${entity}</span>!`
				</span>;
				}
				<span class="lg-highlight-built_in">console</span>
				.log(greeting(
				<span class="lg-highlight-string">'World'</span>));
			</code>
		</pre>
    <div class="leafygreen-ui-1p8ha9w">
      <button
        data-clipboard-text="function greeting(entity) { return `Hello, ${entity}!`; } console.log(greeting('World'));"
        aria-disabled="false"
        aria-label="Copy"
        class="copy-btn leafygreen-ui-nx7krf"
        tabindex="0"
      >
        <span class="leafygreen-ui-1rvdyoi">
          <svg width="16" height="16" role="img" viewBox="0 0 16 16" class="">
            <title>Copy Icon</title>
            <g fill="currentColor" fill-rule="evenodd">
              <path d="M13 5h-3V2H7v9h6V5zm2 0v8H5V0h5l5 5z"></path>
              <path d="M4 4v1H2v10h7v-1h1v2H1V4h3z"></path>
            </g>
          </svg>
        </span>
      </button>
    </div>
  </div>
</div>

Properties

Prop Type Description Default
children (Required) string This is the code snippet that will be rendered in the code block. ''
language (Required) 'javascript', 'typescript', 'cs', 'csharp', 'cpp', 'go', 'http','java', 'perl', 'php', 'python', 'ruby', 'scala', 'swift', 'kotlin','objectivec', 'bash', 'shell', 'sql', 'yaml', 'json', 'diff', 'xml', 'none' The language to highlight the code block as. When set to 'none', no syntax highlighting will be applied.
darkMode boolean Determines whether or not the component will appear in dark mode. false
className string Applies a className to the root element's classList.
showLineNumbers boolean Shows line numbers next to each line of code in the passed code snippet. NOTE: While you can set this to true regardless of the code component being multiline, the line numbers will not be displayed if the multiline prop is true. false
showWindowChrome boolean Shows a stylized window chrome frame around the code snippet. This is purely stylistic. false
chromeTitle string Shows a filename-like title in the window chrome frame.NOTE: While you can set this prop if showWindowChrome is false, it will not be displayed unless the showWindowChrome prop is true. ''
onCopy Function Callback fired when Code is copied
copyable boolean When true, allows the code block to be copied to the user's clipboard true
highlightLines Array<number | [number, number]> An optional array of lines to highlight. The array can only contain numbers corresponding to the line numbers to highlight, and / or tuples representing a range (e.g. [6, 10]);

Current Tags

  • 8.0.1                                ...           latest (5 days ago)

32 Versions

  • 8.0.1                                ...           5 days ago
  • 8.0.0                                ...           21 days ago
  • 7.3.1                                ...           a month ago
  • 7.3.0                                ...           2 months ago
  • 7.2.0                                ...           3 months ago
  • 7.1.0                                ...           3 months ago
  • 7.0.1                                ...           3 months ago
  • 7.0.0                                ...           4 months ago
  • 6.0.0                                ...           4 months ago
  • 5.0.0                                ...           4 months ago
  • 4.0.4                                ...           4 months ago
  • 4.0.3                                ...           5 months ago
  • 4.0.2                                ...           5 months ago
  • 4.0.1                                ...           5 months ago
  • 4.0.0                                ...           6 months ago
  • 3.5.0                                ...           6 months ago
  • 3.4.6                                ...           7 months ago
  • 3.4.5                                ...           8 months ago
  • 3.4.4                                ...           8 months ago
  • 3.4.3                                ...           9 months ago
  • 3.4.2                                ...           10 months ago
  • 3.4.1                                ...           10 months ago
  • 3.4.0                                ...           10 months ago
  • 3.3.0                                ...           10 months ago
  • 3.2.0                                ...           a year ago
  • 3.1.0                                ...           a year ago
  • 3.0.0                                ...           a year ago
  • 2.0.3                                ...           a year ago
  • 2.0.2                                ...           a year ago
  • 2.0.1                                ...           2 years ago
  • 2.0.0                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Downloads
Today 0
This Week 0
This Month 19
Last Day 0
Last Week 10
Last Month 40
Dev Dependencies (0)
None
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |