question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enable or disable on certain screen sizes

See original GitHub issue

I’m trying to use Embla (which is fantastic btw, great job) only on mobile, so sort of disabling it on desktop when it’s been enabled on mobile (i.e. window resize or orientation change).

So for instance:

  1. Load: Desktop screen size (>768) => No carousel
  2. Resize: to Mobile (<768) => Init carousel
  3. Resize: to Desktop (>768) => Destroy carousel
  4. etc…

Flickity has this cool feature where it tracks if a CSS after element has a certain content, then enables or disables it with CSS breakpoints. But it’s also 51kb of JS, so Embla is def more optimized and lightweight!

Is there any way to have a similar kind of behavior with Embla without having to listen to a window resize event and embla.destroy() (which doesn’t seem to completely destroy my instances btw) + embla.reInit(options) everytime? Or am I missing something?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
davidjerlekecommented, Sep 9, 2020

Hello Félix (@flayks),

Release 4.0.2 comes with a fix for this issue. Please let me know if it’s working as intended.

Best, David

2reactions
davidjerlekecommented, Sep 8, 2020

Hello Félix (@flayks),

Thank you for your question. I think the code in this old issue you mention is probably outdated. A lot has changed with the React implementation of Embla Carousel since then.

I took some time to create a CodeSandbox that implements what you want to achieve. Here’s the CodeSandbox with its related code if you want to check it out:

const EmblaCarousel = ({ children }) => {
  const [emblaRef, emblaApi] = useEmblaCarousel({ loop: false });
  const [emblaIsActive, setEmblaIsActive] = useState(false);

  const initEmblaBelow768px = useCallback(() => {
    const isActive = window.innerWidth < 768;
    setEmblaIsActive(isActive);
  }, [setEmblaIsActive]);

  useEffect(() => {
    initEmblaBelow768px();
    window.addEventListener("resize", initEmblaBelow768px);
    return () => window.removeEventListener("resize", initEmblaBelow768px);
  }, [initEmblaBelow768px]);

  return (
    <div className="embla">
      <div className="embla__viewport" ref={emblaIsActive ? emblaRef : null}>
        <div className="embla__container">
          {/* ...slides */ }
        </div>
      </div>
    </div>
  );
};

So here’s what’s happening:

  • Embla reacts to when its containerRef (called emblaRef in the code) changes and won’t initialize when it’s null, and if there’s an activated instance of Embla it will destroy it automatically.
  • So basically we just skip using the emblaRef by passing null when the screen width is >= 768.

Note

  • If this is a part of a bigger project, I would probably create a reusable hook that exposes the window size so other components can make use of it, instead of having it locally in the EmblaCarousel component.
  • I would also debounce the resize event with a reusable debounce hook.
  • If you’re server side rendering your app you should perform a check if the window object is available before you proceed with the window size check.
  • Here’s the recommended bare minimum setup for using Embla Carousel with React, if you want to check out the documentation example.

Let me know if it helps! David

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use the Screen Size Control - Block Visibility
The Screen Size control in Block Visibility allows you to conditionally show or hide blocks based on the width of the current screen....
Read more >
Declare restricted screen support - Android Developers
Declare a maximum screen size​​ However, if you're still not satisfied with the way Android resizes your app for large screens, you can...
Read more >
Disable Javascript effect based on the screen's size
My source code is here for you to try it, make sure you reduice the size of the screen for the responsive to...
Read more >
missing enable/disable by screen size for dashicons.min.css
missing enable/disable by screen size for dashicons.min.css ... What I am trying to do is to disable it on all mobile pages except...
Read more >
How do I disable the scrollTrigger animation on a certain ...
Hello. On low screen resolutions I will have a completely different header, so I don't need the scrollTrigger animation. I understand that I ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found