-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce styled tooltips. Refactor to be cross-platform (#151)
* Introduce styled tooltips. Refactor to be cross-platform * Fix lint issues
- Loading branch information
Showing
7 changed files
with
289 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import React from 'react'; | ||
|
||
type Platform = 'mac' | 'windows' | 'linux' | 'other'; | ||
type KeyType = 'mod' | 'alt'; | ||
|
||
const getPlatform = () => { | ||
const platform = navigator.platform.toLowerCase(); | ||
const userAgent = navigator.userAgent.toLowerCase(); | ||
|
||
if (platform.includes('mac') || userAgent.includes('mac')) { | ||
return 'mac'; | ||
} else if (platform.includes('win') || userAgent.includes('win')) { | ||
return 'windows'; | ||
} else if (platform.includes('linux') || userAgent.includes('linux')) { | ||
return 'linux'; | ||
} else { | ||
return 'other'; | ||
} | ||
}; | ||
|
||
const keyMappings: Record<KeyType, Record<Platform, string>> = { | ||
mod: { | ||
mac: '⌘', | ||
windows: 'Ctrl', | ||
linux: 'Ctrl', | ||
other: 'Ctrl', | ||
}, | ||
alt: { | ||
mac: '⌥', | ||
windows: 'Alt', | ||
linux: 'Alt', | ||
other: 'Alt', | ||
}, | ||
}; | ||
|
||
const getPlatformSpecificKey = (keyType: KeyType): string => { | ||
const platform = getPlatform(); | ||
return keyMappings[keyType][platform]; | ||
}; | ||
|
||
export default function Shortcut({ keys }: { keys: string[] }) { | ||
// Replace keys that are in the keyMappings with the platform specific key | ||
keys = keys.map((key) => { | ||
if (key === 'mod') { | ||
return getPlatformSpecificKey('mod'); | ||
} else if (key === 'alt') { | ||
return getPlatformSpecificKey('alt'); | ||
} else { | ||
return key; | ||
} | ||
}); | ||
return ( | ||
<> | ||
{keys.map((key) => { | ||
return ( | ||
<React.Fragment key={key}> | ||
<span className="font-mono bg-background text-foreground border py-[1px] px-1.5 rounded-sm drop-shadow-key mx-0.5"> | ||
{key} | ||
</span> | ||
</React.Fragment> | ||
); | ||
})} | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as React from 'react'; | ||
import * as TooltipPrimitive from '@radix-ui/react-tooltip'; | ||
|
||
import { cn } from '@/lib/utils'; | ||
|
||
const TooltipProvider = TooltipPrimitive.Provider; | ||
|
||
const Tooltip = TooltipPrimitive.Root; | ||
|
||
const TooltipTrigger = TooltipPrimitive.Trigger; | ||
|
||
const TooltipContent = React.forwardRef< | ||
React.ElementRef<typeof TooltipPrimitive.Content>, | ||
React.ComponentPropsWithoutRef<typeof TooltipPrimitive.Content> | ||
>(({ className, sideOffset = 4, ...props }, ref) => ( | ||
<TooltipPrimitive.Content | ||
ref={ref} | ||
sideOffset={sideOffset} | ||
side="bottom" | ||
className={cn( | ||
'z-50 overflow-hidden rounded-sm bg-muted px-1.5 py-1.5 text-xs font-medium border animate-in fade-in-0 zoom-in-95 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2', | ||
className, | ||
)} | ||
{...props} | ||
/> | ||
)); | ||
TooltipContent.displayName = TooltipPrimitive.Content.displayName; | ||
|
||
export { Tooltip, TooltipTrigger, TooltipContent, TooltipProvider }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.