@@ -8,33 +8,21 @@ interface StyleEntry {
88const 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}
0 commit comments