johnlindquist / kit

Script Kit. Automate Anything.
https://scriptkit.com
MIT License
3.91k stars 138 forks source link

`defaultValue` doesn't scroll to choice if not visible #1248

Open BeSpunky opened 1 year ago

BeSpunky commented 1 year ago

Specifiying a defaultValue for arg doesn't scroll the list to reveal the corresponding item.

Current behavior The item indicated by defaultValue is selected ('15' in this example), but not scrolled into view. Notice that when I press ⬆️ I do get to the adjacent item ('14'), but the list started at the top.

https://user-images.githubusercontent.com/47897671/236698045-fd7a4152-de83-41d3-9ca0-0a1ac5f71be4.mp4

Expected behavior The item is selected and scrolled into view.

Sidenote: scrolling into view should be deterministic and position the item at an expected position on the screen. Consider the following example where the kit doesn't disappear from the screen... Each call to arg should keep the list at its previous scroll position.

let defaultValue = '1';

while (condition)
{
    const value = await arg({
        ...
        defaultValue
    }, ['1', '2', '3', ........... '20']);

    defaultValue = value;

    // Stuff....
}
johnlindquist commented 1 year ago

@BeSpunky I can do a "scrollToItem" based on these 'start', 'center', 'end', 'smart' options: https://react-window.vercel.app/#/examples/list/scroll-to-item

Considering the fact that most lists will shrink/change/etc based on user input/filtering, remembering the scroll position when re-rendering the complete list doesn't make logical sense to me. I would have to check too many variables: prompt size, list length, if choices match exactly, user input, etc.

I think 'center' makes the most sense based on my brief explorations/implementation.

johnlindquist commented 1 year ago

I'll add it to my backlog to send the current scroll position in an onScroll handler, then allow the user to set a starting scrollPosition. That way if the script author knows the choices/etc will remain the same, the can manually control the scroll as well

BeSpunky commented 1 year ago

It would be nice to have the flexibility of both. You either set a prop with center | start | end and get out of the box scrolling, or use a custom function.

Makes sense? 🤔

Otherwise, I already see myself writing and reusing a util restoreScrollPosition funcion...