141 lines
4.7 KiB
TypeScript
141 lines
4.7 KiB
TypeScript
import { Ionicons } from "@expo/vector-icons"
|
|
import type { ComponentProps } from "react"
|
|
import type { MediaAction } from "@/components/media-context-menu"
|
|
import type { MenuComponentProps } from "@expo/ui/community/menu"
|
|
import {
|
|
DropdownMenu,
|
|
DropdownMenuItem,
|
|
Host,
|
|
RNHostView,
|
|
Text,
|
|
} from "@expo/ui/jetpack-compose"
|
|
import { useState } from "react"
|
|
import { Pressable, View } from "react-native"
|
|
import { Gesture, GestureDetector } from "react-native-gesture-handler"
|
|
|
|
import { appleMusic } from "@/lib/theme"
|
|
|
|
const actionIcons = {
|
|
share: "share-outline",
|
|
playlist: "list-outline",
|
|
"play-next": "play-forward-outline",
|
|
"play-last": "list",
|
|
shuffle: "shuffle",
|
|
"create-station": "radio-outline",
|
|
favorite: "star-outline",
|
|
"go-artist": "person-outline",
|
|
"go-album": "disc-outline",
|
|
"delete-playlist": "trash-outline",
|
|
} satisfies Record<MediaAction, ComponentProps<typeof Ionicons>["name"]>
|
|
|
|
// The community MenuView creates a dynamic-color Host internally. Own this
|
|
// Android host so menus use the same fixed palette as the rest of the app.
|
|
export function MenuView({
|
|
actions,
|
|
children,
|
|
style,
|
|
shouldOpenOnLongPress,
|
|
onPressAction,
|
|
}: MenuComponentProps) {
|
|
const [expanded, setExpanded] = useState(false)
|
|
const longPress = Gesture.LongPress()
|
|
.runOnJS(true)
|
|
.onStart(() => setExpanded(true))
|
|
return (
|
|
<View style={style}>
|
|
<Host
|
|
colorScheme="dark"
|
|
seedColor={appleMusic.pink}
|
|
colorOverrides={{
|
|
primary: appleMusic.pink,
|
|
onPrimary: "#FFFFFF",
|
|
surface: "#000000",
|
|
onSurface: "#FFFFFF",
|
|
onSurfaceVariant: "#FFFFFF",
|
|
surfaceContainer: "#1C1C1E",
|
|
surfaceContainerHigh: "#1C1C1E",
|
|
surfaceTint: "#000000",
|
|
outline: "#545458",
|
|
outlineVariant: "#545458",
|
|
error: "#FF453A",
|
|
}}
|
|
// Rows fill their parent width; compact action buttons keep intrinsic sizing.
|
|
matchContents={shouldOpenOnLongPress ? { vertical: true } : true}
|
|
>
|
|
<DropdownMenu
|
|
expanded={expanded}
|
|
onDismissRequest={() => setExpanded(false)}
|
|
color={appleMusic.elevated}
|
|
cornerRadius={4}
|
|
shadowElevation={0}
|
|
properties={{ clippingEnabled: false }}
|
|
>
|
|
<DropdownMenu.Trigger>
|
|
<RNHostView
|
|
matchContents={shouldOpenOnLongPress ? { vertical: true } : true}
|
|
>
|
|
{shouldOpenOnLongPress ? (
|
|
<GestureDetector gesture={longPress}>
|
|
<View collapsable={false}>{children}</View>
|
|
</GestureDetector>
|
|
) : (
|
|
<Pressable
|
|
onPress={() => setExpanded(true)}
|
|
accessible={false}
|
|
focusable={false}
|
|
android_disableSound
|
|
>
|
|
<View pointerEvents="none">{children}</View>
|
|
</Pressable>
|
|
)}
|
|
</RNHostView>
|
|
</DropdownMenu.Trigger>
|
|
<DropdownMenu.Items>
|
|
{actions
|
|
.filter((action) => !action.attributes?.hidden)
|
|
.map((action) => (
|
|
<DropdownMenuItem
|
|
key={action.id ?? action.title}
|
|
enabled={!action.attributes?.disabled}
|
|
elementColors={{
|
|
textColor: action.attributes?.destructive
|
|
? "#FF453A"
|
|
: "#FFFFFF",
|
|
}}
|
|
onClick={() => {
|
|
setExpanded(false)
|
|
onPressAction?.({
|
|
nativeEvent: { event: action.id ?? action.title },
|
|
})
|
|
}}
|
|
>
|
|
<DropdownMenuItem.LeadingIcon>
|
|
<RNHostView matchContents>
|
|
<Ionicons
|
|
name={
|
|
action.id === "favorite" &&
|
|
action.image === "star.fill"
|
|
? "star"
|
|
: (actionIcons[action.id as MediaAction] ??
|
|
"ellipsis-horizontal")
|
|
}
|
|
size={20}
|
|
color={
|
|
action.attributes?.destructive ? "#FF453A" : "#FFFFFF"
|
|
}
|
|
accessible={false}
|
|
/>
|
|
</RNHostView>
|
|
</DropdownMenuItem.LeadingIcon>
|
|
<DropdownMenuItem.Text>
|
|
<Text>{action.title}</Text>
|
|
</DropdownMenuItem.Text>
|
|
</DropdownMenuItem>
|
|
))}
|
|
</DropdownMenu.Items>
|
|
</DropdownMenu>
|
|
</Host>
|
|
</View>
|
|
)
|
|
}
|