Comments (7)
@sarussss The requirement is that reInit
should be only fired after the first user interaction. Initially, the carousel must not loop. So I'm not sure that listening to the init
event would be helpful here.
I believe that the only available event that is fired on user interactions (after the current index has been changed( is the select event, that's why I'm trying to use it.
from embla-carousel.
@rodrigovallades I haven't investigated yet so can't be sure but it might be related to this bug:
There's already a bug fix done for that but it hasn't been released yet. It will be released with v8.0.0-rc23
.
from embla-carousel.
Hi @rodrigovallades
I think you should listen to the init event first to make sure the slider has been created, then run the reInit function.
Hope it's of some help to you, because I didn't code React and don't understand the specific structure to be able to help you more.
emblaApi.on('init', () => {
console.log('emblaApi init')
const engine = emblaApi.internalEngine()
if (!engine.options.loop) emblaApi.reInit({ loop: true })
});
from embla-carousel.
@rodrigovallades I can't access your sandbox. Please provide a link that works:
from embla-carousel.
I'm sorry @davidjerleke , the sandbox was private. Try again, please
https://codesandbox.io/p/sandbox/embla-carousel-generator-react-forked-l57274
from embla-carousel.
@rodrigovallades thanks. This is expected behaviour. What the docs say about reInit()
:
Hard reset the carousel after it has been initialized. This method allows for changing options and plugins after initializing a carousel.
Hard reset being the keywords. reInit()
will stop any active animations and hard reset the carousel. So there's no promise from the Embla side that animations should be fluid when changing plugins and options. If you think this is unclear or vague, you're welcome to create a pull request and improve the docs.
With that said, you can achieve a fluid reInit in an unofficial way like so:
function reInitFluid() {
const oldEngine = emblaApi.internalEngine();
emblaApi.reInit();
const newEngine = emblaApi.internalEngine();
const copyEngineModules: (keyof EngineType)[] = ['location', 'target', 'scrollBody'];
copyEngineModules.forEach(engineModule => {
Object.assign(newEngine[engineModule], oldEngine[engineModule]);
});
newEngine.translate.to(oldEngine.location.get());
const { index } = newEngine.scrollTarget.byDistance(0, false);
newEngine.index.set(index);
newEngine.animation.start();
};
But I can't support this feature officially. The problem with supporting a feature like that is that there's no way to write tests to cover all scenarios that can happen (think about the number of options, methods and events Embla has, and then think about the number of combinations possible). Because devs do all sorts of stuff that will break that, so I don't want to spend the little time I have answering a bunch of "false" bug reports where people claim the library is broken because it can't do magic to patch all their crazy cases.
Best,
David
from embla-carousel.
Thanks for que quick response @davidjerleke .
I'm sorry for the false bug report. I misinterpreted the "hard reset" keywords.
I would have assumed it's not a bug if the carousel would have remained stationary ta the first item after being reinitialized. But since it just "jumped" to the second item, it felt like a bug.
This behavior of becoming "looped" only after the first interaction is similar to Netflix's carousel, btw.
I tried your solution. It kinda works if the first interaction with the carousel is "swipe" or "drag". But if the first interaction is a click on the "next" button, then there's a weird edge case where it doesn't work going BACKWARD from item 2 to 1. Going forward always works.
I'll try to sort it out. You can see it here, if you want.
https://codesandbox.io/p/sandbox/reinit-with-animation-fixed-forked-97y73y
Best,
Rodrigo
from embla-carousel.
Related Issues (20)
- [Bug]: When the direction is set to 'rtl' and the loop is set to true, it doesn't loop infinitely HOT 1
- [Bug]: canScrollNext method returns false even when there are items to scroll HOT 1
- [Bug]: AutoScroll position jumps when clicked and dragged HOT 7
- [Bug]: Input click block after dragging HOT 2
- [Feat]: Add TypeScript moduleResolution: bundler support HOT 11
- [Bug]: Autplay stops working after drag HOT 2
- [Bug]: Cannot find module 'embla-carousel/components/Options' or its corresponding type declarations. TypeScript, React with Vite HOT 2
- [Bug]: Flickering when adding icon or image in slides (8.0) HOT 3
- [Bug]: Direction rtl reverses y axis direction HOT 5
- [Bug]: Slide hanging if overdragged/scrolled HOT 11
- [Bug]: Angular autoplay error HOT 1
- [Bug]: Autoplay fast scroll when opened in a new tab HOT 7
- [Bug]: no specific name for plugin import, have to use "index" instead HOT 4
- [Bug]: Embla Carousel's animation becomes less smooth when the battery saving mode is enabled. HOT 1
- [Bug]: Render when slides are wider than view size HOT 5
- [Bug]: When using the wheel gesture to swipe the carousel, for the first slide and the last slide, there are excessive blank space. HOT 2
- [Bug]: Thumbnails example won't work in RTL HOT 2
- [Bug]: Height is getting set to 0 HOT 4
- [Bug]: Scroll broken after direction update HOT 15
- [Bug]: Jest testing bug return TypeError: undefined is not a function HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from embla-carousel.