diff --git a/packages/grafana-ui/src/components/ToolbarButton/ToolbarButtonRow.tsx b/packages/grafana-ui/src/components/ToolbarButton/ToolbarButtonRow.tsx index 10e1cb9f31f..1f43cd7d7b3 100644 --- a/packages/grafana-ui/src/components/ToolbarButton/ToolbarButtonRow.tsx +++ b/packages/grafana-ui/src/components/ToolbarButton/ToolbarButtonRow.tsx @@ -19,9 +19,9 @@ const OVERFLOW_BUTTON_ID = 'overflow-button'; export const ToolbarButtonRow = forwardRef( ({ alignment = 'left', className, children, ...rest }, ref) => { - const [childVisibility, setChildVisibility] = useState( - Array(React.Children.toArray(children).length).fill(true) - ); + // null is a valid react child so we need to filter it out to prevent unnecessary padding + const childrenWithoutNull = React.Children.toArray(children).filter((child) => child !== null); + const [childVisibility, setChildVisibility] = useState(Array(childrenWithoutNull.length).fill(true)); const containerRef = useRef(null); const [showOverflowItems, setShowOverflowItems] = useState(false); const overflowItemsRef = createRef(); @@ -66,8 +66,9 @@ export const ToolbarButtonRow = forwardRef( return (
- {React.Children.map(children, (child, index) => ( + {childrenWithoutNull.map((child, index) => (
@@ -87,7 +88,7 @@ export const ToolbarButtonRow = forwardRef( {showOverflowItems && (
- {React.Children.toArray(children).map((child, index) => !childVisibility[index] && child)} + {childrenWithoutNull.map((child, index) => !childVisibility[index] && child)}
)}