npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@snappywc/carousel

v1.0.28

Published

A tiny, customizable, and accessible carousel that works anywhere!

Downloads

167

Readme

<snappy-carousel>

The tiny, no-nonsense carousel that works anywhere. 1.9kb (Brotli). 0 Dependencies.

Demo with Vanilla, React and Vue examples

Installation

Option 1: As a package.

npm i @snappywc/carousel
import '@snappywc/carousel'

Option 2: In your markup.

<script type="module">
	import '//unpkg.com/@snappywc/carousel'
</script>

Usage

Just put a <snappy-carousel> anywhere in your markup and you're ready to go. This is a native web component and works in React, Vue, Svelte, Angular, and any other web framework because all @snappywc packages have 0 dependencies and are built using low-level APIs.

<snappy-carousel>
	<img src="//picsum.photos/seed/1/600/375" />
	<img src="//picsum.photos/seed/2/600/375" />
	<img src="//picsum.photos/seed/3/600/375" />
</snappy-carousel>

That's it! Pat yourself on the back and grab a drink!

API

Carousel Methods

The following methods can be used to control the carousel. Note that there are no methods to manually refresh or re-initialize the carousel: any content changes to the main slot will automatically trigger a refresh.

| Method | Description | | ------------- | ------------------------ | | .prev() | go to the previous slide | | .next() | go to the next slide | | .goToSlide(#) | go to slide at index # |

Slide Events

It is a common requirement to execute code when a slide becomes visible or is hidden, perhaps to apply an animation or to support analytics. Each slide provides a pair of events that you can listen for to support these requirements. You can either target an individual slide or loop over every slide in the carousel using the slides attribute.

| Event | Description | | ------- | --------------------------- | | visible | fired when slide is visible | | hidden | fired when slide is hidden |

An example using visible and hidden events to apply slide effects.

const carousel = document.querySelector('snappy-carousel#demo')
const specialSlide = carousel.querySelector('.special')

// target a specific slide
specialSlide.addEventListener('visible', () => {
	specialSlide.setAttribute('class', 'animate__animated animate__pulse')
})

specialSlide.addEventListener('hidden', () => {
	specialSlide.setAttribute('class', '')
})

// target every slide
carousel.slides.forEach((slide) => {
	slide.addEventListener('visible', () => {
		console.log(slide)
	})
})

Customization

@snappywc components provide CSS variables, named slots and special CSS selectors for custom styling. This provides greater flexibility than offering dozens of parameters that may or may not accomplish what you need.

For a better idea of how this works, check out the demos!

CSS Variables and Defaults

Change the basic layout and behavior of the carousel.

snappy-carousel {
	// slide width per breakpoint
	--mobile: 100%;
	--tablet: 50%; // >= 768
	--desktop: 33.33%; // >= 1024

	// styling
	--gap: 0;
	--icon-color: currentcolor;
	--arrow-alignment: flex-end; //vertical, flex values
	--arrow-size: 48px;
	--indicator-size: 16px;
}

Slots

Named slots allow you to customize the icons used for the previous button, next button, indicators and active indicators. These can be SVG icons, icons from a font, images, or even a custom component. Total freedom!

Pro tip: you can use inline styles on a slotted element to override the color and size variables. There is an example of this in the demo.

  • prev-icon
  • next-icon
  • indicator-icon
  • indicator-icon-active

An example using named slots to customize the previous and next icons using inline SVGs.

<snappy-carousel>
	<!-- Default slot for slides -->
	<img src="whatever" />
	<img src="whatever" />
	<img src="whatever" />

	<!-- Named slots for controls -->
	<svg slot="prev-icon">
		<path d="whatever" />
	</svg>

	<svg slot="next-icon">
		<path d="whatever" />
	</svg>
</snappy-carousel>

Parts

Web components have a reputation for being hard to style, but CSS ::part() selectors make it trivial to target an element inside the component's Shadow DOM and apply any styling you want to it.

  • carousel
  • track
  • indicators
  • prev
  • next

A simple example using ::part() selectors to style the indicators and previous/next buttons.

snappy-carousel {
	&::part(indicators) {
		position: absolute;
		bottom: 0;
		filter: drop-shadow(0 1px 2px black);
	}

	&::part(prev),
	&::part(next) {
		filter: drop-shadow(0 1px 2px black);
	}
}

Accessibility

This should be everyone's first priority when implementing a carousel. I had a blind roommate in college and carousels were the bane of his existence. If you're going to use a carousel, it needs to be done right. @snappywc web components cover all of the basics, including appropriate aria labels, aria roles, and announcing changes to screen readers.

You may have noticed that <snappy-carousel> doesn't provide any default support for auto-play. That's because auto-play is terrible for everybody, but especially for users who depend on keyboard navigation or screen readers. That said, if you really need auto-play, you can easily implement it using the .next() method. Please be kind to your users and pause auto-play on hover, as in the example below.

// auto-play is evil, but here's how you can do it
const carousel = document.querySelector('snappy-carousel#custom')

carousel.onmouseover = () => {
	carousel.dataset.hovering = true
}

carousel.onmouseleave = () => {
	carousel.dataset.hovering = false
}

setInterval(() => {
	carousel.dataset.hovering !== 'true' && carousel.next()
}, 5000)

So there you have it.

An easy-to-use, fast and accessible carousel that you can quickly use anywhere and style any way you like, all for less than 2kb.