{
if (!search) return
navigate(pathname)
}
- }, [index, mode])
+ }, [mode])
return false
}
diff --git a/packages/components/src/Slide.js b/packages/components/src/Slide.js
index d6f2707..defa476 100644
--- a/packages/components/src/Slide.js
+++ b/packages/components/src/Slide.js
@@ -72,7 +72,6 @@ export const Slide = ({ index, context, ...props }) => (
value={{
index,
...context,
- ...props,
}}
>
@@ -80,7 +79,9 @@ export const Slide = ({ index, context, ...props }) => (
)
Slide.defaultProps = {
- step: Infinity,
+ context: {
+ step: Infinity,
+ },
}
export default Slide
diff --git a/packages/components/src/Steps.js b/packages/components/src/Steps.js
index a774363..75c9012 100644
--- a/packages/components/src/Steps.js
+++ b/packages/components/src/Steps.js
@@ -1,21 +1,10 @@
import React from 'react'
-import { withContext } from './context'
+import { useDeck } from './context'
+import useSteps from './useSteps'
-export const Steps = withContext(
- class extends React.Component {
- constructor(props) {
- super(props)
- const { register, index } = props.context
- const { length } = props
- if (typeof register !== 'function') return
- register(index, { steps: length })
- }
- render() {
- const { context, render } = this.props
- const { step } = context
- return render({ step })
- }
- }
-)
+export const Steps = props => {
+ const step = useSteps(props.length)
+ return props.render({ step })
+}
export default Steps
diff --git a/packages/components/src/Storage.js b/packages/components/src/Storage.js
index 79dad4a..f7a5d13 100644
--- a/packages/components/src/Storage.js
+++ b/packages/components/src/Storage.js
@@ -1,15 +1,27 @@
-import { useEffect } from 'react'
+import { useEffect, useState } from 'react'
const STORAGE_INDEX = 'mdx-slide'
const STORAGE_STEP = 'mdx-step'
export const useLocalStorage = (handler, args = []) => {
+ const [focused, setFocused] = useState(false)
+ const handleFocus = () => {
+ setFocused(true)
+ }
+ const handleBlur = () => {
+ setFocused(false)
+ }
useEffect(() => {
- window.addEventListener('storage', handler)
+ setFocused(document.hasFocus())
+ if (!focused) window.addEventListener('storage', handler)
+ window.addEventListener('focus', handleFocus)
+ window.addEventListener('blur', handleBlur)
return () => {
- window.removeEventListener('storage', handler)
+ if (!focused) window.removeEventListener('storage', handler)
+ window.removeEventListener('focus', handleFocus)
+ window.removeEventListener('blur', handleBlur)
}
- }, [...args])
+ }, [focused, ...args])
}
export const useSetStorage = (key, value) => {
diff --git a/packages/components/src/Style.js b/packages/components/src/Style.js
new file mode 100644
index 0000000..8aabc61
--- /dev/null
+++ b/packages/components/src/Style.js
@@ -0,0 +1,24 @@
+import React from 'react'
+import { globalHistory } from '@reach/router'
+import { Global } from '@emotion/core'
+
+const isPrintPath = () => {
+ const { pathname } = globalHistory.location
+ const parts = pathname.split('/')
+ const path = parts[parts.length - 1]
+ return path === 'print'
+}
+
+export default ({ mode, modes }) => {
+ if (mode === modes.PRINT) return false
+ if (isPrintPath()) return false
+ return (
+
+ )
+}
diff --git a/packages/components/src/__tests__/MDXDeck.js b/packages/components/src/__tests__/MDXDeck.js
new file mode 100644
index 0000000..7546567
--- /dev/null
+++ b/packages/components/src/__tests__/MDXDeck.js
@@ -0,0 +1,35 @@
+import React, { useContext } from 'react'
+import { render, cleanup } from 'react-testing-library'
+import renderer from 'react-test-renderer'
+import { MDXDeckState, MDXDeckContext, MDXDeck } from '../MDXDeck'
+
+afterEach(cleanup)
+
+const slides = [() =>
one
, () =>
two
]
+
+describe('MDXDeckState', () => {
+ test('provides state', () => {
+ let context
+ const Consumer = props => {
+ context = useContext(MDXDeckContext)
+ return false
+ }
+ const deck = renderer.create(
+
+
+
+ )
+ expect(typeof context.state).toBe('object')
+ expect(typeof context.state.metadata).toBe('object')
+ expect(context.state.step).toBe(0)
+ expect(context.state.mode).toBe('normal')
+ expect(typeof context.setState).toBe('function')
+ })
+
+ test.todo('setState updates state')
+})
+
+test('renders', () => {
+ const json = renderer.create(
).toJSON()
+ expect(json).toMatchSnapshot()
+})
diff --git a/packages/components/src/__tests__/__snapshots__/MDXDeck.js.snap b/packages/components/src/__tests__/__snapshots__/MDXDeck.js.snap
new file mode 100644
index 0000000..c6516c3
--- /dev/null
+++ b/packages/components/src/__tests__/__snapshots__/MDXDeck.js.snap
@@ -0,0 +1,33 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`renders 1`] = `
+
+`;
diff --git a/packages/components/src/context.js b/packages/components/src/context.js
index 23d2092..069fa7a 100644
--- a/packages/components/src/context.js
+++ b/packages/components/src/context.js
@@ -1,11 +1,11 @@
+// slide context
import React, { useContext } from 'react'
export const Context = React.createContext({})
-export const withContext = Component => props => (
-
}
- />
-)
-
export const useDeck = () => useContext(Context)
+
+export const withContext = Component => props => {
+ const context = useDeck()
+ return
+}
diff --git a/packages/components/src/index.js b/packages/components/src/index.js
index 5ba5051..c8fece8 100644
--- a/packages/components/src/index.js
+++ b/packages/components/src/index.js
@@ -1,4 +1,4 @@
-export { MDXDeck } from './MDXDeck'
+export { MDXDeck, MDXDeckState } from './MDXDeck'
export { Head } from './Head'
export { Image } from './Image'
export { Notes } from './Notes'
diff --git a/packages/components/src/useSteps.js b/packages/components/src/useSteps.js
index 4b36791..c3cfb07 100644
--- a/packages/components/src/useSteps.js
+++ b/packages/components/src/useSteps.js
@@ -1,10 +1,10 @@
-import { useContext, useMemo } from 'react'
+import { useContext, useEffect } from 'react'
import { Context } from './context'
export default length => {
const context = useContext(Context)
const { register, index, step } = context
- useMemo(() => {
+ useEffect(() => {
if (typeof register !== 'function') return
register(index, { steps: length })
}, [length])
diff --git a/packages/export/index.js b/packages/export/index.js
index e62ec98..3e2f289 100644
--- a/packages/export/index.js
+++ b/packages/export/index.js
@@ -23,7 +23,8 @@ module.exports = async opts => {
switch (type) {
case 'pdf':
- await page.goto(`http://localhost:${port}/print`, {
+ const url = `http://localhost:${port}/print`
+ await page.goto(url, {
waitUntil: 'networkidle2',
})
await page.pdf({
diff --git a/packages/export/package.json b/packages/export/package.json
index 7a8a912..a9c5ad5 100644
--- a/packages/export/package.json
+++ b/packages/export/package.json
@@ -8,8 +8,8 @@
"mdx-deck-export": "./cli.js"
},
"scripts": {
- "pdf": "./cli.js pdf ../../docs/demo.mdx",
- "png": "./cli.js png ../../docs/demo.mdx"
+ "pdf": "./cli.js pdf ../../docs/demo.mdx -d ../../docs/dist",
+ "png": "./cli.js png ../../docs/demo.mdx -d ../../docs/dist"
},
"dependencies": {
"mdx-deck": "^2.2.3",
diff --git a/packages/gatsby-theme/gatsby-browser.js b/packages/gatsby-theme/gatsby-browser.js
new file mode 100644
index 0000000..1c5f973
--- /dev/null
+++ b/packages/gatsby-theme/gatsby-browser.js
@@ -0,0 +1 @@
+export { wrapPageElement } from './src'
diff --git a/packages/gatsby-theme/src/index.js b/packages/gatsby-theme/src/index.js
index b1c6ea4..0060725 100644
--- a/packages/gatsby-theme/src/index.js
+++ b/packages/gatsby-theme/src/index.js
@@ -1 +1,6 @@
-export default {}
+import React from 'react'
+import { MDXDeckState } from '@mdx-deck/components'
+
+export const wrapPageElement = ({ props, element }) => (
+ {element}
+)
diff --git a/packages/gatsby-theme/src/layouts/deck.js b/packages/gatsby-theme/src/layouts/deck.js
index df19edf..f0da91f 100644
--- a/packages/gatsby-theme/src/layouts/deck.js
+++ b/packages/gatsby-theme/src/layouts/deck.js
@@ -1,22 +1,17 @@
import React from 'react'
-import { MDXProvider } from '@mdx-js/react'
import { MDXDeck, splitSlides } from '@mdx-deck/components'
import Root from './root'
-const wrapper = page => props => (
-
-)
+const wrapper = props =>
-export default props => {
- const components = {
- wrapper: wrapper(props),
- }
+const components = {
+ wrapper,
+}
+
+export default ({ Component, ...props }) => {
return (
- {props.children}
+
)
}
diff --git a/packages/gatsby-theme/src/templates/deck.js b/packages/gatsby-theme/src/templates/deck.js
index 37fecc9..134c322 100644
--- a/packages/gatsby-theme/src/templates/deck.js
+++ b/packages/gatsby-theme/src/templates/deck.js
@@ -5,8 +5,9 @@ import Layout from '../layouts/deck'
export default props => {
const { mdx } = props.data
- const children =
- return
+ const Component = props =>
+
+ return
}
export const query = graphql`
diff --git a/yarn.lock b/yarn.lock
index 535f061..cea645b 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -747,7 +747,7 @@
dependencies:
regenerator-runtime "^0.12.0"
-"@babel/runtime@^7.0.0", "@babel/runtime@^7.1.2", "@babel/runtime@^7.3.1", "@babel/runtime@^7.4.2":
+"@babel/runtime@^7.0.0", "@babel/runtime@^7.1.2", "@babel/runtime@^7.3.1", "@babel/runtime@^7.3.4", "@babel/runtime@^7.4.2":
version "7.4.3"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.4.3.tgz#79888e452034223ad9609187a0ad1fe0d2ad4bdc"
integrity sha512-9lsJwJLxDh/T3Q3SZszfWOTkk3pHbkmH+3KY+zwIDmsNlxsumuhS2TH3NIpktU4kNvfzy+k3eLT7aTJSPTo0OA==
@@ -1823,6 +1823,11 @@
dependencies:
any-observable "^0.3.0"
+"@sheerun/mutationobserver-shim@^0.3.2":
+ version "0.3.2"
+ resolved "https://registry.yarnpkg.com/@sheerun/mutationobserver-shim/-/mutationobserver-shim-0.3.2.tgz#8013f2af54a2b7d735f71560ff360d3a8176a87b"
+ integrity sha512-vTCdPp/T/Q3oSqwHmZ5Kpa9oI7iLtGl3RQaA/NyLHikvcrPxACkkKVr/XzkSPJWXHRhKGzVvb0urJsbMlRxi1Q==
+
"@stefanprobst/lokijs@^1.5.6-b":
version "1.5.6-b"
resolved "https://registry.yarnpkg.com/@stefanprobst/lokijs/-/lokijs-1.5.6-b.tgz#6a36a86dbe132e702e6b15ffd3ce4139aebfe942"
@@ -5002,6 +5007,16 @@ dom-serializer@0, dom-serializer@~0.1.0:
domelementtype "^1.3.0"
entities "^1.1.1"
+dom-testing-library@^3.19.0:
+ version "3.19.3"
+ resolved "https://registry.yarnpkg.com/dom-testing-library/-/dom-testing-library-3.19.3.tgz#fba399987be1bdd57b07c4bc3ef46c3c084b26d9"
+ integrity sha512-oiI+oq91iO/Vpp+pt8PqfqLfBK074FH0eprhoFNvBCvJOk7vL4ozbe/yj/kEEGR6kiT4F3MAam19AX1fdGFjrA==
+ dependencies:
+ "@babel/runtime" "^7.3.4"
+ "@sheerun/mutationobserver-shim" "^0.3.2"
+ pretty-format "^24.5.0"
+ wait-for-expect "^1.1.0"
+
dom-walk@^0.1.0:
version "0.1.1"
resolved "https://registry.yarnpkg.com/dom-walk/-/dom-walk-0.1.1.tgz#672226dc74c8f799ad35307df936aba11acd6018"
@@ -11470,7 +11485,7 @@ pretty-error@^2.1.1:
renderkid "^2.0.1"
utila "~0.4"
-pretty-format@^24.7.0:
+pretty-format@^24.5.0, pretty-format@^24.7.0:
version "24.7.0"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-24.7.0.tgz#d23106bc2edcd776079c2daa5da02bcb12ed0c10"
integrity sha512-apen5cjf/U4dj7tHetpC7UEFCvtAgnNZnBDkfPv3fokzIqyOJckAG9OlAPC1BlFALnqT/lGB2tl9EJjlK6eCsA==
@@ -11901,6 +11916,14 @@ react-test-renderer@^16.8.4:
react-is "^16.8.6"
scheduler "^0.13.6"
+react-testing-library@^6.1.2:
+ version "6.1.2"
+ resolved "https://registry.yarnpkg.com/react-testing-library/-/react-testing-library-6.1.2.tgz#f6bba6eeecedac736eb00b22b4c70bae04535a4f"
+ integrity sha512-z69lhRDGe7u/NOjDCeFRoe1cB5ckJ4656n0tj/Fdcr6OoBUu7q9DBw0ftR7v5i3GRpdSWelnvl+feZFOyXyxwg==
+ dependencies:
+ "@babel/runtime" "^7.4.2"
+ dom-testing-library "^3.19.0"
+
react@^16.8.3, react@^16.8.6:
version "16.8.6"
resolved "https://registry.yarnpkg.com/react/-/react-16.8.6.tgz#ad6c3a9614fd3a4e9ef51117f54d888da01f2bbe"
@@ -14432,6 +14455,11 @@ w3c-hr-time@^1.0.1:
dependencies:
browser-process-hrtime "^0.1.2"
+wait-for-expect@^1.1.0:
+ version "1.1.1"
+ resolved "https://registry.yarnpkg.com/wait-for-expect/-/wait-for-expect-1.1.1.tgz#9cd10e07d52810af9e0aaf509872e38f3c3d81ae"
+ integrity sha512-vd9JOqqEcBbCDhARWhW85ecjaEcfBLuXgVBqatfS3iw6oU4kzAcs+sCNjF+TC9YHPImCW7ypsuQc+htscIAQCw==
+
walker@^1.0.7, walker@~1.0.5:
version "1.0.7"
resolved "https://registry.yarnpkg.com/walker/-/walker-1.0.7.tgz#2f7f9b8fd10d677262b18a884e28d19618e028fb"