mirror of https://github.com/grafana/grafana.git
parent
1631e41303
commit
604e02be15
|
|
@ -6,6 +6,10 @@ import { useTheme2 } from '../../../themes';
|
||||||
import { Grid } from './Grid';
|
import { Grid } from './Grid';
|
||||||
import mdx from './Grid.mdx';
|
import mdx from './Grid.mdx';
|
||||||
|
|
||||||
|
const dimensions = Array.from({ length: 9 }).map(() => ({
|
||||||
|
minHeight: `${Math.random() * 100 + 100}px`,
|
||||||
|
}));
|
||||||
|
|
||||||
const meta: Meta<typeof Grid> = {
|
const meta: Meta<typeof Grid> = {
|
||||||
title: 'General/Layout/Grid',
|
title: 'General/Layout/Grid',
|
||||||
component: Grid,
|
component: Grid,
|
||||||
|
|
@ -22,15 +26,21 @@ const meta: Meta<typeof Grid> = {
|
||||||
export const ColumnsNumber: StoryFn<typeof Grid> = (args) => {
|
export const ColumnsNumber: StoryFn<typeof Grid> = (args) => {
|
||||||
const theme = useTheme2();
|
const theme = useTheme2();
|
||||||
return (
|
return (
|
||||||
<Grid gap={args.gap} columns={args.columns}>
|
<Grid {...args}>
|
||||||
{Array.from({ length: 9 }).map((_, i) => (
|
{Array.from({ length: 9 }).map((_, i) => (
|
||||||
<div key={i} style={{ background: theme.colors.background.secondary, textAlign: 'center' }}>
|
<div key={i} style={{ background: theme.colors.background.secondary, textAlign: 'center', ...dimensions[i] }}>
|
||||||
N# {i}
|
N# {i}
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
</Grid>
|
</Grid>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
ColumnsNumber.argTypes = {
|
||||||
|
alignItems: {
|
||||||
|
control: 'select',
|
||||||
|
options: ['stretch', 'flex-start', 'flex-end', 'center', 'baseline', 'start', 'end', 'self-start', 'self-end'],
|
||||||
|
},
|
||||||
|
};
|
||||||
ColumnsNumber.args = {
|
ColumnsNumber.args = {
|
||||||
columns: 3,
|
columns: 3,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -4,12 +4,14 @@ import React, { forwardRef, HTMLAttributes } from 'react';
|
||||||
import { GrafanaTheme2, ThemeSpacingTokens } from '@grafana/data';
|
import { GrafanaTheme2, ThemeSpacingTokens } from '@grafana/data';
|
||||||
|
|
||||||
import { useStyles2 } from '../../../themes';
|
import { useStyles2 } from '../../../themes';
|
||||||
|
import { AlignItems } from '../types';
|
||||||
import { getResponsiveStyle, ResponsiveProp } from '../utils/responsiveness';
|
import { getResponsiveStyle, ResponsiveProp } from '../utils/responsiveness';
|
||||||
|
|
||||||
interface GridPropsBase extends Omit<HTMLAttributes<HTMLDivElement>, 'className' | 'style'> {
|
interface GridPropsBase extends Omit<HTMLAttributes<HTMLDivElement>, 'className' | 'style'> {
|
||||||
children: NonNullable<React.ReactNode>;
|
children: NonNullable<React.ReactNode>;
|
||||||
/** Specifies the gutters between columns and rows. It is overwritten when a column or row gap has a value. */
|
/** Specifies the gutters between columns and rows. It is overwritten when a column or row gap has a value. */
|
||||||
gap?: ResponsiveProp<ThemeSpacingTokens>;
|
gap?: ResponsiveProp<ThemeSpacingTokens>;
|
||||||
|
alignItems?: ResponsiveProp<AlignItems>;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface PropsWithColumns extends GridPropsBase {
|
interface PropsWithColumns extends GridPropsBase {
|
||||||
|
|
@ -30,8 +32,8 @@ interface PropsWithMinColumnWidth extends GridPropsBase {
|
||||||
type GridProps = PropsWithColumns | PropsWithMinColumnWidth;
|
type GridProps = PropsWithColumns | PropsWithMinColumnWidth;
|
||||||
|
|
||||||
export const Grid = forwardRef<HTMLDivElement, GridProps>((props, ref) => {
|
export const Grid = forwardRef<HTMLDivElement, GridProps>((props, ref) => {
|
||||||
const { children, gap, columns, minColumnWidth, ...rest } = props;
|
const { alignItems, children, gap, columns, minColumnWidth, ...rest } = props;
|
||||||
const styles = useStyles2(getGridStyles, gap, columns, minColumnWidth);
|
const styles = useStyles2(getGridStyles, gap, columns, minColumnWidth, alignItems);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div ref={ref} {...rest} className={styles.grid}>
|
<div ref={ref} {...rest} className={styles.grid}>
|
||||||
|
|
@ -46,7 +48,8 @@ const getGridStyles = (
|
||||||
theme: GrafanaTheme2,
|
theme: GrafanaTheme2,
|
||||||
gap: GridProps['gap'],
|
gap: GridProps['gap'],
|
||||||
columns: GridProps['columns'],
|
columns: GridProps['columns'],
|
||||||
minColumnWidth: GridProps['minColumnWidth']
|
minColumnWidth: GridProps['minColumnWidth'],
|
||||||
|
alignItems: GridProps['alignItems']
|
||||||
) => {
|
) => {
|
||||||
return {
|
return {
|
||||||
grid: css([
|
grid: css([
|
||||||
|
|
@ -62,6 +65,9 @@ const getGridStyles = (
|
||||||
getResponsiveStyle(theme, columns, (val) => ({
|
getResponsiveStyle(theme, columns, (val) => ({
|
||||||
gridTemplateColumns: `repeat(${val}, 1fr)`,
|
gridTemplateColumns: `repeat(${val}, 1fr)`,
|
||||||
})),
|
})),
|
||||||
|
getResponsiveStyle(theme, alignItems, (val) => ({
|
||||||
|
alignItems: val,
|
||||||
|
})),
|
||||||
]),
|
]),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue