Skip to content

useTheme composable

The useTheme composable provides functions to configure the theme.

You can use the useTheme composable to configure the theme in your .vitepress/theme/index.js file, or in any .md page/file.

ts
import { useTheme, locales } from 'vitepress-openapi'

export default {
    async enhanceApp({app, router, siteData}) {
        const themeConfig = useTheme()
        
        // Set the default schema view.
        themeConfig.setSchemaDefaultView('schema') // schema or contentType
        
        // Set the JSON viewer depth.
        themeConfig.setJsonViewerDeep(Infinity)
        
        // Set the schema viewer depth.
        themeConfig.setSchemaViewerDeep(Infinity)
        
        // Set the heading levels.
        themeConfig.setHeadingLevels({
            h1: 1,
            h2: 2,
            h3: 3,
            h4: 4,
            h5: 5,
            h6: 6,
        })
        
        // Set the response code selector.
        themeConfig.setResponseCodeSelector('tabs') // tabs or select
        
        // Set the maximum number of tabs, after which a Select will be shown.
        themeConfig.setResponseCodeMaxTabs(5)
        
        // Set the mode of the JSON editor.
        themeConfig.setPlaygroundJsonEditorMode('tree') // text, tree, or table
        
        // Set the visibility of the main menu bar.
        themeConfig.setPlaygroundJsonEditorMainMenuBar(false)
        
        // Set the visibility of the navigation bar.
        themeConfig.setPlaygroundJsonEditorNavigationBar(false)
        
        // Get the current operation badges.
        themeConfig.getOperationBadges() // ['deprecated']
        
        // Set the operation badges.
        themeConfig.setOperationBadges(['deprecated'])
        
        // Get the current i18n configuration.
        themeConfig.getI18nConfig() // { locale: 'en', fallbackLocale: 'en', messages: locales }
        
        // Set the i18n configuration.
        themeConfig.setI18nConfig({
            locale: 'en', // en or es
            fallbackLocale: 'en', // en or es
            messages: {
                en: {
                    ...locales.en,
                    'operation.badgePrefix.operationId': 'Operation ID',
                },
                es: {
                    ...locales.es,
                    'operation.badgePrefix.operationId': 'ID de operación',
                },
            },
        })
    }
}

Schema Configuration

FunctionDescriptionDefault ValueAllowed Values
setSchemaDefaultViewSets the default schema view.'schema''schema', 'contentType'
setShowBaseURLSets whether the base URL is shown.truetrue, false

JSON Viewer Configuration

FunctionDescriptionDefault ValueAllowed Values
setJsonViewerDeepSets the JSON viewer depth.Infinitynumber

Schema Viewer Configuration

FunctionDescriptionDefault ValueAllowed Values
setSchemaViewerDeepSets the schema viewer depth.Infinitynumber

Heading Levels Configuration

FunctionDescriptionDefault ValueAllowed Values
setHeadingLevelsSets the heading levels.{ h1: 1, h2: 2, h3: 3, h4: 4, h5: 5, h6: 6 }{ h1: number, h2: number, h3: number, h4: number, h5: number, h6: number }

Response Configuration

FunctionDescriptionDefault ValueAllowed Values
setResponseCodeSelectorSets the response code selector.'tabs''tabs', 'select'
setResponseCodeMaxTabsSets the maximum number of tabs, after which a Select will be shown.5number

Playground JSON Editor Configuration

FunctionDescriptionDefault ValueAllowed Values
setPlaygroundJsonEditorModeSets the mode of the JSON editor.'tree''text', 'tree', 'table'
setPlaygroundJsonEditorMainMenuBarSets the visibility of the main menu bar.falsetrue, false
setPlaygroundJsonEditorNavigationBarSets the visibility of the navigation bar.falsetrue, false

Operation Configuration

FunctionDescriptionDefault ValueAllowed Values
setOperationBadgesSets the operation badges. The order is respected.['deprecated']['deprecated', 'operationId']

I18n Configuration

FunctionDescriptionDefault ValueAllowed Values
setI18nConfigSets the i18n configuration.{ locale: 'en', fallbackLocale: 'en', messages: locales }{ locale: 'es' | 'en', fallbackLocale: 'es' | 'en', messages: Record<'es' | 'en', Record<string, Record<string, string>>> }