UNPKG

1.24 kBJavaScriptView Raw
1import { getWindow } from './dom/getWindow';
2import { Stylesheet } from '@uifabric/merge-styles';
3// Initialize global window id.
4var CURRENT_ID_PROPERTY = '__currentId__';
5var DEFAULT_ID_STRING = 'id__';
6// eslint-disable-next-line @typescript-eslint/no-explicit-any
7var _global = getWindow() || {};
8if (_global[CURRENT_ID_PROPERTY] === undefined) {
9 _global[CURRENT_ID_PROPERTY] = 0;
10}
11var _initializedStylesheetResets = false;
12/**
13 * Generates a unique id in the global scope (this spans across duplicate copies of the same library.)
14 *
15 * @public
16 */
17export function getId(prefix) {
18 if (!_initializedStylesheetResets) {
19 // Configure ids to reset on stylesheet resets.
20 var stylesheet = Stylesheet.getInstance();
21 if (stylesheet && stylesheet.onReset) {
22 stylesheet.onReset(resetIds);
23 }
24 _initializedStylesheetResets = true;
25 }
26 var index = _global[CURRENT_ID_PROPERTY]++;
27 return (prefix === undefined ? DEFAULT_ID_STRING : prefix) + index;
28}
29/**
30 * Resets id counter to an (optional) number.
31 *
32 * @public
33 */
34export function resetIds(counter) {
35 if (counter === void 0) { counter = 0; }
36 _global[CURRENT_ID_PROPERTY] = counter;
37}
38//# sourceMappingURL=getId.js.map
\No newline at end of file