| 1 |
import { useSSRSafeId } from '../useSSRSafeId'; |
| 2 |
|
| 3 |
const ouiaPrefix = 'OUIA-Generated-'; |
| 4 |
|
| 5 |
export interface SSRSafeIdsProps { |
| 6 |
/** Prefix for the generated component ID */ |
| 7 |
prefix: string; |
| 8 |
/** OUIA component type. If provided, a stable OUIA ID will be generated. */ |
| 9 |
ouiaComponentType?: string; |
| 10 |
/** Children render function receiving (generatedId, generatedOuiaId?) */ |
| 11 |
children: (id: string, ouiaId?: string) => React.ReactNode; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Provides SSR/Strict-Mode-safe IDs for class components via render props. |
| 16 |
* Uses useSSRSafeId() (React.useId) for both component ID and OUIA ID, |
| 17 |
* ensuring consistent values between server and client rendering. |
| 18 |
*/ |
| 19 |
export const SSRSafeIds = ({ prefix, ouiaComponentType, children }: SSRSafeIdsProps) => { |
| 20 |
const id = useSSRSafeId(prefix); |
| 21 |
const ouiaId = useSSRSafeId(ouiaComponentType ? `${ouiaPrefix}${ouiaComponentType}-` : 'pf-unused-'); |
| 22 |
return (children(id, ouiaComponentType ? ouiaId : undefined) as React.ReactElement) ?? null; |
| 23 |
}; |
| 24 |
SSRSafeIds.displayName = 'SSRSafeIds'; |