refactor(hooks): replace lock_scroll.js with Rust web_sys implementation#21
Open
mp-c0de wants to merge 2 commits intorust-ui:mainfrom
Open
refactor(hooks): replace lock_scroll.js with Rust web_sys implementation#21mp-c0de wants to merge 2 commits intorust-ui:mainfrom
mp-c0de wants to merge 2 commits intorust-ui:mainfrom
Conversation
Resolves rust-ui#1. Rewrites the scroll lock utility in pure Rust using web_sys, eliminating the vanilla JS dependency. - Add scroll_lock.rs with lock(), unlock(delay), is_locked() API - Register window.ScrollLock via wasm_bindgen closures at app init - Add CssStyleDeclaration to web-sys features for getComputedStyle - Remove <script src="/hooks/lock_scroll.js"> from shell and 8 components - Delete public/hooks/lock_scroll.js (254 lines of JS)
Contributor
Author
Also fixes #23 (Safari tab buttons)While testing this PR I discovered it incidentally fixes #23 as well. The inline Verified by A/B testing — clean Full root cause analysis in #23. |
The init() call is already wrapped with #[cfg(target_arch = "wasm32")], but the use statement was unconditional, causing an unused-import warning on the SSR build.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Resolves #1.
Replaces
public/hooks/lock_scroll.js(254 lines of vanilla JS) with a pure Rust implementation usingweb_sys. The new module lives inapp_crates/registry/src/hooks/scroll_lock.rsand exposes the same API:lock()— locks scroll on body + all scrollable containers, compensates scrollbar widthunlock(delay_ms)— restores scroll after optional delay (for exit animations)is_locked()— returns current lock stateThe Rust implementation:
window.ScrollLockviawasm_bindgenclosures so existing inline scripts work unchangedthread_local! { RefCell }for state (wasm is single-threaded)ScrollArea,CommandList,SelectContent, etc.)Changes
app_crates/registry/src/hooks/scroll_lock.rs— full Rust implementationCargo.toml— addedCssStyleDeclarationto web-sys featuresapp/src/app.rs— callsscroll_lock::init()at app startup (wasm-only via#[cfg])app/src/shell.rs— removed global<script async src="/hooks/lock_scroll.js"><script src="/hooks/lock_scroll.js">fromSheet,Dialog,Select,DropdownMenu,MultiSelect,Command,Menubar,ContextMenupublic/hooks/lock_scroll.jsNot included (follow-up work)
public/registry/files (tree.md, component markdown)crates/ui-cli/test fixtures that referencelock_scroll.jsTest plan
cargo checkcompiles with zero errors onscroll_lock.rscargo leptos watch— server starts without panicswindow.ScrollLockis registered andisLocked()returns correct stateuse-lock-body-scroll.spec.tshook test