question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature description

Create an Embla Carousel Vue wrapper - A component that allows Vue users to consume Embla Carousel.

Release

Released with v7.0.0-rc05. Documentation will be released with stable v7 but until then you can check out usage instructions below:

Vue wrapper

Start by installing the Embla Carousel npm package and add it to your dependencies.

npm install embla-carousel-vue --save

The component structure

Embla Carousel provides the handy emblaCarouselVue function for seamless integration with Vue. A minimal setup requires an overflow wrapper and a scroll container. Start by adding the following structure to your carousel:

<template>
  <div class="embla" ref="emblaNode">
    <div class="embla__container">
      <div class="embla__slide">Slide 1</div>
      <div class="embla__slide">Slide 2</div>
      <div class="embla__slide">Slide 3</div>
    </div>
  </div>
</template>

<script>
  import emblaCarouselVue from 'embla-carousel-vue'

  export default {
    setup() {
      const [emblaNode] = emblaCarouselVue()
      return { emblaNode }
    },
  }
</script>

Styling the carousel

The emblaCarouselVue function gives us an emblaNode to attach to our wrapping element with the classname embla, which is needed to cover the scroll overflow. The element with the container classname is the scroll body that scrolls the slides. Continue by adding the following CSS to these elements:

<style scoped>
  .embla {
    overflow: hidden;
  }
  .embla__container {
    display: flex;
  }
  .embla__slide {
    position: relative;
    flex: 0 0 100%;
  }
</style>

Accessing the carousel API

The emblaCarouselVue function takes the Embla Carousel options as the first argument. Additionally, you can access the API with an onMounted like demonstrated below:

<template>
  <div class="embla" ref="emblaNode">
    <div class="embla__container">
      <div class="embla__slide">Slide 1</div>
      <div class="embla__slide">Slide 2</div>
      <div class="embla__slide">Slide 3</div>
    </div>
  </div>
</template>

<script>
  import { onMounted } from 'vue'
  import emblaCarouselVue from 'embla-carousel-vue'

  export default {
    setup() {
      const [emblaNode, emblaApi] = emblaCarouselVue({ loop: false })

      onMounted(() => {
        if (emblaApi.value) {
          // Embla API is ready
        }
      })

      return { emblaNode, emblaApi }
    },
  }
</script>

Adding plugins

The emblaCarouselVue function also accepts plugins as the second argument. Note that plugins need to be passed in an array like so:

<template>
  <div class="embla" ref="emblaNode">
    <div class="embla__container">
      <div class="embla__slide">Slide 1</div>
      <div class="embla__slide">Slide 2</div>
      <div class="embla__slide">Slide 3</div>
    </div>
  </div>
</template>

<script>
  import emblaCarouselVue from 'embla-carousel-vue'
  import Autoplay from 'embla-carousel-autoplay'

  export default {
    setup() {
      const [emblaNode] = emblaCarouselVue({ loop: false }, [Autoplay()])
      return { emblaNode }
    },
  }
</script>

Congratulations! You just created your first Embla Carousel component.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:20
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
davidjerlekecommented, Sep 12, 2022

@rojadesign yes, this is clearly a TypeScript related bug in the embla-carousel-vue package because the typings are wrong. The type for emblaApi should be EmblaCarouselType | null. I’ll have to look into this when possible. Feel free to propose a solution if you want. The Vue wrapper code is here.

Thank you for reporting this. I created the following bug report where we can track the bug:

Best, David

1reaction
rojadesigncommented, Sep 12, 2022

@davidjerleke thank you for your time. Here is the Vue3 + TypeScript template used - not really working but the same error I have locally also appears there:

Property 'clickAllowed' does not exist on type 'EmblaCarouselType | HTMLElement'. Property 'clickAllowed' does not exist on type 'HTMLElement'.ts(2339)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How and Why to Use Wrapper Components in Vue 3 - LearnVue
Generally, a wrapper component is a custom component that modifies a native element or 3rd party library by adding some custom functionality, ...
Read more >
vue3-webcomponent-wrapper - npm
Vue 3 wrapper to convert a Vue component into Web Component. It supports reactive attributes, events & slots. This is a port of...
Read more >
The Perfect Wrapper Components in Vue 2.6 and soon Vue 3.0
The goal of the wrapper is to essentially provide some design conformity — PS wrappers should 100% be responsible for custom logic as...
Read more >
Vue 3 Wrapper Component Example - CodePen
Insecure Resource. The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https....
Read more >
Wrapper | Vue Test Utils
Vue Test Utils is a wrapper based API. A Wrapper is an object that contains a mounted component or vnode and methods to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found