diff --git a/.changeset/rotten-starfishes-watch.md b/.changeset/rotten-starfishes-watch.md new file mode 100644 index 000000000..dc6f3986c --- /dev/null +++ b/.changeset/rotten-starfishes-watch.md @@ -0,0 +1,5 @@ +--- +'@web3uikit/core': patch +--- + +Fix Hide and copy buttons diff --git a/packages/core/src/lib/CopyButton/CopyButton.tsx b/packages/core/src/lib/CopyButton/CopyButton.tsx index 546916db6..f02fc2af8 100644 --- a/packages/core/src/lib/CopyButton/CopyButton.tsx +++ b/packages/core/src/lib/CopyButton/CopyButton.tsx @@ -49,6 +49,7 @@ const CopyButton: FC = ({ { onCopy(e); diff --git a/packages/core/src/lib/Credentials/Credentials.stories.tsx b/packages/core/src/lib/Credentials/Credentials.stories.tsx index cfab7527a..111145f6a 100644 --- a/packages/core/src/lib/Credentials/Credentials.stories.tsx +++ b/packages/core/src/lib/Credentials/Credentials.stories.tsx @@ -75,4 +75,5 @@ Customize.args = { }, hasHideButton: false, text: 'https://xj5hyiafwkhn.moralis.io:2053/servers', + textColor: 'white', }; diff --git a/packages/core/src/lib/Credentials/Credentials.tsx b/packages/core/src/lib/Credentials/Credentials.tsx index 8e1108d21..f619e2ca1 100644 --- a/packages/core/src/lib/Credentials/Credentials.tsx +++ b/packages/core/src/lib/Credentials/Credentials.tsx @@ -39,10 +39,9 @@ const Credentials: FC = ({ useEffect(() => setIsValueHidden(isHidden), [isHidden]); - useEffect( - () => setIsMultiline((text.match(/\n/g) || []).length > 0), - [text], - ); + useEffect(() => setIsMultiline((text.match(/\n/g) || []).length > 0), [ + text, + ]); return ( = ({ isMultiline={isMultiline} > = ({ )} diff --git a/packages/core/src/lib/Credentials/components/TruncateString.tsx b/packages/core/src/lib/Credentials/components/TruncateString.tsx index 91b4c7f32..eb4ca8781 100644 --- a/packages/core/src/lib/Credentials/components/TruncateString.tsx +++ b/packages/core/src/lib/Credentials/components/TruncateString.tsx @@ -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 = ({ - replaceString = '...', - percentageOfCharsAfterTrunc = 50, - text = '' +export const TruncateString: FC = ({ + fontSize = '16px', + percentageOfCharsAfterTrunc = 50, + replaceString = '...', + text = '', + textColor = color.blue70, }) => { - const [truncating, setTruncating] = useState(true); - const [truncatedString, setTruncatedString] = useState(null); - const componentRef = useRef(null); - const textRef = useRef(null); - const replaceStrRef = useRef(null); + const [truncating, setTruncating] = useState(true); + const [truncatedString, setTruncatedString] = useState(null); + const componentRef = useRef(null); + const textRef = useRef(null); + const replaceStrRef = useRef(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 ( -
- {truncating && {text}} - {truncating && {replaceString}} - {!truncating && truncatedString} -
- ); + return ( +
+ {truncating && {text}} + {truncating && {replaceString}} + {!truncating && truncatedString} +
+ ); }; export default TruncateString; diff --git a/packages/core/src/lib/Credentials/types.ts b/packages/core/src/lib/Credentials/types.ts index c2abde3a2..5a8a4f23e 100644 --- a/packages/core/src/lib/Credentials/types.ts +++ b/packages/core/src/lib/Credentials/types.ts @@ -42,7 +42,7 @@ export interface ICredentialsProps /** * color of the text */ - textColor?: string | typeof color; + textColor?: string; /** * width of component @@ -85,6 +85,16 @@ export interface ITruncateStringProps { * Percentage of characters(approx) to remain after truncate */ percentageOfCharsAfterTrunc?: number; + + /** + * color of text + */ + textColor: string; + + /** + * size of font + */ + fontSize: string; } export type TStringTruncate = { diff --git a/packages/core/src/lib/HideButton/HideButton.tsx b/packages/core/src/lib/HideButton/HideButton.tsx index 9647fdd1a..f45b4a96c 100644 --- a/packages/core/src/lib/HideButton/HideButton.tsx +++ b/packages/core/src/lib/HideButton/HideButton.tsx @@ -18,6 +18,7 @@ const HideButton: FC = ({ }) => { return (