Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve mobile support #814

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/gatsby-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@
"react-dom": "^16.10.0"
},
"devDependencies": {
"@types/theme-ui": "^0.6.0",
"gatsby": "^2.14.0",
"react": "^16.10.0",
"react-dom": "^16.10.0"
},
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^1.2.36",
"@fortawesome/free-solid-svg-icons": "^5.15.4",
"@fortawesome/react-fontawesome": "^0.1.17",
"@mdx-js/loader": "^1.5.3",
"@mdx-js/mdx": "^1.5.3",
"gatsby-page-utils": "^0.0.39",
"gatsby-plugin-react-helmet": "^3.1.18",
"hhmmss": "^1.0.0",
"react-helmet": "^5.2.1",
"react-swipeable": "^6.2.0",
"remark-emoji": "^2.0.2",
"remark-images": "^2.0.0",
"remark-unwrap-images": "^2.0.0",
Expand Down
129 changes: 129 additions & 0 deletions packages/gatsby-plugin/src/control.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/** @jsx jsx */
import { jsx, Button, IconButton, Flex, Box } from 'theme-ui'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import {
faChevronLeft,
faChevronRight,
faExpand,
faPrint,
faTh,
faDesktop,
faThList,
faFilePowerpoint,
} from '@fortawesome/free-solid-svg-icons'
import { useDeck } from './context'
import modes from './modes'
import React, { useState, useRef } from 'react'

const toolbarShowPeriod = 2 * 1000

export default () => {
const context = useDeck()
const [showToolbar, setShowToolbar] = useState(false)
const closeToolbarTimeoutRef = useRef(null)
const toggleToolbar = () => {
if (closeToolbarTimeoutRef.current) {
clearTimeout(closeToolbarTimeoutRef.current)
}
closeToolbarTimeoutRef.current = setTimeout(() => {
setShowToolbar(false)
}, toolbarShowPeriod)
setShowToolbar(true)
}
React.useEffect(() => {
document.documentElement.addEventListener('mousedown', toggleToolbar)
document.documentElement.addEventListener('mousemove', toggleToolbar)
document.documentElement.addEventListener('touchend', toggleToolbar)
document.documentElement.addEventListener('touchmove', toggleToolbar)
document.documentElement.addEventListener('touchstart', toggleToolbar)
return () => {
if (closeToolbarTimeoutRef.current) {
clearTimeout(closeToolbarTimeoutRef.current)
}
document.documentElement.removeEventListener('mousedown', toggleToolbar)
document.documentElement.removeEventListener('mousemove', toggleToolbar)
document.documentElement.removeEventListener('touchend', toggleToolbar)
document.documentElement.removeEventListener('touchmove', toggleToolbar)
document.documentElement.removeEventListener('touchstart', toggleToolbar)
}
}, [context])
return (
<div
sx={{
position:
context.mode === modes.grid || context.mode === modes.print
? 'fixed'
: 'absolute',
zIndex: 10,
left: 0,
bottom: 0,
right: 0,
variant: 'styles.Footer',
color: 'white',
visibility: showToolbar ? 'visible' : 'hidden',
}}>
<Flex
sx={{
borderRadius: 5,
backgroundColor: 'black',
padding: 1,
opacity: '70%',
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
margin: '0 auto',
width: 'max-content',
}}>
<IconButton
bg="transparent"
color="text"
onClick={() => context.toggleMode(modes.presenter)}>
<FontAwesomeIcon icon={faFilePowerpoint} size="2x" />
</IconButton>
<IconButton
bg="transparent"
color="text"
onClick={() => context.toggleMode(modes.print)}>
<FontAwesomeIcon icon={faPrint} size="2x" />
</IconButton>
<IconButton
bg="transparent"
color="text"
onClick={() =>
document.fullscreenElement
? document.exitFullscreen()
: document.documentElement.requestFullscreen()
}>
<FontAwesomeIcon icon={faExpand} size="2x" />
</IconButton>
<IconButton bg="transparent" color="text" onClick={context.previous}>
<FontAwesomeIcon icon={faChevronLeft} size="2x" />
</IconButton>
<Box sx={{ marginTop: 'auto', marginBottom: 'auto' }}>
{context.index + 1} / {context.length + 1}
</Box>
<IconButton bg="transparent" color="text" onClick={context.next}>
<FontAwesomeIcon icon={faChevronRight} size="2x" />
</IconButton>
<IconButton
bg="transparent"
color="text"
onClick={() => context.toggleMode(modes.grid)}>
<FontAwesomeIcon icon={faTh} size="2x" />
</IconButton>
<IconButton
bg="transparent"
color="text"
onClick={() => context.toggleMode(modes.overview)}>
<FontAwesomeIcon icon={faThList} size="2x" />
</IconButton>
<IconButton
bg="transparent"
color="text"
onClick={() => context.setMode(modes.default)}>
<FontAwesomeIcon icon={faDesktop} size="2x" />
</IconButton>
</Flex>
</div>
)
}
26 changes: 14 additions & 12 deletions packages/gatsby-plugin/src/deck.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { ThemeProvider, merge } from 'theme-ui'
import split from './split-slides'
import { Context } from './context'
import Keyboard from './keyboard'
import { useSwipeWithCustomContext } from './use-swipe'
import modes from './modes'
import Storage from './storage'
import Container from './container'
import Slide from './slide'
import baseTheme from './theme'
import Control from './control'

