Utilities
usePlatform
Two hooks and two constants that tell if the user has an Apple platform, for keyboard hints.
Utilities
Two hooks and two constants that tell if the user has an Apple platform, for keyboard hints.
Open the command menu Ctrl K
import { Kbd, useModKey } from 'ferry-ui'
export default function UsePlatformHero() {
// "⌘" on an Apple device, "Ctrl" on other devices.
const mod = useModKey()
return (
<p className="flex items-center gap-2 text-sm text-foreground-light">
Open the command menu <Kbd>{mod} K</Kbd>
</p>
)
}The use-platform module has two hooks: useIsMac() and useModKey(). ferry-ui has no export with the name usePlatform.
useModKey() returns the label of the main modifier key. The label is ⌘ on an Apple platform (Mac, iPhone, iPad) and Ctrl on the other platforms. Show it in a Kbd.
import { Kbd, useModKey } from 'ferry-ui'
function SearchHint() {
const mod = useModKey()
return <Kbd>{mod} K</Kbd>
}useIsMac() returns false and useModKey() returns Ctrl. Then React renders again with the real platform.useIsMac() returns true on an Apple platform. Use it when the two platforms need different keys or different text.
Delete the selected rowsDel
import { Kbd, useIsMac } from 'ferry-ui'
export default function UsePlatformIsMac() {
const isMac = useIsMac()
return (
<p className="flex items-center gap-2 text-sm text-foreground-light">
Delete the selected rows
{/* The two platforms use different keys for this action. */}
{isMac ? <Kbd>⌘ ⌫</Kbd> : <Kbd>Del</Kbd>}
</p>
)
}isMac and modKey are constants with the same values as the hooks. The module reads the platform one time, when it loads.
Use the constants in code that runs only in the browser, for example an event handler. In markup that a server can render, use the hooks. On a server, the constants describe the platform of the server.
import { isMac } from 'ferry-ui'
function onKeyDown(event: KeyboardEvent) {
const mod = isMac ? event.metaKey : event.ctrlKey
if (mod && event.key === 's') saveDraft()
}The hooks take no argument.
| Name | Type | Value |
|---|---|---|
useIsMac() | boolean | true on an Apple platform. false on the server and during hydration. |
useModKey() | string | '⌘' on an Apple platform, 'Ctrl' on the other platforms. 'Ctrl' on the server and during hydration. |
isMac | boolean | true on an Apple platform. The module reads it one time. |
modKey | string | '⌘' if isMac is true, 'Ctrl' if not. |