2.1.2 • Published 16 days ago

@diplodoc/tabs-extension v2.1.2

Weekly downloads
-
License
MIT
Repository
-
Last release
16 days ago

Diplodoc tabs extension

NPM version

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 '@doc-tools/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 @doc-tools/transform package.

Options:

  • runtimeJsPath - name on runtime script which will be exposed in results script section. Default: _assets/tabs-extension.js

  • runtimeCssPath - name on runtime css file which will be exposed in results style 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 %}

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 hook for smart control

You can use the React hook to handle active tab changing or to select opened tab programmatically.

import React, { useEffect } from 'react'
import {UseDiplodocTabsCallback, useDiplodocTabs, Tab} from '@diplodoc/tabs-extension/react';

export const App: React.FC = () => {
    const selectTabHandler = useCallback<UseDiplodocTabsCallback>(
        (tab: Tab, currentTabId?: string) => {
            const {group, key} = tab;
            // Group could be empty
            if (group) {
                // ...
            }
        },
        [],
    );

    const {selectTab, selectTabById} = useDiplodocTabs(selectTabHandler);

    useEffect(() => {
        selectTab({ group: 'group_1', key: 'my-key' });
        // selectTabById('my-key-2');
    }, [selectTab, selectTabById]);

}
2.1.2

16 days ago

2.1.1

2 months ago

2.1.0

2 months ago

2.0.18

3 months ago

2.0.17

3 months ago

2.0.16

3 months ago

2.0.15

5 months ago

2.0.14

5 months ago

2.0.13

5 months ago

2.0.12

8 months ago

2.0.11

8 months ago

2.0.10

9 months ago

2.0.9

9 months ago

2.0.8

9 months ago

2.0.7

9 months ago

2.0.6

9 months ago

2.0.5

9 months ago

2.0.4

10 months ago

2.0.3

10 months ago

2.0.2

10 months ago

2.0.1

10 months ago

2.0.0

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago

0.0.5

10 months ago