// Copyright 2025 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import type { RefCallback } from 'react'; import { useMemo, useState } from 'react'; import { createStrictContext, useStrictContext } from './StrictContext.dom.js'; type AriaLabellingContextType = Readonly<{ labelRef: RefCallback; descriptionRef: RefCallback; }>; const AriaLabellingContext = createStrictContext( 'AriaLabellingContext.Provider' ); export type CreateAriaLabellingContextResult = Readonly<{ context: AriaLabellingContextType; labelId: string | undefined; descriptionId: string | undefined; }>; export function useCreateAriaLabellingContext(): CreateAriaLabellingContextResult { const [labelId, setLabelId] = useState(); const [descriptionId, setDescriptionId] = useState(); const context = useMemo((): AriaLabellingContextType => { function labelRef(element: HTMLElement | null) { setLabelId(element?.id); } function descriptionRef(element: HTMLElement | null) { setDescriptionId(element?.id); } return { labelRef, descriptionRef }; }, []); return { context, labelId, descriptionId }; } export const AriaLabellingProvider = AriaLabellingContext.Provider; export function useAriaLabellingContext( providerName: string ): AriaLabellingContextType { return useStrictContext( AriaLabellingContext, `Must be wrapped with a <${providerName}>` ); }