/* eslint-disable @typescript-eslint/no-unused-vars */ import React, { createContext, PropsWithChildren, useContext, useMemo } from 'react'; /** * */ export interface ColorPrimitive { /** * The color */ color: string; /** * Color for content such as icons, text displayed above this color. */ content: string; } /** * */ type ColorPrimitivesDefinitionValidator = Candidate extends Record< infer U, ColorPrimitive | string > ? Candidate : never; /** * */ type ColorRolesDefinitionValidator = Candidate extends Record< infer U, any > ? Candidate : never; /** * */ export default function createColorSystem() { const colorPrimitivesContext = createContext< ColorPrimitivesDefinitionValidator >({} as any); function useColorPrimitives() { return useContext(colorPrimitivesContext); } const ColorPrimitivesProvider = colorPrimitivesContext.Provider; // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore ColorPrimitivesProvider.displayName = ColorPrimitivesProvider; function createColorRoles() { const colorRolesContext = createContext>( {} as any ); function useColorRoles() { return useContext(colorRolesContext); } function ColorRolesProvider({ children, mapPrimitivesToColorRoles }: PropsWithChildren<{ mapPrimitivesToColorRoles: ( primitives: ColorPrimitivesDefinitionValidator, parentColorRoles: ColorRolesDefinitionValidator ) => ColorRolesDefinitionValidator; }>) { const colorPrimitives = useContext(colorPrimitivesContext); const parentColorRoles = useColorRoles(); const value = useMemo( () => mapPrimitivesToColorRoles(colorPrimitives, parentColorRoles), [colorPrimitives, mapPrimitivesToColorRoles, parentColorRoles] ); return ( {children} ); } return { useColorRoles, ColorRolesProvider }; } return { createColorRoles, useColorPrimitives, ColorPrimitivesProvider }; }