import type { EmblaCarouselSvelteType } from 'embla-carousel-svelte'; import type emblaCarouselSvelte from 'embla-carousel-svelte'; import { getContext, hasContext, setContext } from 'svelte'; import type { HTMLAttributes } from 'svelte/elements'; import type { Readable, Writable } from 'svelte/store'; export type CarouselAPI = NonNullable['on:emblaInit']> extends ( evt: CustomEvent ) => void ? CarouselAPI : never; type EmblaCarouselConfig = NonNullable[1]>; export type CarouselOptions = EmblaCarouselConfig['options']; export type CarouselPlugins = EmblaCarouselConfig['plugins']; //// export type CarouselProps = { opts?: CarouselOptions; plugins?: CarouselPlugins; api?: CarouselAPI; orientation?: 'horizontal' | 'vertical'; } & HTMLAttributes; const EMBLA_CAROUSEL_CONTEXT = Symbol('EMBLA_CAROUSEL_CONTEXT'); type EmblaContext = { api: Writable; orientation: Writable<'horizontal' | 'vertical'>; scrollNext: () => void; scrollPrev: () => void; canScrollNext: Readable; canScrollPrev: Readable; handleKeyDown: (e: KeyboardEvent) => void; options: Writable; plugins: Writable; onInit: (e: CustomEvent) => void; scrollTo: (index: number, jump?: boolean) => void; scrollSnaps: Readable; selectedIndex: Readable; }; export function setEmblaContext(config: EmblaContext): EmblaContext { setContext(EMBLA_CAROUSEL_CONTEXT, config); return config; } export function getEmblaContext(name = 'This component') { if (!hasContext(EMBLA_CAROUSEL_CONTEXT)) { throw new Error(`${name} must be used within a component`); } return getContext>(EMBLA_CAROUSEL_CONTEXT); }