Files
portainer/app/react/components/Widget/Widget.tsx
T
Cara Ryan 6a30138b3c feat(home): environment home page ui improvements to highlight groups [C9S-23] (#2487)
Signed-off-by: Bernard Setz <bernard.setz@portainer.io>
Co-authored-by: bernard-portainer <bernard.setz@portainer.io>
Co-authored-by: Ali <83188384+testA113@users.noreply.github.com>
Co-authored-by: Yajith Dayarathna <yajith.dayarathna@portainer.io>
Co-authored-by: Chaim Lev-Ari <chiptus@users.noreply.github.com>
Co-authored-by: andres-portainer <91705312+andres-portainer@users.noreply.github.com>
Co-authored-by: Josiah Clumont <josiah.clumont@portainer.io>
Co-authored-by: Dakota Walsh <101994734+dakota-portainer@users.noreply.github.com>
2026-04-29 14:59:39 +12:00

67 lines
1.4 KiB
TypeScript

import clsx from 'clsx';
import {
createContext,
PropsWithChildren,
Ref,
useContext,
useMemo,
useState,
} from 'react';
interface WidgetContextValue {
titleId: string | undefined;
}
const Context = createContext<null | WidgetContextValue>(null);
Context.displayName = 'WidgetContext';
// Simple ID generator for React 17 compatibility
let idCounter = 0;
function generateId() {
return `widget-title-${++idCounter}`;
}
export function useWidgetContext() {
const context = useContext(Context);
if (context == null) {
throw new Error('Should be inside a Widget component');
}
return context;
}
export function Widget({
children,
className,
mRef,
id,
'aria-label': ariaLabel,
'data-cy': dataCy,
}: PropsWithChildren<{
className?: string;
mRef?: Ref<HTMLDivElement>;
id?: string;
'aria-label'?: string;
'data-cy'?: string;
}>) {
// Only generate titleId once on mount if aria-label is not provided
const [titleId] = useState(() => (ariaLabel ? undefined : generateId()));
const contextValue = useMemo(() => ({ titleId }), [titleId]);
return (
<Context.Provider value={contextValue}>
<section
id={id}
className={clsx('widget', className)}
ref={mRef}
aria-label={ariaLabel}
aria-labelledby={titleId}
data-cy={dataCy}
>
{children}
</section>
</Context.Provider>
);
}