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/client'

export default {
    async enhanceApp({app, router, siteData}) {
        useTheme({
            theme: {
                highlighterTheme: {
                    light: 'vitesse-light',
                    dark: 'vitesse-dark',
                },
            },
            path: {
                // Show the base URL in the path component.
                showBaseURL: false,
            },
            requestBody: {
                // Set the default schema view.
                defaultView: 'schema', // schema or contentType
            },
            jsonViewer: {
                // How many levels are expanded on load.
                deep: Infinity,
                // Set the JSON viewer renderer.
                renderer: 'vue-json-pretty', // vue-json-pretty or shiki
            },
            schemaViewer: {
                // How many levels are expanded on load.
                deep: 1,
            },
            // Set the heading levels.
            headingLevels: {
                h1: 1,
                h2: 2,
                h3: 3,
                h4: 4,
                h5: 5,
                h6: 6,
            },
            response: {
                // Set the response code selector.
                responseCodeSelector: 'tabs', // tabs or select
                // Set the maximum number of tabs, after which a Select will be shown.
                maxTabs: 5,
                body: {
                    // Set the default view.
                    defaultView: 'schema', // schema or contentType
                },
            },
            playground: {
                jsonEditor: {
                    // Set the mode of the JSON editor.
                    mode: 'tree', // text, tree, or table
                    // Set the visibility of the main menu bar.
                    mainMenuBar: false,
                    // Set the visibility of the navigation bar.
                    navigationBar: false,
                    // Set the visibility of the status bar.
                    statusBar: false,
                },
                examples: {
                    // Behavior for standard `example` / `examples` fields.
                    behavior: 'value', // placeholder, value, or ignore
                    // Behavior for `x-playground-example` extension field.
                    playgroundExampleBehavior: 'value', // placeholder, value, or ignore
                },
            },
            operation: {
                // Set the operation badges. The order is respected.
                badges: ['deprecated'],
                // Slots to render in the OAOperation component.
                slots: [
                    'header',
                    'path',
                    'description',
                    'security',
                    'parameters',
                    'request-body',
                    'responses',
                    'playground',
                    'code-samples',
                    'branding',
                    'footer',
                ],
                // Slots to hide in the OAOperation component.
                hiddenSlots: [],
                // Set the number of columns to use in the OAOperation component.
                cols: 2,
                // Set the default base URL.
                defaultBaseUrl: 'http://localhost',
                // Deprecated. Use `server.getServers` instead.
                getServers: ({ method, path, operation }) => Array<string>,
            },
            // Set the i18n configuration.
            i18n: {
                locale: 'en', // en | es | ja | pt-BR | string
                fallbackLocale: 'en', // en | es | ja | pt-BR | string
                messages: {
                    en: {
                        ...locales.en,
                        'operation.badgePrefix.operationId': 'Operation ID',
                    },
                    es: {
                        ...locales.es,
                        'operation.badgePrefix.operationId': 'ID de operación',
                    },
                },
                availableLocales: [
                    { code: 'en', label: 'English' },
                    { code: 'es', label: 'Español' },
                    { code: 'ja', label: 'Japanese' },
                    { code: 'pt-BR', label: 'Português (Brasil)' },
                    { code: 'zh', label: '中文' },
                ],
            },
            // Set spec configuration.
            spec: {
                groupByTags: true, // Group paths by tags.
                collapsePaths: false, // Collapse paths when grouping by tags.
                showPathsSummary: true, // Show a summary of the paths when grouping by tags.
                avoidCirculars: false, // Avoid circular references when parsing schemas.
                lazyRendering: false, // Lazy render Paths and Tags components.
                defaultTag: 'Default', // Default tag to use when a path has no tags.
                defaultTagDescription: '', // Description for the default tag.
                wrapExamples: true, // Wrap examples in a row or show them in a column.
                disableDownload: false, // Disable the download button in the info section.
            },
            server: {
                // Set a custom function to get servers.
                getServers: ({ method, path, operation }) => Array<string>,
                // Allow custom servers.
                allowCustomServer: true,
            },
            storage: {
                // Set the localStorage key prefix.
                prefix: '--oa',
                // Set to false to disable persisting auth values to localStorage.
                persistAuth: true,
            },
            markdown: {
                operationLink: {
                    // Link prefix to search for operations.
                    linkPrefix: '/operations/',
                    // Transform the href of operation links.
                    transformHref: (href) => {
                        return `/example${href}`
                    },
                },
                externalLinksNewTab: true,
                config: md => md,
            },
        })
    }
}

Utility Functions

General-purpose functions available from the composable.

FunctionDescription
isDarkReactive Ref<boolean> that reflects the current VitePress dark mode.
reset()Resets all theme configuration back to the default values.
getState()Returns a deep plain object (non-reactive) snapshot of the full config.
ts
const { isDark, reset, getState } = useTheme()

// Reactively read dark mode
console.log(isDark.value) // true | false

// Reset all config to defaults
reset()

