DK Table of Contents Preview Toggle

Description

This plugin is not affiliated with, endorsed by, or sponsored by Rank Math. Rank Math is a trademark of its respective owner.

Works with Rank Math’s Table of Contents block. DK Table of Contents Preview Toggle is an independent add-on for the Table of Contents block in the free Rank Math SEO plugin. Long tables of contents push your article below the fold, especially on phones. This plugin shows a short preview of the list instead, with a soft fade at the bottom, and turns the block’s title into a button that expands and collapses the full list. On a touch screen, the first tap on that preview opens the full list instead of jumping to a heading. The next tap on a heading goes there.

It was built for Core Web Vitals first:

  • No layout shift. The list is already collapsed when the page first paints, using a few lines of inline CSS. Nothing jumps when scripts load, and there is no hover expansion. Measured Cumulative Layout Shift from the plugin is 0 on load and on toggle.
  • Fast interactions. A few delegated listeners (about 10 KB with comments, about 3 KB minified) flip a class and aria-expanded. They never measure the page when someone taps or clicks, so they add next to nothing to Interaction to Next Paint.
  • No effect on Largest Contentful Paint. No web fonts, images or extra requests beyond one small deferred script.
  • Loads only where it is used. Pages without the Table of Contents block get no CSS and no JavaScript from this plugin.

It is accessible:

  • The title becomes a real <button> with aria-expanded and aria-controls, so it works with a mouse, a tap, and the Enter and Space keys, and screen readers announce its state.
  • The entries stay links to your headings. With a mouse, a click goes straight to the heading. On a touch screen, the first tap anywhere on a collapsed list opens it, including a tap on a heading, and the next tap follows the heading.
  • Keyboard users who tab into the list see the whole list, even before the script loads.
  • If JavaScript is turned off, the full list is shown.
  • Respects “reduce motion”.

It works with caching and optimization plugins, including NitroPack, WP Rocket, LiteSpeed Cache, W3 Total Cache, Autoptimize, Perfmatters, FlyingPress, SiteGround Speed Optimizer, and Cloudflare APO and Rocket Loader. See the FAQ for details.

How it works

Add the Table of Contents block from Rank Math to a post or page as usual. When the list has four or more entries and a visible title, this plugin:

  1. Adds the dknm-ctoc class to the block and an ID to its list.
  2. Wraps the title in a toggle button.
  3. Prints a small inline style that limits the list to a 120px preview with a fade.
  4. Loads a small deferred script that opens and closes the list. On a touch screen, the first tap on a collapsed list opens it.

Lists with fewer than four entries, and blocks without a title, are left exactly as Rank Math renders them. AMP pages, feeds and embeds always get the full, open list.

There is no settings screen. Developers can change the behavior with filters (see the FAQ).

Privacy

This plugin makes no external requests, sets no cookies, collects no data and does no tracking. It stores nothing except a per-user flag when an administrator dismisses its “Rank Math is not active” notice, and it removes that flag when the plugin is deleted.

Built by DK New Media. Visit Martech Zone for updates on this plugin and other marketing tools.

Screenshots

Installation

  1. Install and activate the free Rank Math SEO plugin, and finish its setup wizard. The Table of Contents block needs Rank Math’s Schema (Structured Data) module, which is on by default.
  2. Install and activate DK Table of Contents Preview Toggle from Plugins > Add New, or upload the zip from Plugins > Add New > Upload Plugin.
  3. Add the “Table of Contents by Rank Math” block to a post. Collapsing happens automatically on the front end.

If Rank Math or its Table of Contents block is not available, the plugin does nothing and shows a notice on the Dashboard and Plugins screens that you can dismiss.

FAQ

Is this plugin made by Rank Math?

No. This plugin is not affiliated with, endorsed by, or sponsored by Rank Math. It is an independent add-on by DK New Media that works with the Table of Contents block from the Rank Math SEO plugin.

Does it work without Rank Math?

No. It only changes the Table of Contents block from Rank Math. Without that block it does nothing.

On a phone, why doesn’t the first tap on a heading go there?

On a touch screen the collapsed list is a preview, so the first tap opens it. That includes a tap on a heading you can already see, and a heading under the fade. Tap the heading again to go to it. A mouse click on a heading still goes straight there, and the title button still opens and closes the list. Keyboard users can tab to any entry and press Enter to follow it.

Why does a short table of contents not collapse?

Collapsing a list that already fits would only add a control that does nothing. Lists with fewer than four entries are left open, and lists that fit inside the preview height are detected in the browser and left open too. To change the minimum:

add_filter( 'dknm_ctoc_min_items', function () {
    return 6;
} );

How do I change the preview height?

The default is 120 pixels:

add_filter( 'dknm_ctoc_peek_height', function () {
    return 160;
} );

Can I remove the arrow next to the title?

