Skip to content

Commit

Permalink
chore: remove switch search and add text copy
Browse files Browse the repository at this point in the history
  • Loading branch information
Envoy-VC committed Sep 8, 2024
1 parent 2cdc09c commit cb65259
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Input } from '../ui/input';
import { useConnectKitStore, useGraz } from '~/lib/hooks';
import { ScrollArea } from '@radix-ui/react-scroll-area';
import { Button } from '../ui/button';
Expand All @@ -9,12 +8,9 @@ import { NetworkIcon } from 'lucide-react';
export const SwitchChainScreen = () => {
const { chains } = useConnectKitStore();
const { onSwitchChain } = useGraz();

return (
<div className='ck-flex ck-flex-col'>
<Input
className='!ck-rounded-2xl ck-h-10 ck-my-3'
placeholder='Search Chain'
/>
<ScrollArea className='ck-h-[400px] ck-my-4 ck-overflow-scroll'>
<div className='ck-grid ck-grid-cols-4 ck-gap-2'>
{chains.map((chain) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Avatar from 'avvvatars-react';
import { useConnectKitStore, useGraz, useUser } from '~/lib/hooks';
import { truncate } from '~/lib/utils';
import { Skeleton } from '../ui/skeleton';
import { Button } from '../ui/button';
import { ArrowRightLeftIcon, ChevronRightIcon, LogOutIcon } from 'lucide-react';
Expand All @@ -26,7 +25,9 @@ export const UserModalHome = () => {
</div>
<TextCopy
className='ck-translate-x-3'
content={truncate(account[activeChainId]?.bech32Address ?? '', 10)}
text={account[activeChainId]?.bech32Address ?? ''}
truncateOptions={{ length: 10, fromMiddle: true, enabled: true }}
type='text'
>
<TextCopyContent className='ck-font-semibold' />
<TextCopyButton
Expand Down
46 changes: 27 additions & 19 deletions packages/connect-kit/src/components/ui/text-copy.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client';

/* eslint-disable no-nested-ternary -- allow nested */
import {
type ComponentProps,
Expand All @@ -10,6 +12,8 @@ import {

import { cn, errorHandler, truncate } from '~/lib/utils';

import { useCopyToClipboard } from 'usehooks-ts';

import { Button, type ButtonProps } from './button';

import {
Expand All @@ -21,7 +25,7 @@ import {
} from 'lucide-react';

const TextCopyContext = createContext<{
content?: string;
text?: string;
type?: 'text' | 'password';
truncateOptions?: {
enabled?: boolean;
Expand All @@ -31,7 +35,7 @@ const TextCopyContext = createContext<{
hidden: boolean;
setHidden: (value: boolean) => void;
}>({
content: '',
text: '',
hidden: false,
type: 'text',
truncateOptions: {
Expand All @@ -45,7 +49,7 @@ const TextCopyContext = createContext<{
});

interface TextCopyProps extends ComponentProps<'div'> {
content?: string;
text?: string;
type?: 'text' | 'password';
truncateOptions?: {
enabled?: boolean;
Expand All @@ -66,14 +70,14 @@ interface TextCopyButtonProps extends ButtonProps {
}

export const TextCopy = forwardRef<HTMLDivElement, TextCopyProps>(
({ children, className, content, type, truncateOptions, ...props }, ref) => {
({ children, className, text, type, truncateOptions, ...props }, ref) => {
const [hidden, setHidden] = useState<boolean>(false);
return (
<TextCopyContext.Provider
value={{
hidden,
setHidden,
content,
text,
type,
truncateOptions,
}}
Expand All @@ -95,19 +99,26 @@ export const TextCopy = forwardRef<HTMLDivElement, TextCopyProps>(

export const TextCopyContent = forwardRef<HTMLDivElement, TextCopyContentProps>(
({ className, ...props }, ref) => {
const { hidden, type, truncateOptions, content } =
useContext(TextCopyContext);
const { hidden, type, truncateOptions, text } = useContext(TextCopyContext);
return (
<div className={cn('ck-font-medium', className)} {...props} ref={ref}>
{type === 'text'
? truncateOptions?.enabled
? truncate(content ?? '', length, truncateOptions.fromMiddle)
: content
? truncate(
text ?? '',
truncateOptions.length,
truncateOptions.fromMiddle
)
: text
: hidden
? '*'.repeat(24)
: truncateOptions?.enabled
? truncate(content ?? '', length, truncateOptions.fromMiddle)
: content}
? truncate(
text ?? '',
truncateOptions.length,
truncateOptions.fromMiddle
)
: text}
</div>
);
}
Expand All @@ -118,7 +129,7 @@ export const TextCopyEye = forwardRef<HTMLButtonElement, TextCopyEyeProps>(
const { hidden, setHidden } = useContext(TextCopyContext);
return (
<Button
className='ck-h-8 ck-w-8 ck-p-0'
className='ck-h-8 ck-w-8 !ck-p-0'
variant='ghost'
onClick={() => setHidden(!hidden)}
{...props}
Expand All @@ -134,16 +145,13 @@ export const TextCopyButton = forwardRef<
HTMLButtonElement,
TextCopyButtonProps
>(({ iconProps = { strokeWidth: 2.5 }, canCopy = true, ...props }, ref) => {
const { content } = useContext(TextCopyContext);
const { text } = useContext(TextCopyContext);
const [, copy] = useCopyToClipboard();
const [copied, setCopied] = useState<boolean>(false);

const copy = async (data: string) => {
await navigator.clipboard.writeText(data);
};

const copyText = async () => {
try {
await copy(content ?? '');
await copy(text ?? '');
setCopied(true);
} catch (error) {
errorHandler(error);
Expand All @@ -164,7 +172,7 @@ export const TextCopyButton = forwardRef<
{canCopy ? (
<Button
className='ck-h-8 ck-w-8 !ck-p-0'
variant='secondary'
variant='ghost'
onClick={copyText}
{...props}
ref={ref}
Expand Down

0 comments on commit cb65259

Please sign in to comment.