const getIndex = props => {
if (!props.location) return 0
Expand All @@ -23,9 +25,8 @@ export default props => {
const slide = slides[index]

const [mode, setMode] = React.useState(modes.default)
const toggleMode = next => setMode(current =>
current === next ? modes.default : next
)
const toggleMode = next =>
setMode(current => (current === next ? modes.default : next))

const [step, setStep] = React.useState(0)
const [steps, setSteps] = React.useState(0)
Expand Down Expand Up @@ -54,8 +55,6 @@ export default props => {
}
}, [])

if (!slide) return false

const context = {
slides,
slug,
Expand All @@ -80,7 +79,7 @@ export default props => {
if (steps && step > 0) {
setStep(n => n - 1)
} else {
setIndex(n => n > 0 ? n - 1 : n)
setIndex(n => (n > 0 ? n - 1 : n))
setStep(0)
setSteps(0)
}
Expand All @@ -90,27 +89,30 @@ export default props => {
if (step < steps) {
setStep(n => n + 1)
} else {
setIndex(n => n < slides.length - 1 ? n + 1 : n)
setIndex(n => (n < slides.length - 1 ? n + 1 : n))
setStep(0)
setSteps(0)
}
}

const theme = merge(baseTheme, props.theme || {})

const { ref, ...swipeHandlers } = useSwipeWithCustomContext({ context })

if (!slide) return false

return (
<Context.Provider value={context}>
<Control />
<Keyboard />
<Storage />
<Helmet>
{slides.head.children}
{theme.googleFont && <link rel='stylesheet' href={theme.googleFont} />}
{theme.googleFont && <link rel="stylesheet" href={theme.googleFont} />}
</Helmet>
<ThemeProvider
theme={theme}
components={theme.components}>
<ThemeProvider theme={theme} components={theme.components}>
<Container>
<Slide>
<Slide innerRef={ref} {...swipeHandlers}>
{slide}
</Slide>
</Container>
Expand Down
5 changes: 4 additions & 1 deletion packages/gatsby-plugin/src/slide.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ export default ({
width = '100%',
height = '100%',
children,
innerRef,
...props
}) =>
}) => (
<div
ref={innerRef}
{...props}
sx={{
boxSizing: 'border-box',
Expand All @@ -27,3 +29,4 @@ export default ({
}}>
{children}
</div>
)
3 changes: 2 additions & 1 deletion packages/gatsby-plugin/src/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default {
text: '#fff',
background: '#000',
backdrop: '#111',
primary: '#fff',
},
fonts: {
body: 'system-ui, sans-serif',
Expand Down Expand Up @@ -69,5 +70,5 @@ export default {
Footer: {
px: 3,
},
}
},
}
54 changes: 54 additions & 0 deletions packages/gatsby-plugin/src/use-swipe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { useSwipeable } from 'react-swipeable'
import { useDeck } from './context'
import modes from './modes'

const toggleMode = next => state =>
state.mode === next ? { mode: modes.normal } : { mode: next }

export const useSwipe = () => {
const context = useDeck()

const onSwipedLeft = e => {
context.next()
}

const onSwipedRight = e => {
context.previous()
}

const onSwipedUp = e => {
context.setMode(modes.presenter)
}

const onSwipedDown = e => {
context.setMode(modes.normal)
}

const props = useSwipeable({
onSwipedLeft,
onSwipedRight,
onSwipedUp,
onSwipedDown,
})

return props
}

export const useSwipeWithCustomContext = ({ context }) => {
const onSwipedLeft = e => {
context.next()
}

const onSwipedRight = e => {
context.previous()
}

const props = useSwipeable({
onSwipedLeft,
onSwipedRight,
})

return props
}

export default useSwipe
Loading