Last active
April 16, 2024 01:33
-
-
Save DefectingCat/18a58e41ba53936834b9d579c3d386e5 to your computer and use it in GitHub Desktop.
Tooltip with CSS
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
.body { | |
display: flex; | |
} | |
.btn { | |
display: flex; | |
position: relative; | |
// &:hover { | |
// background: rgba(0, 0, 0, 0.15); | |
// } | |
} | |
.btn-right { | |
&::after { | |
position: absolute; | |
left: calc(100% + 0.14rem); | |
border-radius: 0.04rem; | |
padding: 0.05rem 0.06rem; | |
display: flex; | |
align-items: center; | |
width: max-content; | |
background: rgba(0, 0, 0, 0.4); | |
color: #fff; | |
} | |
} | |
.btn-top { | |
justify-content: center; | |
&::after { | |
position: absolute; | |
bottom: calc(100% + 0.14rem); | |
border-radius: 0.04rem; | |
padding: 0.05rem 0.06rem; | |
display: flex; | |
align-items: center; | |
width: max-content; | |
background: rgba(0, 0, 0, 0.4); | |
color: #fff; | |
} | |
} |
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
import styles from '@/projects/viewer/Layouts/Default/Tooltip.module.less'; | |
import { buildCN } from '@/utils'; | |
import { nanoid } from 'nanoid'; | |
import { memo, useMemo } from 'react'; | |
const cn = buildCN(styles); | |
export type ToolTipProps = { | |
title: string; | |
position?: 'top' | 'right'; | |
} & React.DetailedHTMLProps< | |
React.HTMLAttributes<HTMLDivElement>, | |
HTMLDivElement | |
>; | |
/** | |
* 使用 CSS 的 Tooltip | |
*/ | |
const Tooltip = ({ title, position, children, ...rest }: ToolTipProps) => { | |
const id = useMemo(() => `more-btn-${nanoid()}`, []); | |
const p = position ?? 'top'; | |
return ( | |
<div className={cn('body')} {...rest}> | |
<style>{`#${id}:hover::after {content: '${title}';}`}</style> | |
<div id={id} className={cn('btn', `btn-${p}`)}> | |
{children} | |
</div> | |
</div> | |
); | |
}; | |
export default memo(Tooltip); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment