Skip to content

Commit

Permalink
fix copy button interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
souporserious committed May 12, 2024
1 parent 6063885 commit 759bb79
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-news-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mdxts": patch
---

Fixes interaction when copy button covers code block text by hiding the copy button on the first pointer down until entering the code block area again.
24 changes: 22 additions & 2 deletions packages/mdxts/src/components/CodeBlock/Pre.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@ import React, { createContext, useState } from 'react'

export const PreHoverContext = createContext<boolean | null>(null)

function isTargetValid(event: React.PointerEvent<HTMLPreElement>) {
if (
event.target instanceof HTMLElement ||
event.target instanceof SVGElement
) {
const hasButton = event.target.closest('button')
if (hasButton) {
return false
}

const hasAnchor = event.target.closest('a')
if (hasAnchor) {
return false
}
}

return true
}

export function Pre({
children,
className,
Expand All @@ -13,8 +32,9 @@ export function Pre({

return (
<pre
onMouseEnter={() => setHover(true)}
onMouseLeave={() => setHover(false)}
onPointerEnter={() => setHover(true)}
onPointerLeave={() => setHover(false)}
onPointerDown={(event) => isTargetValid(event) && setHover(false)}
className={className}
style={{
whiteSpace: 'pre',
Expand Down

0 comments on commit 759bb79

Please sign in to comment.