Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function register_blocks(): void {
$blocks = [
'carousel',
'carousel/controls',
'carousel/counter',
'carousel/dots',
'carousel/viewport',
'carousel/slide',
Expand Down
20 changes: 20 additions & 0 deletions src/blocks/carousel/counter/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"version": "1.0.0",
"name": "carousel-kit/carousel-counter",
"title": "Carousel Counter",
"category": "carousel-kit",
"icon": "editor-ol",
"ancestor": [
"carousel-kit/carousel"
],
"description": "Displays current slide number and total slides (e.g., 1 of 3).",
"textdomain": "carousel-kit",
"attributes": {},
"supports": {
"interactivity": true
},
"editorScript": "file:./index.js",
"style": "file:./style-index.css"
}
52 changes: 52 additions & 0 deletions src/blocks/carousel/counter/edit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { useBlockProps } from '@wordpress/block-editor';
import { useEffect, useState, useContext } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { EditorCarouselContext } from '../editor-context';

export default function Edit() {
const blockProps = useBlockProps( {
className: 'carousel-kit-counter',
} );

const { emblaApi } = useContext( EditorCarouselContext );
const [ totalSlides, setTotalSlides ] = useState( 0 );
const [ selectedIndex, setSelectedIndex ] = useState( 0 );

useEffect( () => {
if ( ! emblaApi ) {
return;
}

const onInit = () => {
setTotalSlides( emblaApi.slideNodes().length );
setSelectedIndex( emblaApi.selectedScrollSnap() );
};

const onSelect = () => {
setSelectedIndex( emblaApi.selectedScrollSnap() );
};

emblaApi.on( 'init', onInit );
emblaApi.on( 'reInit', onInit );
emblaApi.on( 'select', onSelect );

onInit(); // Initial load.

return () => {
emblaApi.off( 'init', onInit );
emblaApi.off( 'reInit', onInit );
emblaApi.off( 'select', onSelect );
};
}, [ emblaApi ] );

// Calculate current slide (1-based) - fallback to 1 if no slides yet.
const currentSlide = selectedIndex + 1;

return (
<div { ...blockProps }>
<span className="carousel-kit-counter__current">{ currentSlide }</span>
<span className="carousel-kit-counter__separator"> { __( 'of', 'carousel-kit' ) } </span>
<span className="carousel-kit-counter__total">{ totalSlides || 1 }</span>
</div>
);
}
11 changes: 11 additions & 0 deletions src/blocks/carousel/counter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { registerBlockType, type BlockConfiguration } from '@wordpress/blocks';
import Edit from './edit';
import Save from './save';
import metadata from './block.json';
import type { CarouselCounterAttributes } from '../types';
import './style.scss';

registerBlockType( metadata as BlockConfiguration<CarouselCounterAttributes>, {
edit: Edit,
save: Save,
} );
22 changes: 22 additions & 0 deletions src/blocks/carousel/counter/save.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useBlockProps } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';

export default function Save() {
const blockProps = useBlockProps.save( {
className: 'carousel-kit-counter',
} );

return (
<div { ...blockProps }>
<span
className="carousel-kit-counter__current"
data-wp-text="callbacks.getCurrentSlideNumber"
/>
<span className="carousel-kit-counter__separator"> { __( 'of', 'carousel-kit' ) } </span>
<span
className="carousel-kit-counter__total"
data-wp-text="callbacks.getTotalSlides"
/>
</div>
);
}
19 changes: 19 additions & 0 deletions src/blocks/carousel/counter/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Styles for carousel counter/slide indicator
*/
.carousel-kit-counter {
display: inline-flex;
align-items: center;
gap: var(--carousel-kit-counter-gap, 0.25rem);
font-size: var(--carousel-kit-counter-font-size, inherit);
line-height: 1.5;
color: var(--carousel-kit-counter-color, inherit);
}

.carousel-kit-counter__separator {
color: var(--carousel-kit-counter-separator-color, inherit);
}

.carousel-kit-counter__total {
color: var(--carousel-kit-counter-total-color, inherit);
}
1 change: 1 addition & 0 deletions src/blocks/carousel/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type CarouselViewportAttributes = Record<string, never>;
export type CarouselSlideAttributes = Record<string, never>;
export type CarouselControlsAttributes = Record<string, never>;
export type CarouselDotsAttributes = Record<string, never>;
export type CarouselCounterAttributes = Record<string, never>;

export type CarouselContext = {
options: EmblaOptionsType & {
Expand Down
8 changes: 8 additions & 0 deletions src/blocks/carousel/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ store( 'carousel-kit/carousel', {
const index = ( snap?.index || 0 ) + 1;
return context.ariaLabelPattern.replace( '%d', index.toString() );
},
getCurrentSlideNumber: () => {
const context = getContext<CarouselContext>();
return ( context.selectedIndex + 1 ).toString();
},
getTotalSlides: () => {
const context = getContext<CarouselContext>();
return ( context.scrollSnaps?.length || 1 ).toString();
},
initCarousel: () => {
try {
const context = getContext<CarouselContext>();
Expand Down