Skip to content

Commit

Permalink
fix hide button not works
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoYhun committed Sep 22, 2023
1 parent d375ad9 commit 993d8a6
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions src/pages/TrueSightV2/pages/SingleToken.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Trans, t } from '@lingui/macro'
import { rgba } from 'polished'
import { stringify } from 'querystring'
import { ReactNode, useEffect, useLayoutEffect, useRef, useState } from 'react'
import React, { ReactNode, useCallback, useEffect, useRef, useState } from 'react'
import { ChevronLeft } from 'react-feather'
import Skeleton, { SkeletonTheme } from 'react-loading-skeleton'
import { useLocation, useNavigate, useParams, useSearchParams } from 'react-router-dom'
Expand Down Expand Up @@ -160,7 +160,7 @@ export const defaultExplorePageToken = {
assetId: 1,
}

const StyledTokenDescription = styled.div<{ show?: boolean }>`
const StyledTokenDescription = styled.span<{ show?: boolean }>`
text-overflow: ellipsis;
overflow: hidden;
font-size: 12px;
Expand Down Expand Up @@ -188,11 +188,16 @@ const TokenDescription = ({ description }: { description: string }) => {
const theme = useTheme()
const [show, setShow] = useState(true)
const [isTextExceeded, setIsTextExceeded] = useState(false)
const ref = useRef<HTMLDivElement>(null)
console.log('🚀 ~ file: SingleToken.tsx:191 ~ TokenDescription ~ isTextExceeded:', isTextExceeded)

useLayoutEffect(() => {
setIsTextExceeded((!!description && ref.current && ref.current?.clientWidth <= ref.current?.scrollWidth) || false)
}, [description])
const ref = useCallback(
(el: HTMLDivElement) => {
if (el && !!description) {
setIsTextExceeded(el.clientHeight > 18 || el.scrollWidth > el.clientWidth || false)
}
},
[description],
)

useEffect(() => {
const hideBtn = document.getElementById('hide-token-description-span')
Expand All @@ -205,19 +210,30 @@ const TokenDescription = ({ description }: { description: string }) => {

return (
<Row style={{ position: 'relative' }}>
<StyledTokenDescription
ref={ref}
show={show}
dangerouslySetInnerHTML={{
__html:
linkify(description) +
(isTextExceeded
? `<span style="color:${
theme.primary
}; cursor:pointer; margin-left:4px;" id="hide-token-description-span">${t`Hide`}</span>`
: ''),
}}
/>
<StyledTokenDescription ref={ref} show={show}>
<span
dangerouslySetInnerHTML={{
__html: linkify(description),
}}
/>
{isTextExceeded && show && (
<Text
as="span"
fontSize="12px"
color={theme.primary}
width="fit-content"
style={{
padding: '0 6px',
cursor: 'pointer',
flexBasis: 'fit-content',
whiteSpace: 'nowrap',
}}
onClick={() => setShow(false)}
>
Hide
</Text>
)}
</StyledTokenDescription>
{isTextExceeded && !show && (
<Text
as="span"
Expand Down

0 comments on commit 993d8a6

Please sign in to comment.