import React, { createContext, useContext } from "react"; import { PartialTranslations, Translations } from "../utils/i18n/i18n.types"; import nb from "../utils/i18n/locales/nb"; type ProviderContextType = { rootElement?: HTMLElement | null; locale: Translations; translations?: PartialTranslations | PartialTranslations[]; }; export const ProviderContext = createContext({ locale: nb, }); export type ProviderProps = { children: React.ReactNode; /** * Global root-element to attach portals to. Used by Tooltip, Modal (optionally) and ActionMenu. */ rootElement?: HTMLElement | null; } & ( | { /** * Aksel locale * @default nb * @example * import { en } from "@navikt/ds-react/locales"; * * {app} * */ locale: Translations; /** * Use this if you need to override some of the default translations. * Can be a single object or an array of objects. * Must be used together with the `locale` prop. */ translations?: PartialTranslations | PartialTranslations[]; } | { locale?: never; translations?: never; } ); export const useProvider = () => useContext(ProviderContext); /** * Provides added context to the component tree. * * @see [📝 Documentation](https://aksel.nav.no/komponenter/core/provider) * @see 🏷️ {@link ProviderProps} * * @example * * {app} * */ export const Provider = ({ children, rootElement, locale, translations, }: ProviderProps) => { const parentContext = useProvider(); return ( {children} ); }; export default Provider;