r/reactjs • • 3d ago

Show /r/reactjs Made my first npm library β€” <MrScroll /> stops scrollbars from shoving your layout around. Be honest with me πŸ™

Hey folks,

You know when a scrollbar pops in and everything jumps a few pixels to the left? That annoyed me enough that I made a little library for it.

**See it in 10 seconds (live demo):** https://justicehwan.github.io/plz-mr-scroll/

One line at the root of your app, that's it:

```tsx

import { MrScroll } from "plz-mr-scroll/react";

<MrScroll />

```

No reserved gutter, still the native scrollbar. It just finds a spot for the scrollbar so your content stays put. It never touches your children, so it doesn't fight React over the DOM.

- GitHub (GIFs in the README): https://github.com/justicehwan/plz-mr-scroll

- npm: https://www.npmjs.com/package/plz-mr-scroll

It's my first library ever, so I honestly have no idea what I got wrong. If you have a minute, please take a look and tell me what's broken or dumb πŸ₯²

(Also haven't tried it on a real Windows machine yet β€” if you do, I'd love to know how it goes.)

P.S. I'm Korean and English isn't my first language, so sorry if anything here reads a bit off.

Thanks!

0 Upvotes

13 comments sorted by

2

u/Honey-Entire 2d ago

Why is this a react component instead of a JS script you load from a CDN? I'd also recommend learning how to organize your code better because this is some of the most convoluted, difficult to read or maintain JS code I've ever seen. Very clearly AI-generated which leads me to believe you had a weird problem caused by poor design decisions rather than understanding how the browser renders content...

0

u/Harleny-Quennet 2d ago

having it in the component tree is nice for cleanup on unmount though, a plain script tag in the head just sits there forever.

1

u/Honey-Entire 2d ago

What’s wrong with it sitting there forever? You pay the download price once, then it gets cached and you never have to worry about it. And if you care enough about cleaning up KB of data, there are plenty of ways to β€œunmount” the script.

Stop defending a bot with insignificant β€œgotchas”

0

u/justicehwan 2d ago

Thanks! Yes, that's exactly why. Unmounting runs the cleanup and puts everything back the way it was.

1

u/minimuscleR 2d ago

Why would you need this when you can just apply scrollbar-gutter: stable; to the same effect.

1

u/justicehwan 2d ago

Not the same effect, actually. scrollbar-gutter: stable is the exact thing I made this library to get away from.

With gutter, the scrollbar's strip stays reserved even when there is no scrollbar. So a short list ends up with 16px of padding on the left and 16px plus an empty strip on the right. The content stops jumping, sure, but now it sits off-center the whole time.

That lopsided padding bugged me more than the jump did. So this doesn't reserve anything. No scrollbar = same padding on both sides. When the scrollbar shows up, it finds room for it (inside the padding you already have, or just outside the box), so nothing moves either.

Doesn't the uneven left/right padding bother you?

1

u/minimuscleR 2d ago

Doesn't the uneven left/right padding bother you?

Not really noticeable imo. Maybe if it was a tiny container but it rarely is. We use this on our full-page overflows and yah at that size it doesn't look off

1

u/justicehwan 2d ago

It just really bothered me personally. In the projects I've worked on, there are a lot of elements that update dynamically in real time. Every time a scrollbar appeared or disappeared, the layout would shift, and I hated that.

I understand it's not a big deal for everyone, but I made this library for people like me who are more sensitive to these little layout inconsistencies. It's a small detail, but one I wanted to solve.

1

u/ybadev 2d ago

One Windows test I’d put in the README: open and close a modal halfway down a long page with scrollbars set to always show. Check whether the header shifts and whether you end up at the same scroll position.