import React, { useMemo } from 'react' import useTheme from '../use-theme' import { GridJustify, GridDirection, GridAlignItems, GridAlignContent, } from './grid-types' import useScaleable from '../use-scaleable' export type GridBreakpointsValue = number | boolean export interface GridBasicComponentProps { xs?: GridBreakpointsValue sm?: GridBreakpointsValue md?: GridBreakpointsValue lg?: GridBreakpointsValue xl?: GridBreakpointsValue justify?: GridJustify direction?: GridDirection alignItems?: GridAlignItems alignContent?: GridAlignContent className?: string } const defaultProps = { xs: false as GridBreakpointsValue, sm: false as GridBreakpointsValue, md: false as GridBreakpointsValue, lg: false as GridBreakpointsValue, xl: false as GridBreakpointsValue, className: '', } type NativeAttrs = Omit, keyof GridBasicComponentProps> export type GridBasicItemProps = GridBasicComponentProps & NativeAttrs type ItemLayoutValue = { grow: number width: string basis: string display: string } const getItemLayout = (val: GridBreakpointsValue): ItemLayoutValue => { const display = val === 0 ? 'display: none;' : 'display: inherit;' if (typeof val === 'number') { const width = (100 / 24) * val const ratio = width > 100 ? '100%' : width < 0 ? '0' : `${width}%` return { grow: 0, display, width: ratio, basis: ratio, } } return { grow: 1, display, width: '100%', basis: '0', } } const GridBasicItem: React.FC> = ({ xs, sm, md, lg, xl, justify, direction, alignItems, alignContent, children, className, ...props }: React.PropsWithChildren & typeof defaultProps) => { const theme = useTheme() const { SCALES } = useScaleable() const classes = useMemo(() => { const aligns: { [key: string]: any } = { justify, direction, alignItems, alignContent, xs, sm, md, lg, xl, } const classString = Object.keys(aligns).reduce((pre, name) => { if (aligns[name] !== undefined && aligns[name] !== false) return `${pre} ${name}` return pre }, '') return classString.trim() }, [justify, direction, alignItems, alignContent, xs, sm, md, lg, xl]) const layout = useMemo< { [key in ['xs', 'sm', 'md', 'lg', 'xl'][number]]: ItemLayoutValue } >( () => ({ xs: getItemLayout(xs), sm: getItemLayout(sm), md: getItemLayout(md), lg: getItemLayout(lg), xl: getItemLayout(xl), }), [xs, sm, md, lg, xl], ) return (
{children}
) } GridBasicItem.defaultProps = defaultProps GridBasicItem.displayName = 'GeistGridBasicItem' export default GridBasicItem