Skip to content
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

Add createScrollTo function for scrolling #672

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

lidarbtc
Copy link

@lidarbtc lidarbtc commented Aug 6, 2024

With options and example

Copy link

changeset-bot bot commented Aug 6, 2024

🦋 Changeset detected

Latest commit: 5071128

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@solid-primitives/scroll Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

packages/scroll/README.md Show resolved Hide resolved
Copy link
Member

@atk atk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you're at it, please add a changeset using npx changeset.

packages/scroll/README.md Show resolved Hide resolved
@lidarbtc
Copy link
Author

While you're at it, please add a changeset using npx changeset.

Thanks for noticing. I added it and changed "scroll" to "scrolling".

Copy link
Member

@thetarnav thetarnav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that the primitive was listed in the ideas to implement. But I wonder whats the actual usecase for this primitive.

  • that it works with both signals and element values? I doubt thats usually a problem, and can be solved with utils.access or just checking the type.
  • that it is a noop on the server? since this is an action-something what you call after user interaction or on mount, it shouldn't ever run on a server, and if it does it's a bug and it probably should error then.
  • it doesn't really leverage the reactivity in any way

Why yould you use this instead of just calling el.scrollTo, it's not much more code and it will be clearer. in this current form I don't really see it.

Maybe this would make more sense?

function createScrollTo(el: Accessor<Element | Window | null> | Element | Window, options: {
	top:     MaybeAccessor<number>,
	left:    MaybeAccessor<number>,
	behavor: string,
}): void

let el
createScrollTo(() => el, {top: signal}) // scroll to signal whenever it changes
<div ref={el} />


createScrollTo(signal, {top: 50, behavior: "smooth"}) // scroll to 50 whenever signal changes

It would actually laverage solid effects to call scrollTo when the component is mounted.


const { left, top, behavior = "auto" } = options;

if (currentTarget instanceof Window) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats this check for? both branches do the same thing

Comment on lines +143 to +156
if (typeof options === "number") {
options = {
left: options,
top: y,
};
}

const { left, top, behavior = "auto" } = options;

if (currentTarget instanceof Window) {
currentTarget.scrollTo({ left, top, behavior });
} else {
currentTarget.scrollTo({ left, top, behavior });
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume it could be simplified to

currentTarget.scrollTo(typeof options === "number" ? {left: options, top: y} : options)

* scrollTo(100, 200);
*/
export function createScrollTo(
target: Accessor<Element | Window | undefined> | Element | Window = window,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default value of window could throw on the server. () => window should work.

export function createScrollTo(
target: Accessor<Element | Window | undefined> | Element | Window = window,
) {
return (options: ScrollToOptions | number, y?: number) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is just returning a callback, it could be simplified to this to avoid currying

function scrollTo(target: Accessor<Element | Window | undefined> | Element | Window, options: ScrollToOptions | number, y?: number): void

this way it's clear that the utility is not reactive
functions with create__(source: Accessor<T>) signature usually scream that they are reactive

@thetarnav
Copy link
Member

Now that I think about it

  • createScrollTo - A primitive to support scroll to a target

it says "scroll to a target" not "use el.scrollTo", since those are different ideas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants