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

add mac compatibility mode #15

Merged
merged 5 commits into from
Apr 4, 2024
Merged
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
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ external keyboard).
For instance, for `alt+b` it will return `Alt + B` on Windows/Linux or `⌥ B`
on Mac.

If you're using overrides, pass the same override config both to `hotkeyKeyUX()`
If youre using overrides, pass the same override config both to `hotkeyKeyUX()`
and `getHotKeyHint()` for accurate hints:

```js
Expand Down Expand Up @@ -431,3 +431,26 @@ startKeyUX(window, [
hiddenKeyUX()
])
```


### Mac Compatibility Mode

It’s common to use the <kbd>Meta</kbd> (or <kbd>⌘</kbd>) modifier for hotkeys
on Mac, while Window and Linux usually favor the <kbd>Ctrl</kbd> key. To provide
familiar experience on all platforms, enable the Mac compatibility transform:

```js
import {
hotkeyMacCompat,
hotkeyKeyUX,
startKeyUX,
getHotKeyHint
} from 'keyux'

const macCompat = hotkeyMacCompat();
startKeyUX(window, [hotkeyKeyUX([macCompat])])
getHotKeyHint(window, 'ctrl+b', [macCompat])
```

Hotkeys pressed with the <kbd>Meta</kbd> modifier will work as if the <kbd>Ctrl</kbd>
modifier was pressed.
16 changes: 16 additions & 0 deletions compat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function maybeApplyCompat(code, window, from, to) {
ai marked this conversation as resolved.
Show resolved Hide resolved
if (
window.navigator.platform.indexOf('Mac') === 0 &&
!code.includes('meta+ctrl')
) {
return code.replace(from, to)
}
return code
}

export function hotkeyMacCompat() {
return [
(code, window) => maybeApplyCompat(code, window, 'meta', 'ctrl'),
(code, window) => maybeApplyCompat(code, window, 'ctrl', 'meta')
]
}
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,9 @@ export function getHotKeyHint(
* ```
*/
export function hotkeyOverrides(overrides: HotkeyOverride): Transformer

/**
* Provides a transformer for Mac compatibility mode that can be used
* with `hotkeyKeyUX()` and `getHotKeyHint()`.
*/
export function hotkeyMacCompat(): Transformer
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './hidden.js'
export * from './press.js'
export * from './jump.js'
export * from './overrides.js'
export * from './compat.js'

export function startKeyUX(window, plugins) {
let unbinds = plugins.map(plugin => plugin(window))
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@
{
"name": "All modules",
"import": {
"./index.js": "{ startKeyUX, hotkeyKeyUX, pressKeyUX, focusGroupKeyUX, jumpKeyUX, hiddenKeyUX, likelyWithKeyboard, getHotKeyHint, hotkeyOverrides }"
"./index.js": "{ startKeyUX, hotkeyKeyUX, pressKeyUX, focusGroupKeyUX, jumpKeyUX, hiddenKeyUX, likelyWithKeyboard, getHotKeyHint, hotkeyOverrides, hotkeyMacCompat }"
},
"limit": "1996 B"
"limit": "2059 B"
}
],
"clean-publish": {
Expand Down
30 changes: 30 additions & 0 deletions test/compat.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { JSDOM } from 'jsdom'
import { equal } from 'node:assert'
import { test } from 'node:test'

import { hotkeyMacCompat } from '../compat.js'
import type { MinimalWindow } from '../index.js'

const MAC_WINDOW = {
navigator: {
platform: 'Mac',
userAgent: 'Mac'
}
} as MinimalWindow

const [tranformForward, transformReverse] = hotkeyMacCompat()

test('applies hotkey compatibility for Mac platform', () => {
equal(tranformForward('meta+shift+b', MAC_WINDOW), 'ctrl+shift+b')
equal(tranformForward('meta+ctrl+shift+b', MAC_WINDOW), 'meta+ctrl+shift+b')
})

test('applies hint compatibility for Mac platform', () => {
equal(transformReverse('ctrl+shift+b', MAC_WINDOW), 'meta+shift+b')
equal(transformReverse('meta+ctrl+shift+b', MAC_WINDOW), 'meta+ctrl+shift+b')
})

test('does nothing for non-Mac platform', () => {
equal(tranformForward('meta+shift+b', new JSDOM().window), 'meta+shift+b')
equal(transformReverse('ctrl+shift+b', new JSDOM().window), 'ctrl+shift+b')
})
17 changes: 12 additions & 5 deletions test/demo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getHotKeyHint,
hiddenKeyUX,
hotkeyKeyUX,
hotkeyMacCompat,
hotkeyOverrides,
jumpKeyUX,
likelyWithKeyboard,
Expand All @@ -15,10 +16,11 @@ import {
} from '../../index.js'

let overrides: HotkeyOverride = {}
let overridesTransformer = hotkeyOverrides(overrides);
let overridesTransformer = hotkeyOverrides(overrides)
let macCompatTransformer = hotkeyMacCompat()

startKeyUX(window, [
hotkeyKeyUX([overridesTransformer]),
hotkeyKeyUX([macCompatTransformer, overridesTransformer]),
focusGroupKeyUX(),
pressKeyUX('is-pressed'),
jumpKeyUX(),
Expand All @@ -27,7 +29,12 @@ startKeyUX(window, [

const HotKeyHint: FC<{ hotkey: string }> = ({ hotkey }) => {
return likelyWithKeyboard(window) ? (
<kbd>{getHotKeyHint(window, hotkey, [overridesTransformer])}</kbd>
<kbd>
{getHotKeyHint(window, hotkey, [
overridesTransformer,
macCompatTransformer
])}
</kbd>
) : null
}

Expand Down Expand Up @@ -343,15 +350,15 @@ const Tabs: FC = () => {
const Toolbar: FC = () => {
return (
<>
<div className='toolbar' role="toolbar">
<div className="toolbar" role="toolbar">
<div className="toolbar_group">
<button className="toolbar_button" type="button">Copy</button>
<button className="toolbar_button" type="button">Paste</button>
<button className="toolbar_button" type="button">Cut</button>
</div>
<div className="toolbar_group">
<label className="toolbar_label">
<input type="checkbox"/>
<input type="checkbox" />
Night Mode
</label>
</div>
Expand Down