-
Notifications
You must be signed in to change notification settings - Fork 151
upgrade: scroll package upgrade for Solid 2.0 #857
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
Open
davedbase
wants to merge
4
commits into
solidjs-community:next
Choose a base branch
from
davedbase:update/v2/scroll
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| --- | ||
| "@solid-primitives/scroll": major | ||
| --- | ||
|
|
||
| Migrate to Solid.js v2.0 (beta.10) | ||
|
|
||
| ## Breaking Changes | ||
|
|
||
| **Peer dependency**: `solid-js@^2.0.0-beta.10` and `@solidjs/web@^2.0.0-beta.10` are now required. | ||
|
|
||
| ### `@solid-primitives/scroll` | ||
|
|
||
| - `isServer` now imported from `@solidjs/web` (not `solid-js/web`) | ||
| - `onMount` replaced with `onSettled` for post-render position refresh | ||
| - `sharedConfig.context` replaced with `sharedConfig.hydrating` for hydration detection | ||
| - Internal signal pattern replaced: Solid 2.0's `createSignal(fn)` creates a derived signal rather than storing a function value; now uses a version counter to drive memo re-evaluation on scroll events | ||
| - Signal uses `{ ownedWrite: true }` to allow writes from DOM event handlers within reactive scopes | ||
| - Tests updated: `createComputed` removed (no longer in Solid 2.0), replaced with direct reactive reads and `flush()` for synchronous assertions; `createSignal` in tests uses `{ ownedWrite: true }` | ||
| - README: `onMount` example updated to `onSettled` |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,108 +1,11 @@ | ||
| import { createEventListener } from "@solid-primitives/event-listener"; | ||
| import { createHydratableSingletonRoot } from "@solid-primitives/rootless"; | ||
| import { createDerivedStaticStore } from "@solid-primitives/static-store"; | ||
| import { type Accessor, createSignal, onMount, sharedConfig } from "solid-js"; | ||
| import { isServer } from "solid-js/web"; | ||
|
|
||
| export function getScrollParent(node: Element | null): Element { | ||
| if (isServer) { | ||
| return {} as Element; | ||
| } | ||
| while (node && !isScrollable(node)) { | ||
| node = node.parentElement; | ||
| } | ||
|
|
||
| return node || document.scrollingElement || document.documentElement; | ||
| } | ||
|
|
||
| export function isScrollable(node: Element): boolean { | ||
| if (isServer) { | ||
| return false; | ||
| } | ||
| const style = window.getComputedStyle(node); | ||
| return /(auto|scroll)/.test(style.overflow + style.overflowX + style.overflowY); | ||
| } | ||
|
|
||
| export type Position = { | ||
| x: number; | ||
| y: number; | ||
| }; | ||
|
|
||
| const FALLBACK_SCROLL_POSITION = { x: 0, y: 0 } as const satisfies Position; | ||
|
|
||
| /** | ||
| * Get an `{ x: number, y: number }` object of element/window scroll position. | ||
| */ | ||
| export function getScrollPosition(target: Element | Window | undefined): Position { | ||
| if (isServer || !target) { | ||
| return { ...FALLBACK_SCROLL_POSITION }; | ||
| } | ||
| if (target instanceof Window) | ||
| return { | ||
| x: target.scrollX, | ||
| y: target.scrollY, | ||
| }; | ||
| return { | ||
| x: target.scrollLeft, | ||
| y: target.scrollTop, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Reactive primitive providing a store-like object with current scroll position of specified target. | ||
| * @param target element/window to listen to scroll events. can be a reactive singal. | ||
| * @returns a store-like reactive object `{ x: number, y: number }` of current scroll position of {@link target} | ||
| * @example | ||
| * // target will be window by default | ||
| * const windowScroll = createScrollPosition(); | ||
| * | ||
| * createEffect(() => { | ||
| * // returned object is a reactive store-like structure | ||
| * windowScroll.x; // => number | ||
| * windowScroll.y; // => number | ||
| * }); | ||
| */ | ||
| export function createScrollPosition( | ||
| target?: Accessor<Element | Window | undefined> | Element | Window, | ||
| ): Readonly<Position> { | ||
| if (isServer) { | ||
| return FALLBACK_SCROLL_POSITION; | ||
| } | ||
|
|
||
| target = target || window; | ||
|
|
||
| const isFn = typeof target === "function", | ||
| isHydrating = sharedConfig.context, | ||
| getTargetPos = isFn | ||
| ? () => getScrollPosition((target as Extract<typeof target, Function>)()) | ||
| : () => getScrollPosition(target as Element | Window), | ||
| // changing the calc signal will trigger the derived store to update | ||
| [calc, setCalc] = createSignal(isHydrating ? () => FALLBACK_SCROLL_POSITION : getTargetPos, { | ||
| equals: false, | ||
| }), | ||
| trigger = () => setCalc(() => getTargetPos), | ||
| pos = createDerivedStaticStore(() => calc()()); | ||
|
|
||
| // update the position on mount if we are hydrating (initial pos is null) | ||
| // or if target is a function (which means it could be a ref that will be populated onMount) | ||
| if (isHydrating || isFn) onMount(trigger); | ||
|
|
||
| createEventListener(target, "scroll", trigger, { passive: true }); | ||
|
|
||
| return pos; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a reactive object with current window scroll position. | ||
| * | ||
| * This is a [singleton root](https://github.com/solidjs-community/solid-primitives/tree/main/packages/rootless#createSingletonRoot) primitive. | ||
| * | ||
| * @example | ||
| * const scroll = useWindowScrollPosition(); | ||
| * createEffect(() => { | ||
| * console.log(scroll.x, scroll.y) | ||
| * }) | ||
| */ | ||
| export const useWindowScrollPosition = /*#__PURE__*/ createHydratableSingletonRoot(() => | ||
| createScrollPosition(isServer ? () => undefined : window), | ||
| ); | ||
| export { | ||
| getScrollParent, | ||
| isScrollable, | ||
| getScrollPosition, | ||
| createScrollPosition, | ||
| useWindowScrollPosition, | ||
| } from "./scrollPosition.js"; | ||
| export type { Position } from "./scrollPosition.js"; | ||
|
|
||
| export { createPreventScroll } from "./preventScroll.js"; | ||
| export type { CreatePreventScrollProps } from "./preventScroll.js"; |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should showcase using a signal ref.
createPreventScroll({ element: refAccessor });