Skip to content

Commit

Permalink
Improve getting computed styles performance
Browse files Browse the repository at this point in the history
  • Loading branch information
mj12albert committed Oct 16, 2024
1 parent 0d2b45f commit ddc2547
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions packages/mui-base/src/Collapsible/Content/useCollapsibleContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,25 @@ import { useAnimationsFinished } from '../../utils/useAnimationsFinished';
import { useForkRef } from '../../utils/useForkRef';
import { useId } from '../../utils/useId';

function getComputedStyles(element: HTMLElement) {
let cachedSupportsComputedStyleMap: boolean | undefined;

function supportsComputedStyleMap(element: HTMLElement) {
if (cachedSupportsComputedStyleMap === undefined) {
cachedSupportsComputedStyleMap = 'computedStyleMap' in element;
}
return cachedSupportsComputedStyleMap;
}

function getAnimationNameFromComputedStyles(element: HTMLElement) {
if (supportsComputedStyleMap(element)) {
const styleMap = element.computedStyleMap();
const animationName = styleMap.get('animation-name');
return (animationName as CSSKeywordValue)?.value ?? undefined;
}

const containerWindow = ownerWindow(element);
return containerWindow.getComputedStyle(element);
const computedStyles = containerWindow.getComputedStyle(element);
return computedStyles.animationName;
}

let cachedSupportsHiddenUntilFound: boolean | undefined;
Expand Down Expand Up @@ -80,9 +96,9 @@ export function useCollapsibleContent(

contentElementRef.current = element;

const computedStyles = getComputedStyles(element);
const computedAnimationName = getAnimationNameFromComputedStyles(element);

latestAnimationNameRef.current = computedStyles.animationName ?? 'none';
latestAnimationNameRef.current = computedAnimationName ?? 'none';
originalTransitionDurationStyleRef.current = element.style.transitionDuration;
});

Expand Down

0 comments on commit ddc2547

Please sign in to comment.