43 lines
2.1 KiB
JavaScript
43 lines
2.1 KiB
JavaScript
"use strict";
|
|
'use client';
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.default = useId;
|
|
var React = _interopRequireWildcard(require("react"));
|
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
let globalId = 0;
|
|
function useGlobalId(idOverride) {
|
|
const [defaultId, setDefaultId] = React.useState(idOverride);
|
|
const id = idOverride || defaultId;
|
|
React.useEffect(() => {
|
|
if (defaultId == null) {
|
|
// Fallback to this default id when possible.
|
|
// Use the incrementing value for client-side rendering only.
|
|
// We can't use it server-side.
|
|
// If you want to use random values please consider the Birthday Problem: https://en.wikipedia.org/wiki/Birthday_problem
|
|
globalId += 1;
|
|
setDefaultId(`mui-${globalId}`);
|
|
}
|
|
}, [defaultId]);
|
|
return id;
|
|
}
|
|
|
|
// downstream bundlers may remove unnecessary concatenation, but won't remove toString call -- Workaround for https://github.com/webpack/webpack/issues/14814
|
|
const maybeReactUseId = React['useId'.toString()];
|
|
/**
|
|
*
|
|
* @example <div id={useId()} />
|
|
* @param idOverride
|
|
* @returns {string}
|
|
*/
|
|
function useId(idOverride) {
|
|
if (maybeReactUseId !== undefined) {
|
|
const reactId = maybeReactUseId();
|
|
return idOverride != null ? idOverride : reactId;
|
|
}
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks -- `React.useId` is invariant at runtime.
|
|
return useGlobalId(idOverride);
|
|
} |