Carousel Component

General Information

  • Carousels are generated through a svelte Carousel component, which can be imported by including the following line in the file's script tag:
    import Carousel from '$lib/components/Carousel.svelte';
  • The primary focus of the Carousel component is to generalize the control functions of a carousel, such that said functionality doesn't need to be re-implemented at every use of a carousel. The control functionality is implemented through a controller class, which covers next, previous, reset and slide rendering actions. Additionally, autoplay is supported.
  • The component makes use of the display: contents css property such that the user can place the carousel's elements onto the parent's grid. Furthermore, an effort has been made to cover for the shortcomings of using display: contents, such as a lack of inheriting grid gaps and other miscellaneous properties. The carousel's sub-components are to be defined through svelte 5's Snippet syntax, of which examples will be included in the examples section.
  • The Carousel component has two parameters through which the carousel can be configured.
    1. data: An array with (typed) data that needs to be shown, where each index corresponds to a slide in the carousel.
    2. autoplay: Sets an interval in milliseconds that automatically makes the carousel proceed to the next slide (default 0). Autoplay is disabled if no autoplay value is given, or the interval is equal to zero. Furthermore, the timer will be reset when the user manually clicks on one of the carousel's navigation buttons.
  • Furthermore, several snippets need to be provided through which components of the carousel can be rendered. These snippets are to be included within the body of the Carousel tags, such that it is ensured that the type of the data and the type passed on to the slide snippet are equivalent.
    1. slide(data): A snippet with a one input that renders the slide's data in the desired format.
    2. previous(): A snippet that renders the previous button.
    3. next(): A snippet that renders the next button.
    4. navigationContainer(previous, next) (optional): A snippet with two parameters that renders the previous and next buttons in the desired layout.

Examples

Basic setup

As noted in the general information, the data to be shown in the carousel needs to be passed on to the Carousel component (in this case through the variable slideData). On top of that, all elements within the carousel component are placed directly onto the parent's grid. Finally, the snippets used to render the content in the carousel are put within the body of the Carousel, which automatically assigns them to the appropriate parameters of the component.

<Carousel data={slideData}>
  {#snippet slide(data)}
    <h2>{data.title}</h2>
    <p>{data.description}</p>
  {/snippet}
  
  {#snippet previous()}
    <p class="previous button-link">&#129120;</p>
  {/snippet}
  
  {#snippet next()}
    <p class="next button-link">&#129122;</p>
  {/snippet}
</Carousel>

Custom navigation bar layout

The setup of the carousel shown below is identical to that of the previous example, with two differences: the navigation buttons have been placed in their own container and a five second autoplay interval has been added to the carousel. The inclusion of a navigation container allows for a different layout to be used to place the navigation buttons, with custom stylingβ€”in this case, a flex box is used with a border.

<Carousel data={slideData} autoplay={5000}>
  {#snippet slide(data)}
    <h2>{data.title}</h2>
    <p>{data.description}</p>
  {/snippet}
  
  {#snippet previous()}
    <p class="previous button-link">&#129120;</p>
  {/snippet}
  
  {#snippet next()}
    <p class="next button-link">&#129122;</p>
  {/snippet}
  
  {#snippet navigationContainer(previous, next)}
    <div class="navigation">
      {@render previous()}
      {@render next()}
    </div>
  {/snippet}
</Carousel>

Concrete example

Below is a (more complex) concrete example of how the Carousel component can be used for slides that have elements spread over the parent node. Note however that this example required a decent amount of styling overwrites through the :global() css directive. Please refer to the stylesheet in the svelte file for further information on how to accomplish the latter.

Proces begeleider