-
-
Notifications
You must be signed in to change notification settings - Fork 357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: expose scrollSnaps
selectedIndex
scrollTo
#1052
feat: expose scrollSnaps
selectedIndex
scrollTo
#1052
Conversation
|
built with Refined Cloudflare Pages Action⚡ Cloudflare Pages Deployment
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
scrollSnaps
, selectedIndex
, & scrollTo
in embla contextscrollSnaps
selectedIndex
scrollTo
Hi @shyakadavis, Do you maybe have an example? |
Hello, @danpii Sure. Here is my <script lang="ts">
import type { HTMLAttributes } from 'svelte/elements';
import { crossfade } from 'svelte/transition';
import { cubicInOut } from 'svelte/easing';
import { Button } from '../button';
import { getEmblaContext } from './context';
import { cn } from '$lib/utils';
type $$Props = HTMLAttributes<HTMLDivElement>;
let className: $$Props['class'] = undefined;
export { className as class };
const { handleKeyDown, scroll_to, scroll_snaps, selected_index } =
getEmblaContext('<Carousel.Dots/>');
const [send, receive] = crossfade({
duration: 250,
easing: cubicInOut
});
</script>
<div class="absolute bottom-32 right-28 hidden h-9 items-center gap-2 md:flex">
{#each $scroll_snaps as _, i}
{@const is_active = $selected_index === i}
<Button
size="icon"
on:click={() => scroll_to(i)}
on:keydown={handleKeyDown}
variant="accent"
class={cn(
'relative size-3 touch-manipulation rounded-full bg-border/10 transition-colors ease-linear hover:bg-border/30',
className
)}
>
{#if is_active}
<div
class={cn('absolute inset-0 rounded-full bg-border/60')}
in:send={{ key: 'active_slide' }}
out:receive={{ key: 'active_slide' }}
/>
{/if}
</Button>
{/each}
</div> |
Thanks for your quick reply @shyakadavis! Looks good and the scrollTo is working correctly. The only thing that's not working yet is the active bullet. |
Hmm... hard for me to summarise without looking at some code, but my guess is that the Inside //
++ const selectedIndex = writable(0);
//
function onSelect(api: CarouselAPI) {
if (!api) return;
canScrollPrev.set(api.canScrollPrev());
canScrollNext.set(api.canScrollNext());
++ selectedIndex.set(api.selectedScrollSnap());
}
//
$: if (api) {
++ onSelect(api);
++ api.on('select', onSelect);
++ api.on('reInit', onSelect);
}
// If this is the issue, I wonder if I forgot to add this store in the P.R. Please confirm if it is. |
Thanks again, really appreciated! It works now! In this PR (#1052 )you added: This part was already in the code (if I see it correctly):
So all I had to do was: function onSelect(api: CarouselAPI) {
if (!api) return;
canScrollPrev.set(api.canScrollPrev());
canScrollNext.set(api.canScrollNext());
+ selectedIndexStore.set(api.selectedScrollSnap());
} |
Snap. Thought I had covered this. Thank you for confirming. If I get time soon I'll open a follow-up P.R. (Or if you have the time right now, you could help.) Regardless, thank you. |
I can try as in the course of this week |
This P.R lays the groundwork necessary to create a dots-scroll-to feature like in the video. From now on, one need only reach into the context and grab these stores+function and style however they wish.
It's pretty easy to set up yourself, but having it come with the
carousel
component saves some time. 🙂(And I don't think this introduction violates/deviates from the original, wouldn't you agree? - at least components-wise)
Screen.Recording.2024-04-27.at.10.12.31.mov
Before submitting the PR, please make sure you do the following
feat:
,fix:
,chore:
, ordocs:
.pnpm format
andpnpm lint