Create a create...Effect for many tertiary primitives
#197
Replies: 2 comments 2 replies
-
|
Hey, thanks for opening this, but I don't think I like the idea.
import { useWindowSize } from "@solid-primitives/resize-observer"
import { createElementBounds } from "@solid-primitives/bounds"
import { createThrottledMemo } from "@solid-primitives/memo"
const windowSize = useWindowSize();
const bounds = createElementBounds(target);
const position = createThrottledMemo(() => {
// calculate position with both window size and element bounds
}, 200)If you are looking for a simple "callback that fires when the size changes" then, there are options too. // on window resize:
import { makeEventListener } from "@solid-primitives/event-listener"
makeEventListener(window, "resize", e => {
console.log({ width: window.innerWidth, height: window.innerHeight })
})
// on element resize
import { createResizeObserver } from "@solid-primitives/resize-observer";
createResizeObserver(target, ({ width, height }) => {
console.log(width, height);
});
// on scroll
import { makeEventListener } from "@solid-primitives/event-listener"
makeEventListener(window, "scroll", e => {
console.log({ x: window.scrollX, y: window.scrollY })
})So API-wise I'm happy with the current primitives. import { makeScrollListener } from "@solid-primitives/scroll"
makeScrollListener(window, ({ x, y }) => {
console.log({ x, y })
}) |
Beta Was this translation helpful? Give feedback.
-
|
Thank you for your fast feedback! Is the best approach to use |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I use SolidStart and for me it happens more often that I have to react to a primitiv with an effect. Therefore it would make the code clearer if I could react directly with a special effect.
This is what my code currently looks like:
And this is how it could look with a special effect:
This can be applied to many tertiary primitives such as
createScrollPositionwithcreateScrollPositionEffectandcreateElementSizewithcreateElementSizeEffect.In this way, we can reduce the code and complexity of components and improve readability. Since I've only been using Solid for a few days, I'm eager to hear your feedback. Maybe I have missed something. If not and you like the idea, I'm happy to contribute with pull requests to the implementation.
Beta Was this translation helpful? Give feedback.
All reactions