/* eslint-disable max-lines-per-function */ /* eslint-disable @typescript-eslint/no-var-requires */ import _ from 'lodash' import { useGridTemplate } from './grid-template-area' import { esc, Escape } from './utils' const gapType = { gridColumnGap: 'x-', gridRowGap: 'y-', gridGap: '' } const gap = (type: keyof typeof gapType = 'gridGap', e: Escape = esc) => ( size: string, name: string ) => ({ [`.${e(`grid-gap-${gapType[type]}${name}`)}`]: { [type]: size }, }) const gridColumnCount = (count = 2) => ({ [`.grid-cols-${count}`]: { gridTemplateColumns: `repeat(${count}, 1fr)` }, [`.grid-rows-${count}`]: { gridTemplateRows: `repeat(${count}, min-content)`, }, }) const gridAutoMin = (e: Escape = esc) => (size: number, name: string) => ({ [`.${`grid-rows-min-${e(name)}`}`]: { gridAutoRows: `minmax(${size}, min-content)`, }, [`.${`grid-cols-min-${e(name)}`}`]: { gridTemplateColumns: `repeat(auto-fit, minmax(${size}, 1fr))`, }, }) const makeGridRowsCountAndSize = (e: Escape = esc) => (count = 2) => ( size: number, name: string ) => ({ [`.${`grid-rows-${count}-${e(name)}`}`]: { gridTemplateRows: `repeat(${count}, minmax(${size}, min-content))`, }, }) const defaultTheme = { grid: { grids: [1, 2, 3, 4, 5, 6, 8, 10, 12], template: { 'my-layout': [ ['header', 'header', 'header'], ['main', 'main', 'aside'], ['footer', 'footer', 'aside'], ], }, }, spacing: { '2': '0.5rem', '4': '1rem' }, } import { TailwindPlugin } from '.' export type GridPluginOptions = { templatePrefix?: string areaPrefix?: string areaChildSelectors?: boolean areaStartEnd?: boolean } type GridPlugin = (options?: GridPluginOptions) => TailwindPlugin export const tailwindGrid: GridPlugin = options => { const { templatePrefix = 'grid', areaPrefix = '', areaChildSelectors = true, areaStartEnd = false, } = options || {} const { gridAreas, gridTemplate } = useGridTemplate({ templatePrefix, areaPrefix, areaChildSelectors, areaStartEnd, }) return ({ addUtilities, e, theme, variants }) => { const gridTheme = theme('grid', defaultTheme.grid) const gridCounts = [ ...(gridTheme.grids || defaultTheme.grid.grids), ].map(x => (typeof x === 'number' ? Math.floor(x) : parseInt(`${x}`))) const gridTemplates = gridTheme.template || {} const spacing = theme('spacing', defaultTheme.spacing) const gridVariants = variants('grid', ['responsive']) const gridRowsCountAndSize = makeGridRowsCountAndSize(e) addUtilities( [ { '.grid': { display: 'grid' } }, { '.grid-dense': { gridAutoFlow: 'dense' } }, ...gridCounts.map(gridColumnCount), ...gridCounts .map(count => _.map(spacing, gridRowsCountAndSize(count))) .flat(1), ..._.map(spacing, gap('gridGap', e)), ..._.map(spacing, gap('gridRowGap', e)), ..._.map(spacing, gap('gridColumnGap', e)), ..._.map(spacing, gridAutoMin(e)), ..._.range(1, Math.max(...gridCounts) + 1).map(span => ({ [`.col-span-${span}`]: { gridColumnStart: `span ${span}` }, })), ..._.range(1, Math.max(...gridCounts) + 2).map(line => ({ [`.col-start-${line}`]: { gridColumnStart: `${line}` }, [`.col-end-${line}`]: { gridColumnEnd: `${line}` }, [`.col-start-${line}-last`]: { gridColumnStart: `-${line}` }, [`.col-end-${line}-last`]: { gridColumnEnd: `-${line}` }, })), { '.col-end-last': { gridColumnEnd: '-1' } }, ..._.range(1, Math.max(...gridCounts) + 1).map(span => ({ [`.row-span-${span}`]: { gridRowStart: `span ${span}` }, })), ..._.range(1, Math.max(...gridCounts) + 2).map(line => ({ [`.row-start-${line}`]: { gridRowStart: `${line}` }, [`.row-end-${line}`]: { gridRowEnd: `${line}` }, })), ..._.map(gridTemplates, gridTemplate(e)), ..._.map(gridTemplates, gridAreas(e)), ], gridVariants ) } } export default tailwindGrid