React

Two components, and a hook for markup of your own. React renders the slides, the library moves them; a server renders them first, in Next.js and in Nuxt.

import { Slider, Slide } from 'gpuslider/react';
import { controls } from 'gpuslider/plugins';
import { canvas } from 'gpuslider/canvas';
import { stretch } from 'gpuslider/effects';
import 'gpuslider/style.css';

function Photos( { photos, loop } ) {
	return (
		<Slider
			loop={ loop }
			plugins={ [ controls(), canvas( { effects: [ stretch() ] } ) ] }
			onChange={ ( index ) => console.log( index ) }
			aria-label="Photos"
		>
			{ photos.map( ( photo ) => (
				<Slide key={ photo.src }>
					<img className="gs-media" { ...photo } />
				</Slide>
			) ) }
		</Slider>
	);
}

<Slider> is the element of the slider with its track, <Slide> a slide. Slides that React adds or removes are seen by the slider.

Prop of <Slider>
loop, align, axis, free, duration and the other options of the coreA change of one changes the slider that exists
pluginsRead when the slider is made. Written in the JSX they cost nothing: they are not what it is made again for
remakeA list, as the dependencies of an effect: the slider is made again when one of them changes, and stays at its slide. For other plugins or effects
indexThe snap to be at. The slider goes there when it changes
onChange, onSettle( index, slider )
onListeners for every other event, by its name: { 'lightbox:open': … }
onSliderIs given the slider when it is made, and null when it ends
aroundWhat is in the slider beside its slides: arrows of your own, a caption
asIts element, div when nothing is said. <Slide> has it too
className, style, id, aria-label and every other attributeOf its element. className="gs-stack" for a stack

How many slides are in view and how far they are apart is CSS here as everywhere, see Layout is CSS: --gs-per-view and --gs-gap in a class or in style.

A component in a <Slider> has the slider by useSliderContext(), and is rendered again when the slider goes to another snap:

import { useSliderContext } from 'gpuslider/react';

function Next() {
	const slider = useSliderContext();
	return (
		<button disabled={ ! slider?.canNext } onClick={ () => slider.next() }>
			On
		</button>
	);
}

<Slider around={ <Next /> }>…</Slider>

With markup of your own there is the hook the components are made of:

import { useSlider } from 'gpuslider/react';

const [ ref, slider ] = useSlider( { loop, plugins: [ controls() ] }, [ loop ] );

<div className="gs" ref={ ref }>
	<div className="gs-track">…</div>
</div>

Its second argument is what the slider is made again for.

The components add 0.6 KB to the core, the hook alone 0.1 KB. The page above without the canvas is 5.9 KB in the bundle, arrows and dots in it; with the canvas and stretch it is 13.4 KB.

Rendered on the server

gpuslider/react says 'use client': in Next.js a page that is rendered on the server has a <Slider> in it as it is, and the slides in it may be of the server. The server renders the slider as the browser has it before the script: the element, the track, the slides and their images, and without the script it is a row that scrolls and snaps. No module of the library touches window or document when it is imported; the slider is made in an effect, and the canvas is loaded in the browser, when the page has time.

What keeps the page from moving when the script comes is CSS, which the server sends too:

  • gpuslider/style.css in the page, as early as the rest of its CSS.
  • The layout in the markup: className="gs-stack" for a stack, --gs-per-view and --gs-gap in a class or in style.
  • A size for the slides, as for any image: an aspect-ratio, or a height of the slider.

A test renders a page of sliders with renderToString, hydrates it in Chromium, Firefox and WebKit, and holds it to no mismatch, no slide in view that moves, and no shift of the layout while the canvas takes over.

With next/image the image of a slide is the one of Next, with gs-media for its class; the first one has priority, the others load as they come:

import Image from 'next/image';
import { Slider, Slide } from 'gpuslider/react';
import { controls } from 'gpuslider/plugins';
import { canvas } from 'gpuslider/canvas';
import { stretch } from 'gpuslider/effects';
import 'gpuslider/style.css';

export default function Page( { photos } ) {
	return (
		<Slider
			className="photos"
			plugins={ [ controls(), canvas( { effects: [ stretch() ] } ) ] }
			aria-label="Photos"
		>
			{ photos.map( ( photo, i ) => (
				<Slide key={ photo.src }>
					<Image
						className="gs-media"
						src={ photo.src }
						alt={ photo.alt }
						width={ photo.width }
						height={ photo.height }
						sizes="(max-width: 640px) 100vw, 33vw"
						priority={ i === 0 }
					/>
				</Slide>
			) ) }
		</Slider>
	);
}

A page of Server Components that gives its slides to <Slider> needs nothing more; the plugins are made where <Slider> is, in a component of the client, as they are functions.

In Nuxt, and elsewhere with Nitro, the server renders the markup of the slider as HTML (see Without a script of your own for what it is), and the slider is made once the page is mounted:

<script setup>
import { onMounted, onBeforeUnmount, ref } from 'vue';
import { createSlider } from 'gpuslider';
import { controls } from 'gpuslider/plugins';
import 'gpuslider/style.css';

const root = ref();
let slider;
onMounted( () => ( slider = createSlider( root.value, { plugins: [ controls() ] } ) ) );
onBeforeUnmount( () => slider?.destroy() );
</script>

<template>
	<div ref="root" class="gs" aria-label="Photos">
		<div class="gs-track">
			<div v-for="photo in photos" :key="photo.src" class="gs-slide">
				<img class="gs-media" v-bind="photo" />
			</div>
		</div>
	</div>
</template>