added some docs to use_cookie and the actix feature

This commit is contained in:
Maccesch 2024-01-23 15:39:52 +00:00
parent 5c0503d022
commit 383e8ad689
4 changed files with 25 additions and 15 deletions

1
.idea/leptos-use.iml generated
View file

@ -64,6 +64,7 @@
<sourceFolder url="file://$MODULE_DIR$/examples/use_device_pixel_ratio/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/examples/use_device_pixel_ratio/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/examples/use_element_bounding/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/examples/use_element_bounding/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/examples/use_mouse_in_element/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/examples/use_mouse_in_element/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/examples/use_cookie/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/examples/use_event_listener/target" /> <excludeFolder url="file://$MODULE_DIR$/examples/use_event_listener/target" />
<excludeFolder url="file://$MODULE_DIR$/target" /> <excludeFolder url="file://$MODULE_DIR$/target" />
<excludeFolder url="file://$MODULE_DIR$/docs/book/book" /> <excludeFolder url="file://$MODULE_DIR$/docs/book/book" />

View file

@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### New Functions 🚀 ### New Functions 🚀
- `use_cookie` - `use_cookie` (thanks to @rakshith-ravi)
- `use_mouse_in_element` - `use_mouse_in_element`
- `use_device_pixel_ratio` (thanks to @mondeja) - `use_device_pixel_ratio` (thanks to @mondeja)
- `use_element_bounding` - `use_element_bounding`

View file

@ -104,6 +104,7 @@ features = [
] ]
[features] [features]
actix = ["dep:actix-web"]
axum = ["dep:leptos_axum"] axum = ["dep:leptos_axum"]
docs = [] docs = []
math = ["num"] math = ["num"]

View file

@ -16,21 +16,29 @@ use cookie::Cookie;
/// # /// #
/// # #[component] /// # #[component]
/// # fn Demo() -> impl IntoView { /// # fn Demo() -> impl IntoView {
/// if let Some(cookie) = use_cookie("auth") { /// if let Some(cookie) = use_cookie("auth") {
/// view! { /// view! {
/// <div> /// <div>
/// format!("'auth' cookie set to `{}`", cookie.value()) /// format!("'auth' cookie set to `{}`", cookie.value())
/// </div> /// </div>
/// }.into_view() /// }.into_view()
/// } else { /// } else {
/// view! { /// view! {
/// <div> /// <div>
/// "No 'auth' cookie set" /// "No 'auth' cookie set"
/// </div> /// </div>
/// }.into_view() /// }.into_view()
/// } /// }
/// # } /// # }
/// ``` /// ```
///
/// Server-Side Rendering
///
/// This works equally well on the server or the client.
/// On the server this function gets the cookie from the HTTP request header.
///
/// If you're using `axum` you have to enable the `"axum"` feature in your Cargo.toml.
/// In case it's `actix-web` enable the feature `"actix"`.
pub fn use_cookie(cookie_name: &str) -> Option<Cookie<'static>> { pub fn use_cookie(cookie_name: &str) -> Option<Cookie<'static>> {
let cookies; let cookies;
#[cfg(feature = "ssr")] #[cfg(feature = "ssr")]
@ -39,7 +47,7 @@ pub fn use_cookie(cookie_name: &str) -> Option<Cookie<'static>> {
use leptos::expect_context; use leptos::expect_context;
let headers; let headers;
#[cfg(not(feature = "axum"))] #[cfg(feature = "actix")]
{ {
headers = expect_context::<actix_web::HttpRequest>().headers().clone(); headers = expect_context::<actix_web::HttpRequest>().headers().clone();
} }