Skip to content

Vue Composables

The composables are a flexible way to integrate Compote into your Vue application using the Composition API.

First, use a ref to get your HTMLElement and pass it to your composable as a first argument.

vue
<script setup lang="ts">
import { useMarquee } from '@compotes/vue'
import { useTemplateRef } from 'vue'
import 'compotes/css/marquee.css'

const marqueeEl = useTemplateRef<HTMLElement>('marqueeEl')
const marquee = useMarquee(marqueeEl)
</script>

As the second argument, you can pass the options of the component.

vue
<script setup lang="ts">
import { useMarquee } from '@compotes/vue'
import { useTemplateRef } from 'vue'
import 'compotes/css/marquee.css'

const marqueeEl = useTemplateRef<HTMLElement>('marqueeEl')
const marquee = useMarquee(marqueeEl, { fill: true })
</script>

In the template, follow the HTML structure required by the underlying component.

vue
<script setup lang="ts">
import { useMarquee } from '@compotes/vue'
import { useTemplateRef } from 'vue'
import 'compotes/css/marquee.css'

const marqueeEl = useTemplateRef<HTMLElement>('marqueeEl')
const marquee = useMarquee(marqueeEl, { fill: true })
</script>

<template>
  <div ref="marqueeEl" class="c-marquee">
    <ul class="c-marquee-container">
      <li>This is the default marquee</li>
      <li>Marquee or marquii</li>
    </ul>
  </div>
</template>

WARNING

Don't forget to import the CSS of the component you want to use!

That's it for the basic Vue composable setup.

Available Composables

ComposableStateMethods
useCollapse(el, options)instance, isExpanded, isCollapsingshow(), hide(), toggle(), update(), destroy()
useDrag(el, options)instance, isDragging, isDraggabledestroy()
useDrilldown(el, options)instance, level, currentMenuIdupdate(reloadItems?), back(), reset(), destroy()
useDropdown(el, options)instance, isOpen, typeopen(), close(), toggle(), update(), equalizeWidth(), destroy()
useMarquee(el, options)instance, isPausedplay(), pause(), update(fill?), destroy()

Methods

You can access the component methods through the composable. They will be available after the onMounted lifecycle hook. For advanced usage, instance gives access to the underlying Compotes class instance. Here is an example with the marquee component with a simple play/pause implementation.

vue
<script setup lang="ts">
import { useMarquee } from '@compotes/vue'
import { useTemplateRef } from 'vue'
import 'compotes/css/marquee.css'

const marqueeEl = useTemplateRef<HTMLElement>('marqueeEl')
const marquee = useMarquee(marqueeEl, { fill: true })
</script>

<template>
  <button @click="marquee.pause()">
    Pause
  </button>
  <button @click="marquee.play()">
    Play
  </button>
  <div ref="marqueeEl" class="c-marquee">
    <ul class="c-marquee-container">
      <li>This is the default marquee</li>
      <li>Marquee or marquii</li>
    </ul>
  </div>
</template>

State

All the component state properties are reactive after the onMounted lifecycle hook. You can use them directly in templates and <script setup>. If you need a ref, use toRef on the returned object.

useCollapse

StateTypeDescription
instanceCollapse | nullThe underlying Compotes Collapse instance
isExpandedbooleanWhether the collapse is currently expanded
isCollapsingbooleanWhether the collapse is currently animating

useDrag

StateTypeDescription
instanceDrag | nullThe underlying Compotes Drag instance
isDraggingbooleanWhether the element is currently being dragged
isDraggablebooleanWhether the element is draggable (updates on resize)

useDrilldown

StateTypeDescription
instanceDrilldown | nullThe underlying Compotes Drilldown instance
levelnumberThe current navigation depth level
currentMenuIdstring | nullThe ID of the currently active menu

useDropdown

StateTypeDescription
instanceDropdown | nullThe underlying Compotes Dropdown instance
isOpenbooleanWhether the dropdown is currently open
type'default' | 'menu'The current type of the dropdown

useMarquee

StateTypeDescription
instanceMarquee | nullThe underlying Compotes Marquee instance
isPausedbooleanWhether the marquee is currently paused

Here is an example to show the current status of the marquee component.

vue
<script setup lang="ts">
import { useMarquee } from '@compotes/vue'
import { useTemplateRef } from 'vue'
import 'compotes/css/marquee.css'

const marqueeEl = useTemplateRef<HTMLElement>('marqueeEl')
const marquee = useMarquee(marqueeEl, { fill: true })
</script>

<template>
  <div>{{ marquee.isPaused ? 'Paused' : 'Playing' }}</div>
  <div ref="marqueeEl" class="c-marquee">
    <ul class="c-marquee-container">
      <li>This is the default marquee</li>
      <li>Marquee or marquii</li>
    </ul>
  </div>
</template>

Events

Events are automatically handled by the composables. The reactive state is kept in sync with the underlying component by listening to its events, so you don't need to manually subscribe — simply use the reactive state properties provided by each composable.

If you need to listen to events directly, you can access them through the instance property:

vue
<script setup lang="ts">
import { useMarquee } from '@compotes/vue'
import { useTemplateRef, watchEffect } from 'vue'
import 'compotes/css/marquee.css'

const marqueeEl = useTemplateRef<HTMLElement>('marqueeEl')
const marquee = useMarquee(marqueeEl, { fill: true })

watchEffect((onCleanup) => {
  if (!marquee.instance?.el)
    return

  const handler = () => console.log('Marquee started playing!')
  marquee.instance.el.addEventListener('c.marquee.play', handler)

  onCleanup(() => {
    marquee.instance?.el?.removeEventListener('c.marquee.play', handler)
  })
})
</script>

For a list of available events for each component, refer to their respective documentation pages.

Released under the MIT License.