Swiper
Overview
A responsive content carousel built on Swiper. The parent block holds Swiper Slide child blocks, each a full inner-blocks container, and sizes them by slides per view or by slide width per tier.
- Block name:
method/swiper, with the child blockmethod/swiper-slide - Title: Swiper (Method); the child is Swiper Slide
- Category:
method-layout-blocks - Supports:
align(wide, full);htmloff. The slide hashtmloff and is locked to the parent (parent: ["method/swiper"]). - Render type: Hybrid dynamic: both blocks’
savestore only an inner-blocks wrapper (.swiper-wrapperfor the parent,.method-swiper-slide-innerfor the slide); the PHP render callbacks build everything else - Inner blocks: Parent:
method/swiper-slideonly. Slide: anything (a new slide starts with a Container holding a paragraph). - Scripts:
editorScriptfor each block and aviewScripton the parent (frontend.js, the shared runtime). Swiper itself is enqueued on every page by the theme (handleswiper). - Shared with: Swiper Gallery — the same option attributes, the same inspector panels (
MethodSwiperControls) and the same render function (method_swiper_render()). - Source: lib/blocks/method-swiper/method-swiper.php, lib/blocks/method-swiper/src/swiper/edit.js, lib/blocks/method-swiper/src/swiper-slide/edit.js, lib/blocks/components/MethodSwiperControls.js, lib/blocks/utils/swiperRuntime.js, lib/blocks/utils/swiperSlideWidth.js
Usage
Insert a Swiper and add slides with the Add slide button, which sits outside the track so Swiper never sizes or clips it like a slide. Each slide is an ordinary inner-blocks area. The editor runs a live Swiper instance that mirrors the frontend configuration — touch and drag are off and clicks are left to the editor, so slides stay selectable — and selecting a slide, or any block inside one, in the List View or on the canvas brings that slide into view.
Slides are sized one of two ways. By default, Slides per view and Space between are set per tier — Desktop, Tablet, Mobile — mobile-first: the mobile values are Swiper’s base configuration and the tablet and desktop values are Swiper breakpoints at the theme’s own breakpoints. With Set slide widths on instead, each tier gets a width in pixels (or Match container width), Swiper runs with slidesPerView: 'auto' and shows as many slides as fit, and a full-width swiper lines its first slide up with the constrained content while the slides still travel to the edge of the page. The Fade (crossfade) effect stacks the slides and shows one at a time; it takes precedence over both sizing modes.
Hash navigation gives each slide a hash (set per slide in Slide hash) that Swiper keeps in the URL and jumps to on load; any link on the page whose href is a visible slide’s hash gets method-hash-active, so a row of links can act as tabs for the swiper. Scroll to slide start, only offered with hash navigation, scrolls the page to the top of the swiper when the slide changes. Arrows, pagination (inside the track or outside, beside the arrows) and the arrow icons round out the Navigation panel.
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
slidesPerView | number | 3 | Slides per view on desktop (1–6). |
slidesPerViewTablet | number | 2 | Slides per view on tablet. |
slidesPerViewMobile | number | 1 | Slides per view on mobile — Swiper’s base value; the other two are breakpoints. |
spaceBetween | number | 24 | Gap between slides on desktop, in px (0–120). |
spaceBetweenTablet | number | 24 | Gap on tablet. |
spaceBetweenMobile | number | 24 | Gap on mobile. |
slideWidthMode | boolean | false | Set slide widths: size slides by width instead of by count (method_swiper_is_slide_width_mode(); false whenever fadeEffect is on). |
slideWidth | number | 400 | Slide width on desktop in width mode, 100–1200px. |
slideWidthTablet | number | 320 | Slide width on tablet. |
slideWidthMobile | number | 260 | Slide width on mobile. |
containerWidthSlides | boolean | false | Match container width on desktop: the tier’s slides are as wide as the swiper’s container instead of the px width. |
containerWidthSlidesTablet | boolean | false | As above, tablet. |
containerWidthSlidesMobile | boolean | false | As above, mobile. |
showNavigation | boolean | true | Previous / next arrows. |
showPagination | boolean | true | Bullet pagination. |
altPagination | boolean | false | Place pagination outside: the bullets sit beside the arrows instead of over the slides. |
fadeEffect | boolean | false | Swiper’s fade effect with crossFade: true: one slide at a time, no per-view, spacing or width settings. |
hashNavigation | boolean | false | Swiper’s hashNavigation with watchState: true, replaceState: true. |
scrollToSlideStart | boolean | false | With hash navigation: scroll the page to the swiper when the slide changes (bound after init, so the initial hash jump does not scroll). |
prevIcon | string | — | Slug of a theme icon for the previous arrow; unset is the default chevron. |
nextIcon | string | — | Theme icon for the next arrow. |
textColor | string | — | Arrow color (labelled Navigation Arrows). |
linkColor | string | — | Pagination color (labelled Pagination). |
The parent has no responsiveSettings: its tiers are the three Desktop / Tablet / Mobile attribute sets above, not the base / mobile / tablet / wide object other blocks use, and its only generated CSS is the two colors (and, in width mode, the slide-width custom properties).
Swiper Slide (method/swiper-slide)
| Attribute | Type | Default | Description |
|---|---|---|---|
hash | string | "" | The slide’s URL hash for hash navigation, printed as data-hash on the slide (in the editor too, so the hash works in the canvas). Needs to be unique on the page. |
The slide’s block.json also declares textColor, linkColor, bgColor, bgGradient, bgShadeColor, bgShadeGradient, bgImg, bgSize, bgImgAlign and a responsiveSettings object, but no control writes them and the render callback does not read them. Style a slide through the blocks inside it — the default Container template is there for that.
Control Sets
| Component | Panel | Breakpoints | Notes |
|---|---|---|---|
MethodSwiperControls | Effect | base | Fade (crossfade). With fade on, the sizing and spacing panels are hidden. |
MethodSwiperControls | Slide sizing | desktop / tablet / mobile | Set slide widths; off: Slides per view sliders per tier; on: a width per tier with Match container width. The sliders are color-coded by tier (.method-bpc-desktop / -tablet / -mobile). |
MethodSwiperControls | Space between | desktop / tablet / mobile | A px slider per tier. |
MethodSwiperControls | Navigation | base | Show arrows with Previous Arrow / Next Arrow icon pickers (when the theme registers icons), Show pagination with Place pagination outside, Hash navigation with Scroll to slide start. |
MethodColorControls | Color Settings | base | include={['textColor', 'linkColor']} with labels Navigation Arrows and Pagination. |
MethodSwiperNav | Canvas | — | The editor’s arrow buttons, with the same default chevrons as PHP. |
useCanvasViewport | — | — | Resolves the tier the canvas is in, so the preview is initialised with that tier’s per-view, spacing or width values rather than Swiper’s own breakpoints (which would evaluate against the admin window, not the device preview). |
MethodStyleTag | — | all | Mirrors the two color rules in the canvas. |
| Slide panel: Hash navigation | Hash navigation | — | Slide hash (TextControl) on each method/swiper-slide. |
Data & Dependencies
- Swiper:
inc/swiper/swiper-bundle.min.jsand.css, enqueued on every page bymethod_scripts()(lib/theme-setup.php) under the handleswiper. The block does not enqueue it itself. method_swiper_render( $attributes, $track_markup, $args )— builds the wrapper, arrows, pagination and the per-instance inline init script for both swiper blocks; the Swiper block calls it withdo_blocks( $content )as the track.$args:id,class,watch_slides_progress,center_classandloop(a bool, or per tier); the Swiper block passes only its id, so it never loops —loop: falsesince beta18, which avoids the duplicate-slide quirks loop mode caused with hash navigation.- Breakpoints:
method_swiper_get_breakpoints()derives the tablet and desktop min-widths frommethod_get_block_breakpoints()(tablet_min, andtablet_max+ 1), falling back to 768 and 1200, so the tiers match the editor, which resolves the same tiers frommethodGlobalData.breakpoints. - Width mode:
method_swiper_get_slide_width_css()collects, per tier and mobile-first at the same breakpoints,--method-swiper-slide-width(a px width, or100%of the track’s content box for a container-width tier) and--method-swiper-slide-fill(1 or 0) on the wrapper; assets/scss/global.scss sizes the.swiper-slides from them and, for a full-width block, pads the track so the first slide starts at the content edge (--method-swiper-track-padding, allowing for the arrows through--method-swiper-track-inset: 76px while the wrapper hasmethod-swiper-has-nav, else 0). The editor side is lib/blocks/utils/swiperSlideWidth.js, which the PHP mirrors. - Arrow icons:
method_swiper_get_nav_icons()reads the chosenprevIcon/nextIconfrommethod_get_theme_icons()and falls back to the default chevrons; the editor’sMethodSwiperNavkeeps the same defaults. - Runtime (lib/blocks/utils/swiperRuntime.js, bundled into the view script):
initSwiperRuntime()installs once per page (window.methodSwiperRuntime) and keeps a registry of instances. Each render’s inline script pushes{ id, swiper, centerClass }ontowindow.methodSwiperQueue, which is a plain array until the runtime loads and is then drained and replaced by an object whosepushregisters immediately, so the order the scripts run in does not matter. On every slide change the runtime putsmethod-hash-activeon links whose hash matches any visible slide in any registered swiper (fromactiveIndexand the active breakpoint’sslidesPerView). - Styles (assets/scss/global.scss,
.method-swiper) — the prev / track / next row as flexbox with 44×44px arrow targets, inside and outside pagination, and the width-mode rules above. - Responsive CSS:
method_get_block_responsive_styles()andmethod_collect_css()under#method{id}(note: no hyphen — the id doubles as the prefix of the init script’s JavaScript variable).
Frontend Rendering
<div class="wp-block-method-swiper method-swiper method-swiper-has-nav" id="method68f1b2c3a4d5e">
<div class="swiper-outer">
<div class="swiper-outer-wrap">
<div class="swiper-outer-wrap-inner">
<div class="swiper swiper-container">
<div class="wp-block-method-swiper swiper-wrapper">
<div class="wp-block-method-swiper-slide swiper-slide" id="method-…" data-hash="overview">
<div class="method-swiper-slide">
<div class="method-swiper-slide-inner">
<div class="wp-block-method-swiper-slide method-swiper-slide-inner">…slide blocks…</div>
</div>
</div>
</div>
…
</div>
<div class="swiper-pagination"> </div>
</div>
</div>
</div>
<div class="method-swiper-button-prev"><span class="method-swiper-icon method-swiper-icon-prev"><svg …></svg></span><span class="visually-hidden">Previous</span></div>
<div class="method-swiper-button-next"><span class="method-swiper-icon method-swiper-icon-next"><svg …></svg></span><span class="visually-hidden">Next</span></div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function () {
const method68f1b2c3a4d5eSwiper = new Swiper('#method68f1b2c3a4d5e .swiper-container', {
slidesPerView: 1,
spaceBetween: 24,
breakpoints: {
768: { slidesPerView: 2, spaceBetween: 24 },
1200: { slidesPerView: 3, spaceBetween: 24 }
},
loop: false,
autoHeight: true,
pagination: { el: '#method68f1b2c3a4d5e .swiper-pagination', clickable: true },
navigation: { nextEl: '#method68f1b2c3a4d5e .method-swiper-button-next', prevEl: '#method68f1b2c3a4d5e .method-swiper-button-prev' },
hashNavigation: { watchState: true, replaceState: true },
});
(window.methodSwiperQueue = window.methodSwiperQueue || []).push({ id: 'method68f1b2c3a4d5e', swiper: method68f1b2c3a4d5eSwiper });
});
</script>
With Place pagination outside the bullets render as .method-swiper-pagination.swiper-pagination after the arrows instead of inside the track. With fade, the config is slidesPerView: 1, spaceBetween: 0, effect: 'fade', fadeEffect: { crossFade: true } with no breakpoints. In width mode the wrapper also carries method-swiper-width-mode, every tier is slidesPerView: 'auto', and the slide widths arrive as the custom properties described above. Scroll-to-slide-start adds a slideChange handler that calls scrollIntoView({ behavior: 'instant', block: 'start' }) on the wrapper when realIndex actually changes.
Generated CSS (per instance): #method{id} .method-swiper-button-prev, … .method-swiper-button-next → textColor; #method{id} .swiper-pagination → linkColor.
Filters
Neither block exposes a PHP filter; the Swiper configuration is assembled in method_swiper_render() from the attributes. On the page, scripts can reach every instance through the runtime: window.methodSwiperQueue.push({ id, swiper }) registers a Swiper created elsewhere for hash-link tracking, and the registered instances are the Swiper objects themselves, so swiper.slideTo(), swiper.on() and the rest are available.
Changelog
| Version | Description |
|---|---|
| 2.0.0-beta15 | Introduced, with per-tier slides per view and spacing, arrows and pagination, the editor preview and the slide appender (4682162). |
| 2.0.0-beta16 | Fade (crossfade) effect (1e59b80); hash navigation with a per-slide hash (67f58ff); scroll to slide start (af9bf12). |
| 2.0.0-beta17 | Hash navigation uses replaceState, and the scroll handler tracks realIndex and scrolls instantly (afac8c7); inserter title prefixed, “Method Swiper” (c34e9bb). |
| 2.0.0-beta18 | Previous / next arrow icons from the theme icons, outside pagination, loop: false, and tiers that follow the theme breakpoints with a canvas-aware editor preview (3064cd9). |
| 2.0.0-beta19 | Hash-link active states: window.methodSwipers and the method-hash-active class (6900250). |
| 2.0.0-beta22 | Retitled “Swiper (Method)” and given the method keyword (93e1fcf). |
| 2.0.0-beta23 | Inspector panels moved into the shared MethodSwiperControls, the render into the shared method_swiper_render(), and the inline hash tracking replaced by the swiperRuntime.js view script, which tracks every visible slide of every swiper (eacd345); breakpoint color-coding and px units on the sliders (2a5e8d3). |
| 2.0.0-beta29 | Slide width mode — slideWidthMode, per-tier widths and Match container width, with full-width swipers aligned to the content — and the editor brings the selected slide into view (4c64716, cd3109e). |
| 2.0.0-beta31 | Swiper and Swiper Slide get SVG icons of their own, a framed slide with the neighbouring slides peeking in on the Swiper’s, in place of the images-alt2 and format-image Dashicons, declared in each block’s index.js rather than its block.json (cd6f63f). |