Skip to content

Commit

Permalink
fix(undo): fix undo/redo keys for macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
goveo committed Apr 29, 2024
1 parent a5434cf commit 544151e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/hooks/usePhoneInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { defaultCountries } from '../data/countryData';
import { CountryData, CountryIso2, ParsedCountry } from '../types';
import { getCountry } from '../utils';
import { isMacOS } from '../utils/common/isMacOS';
import {
handlePhoneChange,
PhoneFormattingConfig,
Expand Down Expand Up @@ -277,10 +278,20 @@ export const usePhoneInput = ({
if (!e.key) return;

const ctrlPressed = e.ctrlKey;
const metaPressed = e.metaKey;
const shiftPressed = e.shiftKey;
const zPressed = e.key.toLowerCase() === 'z';

if (!ctrlPressed || !zPressed) return;
if (!zPressed) return;

if (isMacOS()) {
// command+z on macOS
if (!metaPressed) return;
} else {
// ctrl+z on non-macOS
if (!ctrlPressed) return;
}

shiftPressed ? redo() : undo();
};

Expand Down
7 changes: 7 additions & 0 deletions src/utils/common/isMacOS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const isMacOS = () => {
if (typeof window === 'undefined') {
return false;
}

return window.navigator.userAgent.toLowerCase().includes('macintosh');
};

0 comments on commit 544151e

Please sign in to comment.