Yes:

add_filter( 'dknm_ctoc_show_indicator', '__return_false' );

Can I change the CSS?

Add your own rules in your theme, or filter the inline CSS:

add_filter( 'dknm_ctoc_critical_css', function ( $css, $peek ) {
    return $css . '.dknm-ctoc__toggle{text-decoration:underline}';
}, 10, 2 );

The fade is a CSS mask on the list rather than a colored overlay, so it works on any background color and in dark mode.

Can I turn it off on some pages?

Yes, with the dknm_ctoc_enabled filter:

add_filter( 'dknm_ctoc_enabled', function ( $enabled ) {
    return is_page( 'glossary' ) ? false : $enabled;
} );

What happens if the script is delayed or never runs?

The collapsed preview comes from CSS, not JavaScript, so the page never shifts while it waits for the script. Until the script runs:

  • keyboard users who tab into the list see all of it;
  • screen readers read every entry (the list is clipped visually, not hidden);
  • if JavaScript is turned off completely, the full list is shown.

This trade-off is deliberate. Collapsing with JavaScript after the page loads is what causes layout shift, so this plugin collapses with CSS and lets the button take over when the script arrives.

Does it work with caching and optimization plugins?

Yes. The collapsed preview comes from inline CSS, so it never depends on an optimizer’s deferred CSS or delayed JavaScript.

By default the plugin asks optimizers to leave its small toggle script alone, so the button works on the very first tap, even on pages where other scripts are delayed. It adds data-cfasync="false" (Cloudflare Rocket Loader), data-no-optimize and data-no-defer (LiteSpeed Cache), data-no-minify (W3 Total Cache), data-noptimize (Autoptimize), nowprocket (WP Rocket) and data-pagespeed-no-defer to the script tag, and adds the script to the WP Rocket, LiteSpeed Cache, Autoptimize and SiteGround Speed Optimizer exclusion lists. The script is about 3 KB minified and deferred, so skipping optimization costs next to nothing.

  • WP Rocket, Perfmatters, FlyingPress, LiteSpeed Cache “delay JS”: the toggle script is excluded from delay through the attributes and filters above. Perfmatters, FlyingPress and NitroPack read exclusions from their own settings, so if one of them still delays it, add collapsible-toc to its delay exclusions.
  • “Remove unused CSS” (WP Rocket, LiteSpeed Cache, FlyingPress, NitroPack, Perfmatters): the script also sets the open and closed states as inline styles, so the toggle keeps working even if the state rules are removed. The plugin also adds its selectors to the WP Rocket and LiteSpeed Cache safelists.
  • Combining CSS (Autoptimize, LiteSpeed Cache, SiteGround Speed Optimizer, W3 Total Cache): keep the inline style dknm-ctoc-inline-css inline. The plugin excludes it from Autoptimize automatically. Combining it into a render-blocking file in the head also works.
  • Cloudflare APO: full-page caching is fine; nothing in the output varies by visitor.
  • Cloudflare Rocket Loader: skipped by default through data-cfasync="false".

To let your optimizer defer, delay, combine and minify the toggle script like any other, turn the exclusions off:

add_filter( 'dknm_ctoc_exclude_from_optimizers', '__return_false' );

The script is built for that too: it is plain JavaScript with no dependencies, works whether it runs before or after the page finishes loading, and is safe if it gets loaded twice. If scripts are delayed until interaction and your optimizer does not replay the first click, the first tap on the title loads the script and the next one opens the list.

You can add your own attributes with the dknm_ctoc_script_attributes filter:

add_filter( 'dknm_ctoc_script_attributes', function ( $attrs ) {
    $attrs['data-my-optimizer-skip'] = '1';
    return $attrs;
} );

Does it work with AMP?

On AMP pages the plugin steps aside, and the full list is shown.

Does it slow down pages without a table of contents?

No. The CSS and script are only added to pages that render a collapsible Table of Contents block.

What does uninstalling remove?

The only thing it stores, the per-user notice dismissal flag. There are no settings, tables or scheduled tasks.

Reviews

There are no reviews for this plugin.

Contributors & Developers

“DK Table of Contents Preview Toggle” is open source software. The following people have contributed to this plugin.

Contributors

Changelog

1.0.3

  • On a touch screen, the first tap on a collapsed table of contents opens it instead of jumping to a heading. The next tap on a heading goes there. Mouse and keyboard behavior is unchanged.

1.0.2

  • Renamed to “DK Table of Contents Preview Toggle” (slug dk-table-of-contents-preview-toggle).
  • The no-JavaScript fallback now uses the enqueued stylesheet (a scripting media query) instead of an inline noscript style block.
  • Tested up to WordPress 7.2.

1.0.0

  • Initial release. Tested with WordPress 7.1.5 and Rank Math 1.0.282.