diff --git a/.changeset/floppy-vans-melt.md b/.changeset/floppy-vans-melt.md new file mode 100644 index 0000000000000..807d1872bc3be --- /dev/null +++ b/.changeset/floppy-vans-melt.md @@ -0,0 +1,6 @@ +--- +"@gradio/sidebar": patch +"gradio": patch +--- + +fix:Change sidebar to absolute positioning and make accessible diff --git a/js/sidebar/shared/Sidebar.svelte b/js/sidebar/shared/Sidebar.svelte index a8c0f78aa0b92..90d7dc03bfc4e 100644 --- a/js/sidebar/shared/Sidebar.svelte +++ b/js/sidebar/shared/Sidebar.svelte @@ -17,21 +17,28 @@ let width_css = typeof width === "number" ? `${width}px` : width; - // Check if the sidebar overlaps with the main content + let prefersReducedMotion: boolean; + function check_overlap(): void { - if (!sidebar_div.closest(".wrap")) return; - const parent_rect = sidebar_div.closest(".wrap")?.getBoundingClientRect(); + if (!sidebar_div.closest(".gradio-container")) return; + const parent_rect = sidebar_div + .closest(".gradio-container") + ?.getBoundingClientRect(); if (!parent_rect) return; + + const parentHeight = parent_rect.height; + sidebar_div.style.height = `${parentHeight}px`; + const sidebar_rect = sidebar_div.getBoundingClientRect(); const available_space = position === "left" ? parent_rect.left : window.innerWidth - parent_rect.right; - overlap_amount = Math.max(0, sidebar_rect.width - available_space + 30); + overlap_amount = Math.max(0, sidebar_rect.width - available_space); } onMount(() => { - sidebar_div.closest(".wrap")?.classList.add("sidebar-parent"); + sidebar_div.closest(".gradio-container")?.classList.add("sidebar-parent"); check_overlap(); window.addEventListener("resize", check_overlap); const update_parent_overlap = (): void => { @@ -42,8 +49,19 @@ }; update_parent_overlap(); mounted = true; + + const mediaQuery = window.matchMedia("(prefers-reduced-motion: reduce)"); + prefersReducedMotion = mediaQuery.matches; + + const updateMotionPreference = (e: MediaQueryListEvent): void => { + prefersReducedMotion = e.matches; + }; + + mediaQuery.addEventListener("change", updateMotionPreference); + return () => { window.removeEventListener("resize", check_overlap); + mediaQuery.removeEventListener("change", updateMotionPreference); }; }); @@ -56,8 +74,9 @@ class="sidebar" class:open={_open} class:right={position === "right"} + class:reduce-motion={prefersReducedMotion} bind:this={sidebar_div} - style="width: {width_css}; {position}: calc({width_css} * -1)" + style="width: {width_css}; {position}: calc({width_css} * -1.10)" >