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

StyleProvider: Convert component to TypeScript #42541

Merged
merged 4 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- `BoxControl`: Refactor away from `_.isEmpty()` ([#42468](https://github.com/WordPress/gutenberg/pull/42468)).
- `RadioControl`: Refactor away from `_.isEmpty()` ([#42468](https://github.com/WordPress/gutenberg/pull/42468)).
- `SelectControl`: Refactor away from `_.isEmpty()` ([#42468](https://github.com/WordPress/gutenberg/pull/42468)).
- `StyleProvider`: Convert to TypeScript ([#42541](https://github.com/WordPress/gutenberg/pull/42541)).

## 19.15.0 (2022-07-13)

Expand Down
31 changes: 0 additions & 31 deletions packages/components/src/style-provider/index.js

This file was deleted.

41 changes: 41 additions & 0 deletions packages/components/src/style-provider/index.tsx
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;
15 changes: 15 additions & 0 deletions packages/components/src/style-provider/types.ts
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;
};