2018-08-05 20:00:52 +03:00
|
|
|
# Advanced Usage
|
|
|
|
|
|
|
|
## Custom MDX components
|
|
|
|
|
2019-03-10 03:31:30 +03:00
|
|
|
MDX Deck includes default components for MDX, but to provide custom components to the [MDXProvider][], add a `components` object to the `theme`.
|
2018-08-05 20:00:52 +03:00
|
|
|
|
|
|
|
```js
|
|
|
|
// example theme
|
2018-08-05 20:19:42 +03:00
|
|
|
import Heading from './Heading'
|
2018-08-05 20:00:52 +03:00
|
|
|
|
|
|
|
export default {
|
2018-08-05 20:19:42 +03:00
|
|
|
components: {
|
2019-03-10 03:31:30 +03:00
|
|
|
h1: Heading,
|
|
|
|
},
|
2018-08-05 20:00:52 +03:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
See the [MDX][] docs for more or take a look
|
2019-03-10 03:31:30 +03:00
|
|
|
at the [default set of components](../packages/components/src/mdx-components.js) as a reference.
|
2018-08-05 20:00:52 +03:00
|
|
|
|
|
|
|
## Custom Provider component
|
|
|
|
|
2019-03-26 21:23:01 +03:00
|
|
|
A custom Provider component is useful for adding custom context providers in React or adding persistent UI elements to the entire deck.
|
|
|
|
|
2019-04-08 17:31:01 +03:00
|
|
|
To define a custom Provider component, you'll need to import it into your custom theme and set it using the key `Provider` like shown below:
|
2018-08-05 20:00:52 +03:00
|
|
|
|
2018-08-05 20:19:42 +03:00
|
|
|
```js
|
|
|
|
// example theme.js
|
|
|
|
import Provider from './Provider'
|
2018-08-05 20:00:52 +03:00
|
|
|
|
2018-08-05 20:19:42 +03:00
|
|
|
export default {
|
|
|
|
font: 'Georgia, serif',
|
2019-03-10 03:31:30 +03:00
|
|
|
Provider,
|
2018-08-05 20:19:42 +03:00
|
|
|
}
|
2018-08-05 20:00:52 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
A custom Provider component will receive the application's state as props,
|
|
|
|
which can be used to show custom page numbers or add other elements to the UI.
|
|
|
|
|
|
|
|
#### Props
|
|
|
|
|
2019-03-10 03:31:30 +03:00
|
|
|
- `slides`: (array) the components for each slide
|
2018-08-05 20:00:52 +03:00
|
|
|
- `index`: (number) the current slide index
|
2019-03-10 03:31:30 +03:00
|
|
|
- `mode`: (string) the current mode (one of `'normal'`, `'presenter'`, or `'overview'`)
|
|
|
|
- `step`: (number) the current visible step in an Appear or Step component
|
|
|
|
- Each slide includes a `meta` object with a `notes` field when the Notes component is used within a slide
|
2018-08-05 20:00:52 +03:00
|
|
|
|
2019-03-26 21:23:01 +03:00
|
|
|
#### Example
|
|
|
|
|
|
|
|
The example below will display the current slide out of the total amount of slides.
|
|
|
|
|
|
|
|
```js
|
2019-04-23 20:41:50 +03:00
|
|
|
// Example Provider.js
|
2019-04-08 17:31:01 +03:00
|
|
|
import React from 'react'
|
2019-03-26 21:23:01 +03:00
|
|
|
|
|
|
|
function AtTheBottomCenter ({ children }) {
|
2019-04-08 17:31:01 +03:00
|
|
|
const css = {
|
|
|
|
position: 'absolute',
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
bottom: 0,
|
2019-03-26 21:23:01 +03:00
|
|
|
color: #ffffff;
|
2019-04-08 17:31:01 +03:00
|
|
|
textAlign: 'center',
|
|
|
|
}
|
2019-03-26 21:23:01 +03:00
|
|
|
|
2019-04-23 20:41:50 +03:00
|
|
|
return <div css={css}>
|
|
|
|
{children}
|
2019-03-26 21:23:01 +03:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
|
|
|
|
export function Provider ({ children, ...props }) {
|
|
|
|
return <>
|
2019-04-23 20:41:50 +03:00
|
|
|
{children}
|
|
|
|
<AtTheBottomCenter>{props.index}/{props.slides.length}</AtTheBottomCenter>
|
2019-03-26 21:23:01 +03:00
|
|
|
</>
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-08-05 20:00:52 +03:00
|
|
|
## Combining multiple mdx files
|
|
|
|
|
2018-08-05 20:03:49 +03:00
|
|
|
Unlike the official `@mdx-js/loader`,
|
2019-03-10 03:31:30 +03:00
|
|
|
the `@mdx-deck/loader` exports an additional `slides` array of components instead of just the entire document.
|
2018-08-05 20:00:52 +03:00
|
|
|
Multiple MDX files can be combined into a single presentation if the filesize is getting difficult to manage.
|
|
|
|
|
2019-03-10 03:31:30 +03:00
|
|
|
First create a couple `.mdx` files like any other MDX Deck file, with `---` to separate the different slides.
|
2018-08-05 20:00:52 +03:00
|
|
|
|
|
|
|
```mdx
|
|
|
|
# one.mdx
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
This is the first file
|
|
|
|
```
|
|
|
|
|
|
|
|
```mdx
|
|
|
|
# two.mdx
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
This is the second file
|
|
|
|
```
|
|
|
|
|
|
|
|
Next, create a `.js` file to import and combine the two `.mdx` files.
|
|
|
|
|
|
|
|
```js
|
|
|
|
// deck.js
|
2019-03-27 21:04:36 +03:00
|
|
|
// if you want to include a theme, you would export here:
|
|
|
|
// export { dark as theme } from 'mdx-deck/themes';
|
|
|
|
|
2019-03-10 03:31:30 +03:00
|
|
|
import { slides as one } from './one.mdx'
|
|
|
|
import { slides as two } from './two.mdx'
|
2018-08-05 20:00:52 +03:00
|
|
|
|
2019-03-10 05:30:43 +03:00
|
|
|
export const slides = [...one, ...two]
|
2018-08-05 20:00:52 +03:00
|
|
|
```
|
|
|
|
|
2019-03-10 03:31:30 +03:00
|
|
|
Then, point the MDX Deck CLI comment in your `package.json` to the `deck.js` file.
|
2018-08-05 20:00:52 +03:00
|
|
|
|
|
|
|
```json
|
|
|
|
"scripts": {
|
|
|
|
"start": "mdx-deck deck.js"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-08-27 01:12:29 +03:00
|
|
|
## Custom webpack config
|
|
|
|
|
2019-03-10 03:31:30 +03:00
|
|
|
Webpack configuration files named `webpack.config.js` will automatically be merged with the built-in configuration,
|
|
|
|
using [webpack-merge](https://github.com/survivejs/webpack-merge).
|
|
|
|
To use a custom filename, pass the file path to the `--webpack` flag.
|
2018-08-27 01:12:29 +03:00
|
|
|
|
|
|
|
```js
|
|
|
|
// webpack.config.js example
|
|
|
|
module.exports = {
|
|
|
|
module: {
|
|
|
|
rules: [
|
2019-03-10 03:31:30 +03:00
|
|
|
{
|
|
|
|
test: /\.svg$/,
|
|
|
|
use: [{ loader: 'babel-loader' }, { loader: 'react-svg-loader' }],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2018-08-27 01:12:29 +03:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-03-10 03:31:30 +03:00
|
|
|
**Careful**: When overwriting the loader for `mdx` files, make sure to include the default loader from `@mdx-deck/loader`.
|
2018-08-27 04:44:34 +03:00
|
|
|
|
2019-04-21 03:45:59 +03:00
|
|
|
## Custom Components
|
2018-08-05 20:00:52 +03:00
|
|
|
|
2019-04-21 03:45:59 +03:00
|
|
|
To build custom components that hook into internal MDX Deck state, you might want to use the following APIs:
|
2018-08-05 20:00:52 +03:00
|
|
|
|
2019-04-21 03:45:59 +03:00
|
|
|
- [useSteps](api.md#usesteps-hook)
|
|
|
|
- [useDeck](api.md#usedeck-hook)
|
2018-08-05 20:00:52 +03:00
|
|
|
|
2019-03-10 03:31:30 +03:00
|
|
|
[mdx]: https://mdxjs.com
|
2019-03-10 20:29:02 +03:00
|
|
|
[mdxprovider]: https://github.com/mdx-js/mdx/blob/master/docs/getting-started/index.md#mdxprovider
|