Skip to content

Commit

Permalink
feat(ContentPeek): add new prop for specifying the minHeightThreshold…
Browse files Browse the repository at this point in the history
… for showing Peek controls
  • Loading branch information
Aaron Hill authored and Aaron Hill committed Nov 11, 2024
1 parent 7a0ff41 commit 0cb1be6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/components/ContentPeek/ContentPeek.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const meta = {
contentExpandText: { control: 'text' },
contentCollapseText: { control: 'text' },
maxHeight: { control: 'number' },
minHeightThreshold: { control: 'number' },
},
} satisfies Meta<typeof ContentPeek>;

Expand Down
10 changes: 8 additions & 2 deletions src/components/ContentPeek/ContentPeek.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export interface ContentPeekProps extends React.HTMLAttributes<HTMLDivElement> {
* Maximum height of the content when collapsed
*/
maxHeight?: number;
/**
* Used to set a minimum height for the content before enabling the Peek functionality. Defaults to the same value as maxHeight if not provided.
*/
minHeightThreshold?: number;
}

/**
Expand All @@ -48,6 +52,7 @@ const ContentPeek = forwardRef<HTMLDivElement, ContentPeekProps>(
contentExpandText = 'Read More',
contentCollapseText = 'Read Less',
maxHeight = 480,
minHeightThreshold,
...props
},
ref,
Expand All @@ -59,9 +64,10 @@ const ContentPeek = forwardRef<HTMLDivElement, ContentPeekProps>(

useEffect(() => {
if (contentRef.current) {
setHasOverflow(contentRef.current.scrollHeight > maxHeight);
const threshold = minHeightThreshold ?? maxHeight;
setHasOverflow(contentRef.current.scrollHeight > threshold);
}
}, [children, maxHeight]);
}, [children, maxHeight, minHeightThreshold]);

const toggleExpand = useCallback(() => {
setIsExpanded((expanded) => !expanded);
Expand Down

0 comments on commit 0cb1be6

Please sign in to comment.