-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #936 from web3ui/fix/credentials
[Copy,Hide]: Fix button types
- Loading branch information
Showing
8 changed files
with
128 additions
and
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@web3uikit/core': patch | ||
--- | ||
|
||
Fix Hide and copy buttons |
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
172 changes: 98 additions & 74 deletions
172
packages/core/src/lib/Credentials/components/TruncateString.tsx
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 |
---|---|---|
@@ -1,93 +1,117 @@ | ||
import { color } from '@web3uikit/styles'; | ||
import { useEffect, FC, useRef, useState } from 'react'; | ||
import { ITruncateStringProps, TStringTruncate } from '../types'; | ||
|
||
const truncateString = ({ leftPercentage = 50, measurements, replaceString, text }: TStringTruncate): string => { | ||
if (measurements.text && measurements.component && measurements.text > measurements.component) { | ||
const size = (percentage: number) => measurements.component * (percentage / 100); | ||
const portion = (size: number) => Math.floor((text.length * size) / measurements.text); | ||
const truncateString = ({ | ||
leftPercentage = 50, | ||
measurements, | ||
replaceString, | ||
text, | ||
}: TStringTruncate): string => { | ||
if ( | ||
measurements.text && | ||
measurements.component && | ||
measurements.text > measurements.component | ||
) { | ||
const size = (percentage: number) => | ||
measurements.component * (percentage / 100); | ||
const portion = (size: number) => | ||
Math.floor((text.length * size) / measurements.text); | ||
|
||
const leftPart = text.slice(0, Math.max(0, portion(size(leftPercentage)) - replaceString.length)); | ||
const leftPart = text.slice( | ||
0, | ||
Math.max(0, portion(size(leftPercentage)) - replaceString.length), | ||
); | ||
|
||
const rightPart = text.slice(text.length - portion(size(100 - leftPercentage)) + replaceString.length, text.length); | ||
const rightPart = text.slice( | ||
text.length - | ||
portion(size(100 - leftPercentage)) + | ||
replaceString.length, | ||
text.length, | ||
); | ||
|
||
return `${leftPart}${replaceString}${rightPart}`; | ||
} else { | ||
return text; | ||
} | ||
return `${leftPart}${replaceString}${rightPart}`; | ||
} else { | ||
return text; | ||
} | ||
}; | ||
|
||
const TruncateString: FC<ITruncateStringProps> = ({ | ||
replaceString = '...', | ||
percentageOfCharsAfterTrunc = 50, | ||
text = '' | ||
export const TruncateString: FC<ITruncateStringProps> = ({ | ||
fontSize = '16px', | ||
percentageOfCharsAfterTrunc = 50, | ||
replaceString = '...', | ||
text = '', | ||
textColor = color.blue70, | ||
}) => { | ||
const [truncating, setTruncating] = useState(true); | ||
const [truncatedString, setTruncatedString] = useState<string | null>(null); | ||
const componentRef = useRef<HTMLDivElement>(null); | ||
const textRef = useRef<HTMLSpanElement>(null); | ||
const replaceStrRef = useRef<HTMLSpanElement>(null); | ||
const [truncating, setTruncating] = useState(true); | ||
const [truncatedString, setTruncatedString] = useState<string | null>(null); | ||
const componentRef = useRef<HTMLDivElement>(null); | ||
const textRef = useRef<HTMLSpanElement>(null); | ||
const replaceStrRef = useRef<HTMLSpanElement>(null); | ||
|
||
const getTruncatedString = (text: string) => { | ||
const measurements = { | ||
component: componentRef.current?.offsetWidth || 0, | ||
replace: replaceStrRef.current?.offsetWidth || 0, | ||
text: textRef.current?.offsetWidth || 0 | ||
const getTruncatedString = (text: string) => { | ||
const measurements = { | ||
component: componentRef.current?.offsetWidth || 0, | ||
replace: replaceStrRef.current?.offsetWidth || 0, | ||
text: textRef.current?.offsetWidth || 0, | ||
}; | ||
return truncateString({ | ||
leftPercentage: percentageOfCharsAfterTrunc, | ||
measurements, | ||
replaceString, | ||
text, | ||
}); | ||
}; | ||
return truncateString({ | ||
leftPercentage: percentageOfCharsAfterTrunc, | ||
measurements, | ||
replaceString, | ||
text | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
if (!truncatedString?.includes('...')) { | ||
setTruncatedString(text); | ||
} else { | ||
setTruncating(true); | ||
} | ||
}, [text]); | ||
useEffect(() => { | ||
if (!truncatedString?.includes('...')) { | ||
setTruncatedString(text); | ||
} else { | ||
setTruncating(true); | ||
} | ||
}, [text]); | ||
|
||
useEffect(() => { | ||
// 2. Call Truncating function on every state change | ||
if (truncating) { | ||
const ts = getTruncatedString(text); | ||
setTruncatedString(ts); | ||
setTruncating(false); | ||
} | ||
}, [truncating]); | ||
useEffect(() => { | ||
// 2. Call Truncating function on every state change | ||
if (truncating) { | ||
const ts = getTruncatedString(text); | ||
setTruncatedString(ts); | ||
setTruncating(false); | ||
} | ||
}, [truncating]); | ||
|
||
useEffect(() => { | ||
// Debounce Mechanism | ||
let timeoutId: any = null; | ||
const truncateListener = () => { | ||
clearTimeout(timeoutId); | ||
// 1. Resizing window sets Truncating state to True | ||
timeoutId = setTimeout(() => setTruncating(true), 50); | ||
}; | ||
window.addEventListener('resize', truncateListener); | ||
useEffect(() => { | ||
// Debounce Mechanism | ||
let timeoutId: any = null; | ||
const truncateListener = () => { | ||
clearTimeout(timeoutId); | ||
// 1. Resizing window sets Truncating state to True | ||
timeoutId = setTimeout(() => setTruncating(true), 50); | ||
}; | ||
window.addEventListener('resize', truncateListener); | ||
|
||
// Remove listener on component unmount | ||
return () => { | ||
window.removeEventListener('resize', truncateListener); | ||
}; | ||
}, []); | ||
// Remove listener on component unmount | ||
return () => { | ||
window.removeEventListener('resize', truncateListener); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div | ||
ref={componentRef} | ||
style={{ | ||
display: 'block', | ||
overflow: 'hidden', | ||
whiteSpace: 'nowrap' | ||
}} | ||
> | ||
{truncating && <span ref={textRef}>{text}</span>} | ||
{truncating && <span ref={replaceStrRef}>{replaceString}</span>} | ||
{!truncating && truncatedString} | ||
</div> | ||
); | ||
return ( | ||
<div | ||
ref={componentRef} | ||
style={{ | ||
display: 'block', | ||
overflow: 'hidden', | ||
whiteSpace: 'nowrap', | ||
color: textColor, | ||
fontSize: fontSize, | ||
}} | ||
> | ||
{truncating && <span ref={textRef}>{text}</span>} | ||
{truncating && <span ref={replaceStrRef}>{replaceString}</span>} | ||
{!truncating && truncatedString} | ||
</div> | ||
); | ||
}; | ||
|
||
export default TruncateString; |
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