In order to customize the theme, dumi provides a set of theme API, we can import the following from dumi/theme
.
You can get the configuration items of dumi, meta information of the current routing, international language options, etc.
The detailed definition of context can be View source code.
The packaged umi Link
can render external links and automatically add external link icons.
The packaged umi NavLink
can render external links and automatically add external link icons.
The packaged umi NavLink
is used for links with anchor points and can be highlighted.
Object
. The props received by the theme Previewer
component.String
. The api that CodeSandbox calls when creating the demo, the default value is https://codesandbox.io/api/v1/sandboxes/define
.Function
. Open the execution function of the demo in CodeSandbox.ioGenerate a function based on the props of Previewer
, and open the demo in codesandbox.io after execution, for example:
// builtins/Previewer.tsximport React from 'react';import { useCodeSandbox } from 'dumi/theme';export default props => {const openCSB = useCodeSandbox(props);return <button onClick={openCSB}>Click will open the demo on CodeSandbox.io</button>;};
Function
. Copy the execution function, the text passed in during execution will be copied to the clipboard'ready' |'copied'
. The default value is ready
, it will become copied
after the copy is executed, and then return to ready
after 2s, which is convenient for developers to control the prompt message of successful copyProvide copy function and copy status to facilitate source code copy and status display, for example:
// builtins/Previewer.tsximport React from 'react';import { useCopy } from 'dumi/theme';export default props => {const [copyCode, copyStatus] = useCopy();return <button onClick={() => copyCode('Hello')}>Click will copy text</button>;};
String
. The keyword of the current input boxFunction
. If the user opens algolia, it will return to the binding function of algolia, and just pass in the CSS selector of the input box, and all subsequent filtering and rendering work will be handed over to algoliaArray
. If the user does not open algolia, it will return the keyword-based built-in search results, currently only the title can be searchedAccording to the configuration, automatically provide algolia's binding function or return the search results of the built-in search according to the keyword.
For specific usage, please refer to the SearchBar component of dumi built-in themes.
String
. Current locale valueObject
. Props that need to be filtered and convertedObject
. Props after filtering and transformationAutomatically filter and convert props according to locale to facilitate the realization of the internationalization of FrontMatter definitions. For example, title.zh-CN
will be converted to title
in Chinese language.
For specific usage, please refer to the Previewer component of dumi built-in themes.
String
. The identifier
parameter received by the theme Previewer
component, the unique identifier of the demoString
. The address of the demo page opened separatelyGet the URL of the page that opened the demo separately. For example, useDemoUrl(props.identifier)
will return a URL similar to http://example.com/~demos/demo-id
.
String
. The identifier
parameter received by the subject API
component, the unique identifier of the APIArray
. Props property listTo get the API metadata of the specified component, please refer to the API component implementation of the dumi default theme.
String
. Current language optionsString
. The TSX code to be converted in TypeScript PlaygroundString
. The url go to TypeScript PlaygroundGet the link to the Playground of the current TypeScript official website to submit the TSX code to the Playground to display the JSX code.
'light' | 'dark' | 'auto'
. Current prefers color(color: 'light' | 'dark' | 'auto') => void
. A function to change current prefers color for site, the prefers color will follow OS preferences if set auto
This API will be useful if we want to implement dark/light mode for our theme.
For theme developer:
[data-prefers-color=dark]
CSS attribute selector, for example:.navbar { /* light styles */ }[data-prefers-color=dark] .navbar { /* dark styles */ }// or.navbar {/* light styles */[data-prefers-color=dark] & {/* dark styles */}}
import React from 'react';import { usePrefersColor } from 'dumi/theme';export default props => {const [color, setColor] = usePrefersColor();return (<><button onClick={() => setColor('auto')}>Enable auto prefers color</button>Current prefers color: {color}</>);};
More informations in #543。