| 1 |
import { css } from '@patternfly/react-styles'; |
| 2 |
import styles from '@patternfly/react-styles/css/layouts/Gallery/gallery'; |
| 3 |
import cssGridTemplateColumnsMin from '@patternfly/react-tokens/dist/esm/l_gallery_GridTemplateColumns_min'; |
| 4 |
import cssGridTemplateColumnsMax from '@patternfly/react-tokens/dist/esm/l_gallery_GridTemplateColumns_max'; |
| 5 |
|
| 6 |
export interface GalleryProps extends React.HTMLProps<HTMLDivElement> { |
| 7 |
/** content rendered inside the Gallery layout */ |
| 8 |
children?: React.ReactNode; |
| 9 |
/** additional classes added to the Gallery layout */ |
| 10 |
className?: string; |
| 11 |
/** Adds space between children. */ |
| 12 |
hasGutter?: boolean; |
| 13 |
/** Minimum widths at various breakpoints. */ |
| 14 |
minWidths?: { |
| 15 |
default?: string; |
| 16 |
sm?: string; |
| 17 |
md?: string; |
| 18 |
lg?: string; |
| 19 |
xl?: string; |
| 20 |
'2xl'?: string; |
| 21 |
}; |
| 22 |
/** Maximum widths at various breakpoints. */ |
| 23 |
maxWidths?: { |
| 24 |
default?: string; |
| 25 |
sm?: string; |
| 26 |
md?: string; |
| 27 |
lg?: string; |
| 28 |
xl?: string; |
| 29 |
'2xl'?: string; |
| 30 |
}; |
| 31 |
/** Sets the base component to render. defaults to div */ |
| 32 |
component?: React.ElementType<any> | React.ComponentType<any>; |
| 33 |
} |
| 34 |
export const Gallery: React.FunctionComponent<GalleryProps> = ({ |
| 35 |
children = null, |
| 36 |
className = '', |
| 37 |
component = 'div', |
| 38 |
hasGutter = false, |
| 39 |
minWidths, |
| 40 |
maxWidths, |
| 41 |
...props |
| 42 |
}: GalleryProps) => { |
| 43 |
const minWidthStyles: any = {}; |
| 44 |
const Component: any = component; |
| 45 |
|
| 46 |
if (minWidths) { |
| 47 |
Object.entries(minWidths || {}).map( |
| 48 |
([breakpoint, value]) => |
| 49 |
(minWidthStyles[`${cssGridTemplateColumnsMin.name}${breakpoint !== 'default' ? `-on-${breakpoint}` : ''}`] = |
| 50 |
value) |
| 51 |
); |
| 52 |
} |
| 53 |
const maxWidthStyles: any = {}; |
| 54 |
if (maxWidths) { |
| 55 |
Object.entries(maxWidths || {}).map( |
| 56 |
([breakpoint, value]) => |
| 57 |
(maxWidthStyles[`${cssGridTemplateColumnsMax.name}${breakpoint !== 'default' ? `-on-${breakpoint}` : ''}`] = |
| 58 |
value) |
| 59 |
); |
| 60 |
} |
| 61 |
const widthStyles = { ...minWidthStyles, ...maxWidthStyles }; |
| 62 |
|
| 63 |
return ( |
| 64 |
<Component |
| 65 |
className={css(styles.gallery, hasGutter && styles.modifiers.gutter, className)} |
| 66 |
{...props} |
| 67 |
{...((minWidths || maxWidths) && { style: { ...widthStyles, ...props.style } as React.CSSProperties })} |
| 68 |
> |
| 69 |
{children} |
| 70 |
</Component> |
| 71 |
); |
| 72 |
}; |
| 73 |
Gallery.displayName = 'Gallery'; |