From 31b70f440f779d548343d9842540d22e41da4320 Mon Sep 17 00:00:00 2001 From: Myrto Kontouli Date: Tue, 25 Jul 2023 19:42:23 +0100 Subject: [PATCH] final final --- lib/src/components/badge/Badge.tsx | 12 +++++--- lib/src/components/badge/BadgeText.tsx | 40 ++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/lib/src/components/badge/Badge.tsx b/lib/src/components/badge/Badge.tsx index 5d8ad8fdb..59a7dc769 100644 --- a/lib/src/components/badge/Badge.tsx +++ b/lib/src/components/badge/Badge.tsx @@ -65,6 +65,8 @@ export type TBadgeContext = { overflow?: TBadgeProps['overflow'] textContent?: React.ReactNode, setTextContent?: React.Dispatch> + isOverflowing?: boolean, + setIsOverflowing?: React.Dispatch> } export type TBadgeProviderProps = TBadgeContext @@ -76,9 +78,11 @@ export const BadgeProvider: React.FC = ({ children }) => { const [textContent, setTextContent] = React.useState('') + const [isOverflowing, setIsOverflowing] = React.useState(false) + const value = React.useMemo( - () => ({ size, overflow, textContent, setTextContent }), - [size, overflow, textContent, setTextContent] + () => ({ size, overflow, textContent, setTextContent, isOverflowing, setIsOverflowing }), + [size, overflow, textContent, setTextContent, isOverflowing, setIsOverflowing] ) return {children} } @@ -95,7 +99,7 @@ const BadgeRoot = React.forwardRef( }, ref ) => { - const { textContent, size, overflow } = React.useContext(BadgeContext) + const { textContent, size, overflow, isOverflowing } = React.useContext(BadgeContext) const isSemanticTheme = [ 'info', @@ -107,7 +111,7 @@ const BadgeRoot = React.forwardRef( return ( = ({ children, ...rest }) => { - const { size: badgeSize, overflow, setTextContent } = React.useContext(BadgeContext) +export const BadgeTextBase: React.FC = React.forwardRef(({ children, ...rest }, ref) => { + const { size: badgeSize, overflow } = React.useContext(BadgeContext) const size = React.useMemo( () => overrideStitchesVariantValue(badgeSize, (s) => toTextSize[s]), [badgeSize] ) - React.useEffect(() => { - setTextContent?.(children) - }, [setTextContent, children]) + return ( + {children} + ) +}) + +// I cannot in good conscience stick a resize observer on every badge. +// So I'm splitting this into the base logic and the bit that does the ellipsis version which needs extra logic. +// Should I make changes to the utility instead? .-. +export const BadgeTextEllipsis: React.FC = ({ children, ...rest }) => { + const { setTextContent, setIsOverflowing } = React.useContext(BadgeContext) + + React.useEffect(() => setTextContent?.(children), [setTextContent, children]) + + const [elRef, setElRef] = useCallbackRefState() + useResizeObserver({ + delay: 0, elements: [elRef], onResize: () => { + if (!elRef) return; + setIsOverflowing?.(elRef.scrollWidth > elRef.clientWidth) + } + }) return ( - {children} + {children} ) } + + +export const BadgeText: React.FC = (props) => { + const { overflow } = React.useContext(BadgeContext) + + if (overflow === 'ellipsis') return + return +}