Skip to content

Commit

Permalink
feat(IconProvider): add supprt for disableInlineStyles (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonguo authored Oct 16, 2024
1 parent d3570b4 commit 909c7f1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/IconProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export interface IconContextProps {
*/
nonce?: string;
};

/**
* Disable inline styles
* @default false
*/
disableInlineStyles?: boolean;
}

export const IconContext = createContext<IconContextProps>({});
Expand Down
8 changes: 6 additions & 2 deletions src/utils/useIconContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { useContext } from 'react';
import { IconContext } from '../IconProvider';

export function useIconContext() {
const { classPrefix = 'rs-', csp } = useContext(IconContext) || {};
const { classPrefix = 'rs-', csp, disableInlineStyles = false } = useContext(IconContext) || {};

return { classPrefix, csp };
return {
classPrefix,
csp,
disableInlineStyles
};
}
4 changes: 2 additions & 2 deletions src/utils/useInsertStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ const getStyles = (prefix = 'rs-') => {
let cssInjected = false;

const useInsertStyles = () => {
const { csp, classPrefix } = useIconContext();
const { csp, classPrefix, disableInlineStyles } = useIconContext();
useEffect(() => {
// Make sure css injected once.
if (!cssInjected) {
if (!cssInjected && !disableInlineStyles) {
insertCss(getStyles(classPrefix), { prepend: true, nonce: csp?.nonce });
cssInjected = true;
}
Expand Down
14 changes: 14 additions & 0 deletions test/IconProvider.cy.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import React from 'react';
import { Icon, IconProvider } from '../src';

it('Should be able to disable inline styles', () => {
cy.mount(
<IconProvider
value={{
disableInlineStyles: true
}}
>
<Icon />
</IconProvider>
);

cy.get('style').should('not.exist');
});

it('Should custom classPrefix and have csp', () => {
cy.mount(
<IconProvider
Expand Down

0 comments on commit 909c7f1

Please sign in to comment.