-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
StyleProvider: Convert component to TypeScript (#42541)
* StyleProvider: Convert component to TypeScript * Update CHANGELOG.md
- Loading branch information
Petter Walbø Johnsgård
authored
Jul 28, 2022
1 parent
0d47020
commit e3136a4
Showing
4 changed files
with
57 additions
and
31 deletions.
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 was deleted.
Oops, something went wrong.
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,41 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { CacheProvider } from '@emotion/react'; | ||
import createCache from '@emotion/cache'; | ||
import memoize from 'memize'; | ||
import * as uuid from 'uuid'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { StyleProviderProps } from './types'; | ||
|
||
const uuidCache = new Set(); | ||
|
||
const memoizedCreateCacheWithContainer = memoize( | ||
( container: HTMLElement ) => { | ||
// Emotion only accepts alphabetical and hyphenated keys so we just | ||
// strip the numbers from the UUID. It _should_ be fine. | ||
let key = uuid.v4().replace( /[0-9]/g, '' ); | ||
while ( uuidCache.has( key ) ) { | ||
key = uuid.v4().replace( /[0-9]/g, '' ); | ||
} | ||
uuidCache.add( key ); | ||
return createCache( { container, key } ); | ||
} | ||
); | ||
|
||
export function StyleProvider( props: StyleProviderProps ) { | ||
const { children, document } = props; | ||
|
||
if ( ! document ) { | ||
return null; | ||
} | ||
|
||
const cache = memoizedCreateCacheWithContainer( document.head ); | ||
|
||
return <CacheProvider value={ cache }>{ children }</CacheProvider>; | ||
} | ||
|
||
export default StyleProvider; |
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,15 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { ReactNode } from 'react'; | ||
|
||
export type StyleProviderProps = { | ||
/** | ||
* The children elements. | ||
*/ | ||
children: ReactNode; | ||
/** | ||
* Current document. | ||
*/ | ||
document: Document; | ||
}; |