Skip to content

Commit

Permalink
feat(Kbd): improve condition when shortcuts should be hidden (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
sodenn committed Nov 13, 2021
1 parent db7ea07 commit 712b284
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/components/Kbd.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { styled } from "@mui/material";
import React from "react";
import { usePlatform } from "../utils/platform";
import { usePlatform, useTouchScreen } from "../utils/platform";

export const StyledKbd = styled("kbd")`
padding: 0 0.4em;
Expand All @@ -12,9 +12,10 @@ export const StyledKbd = styled("kbd")`
`;

const Kbd: React.FC = ({ children }) => {
const hasTouchScreen = useTouchScreen();
const platform = usePlatform();

if (platform !== "web" && platform !== "electron") {
if (hasTouchScreen || platform === "ios" || platform === "android") {
return null;
}

Expand Down
22 changes: 22 additions & 0 deletions src/utils/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,25 @@ import { Capacitor } from "@capacitor/core";
export function usePlatform() {
return Capacitor.getPlatform();
}

export function useTouchScreen() {
if ("maxTouchPoints" in navigator) {
return navigator.maxTouchPoints > 0;
} else if ("msMaxTouchPoints" in navigator) {
return (navigator as any).msMaxTouchPoints > 0;
} else {
const mediaQueryList = matchMedia("(pointer:coarse)");
if (mediaQueryList && mediaQueryList.media === "(pointer:coarse)") {
return mediaQueryList.matches;
} else if ("orientation" in window) {
return true; // deprecated, but good fallback
} else {
// Only as a last resort, fall back to user agent sniffing
const userAgent = navigator.userAgent;
return (
/\b(BlackBerry|webOS|iPhone|IEMobile)\b/i.test(userAgent) ||
/\b(Android|Windows Phone|iPad|iPod)\b/i.test(userAgent)
);
}
}
}
11 changes: 8 additions & 3 deletions src/utils/testing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import AppTheme from "../components/AppTheme";
import { AppContextProvider } from "../data/AppContext";
import { TaskProvider } from "../data/TaskContext";

jest.mock("../utils/platform", () => ({
...jest.requireActual("../utils/platform"),
useTouchScreen: jest.fn(),
}));

interface TestContextProps {
text?: string;
}
Expand All @@ -23,7 +28,7 @@ i18n.use(initReactI18next).init({
resources: { en: { translations: {} } },
});

const setupMock = {
const mocks = {
Filesystem: {
readFile: (result?: ReadFileResult) => {
Filesystem.readFile = jest
Expand All @@ -48,7 +53,7 @@ export const TestContext = (props: TestContextProps) => {
const { text } = props;

if (text) {
setupMock.Filesystem.readFile({ data: text });
mocks.Filesystem.readFile({ data: text });
}

return (
Expand All @@ -74,7 +79,7 @@ export const EmptyTestContext = (
const { text, children } = props;

if (text) {
setupMock.Filesystem.readFile({ data: text });
mocks.Filesystem.readFile({ data: text });
}

return (
Expand Down

0 comments on commit 712b284

Please sign in to comment.