-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tabs): display highlight on tabs when mouse-in (#696)
* feat(use-classes): add tool to handle class name strings * feat(highlight): add component follows the position of the current component * feat(tabs): display highlight on tabs when mouse-in * docs(tabs): append props * test: fix paths for testcase
- Loading branch information
Showing
18 changed files
with
435 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React, { useMemo, useRef } from 'react' | ||
import { isUnplacedRect, ReactiveDomReact } from '../utils/layouts' | ||
import usePrevious from '../utils/use-previous' | ||
import useTheme from '../use-theme' | ||
|
||
export type HighlightProps = { | ||
rect: ReactiveDomReact | ||
visible?: boolean | ||
hoverHeightRatio?: number | ||
hoverWidthRatio?: number | ||
} | ||
|
||
type HighlightPosition = { | ||
width: string | ||
left: string | ||
height: string | ||
top: string | ||
transition: string | ||
} | ||
|
||
const Highlight: React.FC<HighlightProps> = ({ | ||
rect, | ||
visible, | ||
hoverHeightRatio = 1, | ||
hoverWidthRatio = 1, | ||
...props | ||
}) => { | ||
const theme = useTheme() | ||
const ref = useRef<HTMLDivElement | null>(null) | ||
const isFirstVisible = usePrevious<boolean>(isUnplacedRect(rect)) | ||
const position = useMemo<HighlightPosition>(() => { | ||
const width = rect.width * hoverWidthRatio | ||
const height = rect.height * hoverHeightRatio | ||
return { | ||
width: `${width}px`, | ||
left: `${rect.left + (rect.width - width) / 2}px`, | ||
height: `${height}px`, | ||
top: `${rect.elementTop + (rect.height - height) / 2}px`, | ||
transition: isFirstVisible ? 'opacity' : 'opacity, width, left', | ||
} | ||
}, [rect, hoverWidthRatio, hoverHeightRatio]) | ||
|
||
return ( | ||
<div ref={ref} className="highlight" {...props}> | ||
<style jsx>{` | ||
.highlight { | ||
background: ${theme.palette.accents_2}; | ||
position: absolute; | ||
border-radius: 5px; | ||
width: ${position.width}; | ||
left: ${position.left}; | ||
height: ${position.height}; | ||
top: ${position.top}; | ||
opacity: ${visible ? 0.8 : 0}; | ||
transition: 0.15s ease; | ||
transition-property: ${position.transition}; | ||
} | ||
`}</style> | ||
</div> | ||
) | ||
} | ||
|
||
export default Highlight |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.