diff --git a/packages/grafana-ui/src/components/Layout/Layout.mdx b/packages/grafana-ui/src/components/Layout/Layout.mdx new file mode 100644 index 00000000000..da91db1d3bd --- /dev/null +++ b/packages/grafana-ui/src/components/Layout/Layout.mdx @@ -0,0 +1,104 @@ +import { Story, Preview, Props } from '@storybook/addon-docs/blocks'; +import { Layout, HorizontalGroup, VerticalGroup } from './Layout'; +import { Button } from '../Button'; +import { Select } from '../index'; + +# Layout + +## Generic + +Used for aligning items in specified orientation. Useful for multiple elements that need to be arranged in rows or columns. + +Expects multiple elements as `children` prop. This is the base for more specialised `Horizontal-` and `VerticalGroup` components. + +### Props + + +## Horizontal + +Used for horizontally aligning several elements (e.g. Button, Select) with a predefined spacing between them. + + + + + + + + + + + {}} + options={[ + { value: 1, label: 'Option 1' }, + { value: 2, label: 'Option 2' }, + ]} + /> + {}} + options={[ + { value: 1, label: 'Option 1' }, + { value: 2, label: 'Option 2' }, + ]} + /> + {}} + options={[ + { value: 1, label: 'Option 1' }, + { value: 2, label: 'Option 2' }, + ]} + /> + + + + +### Props + diff --git a/packages/grafana-ui/src/components/Layout/Layout.story.tsx b/packages/grafana-ui/src/components/Layout/Layout.story.tsx index aed3665ac5c..0e11e476725 100644 --- a/packages/grafana-ui/src/components/Layout/Layout.story.tsx +++ b/packages/grafana-ui/src/components/Layout/Layout.story.tsx @@ -4,11 +4,17 @@ import { VerticalGroup, HorizontalGroup, Layout } from './Layout'; import { Button } from '../Button'; import { withStoryContainer } from '../../utils/storybook/withStoryContainer'; import { select } from '@storybook/addon-knobs'; +import mdx from './Layout.mdx'; export default { title: 'Layout/Groups', component: Layout, decorators: [withStoryContainer, withCenteredStory, withHorizontallyCenteredStory], + parameters: { + docs: { + page: mdx, + }, + }, }; const justifyVariants = ['flex-start', 'flex-end', 'space-between']; diff --git a/packages/grafana-ui/src/components/Layout/Layout.tsx b/packages/grafana-ui/src/components/Layout/Layout.tsx index 96f84a94149..a94ec39d9cd 100644 --- a/packages/grafana-ui/src/components/Layout/Layout.tsx +++ b/packages/grafana-ui/src/components/Layout/Layout.tsx @@ -9,12 +9,14 @@ enum Orientation { } type Spacing = 'xs' | 'sm' | 'md' | 'lg'; type Justify = 'flex-start' | 'flex-end' | 'space-between' | 'center'; +type Align = 'normal' | 'flex-start' | 'flex-end' | 'center'; export interface LayoutProps { children: React.ReactNode[]; orientation?: Orientation; spacing?: Spacing; justify?: Justify; + align?: Align; } export interface ContainerProps { @@ -27,9 +29,10 @@ export const Layout: React.FC = ({ orientation = Orientation.Horizontal, spacing = 'sm', justify = 'flex-start', + align = 'normal', }) => { const theme = useTheme(); - const styles = getStyles(theme, orientation, spacing, justify); + const styles = getStyles(theme, orientation, spacing, justify, align); return (
{React.Children.map(children, (child, index) => { @@ -56,25 +59,28 @@ export const Container: React.FC = ({ children, padding, margin return
{children}
; }; -const getStyles = stylesFactory((theme: GrafanaTheme, orientation: Orientation, spacing: Spacing, justify: Justify) => { - return { - layout: css` - display: flex; - flex-direction: ${orientation === Orientation.Vertical ? 'column' : 'row'}; - justify-content: ${justify}; - height: 100%; - `, - buttonWrapper: css` - margin-bottom: ${orientation === Orientation.Horizontal ? 0 : theme.spacing[spacing]}; - margin-right: ${orientation === Orientation.Horizontal ? theme.spacing[spacing] : 0}; +const getStyles = stylesFactory( + (theme: GrafanaTheme, orientation: Orientation, spacing: Spacing, justify: Justify, align) => { + return { + layout: css` + display: flex; + flex-direction: ${orientation === Orientation.Vertical ? 'column' : 'row'}; + justify-content: ${justify}; + align-items: ${align}; + height: 100%; + `, + buttonWrapper: css` + margin-bottom: ${orientation === Orientation.Horizontal ? 0 : theme.spacing[spacing]}; + margin-right: ${orientation === Orientation.Horizontal ? theme.spacing[spacing] : 0}; - &:last-child { - margin-bottom: 0; - margin-right: 0; - } - `, - }; -}); + &:last-child { + margin-bottom: 0; + margin-right: 0; + } + `, + }; + } +); const getContainerStyles = stylesFactory((theme: GrafanaTheme, padding?: Spacing, margin?: Spacing) => { const paddingSize = (padding && theme.spacing[padding]) || 0;