Skip to content

Commit

Permalink
Support a share panel
Browse files Browse the repository at this point in the history
  • Loading branch information
ccouzens committed Apr 14, 2024
1 parent ec53367 commit 2fc2ac2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
17 changes: 15 additions & 2 deletions public/game.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import url("game-win-animations.css") not (prefers-reduced-motion);
@import url("game-win-animations.css") screen and (update: fast) and
(not (prefers-reduced-motion));

:root {
font-family: sans-serif;
Expand Down Expand Up @@ -74,7 +75,7 @@ h1 {
margin: 0;
}

nav > button,
button,
nav > a {
background: none;
border: none;
Expand Down Expand Up @@ -176,3 +177,15 @@ dialog > div {
gap: 1em;
text-align: center;
}

.share-supported {
display: none;
}

:root.supports-share .share-supported {
display: initial;
}

:root.supports-share .share-not-supported {
display: none;
}
6 changes: 5 additions & 1 deletion public/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ <h1>Maze</h1>
<dialog id="winDialog" aria-label="Congratulations dialog">
<div>
<p>Amazing work!</p>
<p>If you enjoyed this, please share it with your friends.</p>
<p>
If you enjoyed this, please
<button value="share" class="share-supported">share</button>
<span class="share-not-supported">share</span> it with your friends.
</p>
</div>
</dialog>
</body>
Expand Down
10 changes: 10 additions & 0 deletions src/game/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ interface WindowType {
replaceState: Window["history"]["replaceState"];
state: Window["history"]["state"];
};
navigator: {
share: Window["navigator"]["share"];
};
location: {
hash: Window["location"]["hash"];
href: Window["location"]["href"];
Expand Down Expand Up @@ -146,6 +149,11 @@ function clickHandlerFactory(
const value = ev.target.value;
if (value === "new") {
newMaze(app);
} else if (value === "share") {
app.window.navigator.share({
url: app.window.location.href,
title: "Maze puzzle",
});
}
} else if (ev.target instanceof Node && app.mazeSvg.contains(ev.target)) {
move(app, ev.x, ev.y);
Expand Down Expand Up @@ -276,5 +284,7 @@ export async function initialiseApp(window: WindowType) {
navigate(app, app.window.location.hash, false);
}

app.root.classList.toggle("supports-share", "share" in window.navigator);

newMaze(app);
}

0 comments on commit 2fc2ac2

Please sign in to comment.