Skip to content

IdentityService

IdentityService centralizes the generation of unique identifiers. Components use it to avoid collisions for id attributes and data-testid hooks, but you can reuse the helpers in your own app code.

Internally for component ids the service uses useId from vue, for data-test-id crypto.randomUUID.

API

MemberDescription
IdentityService.instanceReturns the singleton instance.
getComponentId()Returns a new component id generated by useId
getDataTestId(descriptor?: string)Returns a token in the format of test-{descriptor}-{uuid} test hooks.

In components

vue
<script lang="ts" setup>
	import { IdentityService } from '@mrksbnc/bamboo'

	const props = withDefaults(defineProps<{ id?: string; dataTestId?: string }>(), {
		id: IdentityService.instance.getComponentId(),
		dataTestId: IdentityService.instance.getDataTestId('bo-component'),
	})
</script>