Skip to content

Commit

Permalink
add prop for disabling global style injection
Browse files Browse the repository at this point in the history
  • Loading branch information
South-Paw committed Feb 1, 2021
1 parent 947accd commit 10e5500
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
5 changes: 1 addition & 4 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import React from 'react';
import { injectGlobalStyles, ThemeProvider } from '../src';
import { ThemeProvider } from '../src';

export const parameters = {
controls: { expanded: true },
layout: 'fullscreen',
};

const { GlobalStyle } = injectGlobalStyles();

const withThemeProvider = (Story) => (
<ThemeProvider>
<GlobalStyle />
<Story />
</ThemeProvider>
);
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ Install Spicy UI to your React project with `npm i @spicy-ui/core styled-compone

```js
import React from 'react';
import { Button } from '@spciy-ui/core';
import { Button, ThemeProvider } from '@spciy-ui/core';

export const App = () => <Button>Hey hot stuff 🌶️</Button>;
export const App = () => (
<ThemeProvider>
<Button>Hey hot stuff 🌶️</Button>
</ThemeProvider>
);
```

See the [documentation](https://spicy-ui.netlify.app/) for components, theming and advanced usage.
Expand Down
11 changes: 10 additions & 1 deletion src/components/Theme/ThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@ import deepmerge from 'deepmerge';
import * as React from 'react';
import { ThemeProvider as StyledThemeProvider } from 'styled-components';
import { theme as defaultTheme } from '../../theme';
import { injectGlobalStyles } from './injectGlobalStyles';

const { GlobalStyle } = injectGlobalStyles();

export interface ThemeProps {
/**
* If you want to extend the global styles set to `true` and inject them
* manually via `injectGlobalStyles`.
*/
disableInjection?: boolean;
/** Custom theme to be merged with the default theme. */
theme?: any;
}

export const ThemeProvider: React.FC<ThemeProps> = ({ children, theme = {} }) => {
export const ThemeProvider: React.FC<ThemeProps> = ({ children, disableInjection, theme = {} }) => {
const mergedTheme = React.useMemo(() => deepmerge(defaultTheme, theme), [theme]);

return (
<StyledThemeProvider theme={mergedTheme}>
{!disableInjection && <GlobalStyle />}
<>{children}</>
</StyledThemeProvider>
);
Expand Down

0 comments on commit 10e5500

Please sign in to comment.