Skip to content

Commit 88aba00

Browse files
committed
Simplify useSharedStyle API
1 parent f1541ce commit 88aba00

3 files changed

Lines changed: 15 additions & 37 deletions

File tree

‎storybook/decorators/utils/use-shared-style.ts‎

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,21 @@ interface StyleEntry {
88
const styleRefs = new Map< string, StyleEntry >();
99

1010
/**
11-
* Injects a `<style>` element into the document head, ref-counted by `key`.
11+
* Injects a `<style>` element into the document head, ref-counted so that
12+
* multiple consumers sharing the same CSS text use a single `<style>` element
13+
* (e.g. on the Docs tab where several stories render simultaneously). The
14+
* element is removed from the DOM when the last consumer unmounts.
1215
*
13-
* When multiple Storybook story instances need the same stylesheet (e.g. on
14-
* the Docs tab where several stories render simultaneously), this hook ensures
15-
* only a single `<style>` element is created. It is removed from the DOM when
16-
* the last consumer unmounts.
17-
*
18-
* @param options
19-
* @param options.key A unique identifier for the stylesheet. Callers with
20-
* the same key share one `<style>` element. Pass an
21-
* empty string to skip injection.
22-
* @param options.cssText The CSS text to inject. Pass an empty string to skip
23-
* injection.
16+
* @param cssText The CSS text to inject. Pass an empty string to skip
17+
* injection.
2418
*/
25-
export function useSharedStyle( {
26-
key,
27-
cssText,
28-
}: {
29-
key: string;
30-
cssText: string;
31-
} ): void {
19+
export function useSharedStyle( cssText: string ): void {
3220
useLayoutEffect( () => {
33-
if ( ! key || ! cssText ) {
21+
if ( ! cssText ) {
3422
return;
3523
}
3624

37-
let entry = styleRefs.get( key );
25+
let entry = styleRefs.get( cssText );
3826

3927
if ( entry ) {
4028
entry.refCount++;
@@ -43,15 +31,15 @@ export function useSharedStyle( {
4331
style.textContent = cssText;
4432
document.head.appendChild( style );
4533
entry = { element: style, refCount: 1 };
46-
styleRefs.set( key, entry );
34+
styleRefs.set( cssText, entry );
4735
}
4836

4937
return () => {
5038
entry.refCount--;
5139
if ( entry.refCount === 0 ) {
5240
entry.element.remove();
53-
styleRefs.delete( key );
41+
styleRefs.delete( cssText );
5442
}
5543
};
56-
}, [ key, cssText ] );
44+
}, [ cssText ] );
5745
}

‎storybook/decorators/with-global-css.jsx‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ export const WithGlobalCSS = ( Story, context ) => {
4242
const { lazyStyles, externalStyles, classes } =
4343
config[ context.globals.css ];
4444

45-
useSharedStyle( {
46-
key: 'global:' + context.globals.css,
47-
cssText: lazyStyles.join( '\n' ),
48-
} );
45+
useSharedStyle( lazyStyles.join( '\n' ) );
4946

5047
return (
5148
<div className={ clsx( classes ) }>

‎storybook/decorators/with-rtl.jsx‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,15 @@ export const WithRTL = ( Story, context ) => {
3737
return () => removeFilter( 'i18n.gettext_with_context', 'storybook' );
3838
}, [ context.globals.direction ] );
3939

40-
const matchedIndices = [];
4140
const stylesToUse = [];
4241

43-
CONFIG.forEach( ( item, index ) => {
42+
CONFIG.forEach( ( item ) => {
4443
if ( item.componentIdMatcher.test( context.componentId ) ) {
45-
matchedIndices.push( index );
4644
stylesToUse.push( ...item[ context.globals.direction ] );
4745
}
4846
} );
4947

50-
useSharedStyle( {
51-
key: matchedIndices.length
52-
? matchedIndices.join( ',' ) + ':' + context.globals.direction
53-
: '',
54-
cssText: stylesToUse.join( '\n' ),
55-
} );
48+
useSharedStyle( stylesToUse.join( '\n' ) );
5649

5750
return (
5851
<div ref={ ref } key={ rerenderKey }>

0 commit comments

Comments
 (0)