// Read full config as a plain object
console.log(getState())

Theme / Highlighter Configuration

Configures the syntax highlighter (Shiki) theme for code blocks. You can set different themes for light and dark mode.

FunctionDescription
setHighlighterThemeSets the highlighter theme config.
getHighlighterThemeReturns the current theme config.

Config shape: { light?: ShikiTheme, dark?: ShikiTheme }. Shiki theme names (e.g. from @shikijs/themes) or theme objects are supported.

ts
useTheme({
  theme: {
    highlighterTheme: {
      light: 'vitesse-light',
      dark: 'vitesse-dark',
    },
  },
})

Path Configuration

FunctionDescriptionDefault ValueAllowed Values
setShowBaseURLSets whether the base URL is shown.falsetrue, false
getShowBaseURLGets whether the base URL is shown.

Request Body Configuration

FunctionDescriptionDefault ValueAllowed Values
setRequestBodyDefaultViewSets the default schema view.'contentType''schema', 'contentType'
getRequestBodyDefaultViewGets the default schema view.

JSON Viewer Configuration

FunctionDescriptionDefault ValueAllowed Values
setJsonViewerDeepSets the JSON viewer depth.Infinitynumber
getJsonViewerDeepGets the JSON viewer depth.
setJsonViewerRendererSets the JSON viewer renderer.'vue-json-pretty''vue-json-pretty', 'shiki'
getJsonViewerRendererGets the JSON viewer renderer.

Schema Viewer Configuration

FunctionDescriptionDefault ValueAllowed Values
setSchemaViewerDeepSets the schema viewer depth.1number
getSchemaViewerDeepGets the schema viewer depth.

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 }
getHeadingLevelsGets the full heading levels object.
getHeadingLevelGets the resolved tag name for a specific level.'h1' | 'h2' | ... | 'h6'

getHeadingLevel(level) accepts a key ('h1''h6') and returns the mapped HTML tag string (e.g. 'h2'). Throws if the configured value is out of range [1, 6].

ts
const { getHeadingLevel } = useTheme()
getHeadingLevel('h1') // → 'h1' (or whatever h1 is mapped to)

Response Configuration

FunctionDescriptionDefault ValueAllowed Values
setResponseCodeSelectorSets the response code selector.'tabs''tabs', 'select'
getResponseCodeSelectorGets the response code selector.
setResponseCodeMaxTabsSets the maximum number of tabs, after which a Select will be shown.5number
getResponseCodeMaxTabsGets the maximum number of tabs.
setResponseBodyDefaultViewSets the default view of the response body.'contentType''schema', 'contentType'
getResponseBodyDefaultViewGets the default view of the response body.

Playground JSON Editor Configuration

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

Playground Examples Configuration

Controls how example values from the OpenAPI spec are applied in the playground. Two sources are configured independently:

  • behavior — applies to standard example / examples fields.
  • playgroundExampleBehavior — applies to the x-playground-example extension field.

See Playground Examples for details on x-playground-example.

FunctionDescriptionDefault ValueAllowed Values
setPlaygroundExamplesBehaviorSets the behavior for standard example / examples values.'value''placeholder', 'value', 'ignore'
getPlaygroundExamplesBehaviorGets the current behavior for standard examples.
setPlaygroundXExampleBehaviorSets the behavior for x-playground-example values.'value''placeholder', 'value', 'ignore'
getPlaygroundXExampleBehaviorGets the current behavior for x-playground-example values.
  • placeholder — show as placeholder text only.
  • value — pre-fill the field with the example value.
  • ignore — do not use the example.
ts
useTheme({
  playground: {
    examples: {
      behavior: 'placeholder',           // for spec `example` fields
      playgroundExampleBehavior: 'value', // for spec `x-playground-example` fields
    },
  },
})

Security Configuration

Sets the default security scheme used when multiple schemes are available (e.g. in the playground).

FunctionDescriptionDefault ValueAllowed Values
setSecurityDefaultSchemeSets the default security scheme ID.nullstring | null
getSecurityDefaultSchemeGets the current default scheme.

Code Samples Configuration

Configures how code samples are generated and displayed (languages, generator, default headers).

FunctionDescription
setCodeSamplesConfigSets the full code samples config.
getCodeSamplesDefaultLangGets the default language for the code samples UI.
getCodeSamplesAvailableLanguagesGets the list of available languages.
getCodeSamplesGeneratorGets the custom generator function.
getCodeSamplesDefaultHeadersGets the default headers used when generating samples.

getCodeSamplesAvailableLanguages(filter?) accepts an optional string[] to filter languages by their lang identifier.

Config shape: { defaultLang?, availableLanguages?, generator?, defaultHeaders? }.

ts
useTheme({
  codeSamples: {
    defaultLang: 'curl',
    availableLanguages: [
      {
        lang: 'curl',
        label: 'cURL',
        target: 'shell',
        client: 'curl',
        highlighter: 'bash',
        icon: 'curl',
      },
    ],
    generator: async (langConfig, request) => { /* return generated code string */ },
    defaultHeaders: { 'X-Custom': 'value' },
  },
})

