Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache key in classNames prefix #2434

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/mantine-styles/src/tss/create-styles.ts
@@ -1,7 +1,11 @@
import type { MantineTheme } from '../theme';
import type { CSSObject } from './types';
import { useCss } from './use-css';
import { useMantineTheme, useMantineProviderStyles } from '../theme/MantineProvider';
import {
useMantineTheme,
useMantineProviderStyles,
useMantineEmotionCache,
} from '../theme/MantineProvider';
import { mergeClassNames } from './utils/merge-class-names/merge-class-names';

type ContextStyles = ReturnType<typeof useMantineProviderStyles>;
Expand Down Expand Up @@ -59,6 +63,7 @@ export function createStyles<Key extends string = string, Params = void>(
function useStyles(params: Params, options?: UseStylesOptions<Key>) {
const theme = useMantineTheme();
const context = useMantineProviderStyles(options?.name);
const cache = useMantineEmotionCache();

const { css, cx } = useCss();
const cssObject = getCssObject(theme, params, createRef);
Expand All @@ -84,6 +89,7 @@ export function createStyles<Key extends string = string, Params = void>(
context,
classNames: options?.classNames,
name: options?.name,
cache,
}),
cx,
theme,
Expand Down
@@ -1,3 +1,4 @@
import type { EmotionCache } from '@emotion/cache';
import { useMantineProviderStyles } from '../../../theme/MantineProvider';

interface Input<T extends Record<string, string>> {
Expand All @@ -6,6 +7,7 @@ interface Input<T extends Record<string, string>> {
context: ReturnType<typeof useMantineProviderStyles>;
classNames: Partial<T>;
name: string | string[];
cache: EmotionCache;
}

export function mergeClassNames<T extends Record<string, string>>({
Expand All @@ -14,6 +16,7 @@ export function mergeClassNames<T extends Record<string, string>>({
context,
classNames,
name,
cache,
}: Input<T>) {
const contextClassNames = context.reduce<Record<string, string>>((acc, item) => {
Object.keys(item.classNames).forEach((key) => {
Expand All @@ -35,10 +38,10 @@ export function mergeClassNames<T extends Record<string, string>>({
Array.isArray(name)
? name
.filter(Boolean)
.map((part) => `mantine-${part}-${className}`)
.map((part) => `${cache?.key || 'mantine'}-${part}-${className}`)
.join(' ')
: name
? `mantine-${name}-${className}`
? `${cache?.key || 'mantine'}-${name}-${className}`
: null
);
return acc;
Expand Down