Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor script to account for multiple gallery carousels on front end #2278

Merged
merged 6 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
222 changes: 113 additions & 109 deletions src/blocks/gallery-carousel/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,167 +649,171 @@ const deprecated =
...BackgroundAttributes,
...metadata.attributes,
},
save: ( { attributes, className } ) => {
save( { attributes } ) {
const {
autoPlay,
autoPlaySpeed,
draggable,
freeScroll,
gridSize,
gutter,
gutterMobile,
height,
images,
pageDots,
pauseHover,
freeScroll,
prevNextButtons,
primaryCaption,
alignCells,
thumbnails,
responsiveHeight,
lightbox,
loop,
pageDots,
gutterMobile,
height,
alignCells,
gridSize,
navForClass,
} = attributes;

// Return early if there are no images.
if ( images.length <= 0 ) {
return;
return null;
}

const classes = classnames(
className, {
'has-responsive-height': responsiveHeight,
}
);

const innerClasses = classnames(
'is-cropped',
...GalleryClasses( attributes ), {
...GalleryClasses( attributes ),
{
'has-horizontal-gutter': gutter > 0,
'has-lightbox': lightbox,
'has-no-thumbnails': ! thumbnails,
}
);

const flickityClasses = classnames(
'has-carousel',
`has-carousel-${ gridSize }`, {
'has-aligned-cells': alignCells,
[ `has-margin-bottom-${ gutter }` ]: thumbnails && gutter > 0,
[ `has-margin-bottom-mobile-${ gutterMobile }` ]: thumbnails && gutterMobile > 0,
[ navForClass ]: thumbnails,
const figureClasses = classnames(
'coblocks-gallery--figure', {
[ `has-margin-left-${ gutter }` ]: gutter > 0,
[ `has-margin-left-mobile-${ gutterMobile }` ]: gutterMobile > 0,
[ `has-margin-right-${ gutter }` ]: gutter > 0,
[ `has-margin-right-mobile-${ gutterMobile }` ]: gutterMobile > 0,
}
);

const flickityStyles = {
height: height ? height + 'px' : undefined,
};

const figureClasses = classnames(
'coblocks-gallery--figure', {
const thumbnailClasses = classnames(
'wp-block-coblocks-gallery-carousel-thumbnail',
{
[ `has-margin-left-${ gutter }` ]: gutter > 0,
[ `has-margin-left-mobile-${ gutterMobile }` ]: gutterMobile > 0,
[ `has-margin-right-${ gutter }` ]: gutter > 0,
[ `has-margin-right-mobile-${ gutterMobile }` ]: gutterMobile > 0,
}
);

const flickityOptions = {
autoPlay: autoPlay && autoPlaySpeed ? parseFloat( autoPlaySpeed ) : false,
draggable,
pageDots,
prevNextButtons,
wrapAround: true,
cellAlign: alignCells ? 'left' : 'center',
pauseAutoPlayOnHover: pauseHover,
freeScroll,
arrowShape: {
x0: 10,
x1: 60, y1: 50,
x2: 65, y2: 45,
x3: 20,
},
thumbnails,
responsiveHeight,
};
const thumbnailContainerClasses = classnames(
'wp-block-coblocks-gallery-carousel-thumbnail-pagination',
{
[ `has-margin-top-${ gutter }` ]: gutter > 0,
[ `has-margin-top-mobile-${ gutterMobile }` ]: gutterMobile > 0,
}
);

const captionClasses = classnames(
'coblocks-gallery--caption',
'coblocks-gallery--primary-caption', {}
);

const navClasses = classnames(
'carousel-nav', {
[ `has-margin-top-${ gutter }` ]: gutter > 0,
[ `has-margin-top-mobile-${ gutterMobile }` ]: gutterMobile > 0,
[ `has-negative-margin-left-${ gutter }` ]: gutter > 0,
[ `has-negative-margin-left-mobile-${ gutterMobile }` ]: gutterMobile > 0,
[ `has-negative-margin-right-${ gutter }` ]: gutter > 0,
[ `has-negative-margin-right-mobile-${ gutterMobile }` ]: gutterMobile > 0,
const swiperClasses = classnames(
'has-carousel',
`has-carousel-${ gridSize }`,
'swiper-container',
{
'has-aligned-cells': alignCells,
'has-responsive-height': responsiveHeight,
[ navForClass ]: thumbnails,
}
);

const navFigureClasses = classnames(
'coblocks--figure', {
[ `has-margin-left-${ gutter }` ]: gutter > 0,
[ `has-margin-left-mobile-${ gutterMobile }` ]: gutterMobile > 0,
[ `has-margin-right-${ gutter }` ]: gutter > 0,
[ `has-margin-right-mobile-${ gutterMobile }` ]: gutterMobile > 0,
}
);
const swiperStyles = {
height: height ? `${ height }px` : undefined,
};

const navOptions = {
asNavFor: `.${ navForClass }`,
autoPlay: false,
contain: true,
cellAlign: 'left',
pageDots: false,
thumbnails: false,
const uuid = '12345';

const swiperSizing = {
lrg: 2,
med: 4,
sml: 5,
xlrg: 1,
};

const swiperOptions = {
alignCells,
autoPlay,
autoPlaySpeed,
draggable,
prevNextButtons: false,
wrapAround: false,
freeScroll,
loop,
navigation: prevNextButtons,
pageDots,
pauseHover,
responsiveHeight,
slidesPerView: swiperSizing[ gridSize ],
thumbnails,
uuid,
};

return (
<div aria-label={ __( `Carousel Gallery`, 'coblocks' ) }
className={ classes }>
<div aria-label={ __( `Carousel Gallery`, 'coblocks' ) } >
<div className={ innerClasses }>
<div
className={ flickityClasses }
style={ responsiveHeight ? undefined : flickityStyles }
data-flickity={ JSON.stringify( flickityOptions ) }
>
{ images.map( ( image ) => {
const img = <img src={ image.url } alt={ image.alt } data-id={ image.id } data-link={ image.link } className={ image.id ? `wp-image-${ image.id }` : null } />;

return (
<div key={ image.id || image.url } className="coblocks-gallery--item">
<figure className={ figureClasses }>
{ img }
</figure>
</div>
);
} ) }
</div>
{ thumbnails
? (
<div
className={ navClasses }
data-flickity={ JSON.stringify( navOptions ) }
>
{ images.map( ( image ) => {
const img = <img src={ image.url } alt={ image.alt } data-id={ image.id } data-link={ image.link } />;
return (
<div key={ image.id || image.url } className="coblocks--item-thumbnail">
<figure className={ navFigureClasses }>
{ img }
<div className={ swiperClasses } data-swiper={ JSON.stringify( swiperOptions ) } id={ uuid } style={ responsiveHeight ? undefined : swiperStyles } >
<div className="swiper-wrapper" id="swiper-wrapper">
{ images.map( ( image, index ) => {
return (
<div className="swiper-slide" key={ index }>
<div
className="coblocks-gallery--item"
role="button"
tabIndex={ index }
>
<figure className={ figureClasses }>
<img
alt={ image.alt }
className={ image.id ? `wp-image-${ image.id }` : null }
data-id={ image.id }
data-link={ image.link }
src={ image.url }
/>
</figure>
<RichText.Content className={ captionClasses } tagName="figcaption" value={ image.caption } />
</div>
);
} ) }
</div> ) : null
}
</div>
);
} ) }
</div>
{ prevNextButtons && (
<>
<button className={ `nav-button__prev` } id={ `${ uuid }-prev` } >
<svg className="icon" style={ { transform: 'rotate(180deg)' } } />
</button>
<button className={ `nav-button__next` } id={ `${ uuid }-next` } >
<svg className="icon" />
</button>
</>
) }
</div>
{ thumbnails && (
<div className={ thumbnailContainerClasses }>
{ images.map( ( item, index ) => {
return (
<div className={ thumbnailClasses } id={ `wp-block-coblocks-gallery-carousel-thumbnail-${ index }` } key={ index } style={ { height: '80px', width: '100px' } } >
<img
alt={ item.alt }
data-id={ item.id }
data-link={ item.link }
src={ item.url }
style={ { height: '100%', width: '100%' } }
/>
</div>
);
} ) }
</div>
) }
</div>
{ ! RichText.isEmpty( primaryCaption ) && <RichText.Content tagName="figcaption" className={ captionClasses } value={ primaryCaption } /> }
</div>
);
},
Expand Down
28 changes: 16 additions & 12 deletions src/blocks/gallery-carousel/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const save = ( props ) => {
}

const innerClasses = classnames(
'coblocks-gallery-carousel-swiper-container',
'is-cropped',
...GalleryClasses( attributes ),
{
Expand All @@ -61,15 +62,18 @@ const save = ( props ) => {
}
);

const thumbnailClasses = classnames(
'wp-block-coblocks-gallery-carousel-thumbnail',
{
[ `has-margin-left-${ gutter }` ]: gutter > 0,
[ `has-margin-left-mobile-${ gutterMobile }` ]: gutterMobile > 0,
[ `has-margin-right-${ gutter }` ]: gutter > 0,
[ `has-margin-right-mobile-${ gutterMobile }` ]: gutterMobile > 0,
}
);
const thumbnailClasses = ( index ) => {
return classnames(
'wp-block-coblocks-gallery-carousel-thumbnail',
`wp-block-coblocks-gallery-carousel-thumbnail-${ index }`,
{
[ `has-margin-left-${ gutter }` ]: gutter > 0,
[ `has-margin-left-mobile-${ gutterMobile }` ]: gutterMobile > 0,
[ `has-margin-right-${ gutter }` ]: gutter > 0,
[ `has-margin-right-mobile-${ gutterMobile }` ]: gutterMobile > 0,
}
);
};

const thumbnailContainerClasses = classnames(
'wp-block-coblocks-gallery-carousel-thumbnail-pagination',
Expand Down Expand Up @@ -127,8 +131,8 @@ const save = ( props ) => {
return (
<div aria-label={ __( `Carousel Gallery`, 'coblocks' ) } >
<div className={ innerClasses }>
<div className={ swiperClasses } data-swiper={ JSON.stringify( swiperOptions ) } id={ uuid } style={ responsiveHeight ? undefined : swiperStyles } >
<div className="swiper-wrapper" id="swiper-wrapper">
<div className={ swiperClasses } data-swiper={ JSON.stringify( swiperOptions ) } id={ uuid } style={ swiperStyles } >
<div className="swiper-wrapper">
{ images.map( ( image, index ) => {
return (
<div className="swiper-slide" key={ index }>
Expand Down Expand Up @@ -167,7 +171,7 @@ const save = ( props ) => {
<div className={ thumbnailContainerClasses }>
{ images.map( ( item, index ) => {
return (
<div className={ thumbnailClasses } id={ `wp-block-coblocks-gallery-carousel-thumbnail-${ index }` } key={ index } style={ { height: '80px', width: '100px' } } >
<div className={ thumbnailClasses( index ) } key={ index } style={ { height: '80px', width: '100px' } } >
<img
alt={ item.alt }
data-id={ item.id }
Expand Down
5 changes: 5 additions & 0 deletions src/blocks/gallery-carousel/styles/style.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.coblocks-gallery--item {
height: 100%;
width: 100% !important;
}

.wp-block-coblocks-gallery-carousel {

&,
Expand Down
Loading