Each language in availableLanguages is a LanguageConfig object:

FieldDescriptionRequired
langUnique identifier and Shiki highlighter language (e.g. 'curl', 'javascript')
labelDisplay name shown in the UI
targetTarget language for @scalar/snippetz code generation (e.g. 'js', 'shell')
clientHTTP client for the target language (e.g. 'fetch', 'axios', 'curl')
iconIcon identifier for vitepress-plugin-group-icons
highlighterShiki language for syntax highlighting (defaults to 'plain')

Configures URL prefixes used for generated navigation links (tags and operations). Affects how links to tags and operations are built in the spec UI.

FunctionDescriptionDefault Value
setLinksPrefixesConfigSets { tags: string, operations: string }.
getLinksPrefixesConfigGets the current links prefixes config.
getTagsLinkPrefixGets the prefix used for tag links.'/tags/'
getOperationsLinkPrefixGets the prefix used for operation links.'/operations/'
ts
useTheme({
  linksPrefixes: {
    tags: '/tags/',
    operations: '/operations/',
  },
})

Operation Configuration

FunctionDescriptionDefault ValueAllowed Values
setOperationBadgesSets the operation badges. The order is respected.['deprecated']['deprecated', 'operationId']
getOperationBadgesGets the current operation badges.
setOperationSlotsSets which slots are rendered (order is respected).See Custom SlotsOperationSlot[]
getOperationSlotsGets the list of slots to render.
setOperationHiddenSlotsSets which slots are hidden (filtered out from render).[]OperationSlot[]
getOperationHiddenSlotsGets the list of hidden slots.
setOperationColsSets the operation layout columns (1 or 2).21 | 2
getOperationColsGets the current column layout.
setOperationDefaultBaseUrlSets the default base URL.'http://localhost'string
getOperationDefaultBaseUrlGets the default base URL.

I18n Configuration

FunctionDescriptionDefault ValueAllowed Values
setI18nConfigSets the i18n configuration.{ locale: 'en', fallbackLocale: 'en', messages: locales }{ locale: 'es' | 'en' | 'ja' | 'pt-BR' | string, fallbackLocale: 'es' | 'en' | 'ja' | 'pt-BR' | string, messages: Messages, availableLocales: AvailableLocale[] }
getI18nConfigGets the full i18n config.
getLocaleGets the active locale code.'en'

Spec Configuration

FunctionDescriptionDefault ValueAllowed Values
setSpecConfigSets the spec configuration.{ groupByTags: true, collapsePaths: false, showPathsSummary: true, avoidCirculars: false, lazyRendering: false, defaultTag: 'Default', wrapExamples: true, disableDownload: false }{ groupByTags: boolean, collapsePaths: boolean, showPathsSummary: boolean, avoidCirculars: boolean, lazyRendering: boolean, defaultTag: string, wrapExamples: boolean, disableDownload: boolean }
getSpecConfigGets the full spec config object.
getWrapExamplesGets whether examples are wrapped in a row layout.truetrue, false
getSpecDisableDownloadGets whether the download button in the info section is disabled.falsetrue, false

Server Configuration

FunctionDescriptionDefault ValueAllowed Values
setServerConfigSets the server configuration.{ getServers: null, allowCustomServer: false }{ getServers: ({ method, path, operation }) => string[], allowCustomServer: boolean }
getServerConfigGets the full server config object.
getOperationServersGets the servers function for an operation.null({ method, path, operation }) => string[]
getServerAllowCustomServerGets whether custom servers are allowed.falsetrue, false

Storage Configuration

FunctionDescriptionDefault ValueAllowed Values
setStorageConfigSets the storage configuration.
getStoragePrefixReturns the current localStorage key prefix.'--oa'string
getStoragePersistAuthReturns whether auth values are persisted to localStorage.truetrue, false

Markdown Configuration

FunctionDescriptionDefault ValueAllowed Values
setMarkdownConfigSets the markdown configuration.{ operationLink: { linkPrefix: '/operations/' }, externalLinksNewTab: false }{ operationLink: { linkPrefix: string, transformHref: (href: string) => string } | false, externalLinksNewTab: boolean, config: (md) => MarkdownIt }
getMarkdownConfigGets the markdown configuration.
getOperationLinkConfigGets the operation link configuration.{ linkPrefix: '/operations/' }{ linkPrefix: string, transformHref: (href: string) => string } | false
getExternalLinksNewTabGets whether external links open in new tab.falsetrue, false

Setting operationLink to false disables the operation link plugin entirely.

You can also customize the markdown renderer with the config callback:

ts
useTheme({
  markdown: {
    config: md => {
      // add custom markdown-it plugins
      md.use(myPlugin, myParams)
      return;

      // or return your own instance
      const myMd = (/* ... */)
      return myMd
    },
  },
})

md is an instance of markdown-it.