GCBGutenberg
Control Blocks
v1.3.3
Buy

Carousel, tabs and accordion blocks without a plugin

Build a carousel, tabs or an accordion your client edits on the canvas, slide by slide, with no slider plugin or library. With examples to download.

Carousels, tabs and accordions are the same thing underneath: a list of rich items, shown one or a few at a time. The hard part isn't the script that moves them. It's giving your client a way to edit every slide, tab or row without guessing what's behind the arrows, and without a slider plugin that stores its slides somewhere outside the page.

In Gutenberg Control Blocks (GCB) each item is a child block on the canvas. Your client adds, edits and reorders the items in place, and they're saved in the page like any other blocks. You can start from a downloadable example, or build your own from nothing.

Start from an example

The quickest way in is to download a working one and change it. Each is a pair of ordinary GCB block folders you copy into your theme's blocks/ folder:

ExampleIts itemsIts settings
CarouselSlidesSlides across, Gap, Arrows, Dots, Autoplay
TabsTabs, each with a TitleOn a phone
AccordionRows, each with a TitleOne open at a time, First row open to start

Each one brings its own small script for the page, with no library behind it. The carousel is a scroll-snap track with arrows, dots and autoplay, and swipes on a phone. The tabs are an ARIA tablist with arrow keys that stacks on a phone. The accordion's rows open one at a time. With no script, each shows everything open.

They aren't part of the plugin. They're examples: once the folders are in your theme, they're your blocks, and you change the markup, the fields and the script to suit the design. The rest of this guide is how they're built.

Your own block: a parent and a child

When the design needs its own markup, build it the same way the examples do. The item is an ordinary block folder with its own fields. Here's a tab with a title your client types on the canvas and a panel for any content:

blocks/plan-tab/render.php
<div <?php echo get_block_wrapper_attributes(['class' => 'plan-tab']); ?>>  <h3 class="plan-tab__title" data-gcb-field="title" data-gcb-field-type="text"><?php echo esc_html($attributes['title'] ?? ''); ?></h3>  <div class="plan-tab__panel">    <InnerBlocks allowedBlocks='["core/paragraph", "core/list", "core/buttons"]' />  </div></div>

The parent's template marks where the tabs go with a <Repeater>, which holds every rule about them:

blocks/plans/render.php
<div <?php echo get_block_wrapper_attributes(['class' => 'plans']); ?>>  <Repeater    allowedBlocks='["gcb/plan-tab"]'    addButtonLabel="Add tab"    min="2"    max="6"    defaultChildren="3"    editLayout="tabs"  /></div>

Because allowedBlocks names it, gcb/plan-tab only shows in the inserter inside this block. At max the Add button goes, and WordPress won't save the page with fewer than min or more than max tabs.

How your client edits it: editLayout

editLayout sets how the children look while they're edited. It doesn't change the page. Pick the one that matches what the block does:

editLayoutThe editor showsUse for
carouselone at a time, with arrowsa block that is a one-slide-at-a-time carousel on the page
filmstripone big child with thumbnails beneathslides that are mostly a picture
overflowa strip of cards scrolled sidewayswide items in a row
tabsone child at a time, with a tab stripitems the page shows as tabs
accordionevery child as a collapsible row, one openFAQs and other long items
stackedevery child as a numbered card, top to bottomlong lists that are hard to edit in their page layout
gridthe children where your markup and CSS put themalmost everything else: cards, stats, logos

Set editLayout on every marker. Without it, the editor shows the children as a carousel, whatever the block draws, so a grid of cards or an FAQ looks broken to the person editing it.

If a child needs its number (for "Step 2 of 5", or a tab's id), ask for it in the child's block.json with "usesContext": ["gcb/index", "gcb/count"] and read $block->context['gcb/index'] in render.php. A CSS counter numbers the page, not the canvas.

Making it move on the page

The canvas shows the server's HTML and runs none of the block's front-end script. So write the block's CSS in two layers, and let the script choose:

  • Without is-live, the storyboard. Every tab and its panel, one under another. This is what the canvas shows, and what a visitor gets if the script doesn't run.
  • With is-live, the experience. The script adds the class as its first act, and the hiding and switching are written under .is-live.
blocks/plans/style.css
.plan-tab { margin-bottom: 2rem; }                          /* storyboard */.plans.is-live .plan-tab { display: none; margin: 0; }.plans.is-live .plan-tab.is-active { display: block; }.plans.is-live .plan-tab__title { display: none; }           /* the strip shows the titles */

Name the script in block.json the WordPress way, with "viewScript": "file:./view.js". WordPress loads it only on the front of the site. A short view.js adds is-live, builds a strip of buttons from the titles, and marks the chosen tab is-active. It's plain JavaScript: GCB doesn't need a slider library, and doesn't ship one.

One thing to watch: in the editor, each child is wrapped in elements of GCB's own. CSS that reaches items as direct children, such as .track > *, won't find them on the canvas. Give each item a class, like .plan-tab above, and style that.

Try it

The docs cover editLayout, the <Repeater> options and the is-live convention. The examples have the carousel, tabs and accordion ready to download.

Build anything in the block editor

Every licence includes all features and a year of updates and support.

Buy GCB from $8.25 a month See pricing