diff --git a/packages/expandable-react/src/Expander.tsx b/packages/expandable-react/src/Expander.tsx index 39f1f0c48aa..b282111aaec 100644 --- a/packages/expandable-react/src/Expander.tsx +++ b/packages/expandable-react/src/Expander.tsx @@ -40,10 +40,17 @@ export const Expander = React.forwardRef(function Expander< useEffect(() => { const observer = new ResizeObserver(function () { - setExpanderHeight(internalRef.current!.offsetHeight); + // Default to 64 if the height can not be read because that is + // the height of the default summary element. In a custom component + // this means that the focus ring might be slightly misaligned but + // in most cases we will be able to read the ref correctly. + setExpanderHeight(internalRef.current?.offsetHeight || 64); }); - observer.observe(internalRef.current!); - return () => observer.disconnect(); + if (internalRef.current) { + observer.observe(internalRef.current); + return () => observer.disconnect(); + } + return () => {}; }, [setExpanderHeight]); return ( diff --git a/packages/jokul/src/components/expander/Expander.tsx b/packages/jokul/src/components/expander/Expander.tsx index f8ceebf9da0..f193076d8b9 100644 --- a/packages/jokul/src/components/expander/Expander.tsx +++ b/packages/jokul/src/components/expander/Expander.tsx @@ -45,10 +45,17 @@ export const Expander = React.forwardRef(function Expander< useEffect(() => { const observer = new ResizeObserver(function () { - setExpanderHeight(internalRef.current!.offsetHeight); + // Default to 64 if the height can not be read because that is + // the height of the default summary element. In a custom component + // this means that the focus ring might be slightly misaligned but + // in most cases we will be able to read the ref correctly. + setExpanderHeight(internalRef.current?.offsetHeight || 64); }); - observer.observe(internalRef.current!); - return () => observer.disconnect(); + if (internalRef.current) { + observer.observe(internalRef.current); + return () => observer.disconnect(); + } + return () => {}; }, [setExpanderHeight]); return (