-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(emotion): add preflight component to emotion (#167)
- Loading branch information
1 parent
762aa1c
commit b56e6b6
Showing
5 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import * as React from 'react' | ||
import '@testing-library/jest-dom/extend-expect' | ||
import { render, cleanup } from '@testing-library/react' | ||
import { Preflight } from '.' | ||
|
||
afterEach(cleanup) | ||
|
||
describe('#Preflight', () => { | ||
it('renders as a null component', () => { | ||
const { container } = render(<Preflight />) | ||
|
||
expect(container.firstChild).toBeNull() | ||
}) | ||
|
||
it('outputs all styles', () => { | ||
render(<Preflight />) | ||
|
||
expect(document.querySelectorAll('style').length).toBeGreaterThan(0) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as React from 'react' | ||
import { CSSInterpolation } from '@emotion/serialize' | ||
import { Global, css } from '@emotion/react' | ||
import { createPreflight } from '@xstyled/system' | ||
import { useTheme } from './theme' | ||
|
||
function createGlobalStyle(...styles: Array<CSSInterpolation | Function>) { | ||
return function Preflight() { | ||
const theme = useTheme() | ||
|
||
// emotion does not support function interpolation so we do from our side | ||
// https://github.com/emotion-js/emotion/blob/%40emotion/serialize%401.0.0/packages/serialize/src/index.js#L189 | ||
const parsedStyles = styles.map((style) => { | ||
if (typeof style === 'function') { | ||
return style({ theme }) | ||
} | ||
|
||
return style | ||
}) | ||
|
||
return <Global styles={css(parsedStyles)} /> | ||
} | ||
} | ||
|
||
export const Preflight = createPreflight({ createGlobalStyle }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters