Skip to content

Commit

Permalink
StyleProvider: Convert component to TypeScript (#42541)
Browse files Browse the repository at this point in the history
* StyleProvider: Convert component to TypeScript

* Update CHANGELOG.md
  • Loading branch information
Petter Walbø Johnsgård authored Jul 28, 2022
1 parent 0d47020 commit e3136a4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 31 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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)).
- `ComboboxControl`: Replace `keyboardEvent.keyCode` with `keyboardEvent.code`([#42569](https://github.com/WordPress/gutenberg/pull/42569)).
- `ComboboxControl`: Add support for uncontrolled mode ([#42752](https://github.com/WordPress/gutenberg/pull/42752)).

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;
};

0 comments on commit e3136a4

Please sign in to comment.