Fixed layout and interaction for mobile

This commit is contained in:
Mitchell McCaffrey 2020-03-26 14:53:02 +11:00
parent d2ca6cc177
commit 7c07caa70d
7 changed files with 55 additions and 24 deletions

View File

@ -34,17 +34,31 @@ function Map({
const [mapScale, setMapScale] = useState(1); const [mapScale, setMapScale] = useState(1);
useEffect(() => { useEffect(() => {
interact(".map").draggable({ interact(".map")
inertia: true, .gesturable({
listeners: { listeners: {
move: event => { move: event => {
setMapTranslate(previousMapTranslate => ({ setMapScale(previousMapScale =>
x: previousMapTranslate.x + event.dx, Math.max(Math.min(previousMapScale + event.ds, maxZoom), minZoom)
y: previousMapTranslate.y + event.dy );
})); setMapTranslate(previousMapTranslate => ({
x: previousMapTranslate.x + event.dx,
y: previousMapTranslate.y + event.dy
}));
}
} }
} })
}); .draggable({
inertia: true,
listeners: {
move: event => {
setMapTranslate(previousMapTranslate => ({
x: previousMapTranslate.x + event.dx,
y: previousMapTranslate.y + event.dy
}));
}
}
});
interact(".map").on("doubletap", event => { interact(".map").on("doubletap", event => {
event.preventDefault(); event.preventDefault();
setMapTranslate({ x: 0, y: 0 }); setMapTranslate({ x: 0, y: 0 });
@ -54,8 +68,8 @@ function Map({
function handleZoom(event) { function handleZoom(event) {
const deltaY = event.deltaY * zoomSpeed; const deltaY = event.deltaY * zoomSpeed;
setMapScale(mapScale => setMapScale(previousMapScale =>
Math.max(Math.min(mapScale + deltaY, maxZoom), minZoom) Math.max(Math.min(previousMapScale + deltaY, maxZoom), minZoom)
); );
} }
@ -72,7 +86,9 @@ function Map({
flexGrow: 1, flexGrow: 1,
position: "relative", position: "relative",
overflow: "hidden", overflow: "hidden",
backgroundColor: "rgba(0, 0, 0, 0.1)" backgroundColor: "rgba(0, 0, 0, 0.1)",
userSelect: "none",
touchAction: "none"
}} }}
bg="background" bg="background"
onWheel={handleZoom} onWheel={handleZoom}
@ -109,7 +125,11 @@ function Map({
<Image <Image
ref={mapRef} ref={mapRef}
id="map" id="map"
sx={{ width: "100%" }} sx={{
width: "100%",
userSelect: "none",
touchAction: "none"
}}
src={mapSource} src={mapSource}
/> />
</Box> </Box>
@ -139,6 +159,7 @@ function Map({
tokenId={token.id} tokenId={token.id}
image={token.image} image={token.image}
className={mapTokenClassName} className={mapTokenClassName}
sx={{ position: "absolute" }}
/> />
</Box> </Box>
))} ))}

View File

@ -112,12 +112,12 @@ function ProxyToken({ tokenClassName, onProxyDragEnd, size }) {
return ReactDOM.createPortal( return ReactDOM.createPortal(
<Box <Box
sx={{ sx={{
width: "100vw",
height: "100vh",
position: "absolute", position: "absolute",
overflow: "hidden", overflow: "hidden",
top: 0, top: 0,
left: 0 left: 0,
bottom: 0,
right: 0
}} }}
> >
<Image <Image

View File

@ -1,17 +1,21 @@
import React from "react"; import React from "react";
import { Image } from "theme-ui"; import { Image } from "theme-ui";
function Token({ image, className, tokenId }) { function Token({ image, className, tokenId, sx }) {
// Store the token id in the html element for the drag code to pick it up // Store the token id in the html element for the drag code to pick it up
const idProp = tokenId ? { "data-token-id": tokenId } : {}; const idProp = tokenId ? { "data-token-id": tokenId } : {};
return ( return (
<Image <Image
className={className} className={className}
sx={{ userSelect: "none" }} sx={{ userSelect: "none", touchAction: "none", ...sx }}
src={image} src={image}
{...idProp} {...idProp}
/> />
); );
} }
Token.defaultProps = {
sx: {}
};
export default Token; export default Token;

View File

@ -1 +1,7 @@
@import url("https://fonts.googleapis.com/css?family=Bree+Serif|Pacifico&display=swap"); @import url("https://fonts.googleapis.com/css?family=Bree+Serif|Pacifico&display=swap");
#root,
body,
html {
height: 100%;
}

View File

@ -136,7 +136,7 @@ function Game() {
} }
return ( return (
<Flex sx={{ flexDirection: "column", height: "100vh" }}> <Flex sx={{ flexDirection: "column", height: "100%" }}>
<Flex <Flex
sx={{ justifyContent: "space-between", flexGrow: 1, height: "100%" }} sx={{ justifyContent: "space-between", flexGrow: 1, height: "100%" }}
> >

View File

@ -19,11 +19,11 @@ function Home() {
} }
return ( return (
<Container sx={{ maxWidth: "300px" }}> <Container sx={{ maxWidth: "300px", height: "100%" }}>
<Flex <Flex
sx={{ sx={{
flexDirection: "column", flexDirection: "column",
height: "100vh", height: "100%",
justifyContent: "center" justifyContent: "center"
}} }}
> >

View File

@ -17,11 +17,11 @@ function Join() {
} }
return ( return (
<Container sx={{ maxWidth: "300px" }}> <Container sx={{ maxWidth: "300px", height: "100%" }}>
<Flex <Flex
sx={{ sx={{
flexDirection: "column", flexDirection: "column",
height: "100vh", height: "100%",
justifyContent: "center" justifyContent: "center"
}} }}
> >