-
Notifications
You must be signed in to change notification settings - Fork 1
feat(components): added CodeBlock component (#DS-5224) #463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './components/CodeBlock'; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| import { | ||
| Alert, | ||
| Meta, | ||
| Story, | ||
| Status, | ||
| } from '../../../../../.storybook/components'; | ||
|
|
||
| import * as Stories from './CodeBlock.stories'; | ||
|
|
||
| <Meta of={Stories} /> | ||
|
|
||
| # CodeBlock | ||
|
|
||
| <Status variant="experimental" /> | ||
|
|
||
| `CodeBlock` is a component that displays reformatted text content with syntax highlighting. | ||
|
|
||
| <Alert title="Note"> | ||
| For syntax highlighting, the | ||
| [`highlight.js@^11`](https://github.com/highlightjs/highlight.js/tree/stable-11) peer dependency is required: | ||
|
|
||
| ```bash | ||
| npm install highlight.js@^11 | ||
| ``` | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Сейчас документация начинается с Как идея: можно рассмотреть нейтральный
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. это перенос документации из https://koobiq.io/en/components/code-block код блок может работать и без HighlightConfigProvider, но в таком случае будут загружаться все языки из highlightjs, грубо говоря, мы в самом начале говорим, как лучше всего использовать компонент
|
||
|
|
||
| </Alert> | ||
|
|
||
| ### Configuring highlight.js | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Документация немного выбивается из общей структуры репозитория: отсутствует раздел |
||
|
|
||
| By default, `CodeBlock` lazily loads the full `highlight.js` bundle with all languages (~1 MB). To | ||
| reduce bundle size, wrap the app (or a part of it) with `CodeBlock.HighlightConfigProvider` and specify | ||
| only the languages you need: | ||
|
|
||
| ```tsx | ||
| const highlightConfig = { | ||
| core: () => import('highlight.js/lib/core'), | ||
| languages: { | ||
| typescript: () => import('highlight.js/lib/languages/typescript'), | ||
| css: () => import('highlight.js/lib/languages/css'), | ||
| xml: () => import('highlight.js/lib/languages/xml'), | ||
| }, | ||
| fallbackLanguage: 'plaintext', | ||
| }; | ||
|
|
||
| function App() { | ||
| return ( | ||
| <CodeBlock.HighlightConfigProvider config={highlightConfig}> | ||
| <CodeBlock files={files} /> | ||
| </CodeBlock.HighlightConfigProvider> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| Languages are loaded lazily on first use. Note that `html` is an alias of `xml` in `highlight.js`, so | ||
| register `xml` to enable HTML syntax highlighting. Pass a stable (module-level or memoized) `config` | ||
| object — `CodeBlock` reuses one shared `highlight.js` instance for the app, keyed by that reference. | ||
|
|
||
| See the full list of supported languages in the | ||
| [highlight.js documentation](https://highlightjs.readthedocs.io/en/stable/supported-languages.html). | ||
|
|
||
| ### Line numbers | ||
|
|
||
| Numbering lines is useful for referencing a specific location in the document. Line numbers are | ||
| disabled by default and can be enabled using the `hasLineNumbers` prop. | ||
|
|
||
| <Story of={Stories.Overview} /> | ||
|
|
||
| ### Header with title | ||
|
|
||
| In the header, use `renderTabLabel` to render a document name or the detected syntax. A shadow appears | ||
| under the header when the code content is scrolled. | ||
|
|
||
| <Story of={Stories.HeaderPinned} /> | ||
|
|
||
| ### Changing the block height | ||
|
|
||
| The block can expand to fill the available screen area if reviewing the code is one of the main tasks | ||
| in the interface. When screen space is limited or the code only supplements the main content, use | ||
| `maxHeight` to constrain it. | ||
|
|
||
| For compactness, use `viewAll` to show a portion of the document with the option to reveal everything. | ||
| The block increases in height so all the code is visible, avoiding double scrolling. The wrap setting | ||
| is preserved when collapsing and expanding the block. | ||
|
|
||
| <Story of={Stories.WithMaxHeight} /> | ||
|
|
||
| ### Soft line wrap | ||
|
|
||
| By default, lines don't wrap. Pre-configure the mode using `softWrap`, or let users control it with a | ||
| toggle button in the action panel by enabling `canToggleSoftWrap`. | ||
|
|
||
| The icon in the button represents the future state after activation and changes after the wrap mode | ||
| is toggled. | ||
|
|
||
| <Story of={Stories.WithSoftWrap} /> | ||
|
|
||
| ### Displaying multiple documents | ||
|
|
||
| Multiple documents are placed in the block as tabs. The tab title is formed from the `filename` field. | ||
| Tabs can be hidden using `hideTabs`, and the active tab can be set using `activeFileIndex` or | ||
| `defaultActiveFileIndex`. | ||
|
|
||
| Tabs are hidden automatically for a single document with an empty `filename`. | ||
|
|
||
| <Story of={Stories.WithTabs} /> | ||
|
|
||
| The tab header is displayed without a shadow by default. If content extends beyond the available area, | ||
| a shadow appears under the header when the code content is scrolled. | ||
|
|
||
| <Story of={Stories.WithTabsAndShadow} /> | ||
|
|
||
| ### Visual styling | ||
|
|
||
| Two visual styles are available: an outlined block (default), or a filled block with a background that | ||
| contrasts with the page. The filled style prevents the block from getting lost on a screen with varied | ||
| content and is configured using `isFilled`. | ||
|
|
||
| <Story of={Stories.WithFilled} /> | ||
|
|
||
| ### Without borders | ||
|
|
||
| When the code block should fill an entire container or screen, use `hideBorder` to remove its border. | ||
|
|
||
| <Story of={Stories.WithNoBorder} /> | ||
|
|
||
| ### Action panel | ||
|
|
||
| The action panel is located in the upper-right corner of the block and remains fixed while scrolling. | ||
| The component configuration determines which actions are available. | ||
|
|
||
| The panel is always visible when tabs are shown and on touch devices. When tabs are hidden, it appears | ||
| on hover or focus on other devices. `alwaysShowActionBar` keeps the panel visible regardless of tabs or | ||
| hover and is disabled by default. | ||
|
|
||
| ```tsx | ||
| <CodeBlock files={files} hideTabs alwaysShowActionBar /> | ||
| ``` | ||
|
|
||
| #### Changing wrap mode | ||
|
|
||
| The user can toggle the wrap mode using a button. This option is disabled by default and can be enabled | ||
| using `canToggleSoftWrap`. | ||
|
|
||
| ```tsx | ||
| <CodeBlock files={files} canToggleSoftWrap /> | ||
| ``` | ||
|
|
||
| #### Downloading the document | ||
|
|
||
| The user can download the document using an action that is disabled by default and can be enabled with | ||
| `canDownload`. The file name and extension are taken from `filename`; when it is empty, | ||
| `fallbackFileName` is used instead (`code` by default). | ||
|
|
||
| ```tsx | ||
| <CodeBlock files={files} canDownload /> | ||
| ``` | ||
|
|
||
| #### Copying the document text | ||
|
|
||
| The user can copy the document text by default. Use `hideCopyButton` to hide this action. | ||
|
|
||
| ```tsx | ||
| <CodeBlock files={files} hideCopyButton /> | ||
| ``` | ||
|
|
||
| #### Opening the document in an external system | ||
|
|
||
| The address is taken from the `link` field. The link opens in a new tab. | ||
|
|
||
| <Story of={Stories.WithLink} /> | ||
Uh oh!
There was an error while loading. Please reload this page.