UNPKG

@mantine/core

Version:

React components library focused on usability, accessibility and developer experience

1 lines 5.07 kB
{"version":3,"file":"parse-style-props.mjs","names":[],"sources":["../../../../../src/core/Box/style-props/parse-style-props/parse-style-props.ts"],"sourcesContent":["import { MantineTheme } from '../../../MantineProvider';\nimport { keys } from '../../../utils';\nimport { resolvers } from '../resolvers';\nimport type { SystemPropData } from '../style-props-data';\nimport type { StyleProp } from '../style-props.types';\nimport { sortMediaQueries, SortMediaQueriesResult } from './sort-media-queries';\n\nfunction hasResponsiveStyles(styleProp: StyleProp<unknown>) {\n if (typeof styleProp !== 'object' || styleProp === null) {\n return false;\n }\n\n const breakpoints = Object.keys(styleProp);\n\n if (breakpoints.length === 1 && breakpoints[0] === 'base') {\n return false;\n }\n\n return true;\n}\n\nfunction getBaseValue(value: StyleProp<unknown>) {\n if (typeof value === 'object' && value !== null) {\n if ('base' in value) {\n return value.base;\n }\n\n return undefined;\n }\n\n return value;\n}\n\nfunction getBreakpointKeys(value: StyleProp<unknown>) {\n if (typeof value === 'object' && value !== null) {\n return keys(value).filter((key) => key !== 'base');\n }\n\n return [];\n}\n\nfunction getBreakpointValue(value: StyleProp<unknown>, breakpoint: string) {\n if (typeof value === 'object' && value !== null && breakpoint in value) {\n return value[breakpoint as keyof typeof value];\n }\n\n return value;\n}\n\ninterface ParseStylePropsOptions {\n styleProps: Record<string, StyleProp<any>>;\n theme: MantineTheme;\n data: Record<string, SystemPropData>;\n}\n\nexport interface ParseStylePropsResult {\n hasResponsiveStyles: boolean;\n inlineStyles: React.CSSProperties;\n styles: React.CSSProperties;\n media: Record<string, React.CSSProperties>;\n}\n\nexport function parseStyleProps({\n styleProps,\n data,\n theme,\n}: ParseStylePropsOptions): SortMediaQueriesResult {\n return sortMediaQueries(\n keys(styleProps).reduce<{\n hasResponsiveStyles: boolean;\n inlineStyles: Record<string, unknown>;\n styles: Record<string, unknown>;\n media: Record<string, Record<string, unknown>>;\n }>(\n (acc, styleProp) => {\n if (\n (styleProp as string) === 'hiddenFrom' ||\n (styleProp as string) === 'visibleFrom' ||\n (styleProp as string) === 'sx'\n ) {\n return acc;\n }\n\n const propertyData = data[styleProp];\n const properties = Array.isArray(propertyData.property)\n ? propertyData.property\n : [propertyData.property];\n const baseValue = getBaseValue(styleProps[styleProp]);\n\n if (!hasResponsiveStyles(styleProps[styleProp])) {\n properties.forEach((property) => {\n acc.inlineStyles[property] = resolvers[propertyData.type](baseValue, theme);\n });\n\n return acc;\n }\n\n acc.hasResponsiveStyles = true;\n\n const breakpoints = getBreakpointKeys(styleProps[styleProp]);\n\n properties.forEach((property) => {\n if (baseValue != null) {\n acc.styles[property] = resolvers[propertyData.type](baseValue, theme);\n }\n\n breakpoints.forEach((breakpoint) => {\n const bp = `(min-width: ${theme.breakpoints[breakpoint]})`;\n acc.media[bp] = {\n ...acc.media[bp],\n [property]: resolvers[propertyData.type](\n getBreakpointValue(styleProps[styleProp], breakpoint),\n theme\n ),\n };\n });\n });\n\n return acc;\n },\n {\n hasResponsiveStyles: false,\n styles: {},\n inlineStyles: {},\n media: {},\n }\n )\n );\n}\n"],"mappings":";;;;;AAOA,SAAS,oBAAoB,WAA+B;AAC1D,KAAI,OAAO,cAAc,YAAY,cAAc,KACjD,QAAO;CAGT,MAAM,cAAc,OAAO,KAAK,UAAU;AAE1C,KAAI,YAAY,WAAW,KAAK,YAAY,OAAO,OACjD,QAAO;AAGT,QAAO;;AAGT,SAAS,aAAa,OAA2B;AAC/C,KAAI,OAAO,UAAU,YAAY,UAAU,MAAM;AAC/C,MAAI,UAAU,MACZ,QAAO,MAAM;AAGf;;AAGF,QAAO;;AAGT,SAAS,kBAAkB,OAA2B;AACpD,KAAI,OAAO,UAAU,YAAY,UAAU,KACzC,QAAO,KAAK,MAAM,CAAC,QAAQ,QAAQ,QAAQ,OAAO;AAGpD,QAAO,EAAE;;AAGX,SAAS,mBAAmB,OAA2B,YAAoB;AACzE,KAAI,OAAO,UAAU,YAAY,UAAU,QAAQ,cAAc,MAC/D,QAA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