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

fix(useStylesheet): only remove styles when all components are unmounted #5616

Merged
merged 2 commits into from
Mar 18, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/base/src/context/StyleContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import { createContext, useContext } from 'react';

const SYMBOL = Symbol.for('@ui5/webcomponents-react/StyleContext');

const StyleContext = createContext<{ staticCssInjected: boolean }>({
staticCssInjected: false
interface StyleContextValue {
staticCssInjected: boolean;
componentsMap: Map<string, number>;
}

const StyleContext = createContext<StyleContextValue>({
staticCssInjected: false,
componentsMap: new Map<string, number>()
});

export function getStyleContext(): typeof StyleContext {
Expand Down
35 changes: 35 additions & 0 deletions packages/base/src/hooks/useStylesheet.cy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useReducer } from 'react';
import { ObjectStatus } from '@/packages/main/src';

interface CondRenderCompProps {
testid?: string;
}
const CondRenderComp = ({ testid }: CondRenderCompProps) => {
const [visible, toggle] = useReducer((prev) => !prev, true);
return (
<>
<button onClick={toggle} data-testid={`btn-${testid}`}>
Toggle
</button>
{visible && <ObjectStatus data-testid={`os-${testid}`}>Content</ObjectStatus>}
</>
);
};
describe('useStyleSheet', () => {
it('cleanup styles', () => {
cy.mount(
<>
<CondRenderComp testid="1" />
<CondRenderComp testid="2" />
</>
);
cy.findAllByText('Content').should('be.visible').and('have.length', 2);
cy.findAllByText('Object Status').should('not.be.visible');
cy.findAllByText('Object Status').should('have.length', 2);

cy.findByTestId('btn-1').click();
cy.findAllByText('Content').should('be.visible').and('have.length', 1);
cy.findAllByText('Object Status').should('not.be.visible');
cy.findAllByText('Object Status').should('have.length', 1);
});
});
23 changes: 21 additions & 2 deletions packages/base/src/hooks/useStylesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,37 @@ function getUseInsertionEffect(isSSR: boolean) {
return isSSR ? React.useEffect : Reflect.get(React, 'useInsertionEffect') || React.useLayoutEffect;
}

function trackComponentStyleMount(componentMap: Map<string, number>, componentName: string) {
if (componentMap.has(componentName)) {
componentMap.set(componentName, componentMap.get(componentName)! + 1);
} else {
componentMap.set(componentName, 1);
}
}

function trackComponentStyleUnmount(componentMap: Map<string, number>, componentName: string) {
if (componentMap.has(componentName)) {
componentMap.set(componentName, componentMap.get(componentName)! - 1);
}
}

export function useStylesheet(styles: StyleDataCSP, componentName: string) {
const styleContext = useStyleContext();
const { staticCssInjected } = styleContext;
const { staticCssInjected, componentsMap } = styleContext;

getUseInsertionEffect(typeof window === 'undefined')(() => {
if (!staticCssInjected) {
createOrUpdateStyle(styles, 'data-ui5wcr-component', componentName);
trackComponentStyleMount(componentsMap, componentName);
}

return () => {
if (!staticCssInjected) {
removeStyle('data-ui5wcr-component', componentName);
trackComponentStyleUnmount(componentsMap, componentName);
const numberOfMountedComponents = componentsMap.get(componentName);
if (typeof numberOfMountedComponents === 'number' && numberOfMountedComponents <= 0) {
removeStyle('data-ui5wcr-component', componentName);
}
}
};
}, [styles, staticCssInjected]);
Expand Down
3 changes: 2 additions & 1 deletion packages/main/src/components/ThemeProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ const ThemeProvider: FC<ThemeProviderPropTypes> = (props: ThemeProviderPropTypes
const StyleContext = getStyleContext();
const styleContextValue = useMemo(() => {
return {
staticCssInjected: staticCssInjected ?? false
staticCssInjected: staticCssInjected ?? false,
componentsMap: new Map()
};
}, [staticCssInjected]);

Expand Down
Loading