Skip to content

Commit

Permalink
TruncatedText refactor using library
Browse files Browse the repository at this point in the history
  • Loading branch information
dreadhalor committed Jan 25, 2024
1 parent 3916582 commit c885c08
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 26 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"react-day-picker": "^8.8.0",
"react-icons": "^4.10.1",
"react-resizable-panels": "^1.0.9",
"react-resize-detector": "^10.0.1",
"recharts": "^2.7.3",
"styled-components": "^6.1.1",
"tailwind-merge": "^1.13.2",
Expand Down
11 changes: 10 additions & 1 deletion src/components/combobox/combobox.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ export const Demo2: Story = {
),
};

export const Demo3: Story = {
export const MultipleComboboxes: Story = {
render: (_) => (
<div className='~grid ~grid-flow-col ~grid-cols-2 ~gap-2'>
<Combobox placeholder='Select framework...' values={frameworks} />
<Combobox placeholder='Select framework...' values={frameworks} />
</div>
),
};

export const LongListDemo: Story = {
render: (_) => <Combobox values={longList} />,
};
2 changes: 1 addition & 1 deletion src/components/select/select-variants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cva } from 'class-variance-authority';

const selectVariants = cva([
'tw-reset ~flex ~h-9 ~w-full ~items-center ~justify-between ~rounded-md ~border ~border-border ~bg-background ~px-3 ~py-2 ~shadow-sm',
'tw-reset ~flex ~h-9 ~w-full ~items-center ~justify-between ~rounded-md ~border ~border-border ~bg-transparent ~px-3 ~py-2 ~shadow-sm',
'~ring-offset-background',
'data-[placeholder]:~text-input',
'focus:~outline-none focus:~ring-1 focus:~ring-ring',
Expand Down
34 changes: 10 additions & 24 deletions src/components/truncated-text/truncated-text.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
import { useRef, useEffect, useCallback } from 'react';
import { useRef, useCallback } from 'react';
import { useResizeDetector } from 'react-resize-detector';

type Props = {
children?: React.ReactNode;
onTruncation?: (isTruncated: boolean) => void;
};

/** Text which truncates when it overflows its container, & knows when it is being truncated. */
const TruncatedText = ({ children, onTruncation }: Props) => {
const text_ref = useRef<HTMLDivElement>(null);

// Using useCallback to memoize the function
const checkTruncation = useCallback(() => {
if (text_ref.current && onTruncation) {
onTruncation(text_ref.current.offsetWidth < text_ref.current.scrollWidth);
const targetRef = useRef<HTMLDivElement>(null);
const onResize = useCallback(() => {
if (targetRef.current && onTruncation) {
const { scrollWidth, offsetWidth } = targetRef.current;
onTruncation(offsetWidth < scrollWidth);
}
}, [onTruncation]);

useEffect(() => {
checkTruncation();
}, [children, checkTruncation]); // Added checkTruncation to the dependency array

useEffect(() => {
const resizeObserver = new ResizeObserver(checkTruncation);

const currentTextRef = text_ref.current;
if (currentTextRef) resizeObserver.observe(currentTextRef);
}, [onTruncation, targetRef]);

return () => {
if (currentTextRef) resizeObserver.unobserve(currentTextRef);
};
}, [checkTruncation]); // checkTruncation is now a stable function
useResizeDetector({ onResize, targetRef });

return (
<div className='~truncate' ref={text_ref}>
<div className='~truncate ~bg-gray-200' ref={targetRef}>
{children}
</div>
);
Expand Down

0 comments on commit c885c08

Please sign in to comment.