Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type AtomWithHashOptions #16

Open
AjaxSolutions opened this issue Mar 5, 2023 · 1 comment
Open

type AtomWithHashOptions #16

AjaxSolutions opened this issue Mar 5, 2023 · 1 comment

Comments

@AjaxSolutions
Copy link

I'm learning AtomWithHashOptions. Does this type make sense? If so, please add it at the next opportunity.

export type AtomWithHashOptions<Value> = {
    /**
     * A function to serialize the value before storing it in the URL hash.
     * The serialized string will be decoded by the `deserialize` function.
     * Default: `JSON.stringify`
     */
    serialize?: (val: Value) => string;
    /**
     * A function to deserialize the value retrieved from the URL hash.
     * The deserialized value will be used as the initial value of the atom.
     * If the deserialized value is `NO_STORAGE_VALUE`, the atom will be initialized with the `initialValue`.
     * Default: `(str) => NO_STORAGE_VALUE`
     */
    deserialize?: (str: string | null) => Value | typeof NO_STORAGE_VALUE;
    /**
     * A boolean flag that indicates whether to use `window.history.replaceState` instead of `window.location.hash`.
     * When this flag is set to `true`, the URL hash will be replaced instead of being added to the browser history.
     * Note: This flag is deprecated in favor of using `setHash` with `"replaceState"`.
     * Default: `false`
     * @deprecated Use `setHash` with 'replaceState' instead
     */
    replaceState?: boolean;
    /**
     * A function that will be called when the URL hash changes.
     * The callback should update the atom value with the new hash value.
     * Default: `(callback) => { window.addEventListener("hashchange", callback); return () => window.removeEventListener("hashchange", callback); }`
     * @returns A function that can be called to unsubscribe from the changes.
     */
    subscribe?: (callback: () => void) => () => void;
    /**
     * A function to set the URL hash.
     * By default, it sets the hash using `window.location.hash`.
     * When `replaceState` is set to true, it uses `window.history.replaceState` instead.
     * If a function is provided, it will be used instead.
     * The searchParams argument is the stringified version of the atom value.
     * Default: `"default"`
     * Options:
     * - `"default"`: Sets the hash using `window.location.hash`.
     * - `"replaceState"`: Sets the hash using `window.history.replaceState`.
     * - `(searchParams: string) => void`: A custom function that will be used to set the hash.
     */
    setHash?: "default" | "replaceState" | ((searchParams: string) => void);
};
@dai-shi
Copy link
Member

dai-shi commented Mar 6, 2023

Do you want the type exported, or is your concern about jsdoc?
Feel free to open a PR after #13 is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants