diff --git a/packages/grafana-ui/src/components/Layout/Stack/HorizontalStack.tsx b/packages/grafana-ui/src/components/Layout/Stack/HorizontalStack.tsx new file mode 100644 index 00000000000..cfeb9cb729d --- /dev/null +++ b/packages/grafana-ui/src/components/Layout/Stack/HorizontalStack.tsx @@ -0,0 +1,17 @@ +import React from 'react'; + +import { ThemeSpacingTokens } from '@grafana/data'; + +import { ResponsiveProp } from '../utils/responsiveness'; + +import { Stack } from './Stack'; + +export const HorizontalStack = React.forwardRef< + HTMLDivElement, + React.PropsWithChildren<{ gap?: ResponsiveProp }> +>(({ children, gap = 1 }, ref) => ( + + {children} + +)); +HorizontalStack.displayName = 'HorizontalStack'; diff --git a/packages/grafana-ui/src/components/Layout/Stack/Stack.internal.story.tsx b/packages/grafana-ui/src/components/Layout/Stack/Stack.internal.story.tsx index 852df476317..53ab64778fd 100644 --- a/packages/grafana-ui/src/components/Layout/Stack/Stack.internal.story.tsx +++ b/packages/grafana-ui/src/components/Layout/Stack/Stack.internal.story.tsx @@ -2,7 +2,12 @@ import { Meta, StoryFn } from '@storybook/react'; import React, { ReactNode } from 'react'; import { SpacingTokenControl } from '../../../utils/storybook/themeStorybookControls'; +import { Alert } from '../../Alert/Alert'; +import { Button } from '../../Button'; +import { Card } from '../../Card/Card'; +import { Text } from '../../Text/Text'; +import { HorizontalStack } from './HorizontalStack'; import { Stack } from './Stack'; import mdx from './Stack.mdx'; @@ -16,7 +21,7 @@ const meta: Meta = { }, argTypes: { gap: SpacingTokenControl, - direction: { control: 'select', options: ['row', 'row-reverse', 'column', 'column-reverse'] }, + direction: { control: 'select', options: ['row', 'column'] }, }, }; @@ -34,4 +39,184 @@ export const Basic: StoryFn = ({ direction = 'column', gap = 2 }) ); }; +export const TestCases: StoryFn = () => { + return ( +
+ +

Comparisons Stack vs No stack

+ + + + + + + + + + + + + + + + + + + + + + + + + I am a card heading + + + + I am a card heading + Ohhhhh - and now a description and some actions + + + + + + + + I am a card heading + Ohhhhh - and now a description! + + + + + + + I am a card heading + + + + I am a card heading + Ohhhhh - and now a description and some actions + + + + + + + + I am a card heading + Ohhhhh - and now a description! + + + + + + +
+ + + + + + + + + + + + + + + + + +
+ +

Child alignment

+ +
+ + + +
+ + +
+ + + +
+ + + + + I am a card heading + + + + I am a card heading + Ohhhhh - and now a description and some actions + + + + + + + + I am a card heading + Ohhhhh - and now a description! + + + + + + + + I am a card heading + + + + I am a card heading + Ohhhhh - and now a description! + + + + + + + + + + + + + + + + + + + + + Surprise - a description! What will happen to the height of all the other alerts? + + + + + +
+ ); +}; + +function Example({ title, children }: { title: string; children: React.ReactNode }) { + return ( +
+ {title} +
{children}
+
+ ); +} + +function MyComponent({ children }: { children: React.ReactNode }) { + return
{children}
; +} + export default meta; diff --git a/packages/grafana-ui/src/components/Layout/Stack/Stack.mdx b/packages/grafana-ui/src/components/Layout/Stack/Stack.mdx index a956ea5eb5e..b21e7652f8f 100644 --- a/packages/grafana-ui/src/components/Layout/Stack/Stack.mdx +++ b/packages/grafana-ui/src/components/Layout/Stack/Stack.mdx @@ -1,5 +1,5 @@ import { Meta, ArgTypes, Canvas } from '@storybook/blocks'; -import { Stack } from './Stack'; +import { Stack, HorizontalStack } from './index'; import * as Stories from './Stack.internal.story'; @@ -8,6 +8,8 @@ import * as Stories from './Stack.internal.story'; The `Stack` component is designed to assist with layout and positioning of elements within a container, offering a simple and flexible way to stack elements vertically or horizontally. This documentation outlines the proper usage of the Stack component and provides guidance on when to use it over the Grid or Flex components. +There is also a `HorizontalStack` component, which is a thin wrapper around Stack, equivalent to ``. + ### Usage #### When to use @@ -28,7 +30,15 @@ Use the `Grid` component instead for these use cases: Use the `Flex` component instead for these use cases: -- **Centering:** Perfect for centering elements both vertically and horizontally. -- **Dynamic Order:** Easily reorder elements for responsive layouts without changing code. +- **Alignment:** More options for item alignment. +- **Flex items:** Custom flex basis or configure how items stretch and wrap. + +## Props + +### Stack + +### HorizontalStack + + diff --git a/packages/grafana-ui/src/components/Layout/Stack/Stack.tsx b/packages/grafana-ui/src/components/Layout/Stack/Stack.tsx index c660bfa9ac9..2395610b3a1 100644 --- a/packages/grafana-ui/src/components/Layout/Stack/Stack.tsx +++ b/packages/grafana-ui/src/components/Layout/Stack/Stack.tsx @@ -2,18 +2,23 @@ import React from 'react'; import { ThemeSpacingTokens } from '@grafana/data'; -import { Direction, Flex } from '../Flex/Flex'; +import { Flex } from '../Flex/Flex'; import { ResponsiveProp } from '../utils/responsiveness'; - interface StackProps { - direction?: ResponsiveProp; + direction?: ResponsiveProp<'column' | 'row'>; gap?: ResponsiveProp; } -export const Stack = ({ gap = 1, direction = 'column', children }: React.PropsWithChildren) => { - return ( - - {children} - - ); -}; +export const Stack = React.forwardRef>( + ({ gap = 1, direction = 'column', children }, ref) => { + return ( + + {React.Children.map(children, (child) => ( +
{child}
+ ))} +
+ ); + } +); + +Stack.displayName = 'Stack'; diff --git a/packages/grafana-ui/src/components/Layout/Stack/index.ts b/packages/grafana-ui/src/components/Layout/Stack/index.ts new file mode 100644 index 00000000000..89768e71dc0 --- /dev/null +++ b/packages/grafana-ui/src/components/Layout/Stack/index.ts @@ -0,0 +1,2 @@ +export { Stack } from './Stack'; +export { HorizontalStack } from './HorizontalStack'; diff --git a/packages/grafana-ui/src/unstable.ts b/packages/grafana-ui/src/unstable.ts index 7104a81873a..644f570f4fd 100644 --- a/packages/grafana-ui/src/unstable.ts +++ b/packages/grafana-ui/src/unstable.ts @@ -12,4 +12,4 @@ export * from './components/Layout/Box/Box'; export * from './components/Layout/Flex/Flex'; -export { Stack } from './components/Layout/Stack/Stack'; +export { Stack, HorizontalStack } from './components/Layout/Stack';