-
Notifications
You must be signed in to change notification settings - Fork 606
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
Mechanism to retrieve previous index/step #419
Comments
I think you could use the https://github.com/jxnblk/mdx-deck/blob/master/docs/api.md#usedeck |
I'm not sure that useDeck is enough, but maybe I'm missing something. For the first use case, I'm trying to recreate the slide transitions from earlier versions of mdx-deck default theme. (An example here: https://simply-react.netlify.com/). It is a basic slide-in and slide-out animation. If you are moving forwards in the slide deck, you want slides to "exit" to the left and "enter" from the right. However if you're going backwards through the deck, you want the opposite. I think this is difficult to achieve without being able to establish the direction the user is headed in the slide deck. Unless I'm missing something, the only ways to do this are:
As far as I know useDeck does not expose the previous slide index, only the current slide. |
Yeah, the “direction” is something you’d need to keep track of but useDeck will give you the current slide and step |
Still struggling with this. In order to achieve directional transitions, I need to store the previous slide index in some sort of global state store. But so far I haven't figured out where to store a state variable that would be shared with all slides? Since each slide needs to be wrapped in an individual layout component, you should not store state in these layouts, because even if two slides have the same layout, they will not share state since they are different instances of the same component. Is the Provider the right place to store this? If so, how might I pass it down to a layout component? It doesn't appear that props are passed from Provider->Layout directly, and from my reading of Slide.js each slide has its own context provider. Maybe a better idea would be for me to create a new useDeckEnhanced hook that contains the context I need? Any guidance you could provide would be greatly appreciated. |
Right, so to store the state that you'd need for this I think you'd want the following:
|
Thank you for pointing me in the right direction! I think I'm almost there, but I think I'm struggling to understand how useState/useEffect should be used in the custom provider. It seems that the provider renders a few extra times with the default state values at the end of a slide transition (e.g. 1->2). Here is my custom provider:
Note that I am using a custom layout as well which reads the context value with ExtraContext.Consumer. No state changes are made beyond the custom provider, so hopefully reading the context doesn't impact how the provider renders. Some console output for one slide transition with this custom provider. Note that it renders several times (beyond what seems to be needed by the state updates done in useEffect/useLayoutEffect) and finishes with the initial value for the direction state.
I have a feeling that there is something about the useState/useEffect combination that I have not figured out yet, but I thought I would ask here to ensure the problem I'm seeing is not related to how the Provider renders in mdx-deck. |
Another quick test with a basic custom provider and basic layout seems to indicate that the provider renders multiple (3) times per slide transition: Provider.js
This shouldn't be an issue since useState should use the state values from previous renders, but it appears it does not? Very strange. I also tested with a basic useState/useEffect to see if the state changes are picked up on subsequent renders, but it seems they are not: Provider.js
Console output:
Maybe the Provider is not the right place to put state changes? |
Is there a way to determine which slide was last visited at render time?
It would be very helpful to the following use cases:
Slide transitions: If you need to establish the "direction" you are moving in the slide deck to provide different animations (e.g. slide in/out animations similar to previous versions of mdx-deck)
Customizing Appear: Determine what should happen on a step within an Appear. Similar to Advanced step control #193.
Maybe this can be accomplished through reach router history but I haven't found a way to do so yet.
The text was updated successfully, but these errors were encountered: