frontend(statusbar): use signal-based dropdowns to fix iOS Safari toggle

This commit is contained in:
spinline
2026-02-06 13:57:26 +03:00
parent 3b34b0e61a
commit d1604fb8fb

View File

@@ -1,6 +1,5 @@
use leptos::*;
use shared::GlobalLimitRequest;
use wasm_bindgen::JsCast;
fn format_bytes(bytes: i64) -> String {
const UNITS: [&str; 6] = ["B", "KB", "MB", "GB", "TB", "PB"];
@@ -110,64 +109,41 @@ pub fn StatusBar() -> impl IntoView {
});
};
// Helper to close all details dropdowns
let close_dropdown = move || {
if let Some(doc) = web_sys::window().and_then(|w| w.document()) {
// Close all <details> elements in the status bar
let details_list = doc.query_selector_all("details.dropdown[open]");
if let Ok(nodes) = details_list {
for i in 0..nodes.length() {
if let Some(node) = nodes.item(i) {
if let Ok(el) = node.dyn_into::<web_sys::Element>() {
el.remove_attribute("open").ok();
}
}
}
}
// Also blur active element as fallback
if let Some(active) = doc.active_element() {
let _ = active
.dyn_into::<web_sys::HtmlElement>()
.map(|el| el.blur());
}
// Signal-based dropdown state: 0=none, 1=download, 2=upload, 3=theme
let (active_dropdown, set_active_dropdown) = create_signal(0u8);
// Toggle a specific dropdown
let toggle = move |id: u8| {
if active_dropdown.get_untracked() == id {
set_active_dropdown.set(0);
} else {
set_active_dropdown.set(id);
}
};
// Toggle dropdown: close all, the <details> native toggle handles opening
let toggle_dropdown = move |_id: u8| {
// No-op: <details> handles its own open/close natively
// We just need this for the close_dropdown helper
// Close all dropdowns
let close_all = move || {
set_active_dropdown.set(0);
};
// Global listener to close dropdowns on touchstart outside
let force_blur = move |_| {
if let Some(doc) = web_sys::window().and_then(|w| w.document()) {
let details_list = doc.query_selector_all("details.dropdown[open]");
if let Ok(nodes) = details_list {
for i in 0..nodes.length() {
if let Some(node) = nodes.item(i) {
if let Ok(el) = node.dyn_into::<web_sys::Element>() {
el.remove_attribute("open").ok();
}
}
}
}
}
};
let _ = window_event_listener(ev::touchstart, force_blur);
// Close dropdowns when tapping outside
let _ = window_event_listener(ev::pointerdown, move |_| {
// This fires first; dropdown buttons call stop_propagation to prevent closing
set_active_dropdown.set(0);
});
view! {
<div class="h-8 min-h-8 bg-base-200 border-t border-base-300 flex items-center px-4 text-xs gap-4 text-base-content/70">
// --- DOWNLOAD SPEED DROPDOWN ---
<details
class="dropdown dropdown-top dropdown-start"
on:touchstart=move |e| e.stop_propagation()
>
<summary
class="flex items-center gap-2 cursor-pointer hover:text-primary transition-colors select-none list-none [&::-webkit-details-marker]:hidden"
<div class="relative">
<div
class="flex items-center gap-2 cursor-pointer hover:text-primary transition-colors select-none"
title="Global Download Speed - Click to Set Limit"
on:click=move |_| toggle_dropdown(1)
on:pointerdown=move |e| {
e.stop_propagation();
toggle(1);
}
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 13.5L12 21m0 0l-7.5-7.5M12 21V3" />
@@ -178,23 +154,26 @@ pub fn StatusBar() -> impl IntoView {
{move || format!("(Limit: {})", format_speed(stats.get().down_limit.unwrap_or(0)))}
</span>
</Show>
</summary>
</div>
<ul class="dropdown-content z-[100] menu p-2 shadow bg-base-200 rounded-box w-40 mb-2 border border-base-300">
<ul
class="absolute bottom-full left-0 z-[100] menu p-2 shadow bg-base-200 rounded-box w-40 mb-2 border border-base-300"
style=move || if active_dropdown.get() == 1 { "display: block" } else { "display: none" }
on:pointerdown=move |e| e.stop_propagation()
>
{
limits.clone().into_iter().map(|(val, label)| {
let is_active = move || {
let current = stats.get().down_limit.unwrap_or(0);
(current - val).abs() < 1024
};
let close = close_dropdown.clone();
view! {
<li>
<button
class=move || if is_active() { "bg-primary/10 text-primary font-bold text-xs flex justify-between" } else { "text-xs flex justify-between" }
on:click=move |_| {
set_limit("down", val);
close();
close_all();
}
>
{label}
@@ -207,17 +186,17 @@ pub fn StatusBar() -> impl IntoView {
}).collect::<Vec<_>>()
}
</ul>
</details>
</div>
// --- UPLOAD SPEED DROPDOWN ---
<details
class="dropdown dropdown-top dropdown-start"
on:touchstart=move |e| e.stop_propagation()
>
<summary
class="flex items-center gap-2 cursor-pointer hover:text-primary transition-colors select-none list-none [&::-webkit-details-marker]:hidden"
<div class="relative">
<div
class="flex items-center gap-2 cursor-pointer hover:text-primary transition-colors select-none"
title="Global Upload Speed - Click to Set Limit"
on:click=move |_| toggle_dropdown(2)
on:pointerdown=move |e| {
e.stop_propagation();
toggle(2);
}
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M4.5 10.5L12 3m0 0l7.5 7.5M12 3v18" />
@@ -228,23 +207,26 @@ pub fn StatusBar() -> impl IntoView {
{move || format!("(Limit: {})", format_speed(stats.get().up_limit.unwrap_or(0)))}
</span>
</Show>
</summary>
</div>
<ul class="dropdown-content z-[100] menu p-2 shadow bg-base-200 rounded-box w-40 mb-2 border border-base-300">
<ul
class="absolute bottom-full left-0 z-[100] menu p-2 shadow bg-base-200 rounded-box w-40 mb-2 border border-base-300"
style=move || if active_dropdown.get() == 2 { "display: block" } else { "display: none" }
on:pointerdown=move |e| e.stop_propagation()
>
{
limits.clone().into_iter().map(|(val, label)| {
let is_active = move || {
let current = stats.get().up_limit.unwrap_or(0);
(current - val).abs() < 1024
};
let close = close_dropdown.clone();
view! {
<li>
<button
class=move || if is_active() { "bg-primary/10 text-primary font-bold text-xs flex justify-between" } else { "text-xs flex justify-between" }
on:click=move |_| {
set_limit("up", val);
close();
close_all();
}
>
{label}
@@ -257,31 +239,33 @@ pub fn StatusBar() -> impl IntoView {
}).collect::<Vec<_>>()
}
</ul>
</details>
</div>
<div class="ml-auto flex items-center gap-4">
<details
class="dropdown dropdown-top dropdown-end"
on:touchstart=move |e| e.stop_propagation()
>
<summary
class="btn btn-ghost btn-xs btn-square list-none [&::-webkit-details-marker]:hidden"
<div class="relative">
<div
class="btn btn-ghost btn-xs btn-square"
title="Change Theme"
on:click=move |_| toggle_dropdown(3)
on:pointerdown=move |e| {
e.stop_propagation();
toggle(3);
}
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M9.53 16.122a3 3 0 0 0-5.78 1.128 2.25 2.25 0 0 1-2.4 2.245 4.5 4.5 0 0 0 8.4-2.245c0-.399-.078-.78-.22-1.128Zm0 0a15.998 15.998 0 0 0 3.388-1.62m-5.043-.025a15.994 15.994 0 0 1 1.622-3.395m3.42 3.42a15.995 15.995 0 0 0 4.764-4.648l3.876-5.814a1.151 1.151 0 0 0-1.597-1.597L14.146 6.32a15.996 15.996 0 0 0-4.649 4.763m3.42 3.42a6.776 6.776 0 0 0-3.42-3.42" />
</svg>
</summary>
</div>
<ul class="dropdown-content z-[100] menu p-2 shadow bg-base-200 rounded-box w-52 mb-2 border border-base-300 max-h-96 overflow-y-auto">
<ul
class="absolute bottom-full right-0 z-[100] menu p-2 shadow bg-base-200 rounded-box w-52 mb-2 border border-base-300 max-h-96 overflow-y-auto"
style=move || if active_dropdown.get() == 3 { "display: block" } else { "display: none" }
on:pointerdown=move |e| e.stop_propagation()
>
{
let themes = vec![
"light", "dark", "dim", "nord", "cupcake", "dracula", "cyberpunk", "emerald", "sunset", "abyss"
];
let close = close_dropdown.clone();
themes.into_iter().map(|theme| {
let close = close.clone();
view! {
<li>
<button
@@ -296,7 +280,7 @@ pub fn StatusBar() -> impl IntoView {
let _ = storage.set_item("vibetorrent_theme", theme);
}
}
close();
close_all();
}
>
{theme}
@@ -306,7 +290,7 @@ pub fn StatusBar() -> impl IntoView {
}).collect::<Vec<_>>()
}
</ul>
</details>
</div>
<button
class="btn btn-ghost btn-xs btn-square"