swyxio / svelte-actions

prototype official actions for Svelte
MIT License
227 stars 9 forks source link

Discuss high level policy #7

Open swyxio opened 4 years ago

swyxio commented 4 years ago
kindoflew commented 3 years ago

Can the library be open to generic utility actions or should it be reserved for more specific use cases?

Example: I needed a conditional click listener action the other day and realized that it could just be a conditional anything listener. Something like this (based on clickOutside):


function conditionalEvent(node, {condition, event, cb}) {
    function update(condition) {    
        if (condition) {
            node.addEventListener(event, cb); 
        } else {
            node.removeEventListener(event, cb);
        }
    }

    update(condition);

    return {
        update,
        destroy() {
            node.removeEventListener(event, cb);
        }
    }
}

This is kind of a trivial example, but that's the general idea. Let people wire up their own logic, but the boilerplate is taken care of. The other Svelte built-ins don't have something like this (transition and animate), but actions can apply to so many things and those two are more specific in their applications.

swyxio commented 3 years ago

@kindoflew could you show a usage example? having trouble understanding how to use. in general am concerned about getting too meta too soon, but willing to consider if genuinely useful in many apps

kindoflew commented 3 years ago

This was just a generic example based off a use-case I had. I was submitting a PR (https://github.com/beyonk-adventures/svelte-carousel/pull/56) to svelte-carousel. Basically, it's a carousel with an autoplay option and I needed to add a click-handler, but only if an autoplay duration was specified. My example above was a more generic version of that idea and might be useful for those writing libraries with components that have different needs depending on their configuration?

Anyway, my question was less about the example and more high-level asking about intent of the library I guess. In thinking of ideas for actions to submit/suggest I didn't know if they should be tailored to specific use cases or more like helper actions where the user would fill in the details. Not wanting to get too meta too soon kind of answers that though, I think.

swyxio commented 3 years ago

thanks, yea the idea is that this isn't a library so much as a prototype for what would fit in svelte by default. prone to some bikshedding ofc but also have to consider things like documentation and time to usefulness. could also serve as inspiration (what can we do with actions? well, look at the default ones!)