@mantine/core
Version:
React components library focused on usability, accessibility and developer experience
1 lines • 2.88 kB
Source Map (JSON)
{"version":3,"file":"merge-mantine-theme.cjs","names":["deepMerge"],"sources":["../../../../src/core/MantineProvider/merge-mantine-theme/merge-mantine-theme.ts"],"sourcesContent":["import { deepMerge } from '../../utils';\nimport type { MantineTheme, MantineThemeOverride } from '../theme.types';\n\nexport const INVALID_PRIMARY_COLOR_ERROR =\n '[@mantine/core] MantineProvider: Invalid theme.primaryColor, it accepts only key of theme.colors, learn more – https://mantine.dev/theming/colors/#primary-color';\n\nexport const INVALID_PRIMARY_SHADE_ERROR =\n '[@mantine/core] MantineProvider: Invalid theme.primaryShade, it accepts only 0-9 integers or an object { light: 0-9, dark: 0-9 }';\n\nfunction isValidPrimaryShade(shade: number) {\n if (shade < 0 || shade > 9) {\n return false;\n }\n\n return parseInt(shade.toString(), 10) === shade;\n}\n\nexport function validateMantineTheme(theme: MantineTheme): asserts theme is MantineTheme {\n if (!(theme.primaryColor in theme.colors)) {\n throw new Error(INVALID_PRIMARY_COLOR_ERROR);\n }\n\n if (typeof theme.primaryShade === 'object') {\n if (\n !isValidPrimaryShade(theme.primaryShade.dark) ||\n !isValidPrimaryShade(theme.primaryShade.light)\n ) {\n throw new Error(INVALID_PRIMARY_SHADE_ERROR);\n }\n }\n\n if (typeof theme.primaryShade === 'number' && !isValidPrimaryShade(theme.primaryShade)) {\n throw new Error(INVALID_PRIMARY_SHADE_ERROR);\n }\n}\n\nexport function mergeMantineTheme(\n currentTheme: MantineTheme,\n themeOverride?: MantineThemeOverride\n) {\n if (!themeOverride) {\n validateMantineTheme(currentTheme);\n return currentTheme;\n }\n\n const result = deepMerge(currentTheme, themeOverride);\n\n // deepMerge does a shallow root spread, so result.headings still aliases\n // currentTheme.headings when the override has no `headings` key. Reassign\n // rather than mutate to keep the input (typically DEFAULT_THEME) untouched.\n if (themeOverride.fontFamily && !themeOverride.headings?.fontFamily) {\n result.headings = { ...result.headings, fontFamily: themeOverride.fontFamily };\n }\n\n validateMantineTheme(result);\n return result;\n}\n"],"mappings":";;AAGA,MAAa,8BACX;AAEF,MAAa,8BACX;AAEF,SAAS,oBAAoB,OAAe;CAC1C,IAAI,QAAQ,KAAK,QAAQ,GACvB,OAAO;CAGT,OAAO,SAAS,MAAM,SAAS,GAAG,EAAE,MAAM;AAC5C;AAEA,SAAgB,qBAAqB,OAAoD;CACvF,IAAI,EAAE,MAAM,gBAAgB,MAAM,SAChC,MAAM,IAAI,MAAM,2BAA2B;CAG7C,IAAI,OAAO,MAAM,iBAAiB;MAE9B,CAAC,oBAAoB,MAAM,aAAa,IAAI,KAC5C,CAAC,oBAAoB,MAAM,aAAa,KAAK,GAE7C,MAAM,IAAI,MAAM,2BAA2B;CAAA;CAI/C,IAAI,OAAO,MAAM,iBAAiB,YAAY,CAAC,oBAAoB,MAAM,YAAY,GACnF,MAAM,IAAI,MAAM,2BAA2B;AAE/C;AAEA,SAAgB,kBACd,cACA,eACA;CACA,IAAI,CAAC,eAAe;EAClB,qBAAqB,YAAY;EACjC,OAAO;CACT;CAEA,MAAM,SAASA,mBAAAA,UAAU,cAAc,aAAa;CAKpD,IAAI,cAAc,cAAc,CAAC,cAAc,UAAU,YACvD,OAAO,WAAW;EAAE,GAAG,OAAO;EAAU,YAAY,cAAc;CAAW;CAG/E,qBAAqB,MAAM;CAC3B,OAAO;AACT"}