import { RuleSet, css } from 'styled-components'; import { ThemeProps } from '../components/types'; import { ITheme, IThemeFont, IThemeFontVariant } from '../theme'; import { IResponsiveVariant } from './types'; export function getVariant( type?: T, variants?: { [key in T]?: RuleSet } ): RuleSet | '' { return (type && variants && variants[type]) ? (variants[type] || '') : ''; } export function getVariantKey( key: K, type?: T, variants?: { [key in T]?: Record> } ): RuleSet | '' { if (type !== undefined && variants !== undefined && variants[type] !== undefined) { const variant = variants[type] as Record>; return variant[key] || ''; } else { return ''; } } export function getVariantResponsive(props: IResponsiveVariant & ThemeProps, callback: (key: T, theme: ITheme) => RuleSet | string) { const keys: (keyof IResponsiveVariant)[] = ['variant', 'variantSm', 'variantMd', 'variantLg', 'variantXl']; const theme = props.theme; const mediaQuery = theme.mediaQuery; return keys.map(key => { const value = props[key]; if (value) { const rule = callback(value, theme); if (key !== 'variant') { const size: keyof typeof mediaQuery = key.replace('variant', '').toLowerCase(); // console.log('size', size, 'rule', rule); return css`@media(min-width: ${mediaQuery[size]}) { ${rule} }`; } else { return rule; } } else { return undefined; } }).filter(Boolean); } export function getFontVariantResponsive(props: IResponsiveVariant & ThemeProps, callback: (key: IThemeFontVariant, font: IThemeFont) => RuleSet | string) { return getVariantResponsive(props, (key, theme) => { const font = theme.font[key]; return font ? callback(key, font) : ''; }); }