Components
48
Background Gradients Breadcrumb Content Card Carousel Content Card Grid Content Cards Two Column Content Contact Form Content Contact Form Reveal Content Courses Map Content Courses Search Content Event Details Content Events Carousel Content Faqs Content Featured Cards Content Featured Products Content Featured Products Carousel Content Form Over Image Content Game Elements Content Hall Of Fame Carousel Content Horizontal Accordion Content Horizontal Scrolling Content How To Find Us Content Job Roles Grid Content Map Embed Content Marquee Text Content Media Carousel Content Players Content Quick Links Content Quote Over Image Content Simple Text And Image Variations Content Specification Content Stat Row Content Stat With Images And Content Content Text And Cards Grid Content Text And Image Variations Content Text Columns Content Text Image Carousel Content Text Over Image Content Title Content Useful Links Content Vacancies Grid Cookie Table Example Hero Basic Hero Featured Card Hero Game Hero Video And Carousel Hero Video Cards Playlist Hero Video Player

Content Card Grid

All articles

There are no ACF fields assigned to this component.

				
@import "../../resources/scss/util/variables";
@import "../../resources/scss/util/mixins";
@import "../../resources/scss/vendor/bootstrap/vendor/rfs";

.block-card-grid {
	transition: 0.3s opacity var( --ease );

	&.loading {
		opacity: 0.5;
		pointer-events: none;
	}

	.heading {
		@include margin-bottom( rem-calc(48) );
	}

	&__meta {
		display: flex;
		flex-direction: row;
		gap: rem-calc(16);
		flex-wrap: wrap;
		align-items: center;
		justify-content: space-between;
		@include margin-bottom( rem-calc(60) );

		.heading {
			margin-bottom: 0;
		}
	}

	.filter-row {
		select {
			background-color: $grey;
			border: none;
			text-overflow: ellipsis;
			padding-right: rem-calc(36);
			@include fluid-type(14, 18);
		}
	}

	&__grid {
		@include padding-top( rem-calc(32) );
		@include padding-bottom( rem-calc(32) );

		&.no-filters {
			padding-top: 0;
		}
	}

	.card-grid {
		&__item {
			padding: 0;

			.post-card {
				height: 100%;
			}
		}
	}
}
class CardGrid {
	/**
	 * @param {object} block
	 */
	constructor( block ) {
		this.block = block;
		this.initialised = false;
		this.blockId = this.block.getAttribute( 'id' );

		ScrollTrigger.create({
			trigger: this.block,
			start: 'top bottom',
			end: 'bottom top',
			// markers: true,
			// onEnter: () => this.initGrid(),
		});

		this.filters = block.querySelectorAll( '.filter-row select' );
		this.filters.forEach( filter => {
			filter.addEventListener( 'change', () => {
				this.updateURL();
			});
		});
	}

	updateURL() {
		this.block.classList.add( 'loading' );

		const urlWithParams = new URL( `${window.location.origin}${window.location.pathname}` );

		this.filters.forEach( filter => {
			if ( filter.options[filter.selectedIndex].value !== '' ) {
				urlWithParams.searchParams.append( filter.getAttribute( 'name' ), filter.options[filter.selectedIndex].value );
			}
		});

		let href = urlWithParams.href;
		if ( this.blockId ) {
			href = `${urlWithParams.href}#${this.blockId}`;
		}

		if ( window.location.href !== href ) {
			window.location = href;
		} else {
			this.block.classList.remove( 'loading' );
		}
	}

	initGrid() {
		if ( this.initialised === true ) {
			return;
		}
		this.initialised = true;

		this.grid = this.block.querySelector('.block-card-grid__grid');

		this.items = this.grid.querySelectorAll( '.card-grid__item' );
		this.setHeights();

		this.shuffle = new Shuffle(this.grid, {
			itemSelector: '.card-grid__item',
		});

		this.shuffle.on(Shuffle.EventType.LAYOUT, () => {
			setTimeout( () => {
				this.setHeights();
			}, 0);
		});

		const resizeHandler = debounce( () => {
			// this.setHeights();
		}, 500);
		window.addEventListener('resize', resizeHandler);
	}

	setHeights() {
		let rowHeight = {
			0: 0,
			1: 0,
			2: 0,
			3: 0,
		}

		this.items.forEach( (item, index) => {
			item.style.removeProperty( 'height' );
			let row = this.getRow( index );
			rowHeight[row] = item.clientHeight > rowHeight[row] ? item.clientHeight : rowHeight[row];
		});

		this.items.forEach( (item, index) => {
			let row = this.getRow( index );
			item.style.height = `${rowHeight[row] + 24}px`;
		});
	}

	getRow( index ) {
		let row = 0;
		if ( index >= 4 ) {
			row = 1;
		}
		if ( index >= 8 ) {
			row = 2;
		}
		if ( index >= 12 ) {
			row = 3;
		}

		return row;
	}
}

document.querySelectorAll('.block-card-grid').forEach((block) => {
	new CardGrid( block );
});
  • Filtering is triggered when a dropdown option is changed.
    • Sort by option = newset, oldest
    • Topic = list of categories
    • Author = all post authors
    • Audience = custom taxonomy for audience type

Animation: