Skip to content

Instantly share code, notes, and snippets.

@fuxingloh
Created June 3, 2024 05:45
Show Gist options
  • Save fuxingloh/e8c65e1cb014070e792cc296151a464b to your computer and use it in GitHub Desktop.
Save fuxingloh/e8c65e1cb014070e792cc296151a464b to your computer and use it in GitHub Desktop.
import { clsx } from 'clsx';
import { ReactElement } from 'react';
export function TruncateMiddle(props: {
children: string;
className?: string;
prefixLength?: number;
suffixLength?: number;
}): ReactElement {
const text = props.children;
return (
<div className={clsx('relative inline-block select-none break-keep', props.className)}>
{text.substring(0, props.prefixLength ?? 6)}…{text.substring(text.length - (props.suffixLength ?? 6))}
<div className="absolute inset-0 select-all overflow-clip whitespace-nowrap text-transparent">{text}</div>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment