/* * The MIT License (MIT) * * Copyright (c) 2015 - present Instructure, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import { useTheme } from './useTheme' import { getComponentThemeOverride } from './getComponentThemeOverride' import type { ComponentTheme } from '@instructure/shared-types' import type { Theme } from '@instructure/ui-themes' // returns the second parameter of a function type SecondParameter any> = Parameters[1] extends undefined ? never : Parameters[1] type GenerateComponentTheme = (theme: Theme) => ComponentTheme type UseStyleParamsWithTheme< P extends (componentTheme: any, params: any, theme: any) => any > = { generateStyle: P params?: SecondParameter

generateComponentTheme: GenerateComponentTheme componentId: string displayName?: string } // TODO this is only used by the old themes, remove when everything uses the new // theming system type UseStyleParamsWithoutTheme< P extends (componentTheme: any, params: any, theme: any) => any > = { generateStyle: P params?: SecondParameter

generateComponentTheme?: undefined componentId?: undefined displayName?: undefined } /* * This is only used by the **old themes**, remove when everything uses the new * theming system (InstUI v12) */ const useStyleLegacy = < P extends (componentTheme: any, params: any, theme: any) => any >( useStyleParams: UseStyleParamsWithTheme

| UseStyleParamsWithoutTheme

): ReturnType

=> { const { generateStyle, params, componentId, displayName } = useStyleParams const generateComponentTheme: GenerateComponentTheme = ( useStyleParams as UseStyleParamsWithTheme

)?.generateComponentTheme const theme = useTheme() const baseComponentTheme = typeof generateComponentTheme === 'function' ? generateComponentTheme(theme as Theme) : {} const themeOverride = getComponentThemeOverride( theme, displayName ?? componentId ?? '', componentId, params?.themeOverride, baseComponentTheme ) const componentTheme = { ...baseComponentTheme, ...themeOverride } return generateStyle(componentTheme, params ? params : {}, theme) } export default useStyleLegacy export { useStyleLegacy }