From 88012308d03e611eb4da914f8c1b88ec48c6c851 Mon Sep 17 00:00:00 2001 From: GauthierWebDev Date: Mon, 21 Apr 2025 18:10:44 +0200 Subject: [PATCH] style: Update class attribute in Highlight component --- app/components/Highlight.tsx | 5 +- app/components/SmoothScroll.tsx | 30 +++++++++++ app/components/Snippet.tsx | 93 +++++++++++++++++++++------------ 3 files changed, 91 insertions(+), 37 deletions(-) diff --git a/app/components/Highlight.tsx b/app/components/Highlight.tsx index 342f69f..dc6d2a4 100644 --- a/app/components/Highlight.tsx +++ b/app/components/Highlight.tsx @@ -102,10 +102,7 @@ export const Highlight: ParentComponent = (_props) => { )}
 				 {
+		if (!element) return false;
+		return element.scrollHeight > element.clientHeight;
+	};
+
+	const findScrollableParent = (element: HTMLElement) => {
+		let currentElement: HTMLElement | null = element;
+
+		while (currentElement) {
+			if (isElementScrollable(currentElement)) {
+				return currentElement;
+			}
+
+			currentElement = currentElement.parentElement;
+		}
+
+		return null;
+	};
+
 	const handleWheel = (event: WheelEvent) => {
 		if (isMobile()) return;
 
+		const hoveredElement = document.elementFromPoint(
+			event.clientX,
+			event.clientY,
+		) as HTMLElement;
+		if (findScrollableParent(hoveredElement)) {
+			if (animationFrameId !== null) cancelAnimationFrame(animationFrameId);
+			return;
+		}
+
 		event.preventDefault();
+		event.stopPropagation();
+
 		if (isScrolling()) {
 			if (animationFrameId !== null) {
 				cancelAnimationFrame(animationFrameId);
diff --git a/app/components/Snippet.tsx b/app/components/Snippet.tsx
index bb8efbb..77bd30d 100644
--- a/app/components/Snippet.tsx
+++ b/app/components/Snippet.tsx
@@ -38,6 +38,9 @@ type SnippetProps = {
 };
 
 export function Snippet(props: SnippetProps) {
+	let tabs: HTMLDivElement | undefined;
+	let nav: HTMLDivElement | undefined;
+
 	const [selectedTab, setSelectedTab] = createSignal(
 		props.snippets[0],
 	);
@@ -49,6 +52,27 @@ export function Snippet(props: SnippetProps) {
 	const selectTab = (name: string) => {
 		const tab = props.snippets.find((tab) => tab.name === name);
 		if (tab) setSelectedTab(tab);
+
+		if (!tabs || !nav) return;
+
+		const navWidth = nav.offsetWidth || 0;
+		const tabsWidth = tabs.scrollWidth;
+
+		if (tabsWidth > navWidth) {
+			const tabElement: HTMLDivElement | null = tabs.querySelector(
+				`div[data-tab="${name}"]`,
+			);
+			if (!tabElement) return;
+
+			const tabOffsetLeft = tabElement.offsetLeft;
+			const tabWidth = tabElement.offsetWidth;
+			const scrollLeft = Math.max(
+				0,
+				tabOffsetLeft - navWidth / 2 + tabWidth / 2,
+			);
+
+			nav.scrollTo({ left: scrollLeft, behavior: "smooth" });
+		}
 	};
 
 	const canBeSelected = (tab: SnippetTab | CommonTab) => {
@@ -68,48 +92,51 @@ export function Snippet(props: SnippetProps) {
 			
-
- - {(tab) => ( -
- -
- )} -
-
+ +
+ )} + + + {selectedTab() && (
{selectedTab().code && (