Skip to content

Commit

Permalink
Remove react-svg-tooltip library (#4155)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Dec 31, 2023
1 parent e283f59 commit 951e987
Show file tree
Hide file tree
Showing 15 changed files with 645 additions and 667 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`open up a widget 1`] = `
<div
class="MuiPaper-root MuiPaper-elevation MuiPaper-rounded MuiPaper-elevation1 MuiAccordion-root MuiAccordion-rounded Mui-expanded MuiAccordion-gutters css-1elwnq4-MuiPaper-root-MuiAccordion-root"
class="MuiPaper-root MuiPaper-elevation MuiPaper-rounded MuiPaper-elevation1 MuiAccordion-root MuiAccordion-rounded Mui-expanded MuiAccordion-gutters css-1c35hjw-MuiPaper-root-MuiAccordion-root"
>
<div
aria-expanded="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports[`open up a widget 1`] = `
data-testid="alignment-side-drawer"
>
<div
class="MuiPaper-root MuiPaper-elevation MuiPaper-rounded MuiPaper-elevation1 MuiAccordion-root MuiAccordion-rounded Mui-expanded MuiAccordion-gutters css-1elwnq4-MuiPaper-root-MuiAccordion-root"
class="MuiPaper-root MuiPaper-elevation MuiPaper-rounded MuiPaper-elevation1 MuiAccordion-root MuiAccordion-rounded Mui-expanded MuiAccordion-gutters css-1c35hjw-MuiPaper-root-MuiAccordion-root"
>
<div
aria-expanded="true"
Expand Down
4 changes: 1 addition & 3 deletions plugins/arc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@
"build:commonjs": "tsc --build tsconfig.build.commonjs.json",
"clean": "rimraf dist esm *.tsbuildinfo"
},
"dependencies": {
"react-svg-tooltip": "^0.0.11"
},
"dependencies": {},
"peerDependencies": {
"@jbrowse/core": "^2.0.0",
"@jbrowse/plugin-linear-genome-view": "^2.0.0",
Expand Down
60 changes: 13 additions & 47 deletions plugins/arc/src/ArcRenderer/ArcRendering.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React from 'react'
import React, { useState } from 'react'
import {
AnyConfigurationModel,
readConfObject,
} from '@jbrowse/core/configuration'
import { Feature, Region, bpSpanPx, measureText } from '@jbrowse/core/util'
import { Feature, Region, bpSpanPx } from '@jbrowse/core/util'
import { observer } from 'mobx-react'
import { Tooltip } from 'react-svg-tooltip'

// locals
import ArcTooltip from '../ArcTooltip'

function Arc({
selectedFeatureId,
Expand All @@ -22,6 +24,7 @@ function Arc({
bpPerPx: number
feature: Feature
}) {
const [isMouseOvered, setIsMouseOvered] = useState(false)
const [left, right] = bpSpanPx(
feature.get('start'),
feature.get('end'),
Expand All @@ -40,7 +43,6 @@ function Arc({
const strokeWidth = readConfObject(config, 'thickness', { feature }) || 1
const height = readConfObject(config, 'height', { feature }) || 100
const ref = React.createRef<SVGPathElement>()
const tooltipWidth = 20 + measureText(caption?.toString())

const t = 0.5
// formula: https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Cubic_B%C3%A9zier_curves
Expand All @@ -58,30 +60,12 @@ function Arc({
strokeWidth={strokeWidth}
fill="transparent"
onClick={e => onFeatureClick?.(e, featureId)}
onMouseOver={() => setIsMouseOvered(true)}
onMouseLeave={() => setIsMouseOvered(false)}
ref={ref}
pointerEvents="stroke"
/>
<Tooltip triggerRef={ref}>
<rect
x={12}
y={0}
width={tooltipWidth}
height={20}
rx={5}
ry={5}
fill="black"
fillOpacity="50%"
/>
<text
x={22}
y={14}
fontSize={10}
fill="white"
textLength={tooltipWidth - 20}
>
{caption}
</text>
</Tooltip>
{isMouseOvered ? <ArcTooltip contents={caption} /> : null}
<text
x={left + (right - left) / 2}
y={textYCoord + 3}
Expand Down Expand Up @@ -156,6 +140,7 @@ function SemiCircles({
bpPerPx: number
feature: Feature
}) {
const [isMouseOvered, setIsMouseOvered] = useState(false)
const [left, right] = bpSpanPx(
feature.get('start'),
feature.get('end'),
Expand All @@ -173,7 +158,6 @@ function SemiCircles({
const caption = readConfObject(config, 'caption', { feature })
const strokeWidth = readConfObject(config, 'thickness', { feature }) || 1
const ref = React.createRef<SVGPathElement>()
const tooltipWidth = 20 + measureText(caption?.toString())
const textYCoord = (right - left) / 2

return (
Expand All @@ -190,30 +174,12 @@ function SemiCircles({
strokeWidth={strokeWidth}
fill="transparent"
onClick={e => onFeatureClick?.(e, featureId)}
onMouseOver={() => setIsMouseOvered(true)}
onMouseLeave={() => setIsMouseOvered(false)}
ref={ref}
pointerEvents="stroke"
/>
<Tooltip triggerRef={ref}>
<rect
x={12}
y={0}
width={tooltipWidth}
height={20}
rx={5}
ry={5}
fill="black"
fillOpacity="50%"
/>
<text
x={22}
y={14}
fontSize={10}
fill="white"
textLength={tooltipWidth - 20}
>
{caption}
</text>
</Tooltip>
{isMouseOvered ? <ArcTooltip contents={caption} /> : null}
<text
x={left + (right - left) / 2}
y={textYCoord + 3}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ exports[`one feature 1`] = `
pointer-events="stroke"
stroke-width="1"
/>
<g />
<text
style="stroke: white; stroke-width: 0.6em;"
x="0.6499999999999999"
Expand Down
78 changes: 78 additions & 0 deletions plugins/arc/src/ArcTooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React from 'react'
import { SanitizedHTML } from '@jbrowse/core/ui'
import { observer } from 'mobx-react'
import { Portal, alpha } from '@mui/material'
import { makeStyles } from 'tss-react/mui'
import {
useClientPoint,
useFloating,
useInteractions,
} from '@floating-ui/react'

function round(value: number) {
return Math.round(value * 1e5) / 1e5
}
const useStyles = makeStyles()(theme => ({
// these styles come from
// https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/Tooltip/Tooltip.js
tooltip: {
pointerEvents: 'none',
backgroundColor: alpha(theme.palette.grey[700], 0.9),
borderRadius: theme.shape.borderRadius,
color: theme.palette.common.white,
fontFamily: theme.typography.fontFamily,
padding: '4px 8px',
fontSize: theme.typography.pxToRem(12),
lineHeight: `${round(14 / 10)}em`,
maxWidth: 300,
wordWrap: 'break-word',
},
}))

interface Props {
message: React.ReactNode | string
}
const TooltipContents = React.forwardRef<HTMLDivElement, Props>(
function TooltipContents2({ message }, ref) {
return (
<div ref={ref}>
{React.isValidElement(message) ? (
message
) : message ? (
<SanitizedHTML html={String(message)} />
) : null}
</div>
)
},
)

const ArcTooltip = observer(function ({ contents }: { contents?: string }) {
const { theme, classes } = useStyles()

const { refs, floatingStyles, context } = useFloating({
placement: 'right',
})

const clientPoint = useClientPoint(context)
const { getFloatingProps } = useInteractions([clientPoint])

const popperTheme = theme?.components?.MuiPopper
return contents ? (
<Portal container={popperTheme?.defaultProps?.container}>
<div
className={classes.tooltip}
ref={refs.setFloating}
style={{
...floatingStyles,
zIndex: 100000,
pointerEvents: 'none',
}}
{...getFloatingProps()}
>
<TooltipContents message={contents} />
</div>
</Portal>
) : null
})

export default ArcTooltip
Loading

0 comments on commit 951e987

Please sign in to comment.