# code-block-pro/1.5.0/src/hooks/useTheme.ts

Code Block Pro – Beautiful Syntax Highlighting, version 1.5.0. 33 lines.

- Page: https://pluginprobe.com/plugins/code-block-pro/1.5.0/code/src/hooks/useTheme.ts
- Raw: https://pluginprobe.com/plugins/code-block-pro/1.5.0/raw/src/hooks/useTheme.ts
- Modified: 2022-08-22T22:25:46+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/code-block-pro/1.5.0/code/src/hooks/useTheme.ts#L10-L20`.

```typescript
import { applyFilters } from '@wordpress/hooks';
import { __ } from '@wordpress/i18n';
import { getHighlighter, Lang, setCDN, Theme } from 'shiki';
import useSWRImmutable from 'swr/immutable';

type Params = { theme: Theme; lang: Lang; ready?: boolean };

const fetcher = ({ theme, lang, ready }: Params) => {
    if (!ready) throw new Error(__('Loading...', 'code-block-pro'));
    const themeFiltered = applyFilters(
        'blocks.codeBlockPro.theme',
        theme,
    ) as Theme;
    return getHighlighter({ langs: [lang], theme: themeFiltered });
};
let once = false;

export const useTheme = ({ theme, lang, ready = true }: Params) => {
    if (!once) {
        once = true;
        const assetDir = window.codeBlockPro?.pluginUrl + '/build/shiki/';
        setCDN(
            applyFilters('blocks.codeBlockPro.assetDir', assetDir) as string,
        );
    }
    const { data: highlighter, error } = useSWRImmutable(
        { theme, lang, ready },
        fetcher,
    );

    return { highlighter, error, loading: !highlighter && !error };
};

```
