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

feat(IconProvider): add supprt for disableInlineStyles #13

Merged
merged 1 commit into from
Oct 16, 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
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
Loading