useId.js 717 B

123456789101112131415161718192021222324
  1. import { ref } from 'vue';
  2. import canUseDom from '../../_util/canUseDom';
  3. let uuid = 0;
  4. /** Is client side and not jsdom */
  5. export const isBrowserClient = process.env.NODE_ENV !== 'test' && canUseDom();
  6. /** Get unique id for accessibility usage */
  7. export function getUUID() {
  8. let retId;
  9. // Test never reach
  10. /* istanbul ignore if */
  11. if (isBrowserClient) {
  12. retId = uuid;
  13. uuid += 1;
  14. } else {
  15. retId = 'TEST_OR_SSR';
  16. }
  17. return retId;
  18. }
  19. export default function useId() {
  20. let id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ref('');
  21. // Inner id for accessibility usage. Only work in client side
  22. const innerId = `vc_unique_${getUUID()}`;
  23. return id.value || innerId;
  24. }