Skip to content

Commit d9b6643

Browse files
committed
fix(tasty): improve caching for responsive styles
1 parent 2283ed1 commit d9b6643

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/tasty/tasty.tsx

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ import { ResponsiveStyleValue, stringifyStyles } from './utils/styles';
3131
// Per-style-key sequential class allocator
3232
const styleKeyToClass = new Map<string, string>();
3333
let nextClassId = 0;
34-
function allocateClassName(styleKey: string): string {
35-
const existing = styleKeyToClass.get(styleKey);
34+
function allocateClassName(styleKey: string, contextKey?: string): string {
35+
const key = contextKey ? `${styleKey}||${contextKey}` : styleKey;
36+
const existing = styleKeyToClass.get(key);
3637
if (existing) return existing;
3738
const cls = `t${nextClassId++}`;
38-
styleKeyToClass.set(styleKey, cls);
39+
styleKeyToClass.set(key, cls);
3940
return cls;
4041
}
4142

@@ -87,10 +88,10 @@ type TastyComponentPropsWithDefaults<
8788
> = keyof DefaultProps extends never
8889
? Props
8990
: {
90-
[key in Extract<keyof Props, keyof DefaultProps>]?: Props[key];
91-
} & {
92-
[key in keyof Omit<Props, keyof DefaultProps>]: Props[key];
93-
};
91+
[key in Extract<keyof Props, keyof DefaultProps>]?: Props[key];
92+
} & {
93+
[key in keyof Omit<Props, keyof DefaultProps>]: Props[key];
94+
};
9495

9596
export function tasty<K extends StyleList, V extends VariantMap>(
9697
options: TastyProps<K, V>,
@@ -290,8 +291,10 @@ function tastyElement<K extends StyleList, V extends VariantMap>(
290291
*/
291292
const renderDefaultStyles = cacheWrapper((breakpoints: number[]) => {
292293
// Allocate a stable class for default styles
294+
const bpKey = (breakpoints || []).join(',');
293295
const defaultClassName = allocateClassName(
294296
stringifyStyles(defaultStyles || {}),
297+
`bp:${bpKey}`,
295298
);
296299
return renderStyles(defaultStyles || {}, breakpoints, defaultClassName);
297300
});
@@ -319,7 +322,7 @@ function tastyElement<K extends StyleList, V extends VariantMap>(
319322
className: userClassName,
320323
...otherProps
321324
} = allProps as Record<string, unknown> as AllBasePropsWithMods<K> &
322-
WithVariant<V> & { className?: string };
325+
WithVariant<V> & { className?: string };
323326

324327
let propStyles: Styles | null = (
325328
(styleProps
@@ -360,10 +363,10 @@ function tastyElement<K extends StyleList, V extends VariantMap>(
360363
useDefaultStyles
361364
? defaultStyles
362365
: mergeStyles(
363-
defaultStyles,
364-
styles as Styles,
365-
propStyles as Styles,
366-
),
366+
defaultStyles,
367+
styles as Styles,
368+
propStyles as Styles,
369+
),
367370
[styleCacheKey],
368371
);
369372

@@ -374,8 +377,9 @@ function tastyElement<K extends StyleList, V extends VariantMap>(
374377
// Allocate a stable sequential class per style-key
375378
const className = useMemo(() => {
376379
const stylesKey = stringifyStyles(allStyles || {});
377-
return allocateClassName(stylesKey);
378-
}, [allStyles]);
380+
const bpKey = (breakpoints as number[] | undefined)?.join(',') || '';
381+
return allocateClassName(stylesKey, `bp:${bpKey}`);
382+
}, [allStyles, breakpoints?.join(',')]);
379383

380384
// Compute rules synchronously; inject via insertion effect
381385
const directResult: RenderResult = useMemo(() => {
@@ -452,9 +456,8 @@ function tastyElement<K extends StyleList, V extends VariantMap>(
452456
});
453457
}
454458

455-
_TastyComponent.displayName = `TastyComponent(${
456-
(defaultProps as any).qa || originalAs
457-
})`;
459+
_TastyComponent.displayName = `TastyComponent(${(defaultProps as any).qa || originalAs
460+
})`;
458461

459462
return _TastyComponent;
460463
}

0 commit comments

Comments
 (0)