Stored inputs
Q: Is there an easy solution for persisting input values?
Your color codes:
- sessionStorage:
- localStorage:
This block defines a default color, but a value stored in sessionStorage or localStorage takes precedence.
const defaultColor = d3.rgb(dark ? "orange" : "steelblue").formatHex();
const color = view(Inputs.color({value: sessionStorage.getItem("color") ?? defaultColor}));
const secondColor = d3.rgb(dark ? "lime" : "red").formatHex();
const color2 = view(Inputs.color({value: localStorage.getItem("color2") ?? secondColor}));
Each time color changes, we store it in the session storage.
sessionStorage.setItem("color", color);
Each time color2 changes, we store it in local storage.
localStorage.setItem("color2", color2);
Note that I tend to prefer the lighter sessionStorage; localStorage implies that the browser can get “stuck” on a bad combination of parameters. With sessionStorage, once you close the window, the storage is gone. Also, sessionStorage works in incognito windows.