Connect, collaborate, and grow with a community of TypeScript developers
challenge layout touch resize events, minor solutions card tweaks (#109)
PickleNik15687ebb9fe6
diff --git a/src/components/challenge/challenge-layout.tsx b/src/components/challenge/challenge-layout.tsxindex fbdb6268..5dfbb7b4 100644--- a/src/components/challenge/challenge-layout.tsx+++ b/src/components/challenge/challenge-layout.tsx@@ -54,7 +54,6 @@ export function ChallengeLayout({ left, right, skeleton = false }: ChallengeLayowindow.innerWidth > 1025? (leftRef.style.width = settings.width): (leftRef.style.height = settings.height);-return;}@@ -66,31 +65,25 @@ export function ChallengeLayout({ left, right, skeleton = false }: ChallengeLayo// The current position of mouselet x = 0;let y = 0;-// Width of left side on dekstop, height of top side on mobile;let leftWidth = 0;let topHeight = 0;- const mouseDownHandler = (e: MouseEvent) => {- // Get the current mouse position- window.innerWidth > 1025 ? (x = e.clientX) : (y = e.clientY);-- window.innerWidth > 1025- ? (leftWidth = leftSide?.current?.getBoundingClientRect().width as number | 0)- : (topHeight = leftSide?.current?.getBoundingClientRect().height as number | 0);-- // Attach the listeners to `document`- document.addEventListener('mousemove', mouseMoveHandler);- document.addEventListener('mouseup', mouseUpHandler);- };-- const mouseMoveHandler = (e: MouseEvent) => {- // How far the mouse has been moved- const dx = e.clientX - x;- const dy = e.clientY - y;-- const divideByW = parent.current?.getBoundingClientRect().width as number | 1;- const divideByH = parent.current?.getBoundingClientRect().height as number | 1;+ const mouseMoveHandler = (e: MouseEvent | TouchEvent) => {+ let dx = 0;+ let dy = 0;+ if (e instanceof MouseEvent) {+ // How far the mouse has been moved+ dx = e.clientX - x;+ dy = e.clientY - y;+ } else if (e instanceof TouchEvent) {+ // How far the finger has been moved+ dx = e?.changedTouches?.[0]?.clientX ? e.changedTouches[0].clientX - x : 0;+ dy = e?.changedTouches?.[0]?.clientY ? e.changedTouches[0].clientY - y : 0;+ }++ const divideByW = parent?.current?.getBoundingClientRect().width as number | 1;+ const divideByH = parent?.current?.getBoundingClientRect().height as number | 1;const newLeftWidth = ((leftWidth + dx) * 100) / divideByW;const newTopHeight = ((topHeight + dy) * 100) / divideByH;@@ -109,6 +102,32 @@ export function ChallengeLayout({ left, right, skeleton = false }: ChallengeLayorightRef.style.userSelect = 'none';leftRef.style.userSelect = 'none';};++ const mouseDownHandler = (e: MouseEvent | TouchEvent) => {+ if (e instanceof MouseEvent) {+ // Get the current mouse position+ window.innerWidth > 1025 ? (x = e.clientX) : (y = e.clientY);+ } else if (e instanceof TouchEvent) {+ // Get the current finger position+ window.innerWidth > 1025+ ? (x = e?.touches?.[0]?.clientX ?? 0)+ : (y = e?.touches?.[0]?.clientY ?? 0);+ }++ window.innerWidth > 1025+ ? (leftWidth = leftSide?.current?.getBoundingClientRect().width as number | 0)+ : (topHeight = leftSide?.current?.getBoundingClientRect().height as number | 0);++ // Attach the listeners to `document`+ if (e instanceof MouseEvent) {+ document.addEventListener('mousemove', mouseMoveHandler);+ document.addEventListener('mouseup', mouseUpHandler);+ } else if (e instanceof TouchEvent) {+ document.addEventListener('touchmove', mouseMoveHandler);+ document.addEventListener('touchend', mouseUpHandler, false);+ }+ };+const mouseUpHandler = function () {// undo cursor col-resize from abovedocument.body.style.removeProperty('cursor');@@ -119,8 +138,10 @@ export function ChallengeLayout({ left, right, skeleton = false }: ChallengeLayoleftRef.style.removeProperty('pointer-events');rightRef.style.removeProperty('pointer-events');- // Remove the handlers of `mousemove` and `mouseup`+ // Remove the handlers of `mousemove` and `mouseup` or `touchmove` and `touchend` from the `document`+ document.removeEventListener('touchmove', mouseMoveHandler);document.removeEventListener('mousemove', mouseMoveHandler);+ document.removeEventListener('touchend', mouseUpHandler);document.removeEventListener('mouseup', mouseUpHandler);window.innerWidth > 1025@@ -136,11 +157,13 @@ export function ChallengeLayout({ left, right, skeleton = false }: ChallengeLayo};window.addEventListener('resize', resizeHandler);-ref?.addEventListener('mousedown', mouseDownHandler);+ ref?.addEventListener('touchstart', mouseDownHandler, false);return () => {+ window.removeEventListener('resize', resizeHandler);ref?.removeEventListener('mousedown', mouseDownHandler);+ ref?.removeEventListener('touchstart', mouseDownHandler);};}, [settings, updateSettings, skeleton]);diff --git a/src/components/challenge/solutions.tsx b/src/components/challenge/solutions.tsxindex 79599d13..dff7b0ff 100644--- a/src/components/challenge/solutions.tsx+++ b/src/components/challenge/solutions.tsx@@ -51,20 +51,20 @@ export function Solutions({ challenge }: Props) {function SolutionRow() {const date = new Date();return (- <div className="flex cursor-pointer flex-col gap-2 p-4 duration-300 hover:bg-neutral-100 dark:rounded-none dark:hover:bg-neutral-700">+ <div className="group flex cursor-pointer flex-col gap-2 p-4 duration-300 hover:bg-neutral-100 dark:rounded-none dark:hover:bg-neutral-700"><h3 className="font-bold">🔥🔥Some | LeetCodeLooking✅ | Title🔥🔥</h3>- <div className="mb-2 flex items-center gap-2 text-neutral-600 dark:text-neutral-400">- <div className="rounded-lg bg-neutral-200 pl-1 pr-2 font-bold dark:bg-neutral-700 dark:text-neutral-300">- @ username goes here+ <div className="flex items-center gap-2 text-sm text-neutral-500">+ <div className="rounded-full bg-neutral-200 px-2 py-1 text-xs font-bold text-neutral-600 dark:bg-neutral-700 dark:text-neutral-400">+ @ username</div><div>{date.toLocaleString()}</div></div>- <div className="flex gap-5 text-neutral-600 duration-300 dark:text-neutral-400">- <button className="flex items-center gap-2 rounded-full bg-emerald-600/10 p-2 pr-3 text-emerald-600 duration-300 hover:bg-emerald-400/20 dark:bg-emerald-400/20 dark:text-emerald-400">- <ArrowUp className="stroke-1" /> 70+ <div className="flex gap-3 text-neutral-600 duration-300 dark:text-neutral-400">+ <button className="flex cursor-pointer items-center gap-2 rounded-full bg-emerald-600/10 px-2 py-1 text-sm text-emerald-600 duration-300 hover:bg-emerald-400/40 dark:bg-emerald-400/20 dark:text-emerald-400 dark:hover:bg-emerald-400/40">+ <ArrowUp className="h-4 w-4 stroke-1" /> 70</button>- <button className="flex items-center gap-2 rounded-full bg-white/5 p-2 pr-3 duration-300 hover:bg-black/20 only:dark:hover:bg-white/20">- <MessageCircle className="stroke-1" /> 69+ <button className="flex cursor-pointer items-center gap-2 rounded-full bg-black/5 px-2 py-1 text-sm duration-300 hover:bg-black/20 dark:bg-white/5 dark:hover:bg-white/20">+ <MessageCircle className="h-4 w-4 stroke-1" /> 69</button></div></div>