Menubar
A visually persistent menu common in desktop applications.
Installation
bash npx @moe/cli add menubar Install the following dependency:
npx expo install @rn-primitives/menubarCopy/paste the following code to @/components/ui/menubar.tsx
import * as MenubarPrimitive from "@rn-primitives/menubar";
import * as React from "react";
import { Platform, StyleSheet, View } from "react-native";
import Animated, { FadeIn, FadeOut } from "react-native-reanimated";
import { Check } from "@/lib/icons/Check";
import { ChevronRight } from "@/lib/icons/ChevronRight";
import { cn } from "@/lib/utils";
import { TextClassContext } from "@/components/ui/text";
const MenubarMenu = MenubarPrimitive.Menu;
const MenubarGroup = MenubarPrimitive.Group;
const MenubarSub = MenubarPrimitive.Sub;
const MenubarRadioGroup = MenubarPrimitive.RadioGroup;
const Menubar = React.forwardRef<
React.ElementRef<typeof MenubarPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof MenubarPrimitive.Root>
>(({ className, ...props }, ref) => (
<MenubarPrimitive.Root
ref={ref}
className={cn(
"flex flex-row h-10 native:h-12 items-center gap-1 rounded-md border border-border bg-background p-1",
className,
)}
{...props}
/>
));
Menubar.displayName = MenubarPrimitive.Root.displayName;
const MenubarTrigger = React.forwardRef<
React.ElementRef<typeof MenubarPrimitive.Trigger>,
React.ComponentPropsWithoutRef<typeof MenubarPrimitive.Trigger>
>(({ className, ...props }, ref) => {
const { value } = MenubarPrimitive.useRootContext();
const { value: itemValue } = MenubarPrimitive.useMenuContext();
return (
<MenubarPrimitive.Trigger
ref={ref}
className={cn(
"flex flex-row web:cursor-default web:select-none items-center rounded-sm px-3 py-1.5 text-sm native:h-10 native:px-5 native:py-0 font-medium web:outline-none web:focus:bg-accent web:focus:text-accent-foreground active:bg-accent",
value === itemValue && "bg-accent",
className,
)}
{...props}
/>
);
});
MenubarTrigger.displayName = MenubarPrimitive.Trigger.displayName;
const MenubarContent = React.forwardRef<
React.ElementRef<typeof MenubarPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof MenubarPrimitive.Content>
>(({ className, ...props }, ref) => {
return (
<MenubarPrimitive.Portal>
<MenubarPrimitive.Overlay
style={Platform.OS !== "web" ? StyleSheet.absoluteFill : undefined}
>
<Animated.View
entering={FadeIn.duration(200)}
exiting={FadeOut.duration(200)}
>
<MenubarPrimitive.Content
ref={ref}
className={cn(
"z-50 min-w-[8rem] overflow-hidden rounded-md border border-border bg-popover p-1 shadow-md shadow-foreground/5",
className,
)}
{...props}
/>
</Animated.View>
</MenubarPrimitive.Overlay>
</MenubarPrimitive.Portal>
);
});
MenubarContent.displayName = MenubarPrimitive.Content.displayName;
const MenubarItem = React.forwardRef<
React.ElementRef<typeof MenubarPrimitive.Item>,
React.ComponentPropsWithoutRef<typeof MenubarPrimitive.Item> & {
inset?: boolean;
}
>(({ className, inset, ...props }, ref) => (
<TextClassContext.Provider value="select-none text-sm native:text-lg text-popover-foreground web:group-focus:text-accent-foreground">
<MenubarPrimitive.Item
ref={ref}
className={cn(
"relative flex flex-row web:cursor-default items-center gap-2 rounded-sm px-2 py-1.5 native:py-2 web:outline-none web:focus:bg-accent active:bg-accent web:hover:bg-accent group",
inset && "pl-8",
props.disabled && "opacity-50 web:pointer-events-none",
className,
)}
{...props}
/>
</TextClassContext.Provider>
));
MenubarItem.displayName = MenubarPrimitive.Item.displayName;
const MenubarSubTrigger = React.forwardRef<
React.ElementRef<typeof MenubarPrimitive.SubTrigger>,
React.ComponentPropsWithoutRef<typeof MenubarPrimitive.SubTrigger> & {
inset?: boolean;
}
>(({ className, inset, children, ...props }, ref) => {
const { open } = MenubarPrimitive.useSubContext();
return (
<TextClassContext.Provider
value={cn("select-none text-sm native:text-lg text-popover-foreground")}
>
<MenubarPrimitive.SubTrigger
ref={ref}
className={cn(
"flex flex-row web:cursor-default web:select-none items-center gap-2 rounded-sm px-2 py-1.5 native:py-2 web:outline-none web:focus:bg-accent active:bg-accent web:hover:bg-accent",
open && "bg-accent",
inset && "pl-8",
className,
)}
{...props}
>
<>{children}</>
<ChevronRight size={18} className="ml-auto text-foreground" />
</MenubarPrimitive.SubTrigger>
</TextClassContext.Provider>
);
});
MenubarSubTrigger.displayName = MenubarPrimitive.SubTrigger.displayName;
const MenubarSubContent = React.forwardRef<
React.ElementRef<typeof MenubarPrimitive.SubContent>,
React.ComponentPropsWithoutRef<typeof MenubarPrimitive.SubContent>
>(({ className, ...props }, ref) => {
return (
<MenubarPrimitive.SubContent
ref={ref}
className={cn(
"z-50 min-w-[8rem] overflow-hidden rounded-md border border-border mt-1 bg-popover p-1 shadow-md shadow-foreground/5",
className,
)}
{...props}
/>
);
});
MenubarSubContent.displayName = MenubarPrimitive.SubContent.displayName;
const MenubarCheckboxItem = React.forwardRef<
React.ElementRef<typeof MenubarPrimitive.CheckboxItem>,
React.ComponentPropsWithoutRef<typeof MenubarPrimitive.CheckboxItem>
>(({ className, children, checked, ...props }, ref) => (
<MenubarPrimitive.CheckboxItem
ref={ref}
className={cn(
"relative flex flex-row web:cursor-default items-center rounded-sm py-1.5 native:py-2 pl-8 pr-2 web:outline-none web:focus:bg-accent active:bg-accent group",
props.disabled && "web:pointer-events-none opacity-50",
className,
)}
checked={checked}
{...props}
>
<View className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
<MenubarPrimitive.ItemIndicator>
<Check size={14} strokeWidth={3} className="text-foreground" />
</MenubarPrimitive.ItemIndicator>
</View>
<>{children}</>
</MenubarPrimitive.CheckboxItem>
));
MenubarCheckboxItem.displayName = MenubarPrimitive.CheckboxItem.displayName;
const MenubarRadioItem = React.forwardRef<
React.ElementRef<typeof MenubarPrimitive.RadioItem>,
React.ComponentPropsWithoutRef<typeof MenubarPrimitive.RadioItem>
>(({ className, children, ...props }, ref) => (
<MenubarPrimitive.RadioItem
ref={ref}
className={cn(
"relative flex flex-row web:cursor-default items-center rounded-sm py-1.5 native:py-2 pl-8 pr-2 web:outline-none web:focus:bg-accent active:bg-accent group",
props.disabled && "web:pointer-events-none opacity-50",
className,
)}
{...props}
>
<View className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
<MenubarPrimitive.ItemIndicator>
<View className="h-2 w-2 rounded-full bg-foreground" />
</MenubarPrimitive.ItemIndicator>
</View>
<>{children}</>
</MenubarPrimitive.RadioItem>
));
MenubarRadioItem.displayName = MenubarPrimitive.RadioItem.displayName;
const MenubarLabel = React.forwardRef<
React.ElementRef<typeof MenubarPrimitive.Label>,
React.ComponentPropsWithoutRef<typeof MenubarPrimitive.Label> & {
inset?: boolean;
}
>(({ className, inset, ...props }, ref) => (
<MenubarPrimitive.Label
ref={ref}
className={cn(
"px-2 py-1.5 text-sm native:text-base font-semibold text-foreground web:cursor-default",
inset && "pl-8",
className,
)}
{...props}
/>
));
MenubarLabel.displayName = MenubarPrimitive.Label.displayName;
const MenubarSeparator = React.forwardRef<
React.ElementRef<typeof MenubarPrimitive.Separator>,
React.ComponentPropsWithoutRef<typeof MenubarPrimitive.Separator>
>(({ className, ...props }, ref) => (
<MenubarPrimitive.Separator
ref={ref}
className={cn("-mx-1 my-1 h-px bg-border", className)}
{...props}
/>
));
MenubarSeparator.displayName = MenubarPrimitive.Separator.displayName;
const MenubarShortcut = ({
className,
...props
}: React.ComponentPropsWithoutRef<typeof View>) => {
return (
<View
className={cn("ml-auto flex flex-row items-center gap-1", className)}
{...props}
/>
);
};
MenubarShortcut.displayName = "MenubarShortcut";
export {
Menubar,
MenubarCheckboxItem,
MenubarContent,
MenubarGroup,
MenubarItem,
MenubarLabel,
MenubarMenu,
MenubarRadioGroup,
MenubarRadioItem,
MenubarSeparator,
MenubarShortcut,
MenubarSub,
MenubarSubContent,
MenubarSubTrigger,
MenubarTrigger,
};Usage
import {
Menubar,
MenubarContent,
MenubarItem,
MenubarMenu,
MenubarTrigger,
} from "@moe/registry/ui/menubar";
export function MenubarDemo() {
return (
<Menubar>
<MenubarMenu>
<MenubarTrigger>File</MenubarTrigger>
<MenubarContent>
<MenubarItem>New Tab</MenubarItem>
<MenubarItem>New Window</MenubarItem>
</MenubarContent>
</MenubarMenu>
</Menubar>
);
}