@diplodoc/tabs-extension v3.7.2
Diplodoc tabs extension
This is an extension of the Diplodoc platform, which allows adding switchable tabs in the documentation.
The extension contains some parts:
Quickstart
Attach the plugin to the transformer:
import tabsExtension from '@diplodoc/tabs-extension';
import transform from '@diplodoc/transform';
const {result} = await transform(
`
{% list tabs %}
- Tab 1
- Tab 2
- Tab 3
{% endlist %}
`,
{
plugins: [tabsExtension.transform({bundle: false})],
},
);
Prepared runtime
It is necessary to add runtime
scripts to make tabs interactive on your page.
You can add assets files which were generated by the MarkdownIt transform plugin.
<html>
<head>
<!-- Read more about '_assets/tabs-extension.js' and '_assets/tabs-extension.css' in 'Transform plugin' section -->
<script src="_assets/tabs-extension.js" async></script>
<link rel="stylesheet" href="_assets/tabs-extension.css" />
</head>
<body>
${result.html}
</body>
</html>
Or you can just include runtime's source code in your bundle.
import '@diplodoc/tabs-extension/runtime';
import '@diplodoc/tabs-extension/runtime/styles.css';
MarkdownIt transform plugin
Plugin for @diplodoc/transform package.
Options:
runtimeJsPath
- name on runtime script which will be exposed in resultsscript
section. Default:_assets/tabs-extension.js
runtimeCssPath
- name on runtime css file which will be exposed in resultsstyle
section. (Default:_assets/tabs-extension.css
)bundle
- boolean flag to enable/disable copying of bundled runtime to target directory. Where target directore is<transformer output option>/<plugin runtime option>
Default:true
containerClasses
- additional classes which will be added to tab's container node. It allows to customize the tabs view. Example:my-own-class and-other-class
Default:undefined
API
Syntax
You can synchronize the opening of tabs between different tabs groups on the page. To do this, you just need to add optional property group=<group_key>
in list tab
command. The active tabs with the same keys in one tabs group will be synchronized.
Example:
{% list tabs group=group_1 %}
- Tab 1
- Tab 2
- Tab 3
{% endlist %}
{% list tabs group=group_1 %}
- Tab 1
- Tab 2
- Tab 3
{% endlist %}
Additionally, you can use different render mosed using a contruction
{% list tabs <regular | radio | dropdown> %}
- Tab 1
Text 1.
* You can use list
* And **other** features.
- Tab 2
Text 2.
{% endlist %}
The keys for the tabs are generated automatically. They are based on the tab's names using the github anchors style.
You can set your own keys for tabs with this statement:
{% list tabs group=group_1 %}
- Tab 1 {#my-tab-1}
- Tab 2 {#my-tab-2}
{% endlist %}
React component for easy installation
import {TabsRuntime} from '@diplodoc/tabs-extension/react';
// ...
render() {
return (
<>
<TabsRuntime
saveTabsToLocalStorage={true}
saveTabsToQueryStateMode='page'
router={router} // optional
/>
</>
);
}
Or you can use React hook for smart control
You can use the React hook to handle active tab changing or to select opened tabs programmatically.
import type { UseDiplodocTabsCallback, Tab } from '@diplodoc/tabs-extension/react';
import React, { useEffect, useCallback } from 'react';
import { useDiplodocTabs } from '@diplodoc/tabs-extension/react';
export const App: React.FC = () => {
// Callback function to handle tab selection
const selectTabHandler = useCallback<UseDiplodocTabsCallback>(
(tab: Tab, currentTabId?: string) => {
const { group, key } = tab;
// Group could be empty
if (group) {
// Handle tab selection in a specific group
console.log(`Selected tab with group: ${group} and key: ${key}`);
} else {
// Handle tab selection without a specific group
console.log(`Selected tab with key: ${key}`);
}
},
[],
);
// Initialize the tabs hook with an optional callback
const {
selectTab,
selectTabById,
configure,
restoreTabs,
getTabsFromLocalStorage,
getTabsFromSearchQuery,
onPageChanged
} = useDiplodocTabs(selectTabHandler);
// useEffect to configure and setup state management
useEffect(() => {
// Configure the tabs when you want
configure({
// Enable saving tabs to local storage or not
saveTabsToLocalStorage: true,
// Enable saving tabs to the URL query state tied to the page
// 'all' - all tabs selection in all pages will be saved in URLSearchQuery
// 'page' (recommended) - all tabs selection in the current page will be saved in URLSearchQuery
// 'none' - no tabs selection will be saved in URLSearchQuery
saveTabsToQueryStateMode: 'page',
});
// Notify the tabs manager that the page has changed
// IMPORTANT: Must be called when page changed, and before restore tabs (to keep saveTabsToQueryStateMode: 'page' working correctly)
onPageChanged();
// Restore tabs from local storage and query state
restoreTabs({
...getTabsFromLocalStorage(), // Get tabs from local storage
// URLSearchQuery is primary before localStorage, because it can be provided
// queryExample: ?tabs=g0_cs,g1_macos_radio
// if not variant (third param) specified, it will be search regular tab (out of the box)
...getTabsFromSearchQuery(), // Get tabs from search query
});
}, [
configure,
restoreTabs,
getTabsFromLocalStorage,
getTabsFromSearchQuery,
onPageChanged
]);
// useEffect to programmatically select tabs
useEffect(() => {
// Select a tab by group and key
selectTab({ group: 'group_1', key: 'my-key' });
// Alternatively, select a tab by key
// selectTabById('my-key-2');
}, [selectTab, selectTabById]);
return (
/* Your component JSX here */
);
}
For contributors
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
7 months ago
7 months ago
7 months ago
8 months ago
8 months ago
12 months ago
12 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago