Synphonyte / leptos-use

Collection of essential Leptos utilities inspired by React-Use / VueUse / SolidJS-USE
https://leptos-use.rs/
Apache License 2.0
309 stars 67 forks source link

Discover initial color mode from URL parameter #78

Closed mondeja closed 7 months ago

mondeja commented 7 months ago

Hi. I was wondering if there is some way to standarize how to obtain the initial color mode when using use_color_mode. Currently, I'm getting it from:

  1. The URL parameter color-scheme. If exists, is updated on localstorage and used as the initial color mode.
  2. Otherwise try to get from the localstorage key color-scheme.

Seems to me that it would not be crazy to implement this logic in use_color_mode directly. Perhaps adding the next parameters is enough:

It the color mode is not found trying to discover it following this order would fallback to initial_value and use_preferred_dark. What do you think about it?

Perhaps a more general API to set autodiscovered values in all functions that accept an initial_value would be better.

maccesch commented 7 months ago

Interesting!

I mean there is an option initial_value which you can use for this. (you probably already do?)

But yeah this could be useful to a number of people.

I think the fallback shouldn't change and we already have an option to enable/disable storage. So I don't think we need the option initial_value_from_storage.

I'm not sure what the ...from_url would do. Would that just activate the discovery? And then use the ...param option to see which it is?

I think we can simplify this to one option parameter

#[builder(into)]
inital_value_from_url_param: Option<String>

and if that is Some(...) we try discovering from that url param. If that is not present we use the exisiting discovering mechanisms.

This would be very ergonomic because with this you would only have to call .initial_value_from_url_param("color-scheme") to activate this.

mondeja commented 7 months ago

I mean there is an option initial_value which you can use for this. (you probably already do?)

Yes, I'm using it but want to get rid of.

I think we can simplify this to one option parameter ... and if that is Some(...) we try discovering from that url param. If that is not present we use the exisiting discovering mechanisms.

Looks great 👍🏼