Skip to content

Commit 00933a6

Browse files
committed
Add more TSDocs
1 parent 26adb01 commit 00933a6

File tree

1 file changed

+53
-0
lines changed
  • packages/interactivity-router/src/assets

1 file changed

+53
-0
lines changed

packages/interactivity-router/src/assets/styles.ts

+53
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,31 @@ export function updateStylesWithSCS(
113113
return promises;
114114
}
115115

116+
/**
117+
* Cache of promises per style elements.
118+
*
119+
* Each style element has their own associated `Promise` that resolves
120+
* once the element has been loaded and is ready.
121+
*/
116122
const stylePromiseCache = new WeakMap<
117123
StyleElement,
118124
Promise< StyleElement >
119125
>();
120126

127+
/**
128+
* Prepare and return the corresponding `Promise` for the passed style
129+
* element.
130+
*
131+
* It returns the cached promise if it exists. Otherwise, constructs
132+
* a `Promise` that resolves once the element has finished loading.
133+
*
134+
* For those elements that are not in the DOM yet, this function
135+
* injects a `media="preload"` attribute to the passed element so the
136+
* style is loaded without applying any styles to the document.
137+
*
138+
* @param element Style element.
139+
* @return The associated `Promise` to the passed element.
140+
*/
121141
const prepareStylePromise = (
122142
element: StyleElement
123143
): Promise< StyleElement > => {
@@ -159,8 +179,32 @@ const prepareStylePromise = (
159179
return promise;
160180
};
161181

182+
/**
183+
* Cache of style promise lists per URL.
184+
*
185+
* It contains the list of style elements associated to the page with the
186+
* passed URL. The original order is preserved to respect the CSS cascade.
187+
*
188+
* Each included promise resolves when the associated style element is ready.
189+
*/
162190
const styleSheetCache = new Map< string, Promise< StyleElement >[] >();
163191

192+
/**
193+
* Prepare all style elements contained in the passed document.
194+
*
195+
* This function calls {@link updateStylesWithSCS|`updateStylesWithSCS`}
196+
* to insert only the minimum amount of style elements into the DOM, so
197+
* those present in the passed document end up in the DOM while the order
198+
* is respected.
199+
*
200+
* New appended style elements contain a `media=prefetch` attribute to
201+
* make them effectively disabled until they are applied with the
202+
* {@link applyStyles|`applyStyles`} function.
203+
*
204+
* @param doc Document instance.
205+
* @param url URL for the passed document.
206+
* @return A list of promises for each style element in the passed document.
207+
*/
164208
export const prepareStyles = (
165209
doc: Document,
166210
url: string = ( doc.location || window.location ).href
@@ -186,6 +230,15 @@ export const prepareStyles = (
186230
return styleSheetCache.get( url );
187231
};
188232

233+
/**
234+
* Traverse all style elements in the DOM, enabling only those included
235+
* in the passed list and disabling the others.
236+
*
237+
* If the style element has the `data-original-media` attribute, the
238+
* original `media` value is restored.
239+
*
240+
* @param styles List of style elements to apply
241+
*/
189242
export const applyStyles = ( styles: StyleElement[] ) => {
190243
window.document
191244
.querySelectorAll( 'style,link[rel=stylesheet]' )

0 commit comments

Comments
 (0)