pmndrs / leva

🌋 React-first components GUI
https://leva.pmnd.rs
MIT License
4.88k stars 197 forks source link

Feature request: Export store class so it can be used with Jotai #261

Open astahmer opened 2 years ago

astahmer commented 2 years ago

Hey, I was trying to auto-sync an atom (from jotai) with the results of useControls (so that I can access it anywhere) So I thought of using the jotai/zustand util atomWithStore and by passing a custom leva store to useControls The thing is, the only way to create a custom leva store right now is from the useCreateStore hook

Could you export the Store class or a new createStore function ? We would also need the getValuesForPaths utils exported to get the same output as useControls

Then we would be able to do something like

const stateAtom = atomWithStore(new Store().useStore);
const useControlsResult = () => {
    const state = useAtomValue(stateAtom).data;
    return getValuesForPaths(state, Object.keys(state));
};

This would maybe also make persistence (#133) possible using atomWithStorage ?

Ctrlmonster commented 3 months ago

+1 This would also enable you to create multiple stores upfront and use the correct one depending on the App state, i.e.

const stores = useMemo(() => myList.map(() => new Store()), [myList]) ;
return <LevaPanel store={stores[index]} />

Right now this requires another wrapper component, since you can't call useCreateStore in a loop.