suiss/uim · React Native Component
Context Menu
A menu that opens at the touch point when an area is long-pressed.
Context Menu is an open-source mobile component from suiss/uim for React Native and Expo, with SwiftUI and Jetpack Compose versions built from the same design tokens. The @esuiss/uim CLI copies its source into your project, so you own the code.


import * as React from "react"
import { ContextMenu, ContextMenuCheckboxItem, ContextMenuContent, ContextMenuItem, ContextMenuLabel, ContextMenuRadioGroup, ContextMenuRadioItem, ContextMenuSeparator, ContextMenuSub, ContextMenuSubContent, ContextMenuSubTrigger, ContextMenuTrigger } from "@/components/ui/context-menu"
import { Text } from "@/components/ui/text"
function TriggerArea() {
return (
<ContextMenuTrigger className="border-separator h-36 w-full items-center justify-center rounded-md border border-dashed">
<Text className="text-sm text-label-secondary">Long press here</Text>
</ContextMenuTrigger>
)
}
function ContextMenuDemo({ defaultOpen }: { defaultOpen?: boolean }) {
const [showBookmarks, setShowBookmarks] = React.useState(true)
const [showUrls, setShowUrls] = React.useState(false)
const [person, setPerson] = React.useState("pedro")
return (
<ContextMenu defaultOpen={defaultOpen}>
<TriggerArea />
<ContextMenuContent className="w-64">
<ContextMenuItem inset>
<Text>Back</Text>
</ContextMenuItem>
<ContextMenuItem inset disabled>
<Text>Forward</Text>
</ContextMenuItem>
<ContextMenuItem inset>
<Text>Reload</Text>
</ContextMenuItem>
<ContextMenuSub>
<ContextMenuSubTrigger inset>
<Text>More tools</Text>
</ContextMenuSubTrigger>
<ContextMenuSubContent className="w-48">
<ContextMenuItem>
<Text>Save page</Text>
</ContextMenuItem>
<ContextMenuItem>
<Text>Create shortcut</Text>
</ContextMenuItem>
<ContextMenuSeparator />
<ContextMenuItem variant="destructive">
<Text>Delete</Text>
</ContextMenuItem>
</ContextMenuSubContent>
</ContextMenuSub>
<ContextMenuSeparator />
<ContextMenuCheckboxItem
checked={showBookmarks}
onCheckedChange={setShowBookmarks}
>
<Text>Show bookmarks</Text>
</ContextMenuCheckboxItem>
<ContextMenuCheckboxItem
checked={showUrls}
onCheckedChange={setShowUrls}
>
<Text>Show full URLs</Text>
</ContextMenuCheckboxItem>
<ContextMenuSeparator />
<ContextMenuRadioGroup value={person} onValueChange={setPerson}>
<ContextMenuLabel inset>People</ContextMenuLabel>
<ContextMenuRadioItem value="pedro">
<Text>Pedro Duarte</Text>
</ContextMenuRadioItem>
<ContextMenuRadioItem value="colm">
<Text>Colm Tuite</Text>
</ContextMenuRadioItem>
</ContextMenuRadioGroup>
</ContextMenuContent>
</ContextMenu>
)
}
export function ContextMenuOpenByDefaultExample() {
return (
<ContextMenuDemo defaultOpen />
)
}import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.PathEffect
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.unit.dp
import com.esuiss.ui.components.ContextMenu
import com.esuiss.ui.components.DropdownMenuCheckboxItem
import com.esuiss.ui.components.DropdownMenuItem
import com.esuiss.ui.components.DropdownMenuLabel
import com.esuiss.ui.components.DropdownMenuRadioGroup
import com.esuiss.ui.components.DropdownMenuRadioItem
import com.esuiss.ui.components.DropdownMenuSeparator
import com.esuiss.ui.components.DropdownMenuSub
import com.esuiss.ui.components.MenuItemVariant
import com.esuiss.ui.components.Text
import com.esuiss.ui.theme.EsuissTheme
import com.esuiss.ui.theme.Radius
import com.esuiss.ui.theme.Typography
@Composable
fun ContextMenuOpenByDefaultExample() {
ContextMenuDemo(defaultOpen = true)
}
@Composable
fun ContextMenuTriggerArea() {
val color = EsuissTheme.colors.separator
Box(
modifier = Modifier
.fillMaxWidth()
.height(144.dp)
.drawBehind {
val stroke = 1.dp.toPx()
drawRoundRect(
color = color,
topLeft = Offset(stroke / 2, stroke / 2),
size = Size(size.width - stroke, size.height - stroke),
cornerRadius = CornerRadius(Radius.md.toPx()),
style = Stroke(stroke, pathEffect = PathEffect.dashPathEffect(floatArrayOf(4.dp.toPx(), 3.dp.toPx()))),
)
},
contentAlignment = Alignment.Center,
) {
Text("Long press here", style = Typography.sm.copy(color = EsuissTheme.colors.labelSecondary))
}
}
@Composable
fun ContextMenuDemo(defaultOpen: Boolean) {
var open by rememberSaveable { mutableStateOf(defaultOpen) }
var showBookmarks by rememberSaveable { mutableStateOf(true) }
var showUrls by rememberSaveable { mutableStateOf(false) }
var person by rememberSaveable { mutableStateOf("pedro") }
ContextMenu(
expanded = open,
onExpandedChange = { open = it },
trigger = { ContextMenuTriggerArea() },
modifier = Modifier.width(256.dp),
) {
DropdownMenuItem("Back", onClick = {}, inset = true)
DropdownMenuItem("Forward", onClick = {}, inset = true, enabled = false)
DropdownMenuItem("Reload", onClick = {}, inset = true)
DropdownMenuSub("More tools", inset = true) {
Column(Modifier.width(192.dp)) {
DropdownMenuItem("Save page", onClick = {})
DropdownMenuItem("Create shortcut", onClick = {})
DropdownMenuSeparator()
DropdownMenuItem("Delete", onClick = {}, variant = MenuItemVariant.Destructive)
}
}
DropdownMenuSeparator()
DropdownMenuCheckboxItem("Show bookmarks", checked = showBookmarks, onCheckedChange = { showBookmarks = it })
DropdownMenuCheckboxItem("Show full URLs", checked = showUrls, onCheckedChange = { showUrls = it })
DropdownMenuSeparator()
DropdownMenuRadioGroup(value = person, onValueChange = { person = it }) {
DropdownMenuLabel("People", inset = true)
DropdownMenuRadioItem("pedro", "Pedro Duarte")
DropdownMenuRadioItem("colm", "Colm Tuite")
}
}
}ContextMenu attaches a menu to a ContextMenuTrigger area. A long press opens ContextMenuContent at the position of the finger with a medium impact haptic. The content renders through a portal (inside a full-window overlay on iOS) as a raised, rounded card.
Items can be plain actions, checkbox items, radio items in a group, or a submenu. ContextMenuSubContent expands in place under its trigger, and the caret turns to point down while it is open.
Installation
$ pnpm dlx @esuiss/uim@latest add context-menu$ pnpm dlx @esuiss/uim@latest add context-menu --platform swift$ pnpm dlx @esuiss/uim@latest add context-menu --platform kotlinThe CLI also adds what it builds on: Icon, Text.
Manual
- Install the dependencies:
npx expo install cn phosphor-react-native react-native-reanimated react-native-screens react-native-svg react-native-worklets-
Add the pieces it builds on: Icon, Text, and the primitives
components/primitives/context-menu.tsx,components/primitives/use-render.tsx. -
Copy the source into
components/ui/context-menu.tsx:
import { cn } from "cn"
import { CaretRightIcon, CheckIcon } from "phosphor-react-native"
import * as React from "react"
import { Platform, View } from "react-native"
import { FadeIn, ReduceMotion } from "react-native-reanimated"
import { FullWindowOverlay as RNFullWindowOverlay } from "react-native-screens"
import * as ContextMenuPrimitive from "@/components/primitives/context-menu"
import { resolveClassName } from "@/components/primitives/use-render"
import { AnimatedView } from "@/components/ui/animated-view"
import { Icon } from "@/components/ui/icon"
import { TextClassContext } from "@/components/ui/text"
const ContextMenu = ContextMenuPrimitive.Root
const ContextMenuTrigger = ContextMenuPrimitive.Trigger- Update the import paths to match your project.
- Install the dependencies:
npx expo install cn phosphor-react-native react-native-reanimated react-native-screens react-native-svg react-native-worklets-
Add the pieces it builds on: Icon, Text, and the primitives
components/primitives/context-menu.tsx,components/primitives/use-render.tsx. -
Copy the source into
components/ui/context-menu.tsx:
import { cn } from "cn"
import { CaretRightIcon, CheckIcon } from "phosphor-react-native"
import * as React from "react"
import { Platform, View } from "react-native"
import { FadeIn, ReduceMotion } from "react-native-reanimated"
import { FullWindowOverlay as RNFullWindowOverlay } from "react-native-screens"
import * as ContextMenuPrimitive from "@/components/primitives/context-menu"
import { resolveClassName } from "@/components/primitives/use-render"
import { AnimatedView } from "@/components/ui/animated-view"
import { Icon } from "@/components/ui/icon"
import { TextClassContext } from "@/components/ui/text"
const ContextMenu = ContextMenuPrimitive.Root
const ContextMenuTrigger = ContextMenuPrimitive.Trigger- Update the import paths to match your project.
- Install the dependencies:
npx expo install phosphor-react-native react-native-reanimated react-native-screens react-native-svg react-native-worklets-
Add the pieces it builds on: Icon, Text, and the primitives
components/primitives/context-menu.tsx,components/primitives/use-render.tsx. -
Copy the source into
components/ui/context-menu.tsx:
import type { IconProps as PhosphorIconProps } from "phosphor-react-native"
import { CaretRightIcon, CheckIcon } from "phosphor-react-native"
import * as React from "react"
import {
Platform,
type StyleProp,
StyleSheet,
type TextStyle,
View,
type ViewStyle,
} from "react-native"
import { FadeIn, ReduceMotion } from "react-native-reanimated"
import { FullWindowOverlay as RNFullWindowOverlay } from "react-native-screens"
import * as ContextMenuPrimitive from "@/components/primitives/context-menu"
import { resolveStyle } from "@/components/primitives/use-render"
import { AnimatedView } from "@/components/ui/animated-view"- Update the import paths to match your project.
-
Target iOS 17 or later.
-
Add the Swift package to your app:
.package(url: "https://github.com/phosphor-icons/swift", from: "2.1.0")- Copy the theme files every view uses:
import SwiftUI
import UIKit
struct RGBA: Sendable {
let red: CGFloat
let green: CGFloat
let blue: CGFloat
let alpha: CGFloat
init(_ hex: UInt32, _ alpha: CGFloat = 1) {
red = CGFloat((hex >> 16) & 0xFF) / 255
green = CGFloat((hex >> 8) & 0xFF) / 255
blue = CGFloat(hex & 0xFF) / 255
self.alpha = alpha
}import SwiftUI
extension Color {
public static let surface = Color.adaptive(light: .init(0xFFFFFF), dark: .init(0x000000))
public static let surfaceSecondary = Color.adaptive(light: .init(0xF5F5F7), dark: .init(0x161617))
public static let surfaceTertiary = Color.adaptive(light: .init(0xE8E8ED), dark: .init(0x2C2C2E))
public static let surfaceRaised = Color.adaptive(light: .init(0xFFFFFF), dark: .init(0x1D1D1F))
public static let label = Color.adaptive(light: .init(0x1D1D1F), dark: .init(0xF5F5F7))
public static let labelSecondary = Color.adaptive(light: .init(0x6E6E73), dark: .init(0xA1A1A6))
public static let labelTertiary = Color.adaptive(light: .init(0x86868B), dark: .init(0x86868B))
public static let labelQuaternary = Color.adaptive(light: .init(0xD2D2D7), dark: .init(0x424245))
public static let control = Color.adaptive(light: .init(0x000000, 0.04), dark: .init(0xFFFFFF, 0.1))
public static let controlPressed = Color.adaptive(light: .init(0x000000, 0.12), dark: .init(0xFFFFFF, 0.22))
public static let controlSelected = Color.adaptive(light: .init(0x1D1D1F), dark: .init(0xF5F5F7))
public static let controlDisabled = Color.adaptive(light: .init(0x000000, 0.03), dark: .init(0xFFFFFF, 0.05))
public static let onControlSelected = Color.adaptive(light: .init(0xFFFFFF), dark: .init(0x000000))- Copy the source and every file it uses:
import SwiftUI
public typealias ContextMenuItem<Label: View> = DropdownMenuItem<Label>
public typealias ContextMenuCheckboxItem<Label: View> = DropdownMenuCheckboxItem<Label>
public typealias ContextMenuRadioGroup<Content: View> = DropdownMenuRadioGroup<Content>
public typealias ContextMenuRadioItem<Label: View> = DropdownMenuRadioItem<Label>
public typealias ContextMenuLabel = DropdownMenuLabel
public typealias ContextMenuGroup<Content: View> = DropdownMenuGroup<Content>
public typealias ContextMenuSeparator = DropdownMenuSeparator
public typealias ContextMenuSub<Label: View, Content: View> = DropdownMenuSub<Label, Content>
extension View {
public func contextMenu<Items: View, Preview: View>(
@ViewBuilder items: () -> Items,
@ViewBuilder preview: () -> Preview,
previewRadius: CGFloatimport SwiftUI
public struct VariantButtonStyle: ButtonStyle {
public enum Variant: CaseIterable, Sendable {
case `default`, outline, secondary, tinted, ghost, plain, destructive, link
}
public enum Size: CaseIterable, Sendable {
case `default`, xs, sm, lg, xl, icon, iconXs, iconSm, iconLg, iconXl
var height: CGFloat {
switch self {
case .xs, .iconXs: 24
case .sm, .iconSm: 32
case .default, .icon: 36
case .lg, .iconLg: 44import SwiftUI
extension EnvironmentValues {
@Entry var buttonGroup: Axis? = nil
@Entry var buttonGroupSize: CGSize = .zero
}
enum ButtonGroupSpace: Hashable {
case group
}
struct ButtonGroupFrame {
let bounds: Anchor<CGRect>
let outlined: Bool
}import PhosphorSwift
import SwiftUI
extension Shadow {
static let control = Shadow(color: .black.opacity(0.08), radius: 0.5, y: 0.5)
static let thumb = Shadow(color: .black.opacity(0.12), radius: 1.5, y: 1)
}
struct SelectionIndicator<Mark: View>: View {
let checked: Bool
let pressed: Bool
let cornerRadius: CGFloat
let mark: Mark
@Environment(\.isEnabled) private var isEnabled
@Environment(\.isInvalid) private var invalidimport PhosphorSwift
import SwiftUI
enum MenuPresentation: Sendable {
case system, popup
}
extension EnvironmentValues {
@Entry var menuPresentation: MenuPresentation = .system
@Entry var menuRadio = MenuRadioContext()
}
struct MenuRadioContext: @unchecked Sendable {
var selected: AnyHashable?
var select: @MainActor @Sendable (AnyHashable) -> Void = { _ in }
}import SwiftUI
extension EnvironmentValues {
@Entry public var isInvalid = false
@Entry var fieldInset = false
@Entry var fieldGroupInset = false
@Entry var fieldSetInset = false
}
extension View {
public func invalid(_ isInvalid: Bool = true) -> some View {
environment(\.isInvalid, isInvalid)
}
}
public enum FieldOrientation: CaseIterable, Sendable {import SwiftUI
extension EnvironmentValues {
@Entry public var hapticsEnabled = true
}
extension View {
public func haptics(_ enabled: Bool) -> some View {
environment(\.hapticsEnabled, enabled)
}
func hapticFeedback<Trigger: Equatable>(_ feedback: SensoryFeedback, trigger: Trigger) -> some View {
modifier(HapticFeedbackModifier(feedback: feedback, trigger: trigger) { _, _ in true })
}
func hapticFeedback<Trigger: Equatable>(import PhosphorSwift
import Observation
import SwiftUI
public enum PopupAlignment: Sendable, CaseIterable {
case start, center, end
}
struct OverlayDismiss: Sendable {
let action: @MainActor @Sendable () -> Void
@MainActor func callAsFunction() {
action()
}
}import PhosphorSwift
import SwiftUI
struct SelectContext: @unchecked Sendable {
var selected: AnyHashable?
var registering = false
var select: @MainActor @Sendable (AnyHashable) -> Void = { _ in }
}
extension EnvironmentValues {
@Entry var select = SelectContext()
}
struct SelectLabelsKey: PreferenceKey {
static var defaultValue: [AnyHashable: Text] { [:] }import SwiftUI
public struct Separator: View {
let orientation: Axis
public init(_ orientation: Axis = .horizontal) {
self.orientation = orientation
}
public var body: some View {
Rectangle()
.fill(Color.separator)
.frame(width: orientation == .vertical ? 1 : nil, height: orientation == .horizontal ? 1 : nil)
.frame(maxWidth: orientation == .horizontal ? .infinity : nil, maxHeight: orientation == .vertical ? .infinity : nil)
.accessibilityHidden(true)
}import SwiftUI
public enum TextVariant: CaseIterable, Sendable {
case `default`, h1, h2, h3, h4, p, blockquote, code, lead, large, small, muted
var style: TextStyle {
switch self {
case .h1: .xl3
case .h2: .xl2
case .h3: .xl
case .h4, .lead, .large: .lg
case .code, .small, .muted: .sm
default: .base
}
}- Add the files to your app target.
- Set
minSdkto 24 or higher and add the dependencies:
implementation(platform("androidx.compose:compose-bom:2026.09.00"))
implementation("androidx.compose.foundation:foundation")
implementation("androidx.compose.ui:ui")
implementation("com.adamglin:phosphor-icon-android:1.0.0")- Copy the theme files every view uses:
package com.esuiss.ui.theme
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.dropShadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.shadow.Shadow as DropShadow
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.LineHeightStyle
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpOffsetpackage com.esuiss.ui.theme
import androidx.compose.runtime.Immutable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
@Immutable
data class Colors(
val surface: Color,
val surfaceSecondary: Color,
val surfaceTertiary: Color,
val surfaceRaised: Color,
val label: Color,
val labelSecondary: Color,
val labelTertiary: Color,
val labelQuaternary: Color,- Copy the source and every file it uses:
package com.esuiss.ui.components
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.semantics.onLongClick
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.roundpackage com.esuiss.ui.components
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShapepackage com.esuiss.ui.components
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.CornerSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.Immutablepackage com.esuiss.ui.components
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.selection.triStateToggleable
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Colorpackage com.esuiss.ui.components
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
val LocalContentStyle = compositionLocalOf { TextStyle.Default }
val LocalContentColor = compositionLocalOf { Color.Unspecified }
val LocalIconSize = compositionLocalOf { 20.dp }package com.esuiss.ui.components
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightpackage com.esuiss.ui.components
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.LayoutScopeMarker
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextpackage com.esuiss.ui.components
import android.provider.Settings
import androidx.compose.animation.core.CubicBezierEasing
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.text.selection.TextSelectionColors
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Sizepackage com.esuiss.ui.components
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.hapticfeedback.HapticFeedback
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.LocalHapticFeedback
val LocalHapticsEnabled = staticCompositionLocalOf { true }
@Composable
fun ProvideHaptics(enabled: Boolean, content: @Composable () -> Unit) {
CompositionLocalProvider(LocalHapticsEnabled provides enabled, content = content)
}package com.esuiss.ui.components
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.takeOrElse
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.Dp
import com.esuiss.ui.theme.EsuissTheme
@Composable
fun Icon(
imageVector: ImageVector,package com.esuiss.ui.components
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.esuiss.ui.theme.EsuissTheme
import com.esuiss.ui.theme.Typography
@Composablepackage com.esuiss.ui.components
import android.os.Build
import android.view.Window
import android.view.WindowManager
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.EnterExitState
import androidx.compose.animation.core.MutableTransitionState
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.animation.slideInpackage com.esuiss.ui.components
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.esuiss.ui.theme.EsuissTheme
@Composable
fun Separator(- Update the
com.esuiss.uipackage names to match your project.
Usage
import { ContextMenuContent, ContextMenuSubTrigger, ContextMenuSubContent, ContextMenuItem, ContextMenuCheckboxItem, ContextMenuRadioItem, ContextMenuLabel, ContextMenuSeparator } from "@/components/ui/context-menu"<ContextMenuDemo defaultOpen />Wrap the long-pressable area in ContextMenuTrigger and list ContextMenuItems in ContextMenuContent. Items close the menu when pressed unless closeOnPress={false}.
Use ContextMenuCheckboxItem with checked and onCheckedChange, and ContextMenuRadioItem inside a ContextMenuRadioGroup with value and onValueChange. Group items with ContextMenuGroup and ContextMenuLabel, and divide sections with ContextMenuSeparator.
Set variant="destructive" on an item that deletes or removes. Use inset on items without an icon to align them with items that have one.
Examples
Default


import * as React from "react"
import { ContextMenu, ContextMenuCheckboxItem, ContextMenuContent, ContextMenuItem, ContextMenuLabel, ContextMenuRadioGroup, ContextMenuRadioItem, ContextMenuSeparator, ContextMenuSub, ContextMenuSubContent, ContextMenuSubTrigger, ContextMenuTrigger } from "@/components/ui/context-menu"
import { Text } from "@/components/ui/text"
function TriggerArea() {
return (
<ContextMenuTrigger className="border-separator h-36 w-full items-center justify-center rounded-md border border-dashed">
<Text className="text-sm text-label-secondary">Long press here</Text>
</ContextMenuTrigger>
)
}
function ContextMenuDemo({ defaultOpen }: { defaultOpen?: boolean }) {
const [showBookmarks, setShowBookmarks] = React.useState(true)
const [showUrls, setShowUrls] = React.useState(false)
const [person, setPerson] = React.useState("pedro")
return (
<ContextMenu defaultOpen={defaultOpen}>
<TriggerArea />
<ContextMenuContent className="w-64">
<ContextMenuItem inset>
<Text>Back</Text>
</ContextMenuItem>
<ContextMenuItem inset disabled>
<Text>Forward</Text>
</ContextMenuItem>
<ContextMenuItem inset>
<Text>Reload</Text>
</ContextMenuItem>
<ContextMenuSub>
<ContextMenuSubTrigger inset>
<Text>More tools</Text>
</ContextMenuSubTrigger>
<ContextMenuSubContent className="w-48">
<ContextMenuItem>
<Text>Save page</Text>
</ContextMenuItem>
<ContextMenuItem>
<Text>Create shortcut</Text>
</ContextMenuItem>
<ContextMenuSeparator />
<ContextMenuItem variant="destructive">
<Text>Delete</Text>
</ContextMenuItem>
</ContextMenuSubContent>
</ContextMenuSub>
<ContextMenuSeparator />
<ContextMenuCheckboxItem
checked={showBookmarks}
onCheckedChange={setShowBookmarks}
>
<Text>Show bookmarks</Text>
</ContextMenuCheckboxItem>
<ContextMenuCheckboxItem
checked={showUrls}
onCheckedChange={setShowUrls}
>
<Text>Show full URLs</Text>
</ContextMenuCheckboxItem>
<ContextMenuSeparator />
<ContextMenuRadioGroup value={person} onValueChange={setPerson}>
<ContextMenuLabel inset>People</ContextMenuLabel>
<ContextMenuRadioItem value="pedro">
<Text>Pedro Duarte</Text>
</ContextMenuRadioItem>
<ContextMenuRadioItem value="colm">
<Text>Colm Tuite</Text>
</ContextMenuRadioItem>
</ContextMenuRadioGroup>
</ContextMenuContent>
</ContextMenu>
)
}
export function ContextMenuExample() {
return (
<ContextMenuDemo />
)
}import EsuissUI
import SwiftUI
struct ContextMenuTriggerArea: View {
var body: some View {
Text("Long press here")
.textStyle(.sm)
.foregroundStyle(Color.labelSecondary)
.frame(maxWidth: .infinity)
.frame(height: 144)
.background(Color.surface, in: RoundedRectangle(cornerRadius: Radius.md, style: .continuous))
.overlay {
RoundedRectangle(cornerRadius: Radius.md, style: .continuous)
.strokeBorder(Color.separator, style: StrokeStyle(lineWidth: 1, dash: [4, 3]))
}
}
}
struct ContextMenuDemo: View {
@State private var showBookmarks = true
@State private var showUrls = false
@State private var person = "pedro"
var body: some View {
ContextMenuTriggerArea()
.contextMenu(previewRadius: Radius.md) {
ContextMenuItem("Back", inset: true) {}
ContextMenuItem("Forward", inset: true) {}
.disabled(true)
ContextMenuItem("Reload", inset: true) {}
ContextMenuSub("More tools", inset: true) {
ContextMenuItem("Save page") {}
ContextMenuItem("Create shortcut") {}
ContextMenuSeparator()
ContextMenuItem("Delete", variant: .destructive) {}
}
ContextMenuSeparator()
ContextMenuCheckboxItem("Show bookmarks", isOn: $showBookmarks)
ContextMenuCheckboxItem("Show full URLs", isOn: $showUrls)
ContextMenuSeparator()
ContextMenuRadioGroup(selection: $person) {
ContextMenuLabel("People", inset: true)
ContextMenuRadioItem("Pedro Duarte", value: "pedro")
ContextMenuRadioItem("Colm Tuite", value: "colm")
}
}
}
}import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.PathEffect
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.unit.dp
import com.esuiss.ui.components.ContextMenu
import com.esuiss.ui.components.DropdownMenuCheckboxItem
import com.esuiss.ui.components.DropdownMenuItem
import com.esuiss.ui.components.DropdownMenuLabel
import com.esuiss.ui.components.DropdownMenuRadioGroup
import com.esuiss.ui.components.DropdownMenuRadioItem
import com.esuiss.ui.components.DropdownMenuSeparator
import com.esuiss.ui.components.DropdownMenuSub
import com.esuiss.ui.components.MenuItemVariant
import com.esuiss.ui.components.Text
import com.esuiss.ui.theme.EsuissTheme
import com.esuiss.ui.theme.Radius
import com.esuiss.ui.theme.Typography
@Composable
fun ContextMenuExample() {
ContextMenuDemo(defaultOpen = false)
}
@Composable
fun ContextMenuTriggerArea() {
val color = EsuissTheme.colors.separator
Box(
modifier = Modifier
.fillMaxWidth()
.height(144.dp)
.drawBehind {
val stroke = 1.dp.toPx()
drawRoundRect(
color = color,
topLeft = Offset(stroke / 2, stroke / 2),
size = Size(size.width - stroke, size.height - stroke),
cornerRadius = CornerRadius(Radius.md.toPx()),
style = Stroke(stroke, pathEffect = PathEffect.dashPathEffect(floatArrayOf(4.dp.toPx(), 3.dp.toPx()))),
)
},
contentAlignment = Alignment.Center,
) {
Text("Long press here", style = Typography.sm.copy(color = EsuissTheme.colors.labelSecondary))
}
}
@Composable
fun ContextMenuDemo(defaultOpen: Boolean) {
var open by rememberSaveable { mutableStateOf(defaultOpen) }
var showBookmarks by rememberSaveable { mutableStateOf(true) }
var showUrls by rememberSaveable { mutableStateOf(false) }
var person by rememberSaveable { mutableStateOf("pedro") }
ContextMenu(
expanded = open,
onExpandedChange = { open = it },
trigger = { ContextMenuTriggerArea() },
modifier = Modifier.width(256.dp),
) {
DropdownMenuItem("Back", onClick = {}, inset = true)
DropdownMenuItem("Forward", onClick = {}, inset = true, enabled = false)
DropdownMenuItem("Reload", onClick = {}, inset = true)
DropdownMenuSub("More tools", inset = true) {
Column(Modifier.width(192.dp)) {
DropdownMenuItem("Save page", onClick = {})
DropdownMenuItem("Create shortcut", onClick = {})
DropdownMenuSeparator()
DropdownMenuItem("Delete", onClick = {}, variant = MenuItemVariant.Destructive)
}
}
DropdownMenuSeparator()
DropdownMenuCheckboxItem("Show bookmarks", checked = showBookmarks, onCheckedChange = { showBookmarks = it })
DropdownMenuCheckboxItem("Show full URLs", checked = showUrls, onCheckedChange = { showUrls = it })
DropdownMenuSeparator()
DropdownMenuRadioGroup(value = person, onValueChange = { person = it }) {
DropdownMenuLabel("People", inset = true)
DropdownMenuRadioItem("pedro", "Pedro Duarte")
DropdownMenuRadioItem("colm", "Colm Tuite")
}
}
}Simple
A short list of plain items.


import { View } from "react-native"
import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger } from "@/components/ui/context-menu"
import { Text } from "@/components/ui/text"
function TriggerArea() {
return (
<ContextMenuTrigger className="border-separator h-36 w-full items-center justify-center rounded-md border border-dashed">
<Text className="text-sm text-label-secondary">Long press here</Text>
</ContextMenuTrigger>
)
}
export function ContextMenuSimpleExample() {
return (
<View>
<ContextMenu>
<TriggerArea />
<ContextMenuContent className="w-48">
<ContextMenuItem>
<Text>Profile</Text>
</ContextMenuItem>
<ContextMenuItem>
<Text>Billing</Text>
</ContextMenuItem>
<ContextMenuItem>
<Text>Team</Text>
</ContextMenuItem>
<ContextMenuItem>
<Text>Subscription</Text>
</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>
</View>
)
}import EsuissUI
import SwiftUI
struct ContextMenuSimpleExample: View {
var body: some View {
ContextMenuTriggerArea()
.contextMenu(previewRadius: Radius.md) {
ContextMenuItem("Profile") {}
ContextMenuItem("Billing") {}
ContextMenuItem("Team") {}
ContextMenuItem("Subscription") {}
}
}
}
struct ContextMenuTriggerArea: View {
var body: some View {
Text("Long press here")
.textStyle(.sm)
.foregroundStyle(Color.labelSecondary)
.frame(maxWidth: .infinity)
.frame(height: 144)
.background(Color.surface, in: RoundedRectangle(cornerRadius: Radius.md, style: .continuous))
.overlay {
RoundedRectangle(cornerRadius: Radius.md, style: .continuous)
.strokeBorder(Color.separator, style: StrokeStyle(lineWidth: 1, dash: [4, 3]))
}
}
}import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.PathEffect
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.unit.dp
import com.esuiss.ui.components.ContextMenu
import com.esuiss.ui.components.DropdownMenuItem
import com.esuiss.ui.components.Text
import com.esuiss.ui.theme.EsuissTheme
import com.esuiss.ui.theme.Radius
import com.esuiss.ui.theme.Typography
@Composable
fun ContextMenuTriggerArea() {
val color = EsuissTheme.colors.separator
Box(
modifier = Modifier
.fillMaxWidth()
.height(144.dp)
.drawBehind {
val stroke = 1.dp.toPx()
drawRoundRect(
color = color,
topLeft = Offset(stroke / 2, stroke / 2),
size = Size(size.width - stroke, size.height - stroke),
cornerRadius = CornerRadius(Radius.md.toPx()),
style = Stroke(stroke, pathEffect = PathEffect.dashPathEffect(floatArrayOf(4.dp.toPx(), 3.dp.toPx()))),
)
},
contentAlignment = Alignment.Center,
) {
Text("Long press here", style = Typography.sm.copy(color = EsuissTheme.colors.labelSecondary))
}
}
@Composable
fun ContextMenuSimple() {
var open by rememberSaveable { mutableStateOf(false) }
ContextMenu(
expanded = open,
onExpandedChange = { open = it },
trigger = { ContextMenuTriggerArea() },
modifier = Modifier.width(192.dp),
) {
DropdownMenuItem("Profile", onClick = {})
DropdownMenuItem("Billing", onClick = {})
DropdownMenuItem("Team", onClick = {})
DropdownMenuItem("Subscription", onClick = {})
}
}API Reference
ContextMenuContent
Floating menu card.
React.ComponentProps<typeof ContextMenuPrimitive.Popup>PositionerProps- About
- Name of the portal host to render into.
- Type
string- Default
- —
ContextMenuSubTrigger
Row that expands the submenu.
React.ComponentProps<typeof ContextMenuPrimitive.SubmenuTrigger>- Type
React.ReactNode- Default
- —
- About
- Extra classes for the caret icon.
- Type
string- Default
- —
- About
- Adds start padding.
- Type
boolean- Default
- —
ContextMenuSubContent
Items of the submenu.
React.ComponentProps<typeof ContextMenuPrimitive.Popup>ContextMenuItem
Action row.
React.ComponentProps<typeof ContextMenuPrimitive.Item>- About
- Adds start padding to align with icon items.
- Type
boolean- Default
- —
- About
defaultordestructive.- Type
"default""destructive"- Default
- "default"
ContextMenuCheckboxItem
Row with a check mark that toggles.
React.ComponentProps<typeof ContextMenuPrimitive.CheckboxItem>- Type
React.ReactNode- Default
- —
- About
- Adds start padding.
- Type
boolean- Default
- —
ContextMenuRadioItem
Row that selects one value in its group.
React.ComponentProps<typeof ContextMenuPrimitive.RadioItem>- Type
React.ReactNode- Default
- —
- About
- Adds start padding.
- Type
boolean- Default
- —
ContextMenuLabel
Heading for a group.
React.ComponentProps<typeof ContextMenuPrimitive.GroupLabel>- About
- Adds start padding.
- Type
boolean- Default
- —
ContextMenuSeparator
Line between sections.
React.ComponentProps<typeof ContextMenuPrimitive.Separator>Accessibility
- The trigger exposes a
longpressaccessibility action, so VoiceOver and TalkBack users can open the menu, and reports itsexpandedstate. - The popup has the
menurole; items usemenuitem,checkboxandradioroles, and radio groups useradiogroup. - Separators have the
separatorrole.
Notes
- Uses
bg-surface-raised,bg-item-pressedandbg-danger-surface-pressedtokens; light and dark follow the system appearance. - The fade-in respects the system Reduce Motion setting.