Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Carousel machine with factory exports #62

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

andrewmcoupe
Copy link

@andrewmcoupe andrewmcoupe commented Jul 30, 2021

The same changes as the Carousel PR but includes the ability to export a factory function as well as a standard state machine object.

This is a machine to manage the state and slide transitions for a carousel. This could be a carousel of any sort eg. image, content etc.

Happy to see alternative approaches to this kind of machine. I was thinking that an activity would be the best approach but events can't be sent inside an activity so I went for the approach advised by David here.

@vercel
Copy link

vercel bot commented Jul 30, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/mattpocock/xstate-catalogue/4iZ8TszotYxSqTHArxNTWJw5yhmc
✅ Preview: https://xstate-catalogue-git-fork-andrewmcoupe-carous-7259d1-mattpocock.vercel.app

@andrewmcoupe andrewmcoupe changed the title Carousel/with factory exports Carousel machine with factory exports Jul 30, 2021
@andrewmcoupe andrewmcoupe marked this pull request as ready for review July 30, 2021 06:27
@andrewmcoupe andrewmcoupe marked this pull request as draft July 30, 2021 06:31
@mattpocock
Copy link
Owner

Jeez, @andrewmcoupe - this is killer.

lib/metadata.ts Outdated Show resolved Hide resolved
pages/machines/[id].tsx Outdated Show resolved Hide resolved
Co-authored-by: Matt Pocock <mattpocockvoice@gmail.com>
@damiensedgwick
Copy link
Contributor

I like this a lot! I'm a fan of the ole carousel!

I do have a question though; carousels typically move in two directions, left and right. When I play with the machine and I UPDATE_INDEX from the idle state. It only ever increases or resets to 0 if the totalSlideCount is reached.

Would it be better to split UPDATE_INDEX into 2 events: DECREASE_INDEX and INCREASE_INDEX so that we could send either event depending on whether the left or right control button is clicked or if the indicator that is clicked is lower or higher than the current index?

@andrewmcoupe
Copy link
Author

andrewmcoupe commented Aug 7, 2021

I like this a lot! I'm a fan of the ole carousel!

I do have a question though; carousels typically move in two directions, left and right. When I play with the machine and I UPDATE_INDEX from the idle state. It only ever increases or resets to 0 if the totalSlideCount is reached.

Would it be better to split UPDATE_INDEX into 2 events: DECREASE_INDEX and INCREASE_INDEX so that we could send either event depending on whether the left or right control button is clicked or if the indicator that is clicked is lower or higher than the current index?

You can provide an index with the UPDATE_INDEX event (see below) to set the activeIndex to whatever you want but it made me realise that the index can be set to something outside of the valid range (0 - totalSlidesCount) so I've added a check in for that.

send({ type: "UPDATE_INDEX", index: 2 })

@damiensedgwick
Copy link
Contributor

You can provide an index with the UPDATE_INDEX event (see below) to set the activeIndex to whatever you want but it > made me realise that the index can be set to something outside of the valid range (0 - totalSlidesCount) so I've added a > check in for that.

Good catch! How would you determine the index to pass in the event? I see no logic for decreasing the index at the moment, only increasing or specifying it exactly. Am I being dumb here? I don't see how I would use this and go back to the previous slide unless I could do something like:


<button
  onClick={() => send({ type: "UPDATE_INDEX", index: context.currentIndex - 1})}
>
  Previous Slide
</button

Then there is missing logic for when the index is 0 and it needs to start from the last item in the carousel again. This logic already exists when going the other way and reaches the totalSlideCount.

Again, sorry if I am missing the obvious here!

@andrewmcoupe
Copy link
Author

andrewmcoupe commented Aug 8, 2021

You can provide an index with the UPDATE_INDEX event (see below) to set the activeIndex to whatever you want but it > made me realise that the index can be set to something outside of the valid range (0 - totalSlidesCount) so I've added a > check in for that.

Good catch! How would you determine the index to pass in the event? I see no logic for decreasing the index at the moment, only increasing or specifying it exactly. Am I being dumb here? I don't see how I would use this and go back to the previous slide unless I could do something like:


<button
  onClick={() => send({ type: "UPDATE_INDEX", index: context.currentIndex - 1})}
>
  Previous Slide
</button

Then there is missing logic for when the index is 0 and it needs to start from the last item in the carousel again. This logic already exists when going the other way and reaches the totalSlideCount.

Again, sorry if I am missing the obvious here!

That is how I would imagine a previous slide button working, yes 👍 But you're right about the missing logic. I'll add that in, good spot 👁️

I've also added in NEXT_INDEX and PREV_INDEX as I think that is much easier to use, so now you can just

send({ type: "NEXT_INDEX" })
send({ type: "PREV_INDEX" })

@damiensedgwick
Copy link
Contributor

@andrewmcoupe Nice work! This looks like a super nice way to handle carousel slides! Long gone are the days of jQuery libraries 😂

@andrewmcoupe andrewmcoupe mentioned this pull request Aug 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants