signal-desktop/ts/hooks/useUniqueId.ts
automated-signal 70319b317a
Pause story on mouse/space hold
Co-authored-by: Jamie Kyle <113370520+jamiebuilds-signal@users.noreply.github.com>
2024-04-15 13:45:54 -07:00

24 lines
674 B
TypeScript

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { useMemo, useState } from 'react';
import { v4 as uuid } from 'uuid';
export function useUniqueId(): string {
return useMemo(() => uuid(), []);
}
let nextElementId = 0;
export function useElementId(
namePrefix: string
): [id: string, selector: string] {
// Prefixed to avoid starting with a number (which is invalid in CSS selectors)
const [id] = useState(() => {
const currentId = nextElementId;
nextElementId += 1;
return `${namePrefix}-${currentId}`;
});
// Return the ID and a selector that can be used in CSS or JS
return [id, `#${id}`] as const;
}