136 lines
3.3 KiB
TypeScript
136 lines
3.3 KiB
TypeScript
import { cn } from "@/lib/utils"
|
|
|
|
export type SelectionRangePosition =
|
|
| "none"
|
|
| "only"
|
|
| "start"
|
|
| "middle"
|
|
| "end"
|
|
|
|
export function getSelectionRangePositions(
|
|
itemIds: string[],
|
|
selectedIds: Set<string>
|
|
): SelectionRangePosition[] {
|
|
const positions: SelectionRangePosition[] = itemIds.map((id) =>
|
|
selectedIds.has(id) ? "only" : "none"
|
|
)
|
|
|
|
let index = 0
|
|
while (index < itemIds.length) {
|
|
if (!selectedIds.has(itemIds[index])) {
|
|
index++
|
|
continue
|
|
}
|
|
|
|
const start = index
|
|
while (index < itemIds.length && selectedIds.has(itemIds[index])) {
|
|
index++
|
|
}
|
|
|
|
const end = index - 1
|
|
const count = end - start + 1
|
|
|
|
for (let position = start; position <= end; position++) {
|
|
if (count === 1) {
|
|
positions[position] = "only"
|
|
} else if (position === start) {
|
|
positions[position] = "start"
|
|
} else if (position === end) {
|
|
positions[position] = "end"
|
|
} else {
|
|
positions[position] = "middle"
|
|
}
|
|
}
|
|
}
|
|
|
|
return positions
|
|
}
|
|
|
|
export function selectionHighlightRadiusClass(
|
|
position: SelectionRangePosition
|
|
) {
|
|
switch (position) {
|
|
case "only":
|
|
return "rounded-lg"
|
|
case "start":
|
|
return "rounded-t-lg"
|
|
case "end":
|
|
return "rounded-b-lg"
|
|
default:
|
|
return ""
|
|
}
|
|
}
|
|
|
|
export const TRACK_ROW_HIGHLIGHT_INSET = "absolute inset-y-0 left-2 right-2"
|
|
|
|
/** Full-width, square-corner highlight used by the library and queue lists. */
|
|
export const FLUSH_ROW_HIGHLIGHT_INSET = "absolute inset-0"
|
|
|
|
/**
|
|
* Track row highlights are inset rounded pills by default; `flush` makes them
|
|
* full-width with square corners.
|
|
*/
|
|
export function SelectionRangeHighlight({
|
|
position,
|
|
flush = false,
|
|
}: {
|
|
position: SelectionRangePosition
|
|
flush?: boolean
|
|
}) {
|
|
if (position === "none") {
|
|
return null
|
|
}
|
|
|
|
return (
|
|
<div
|
|
aria-hidden
|
|
className={cn(
|
|
"pointer-events-none bg-primary",
|
|
flush
|
|
? FLUSH_ROW_HIGHLIGHT_INSET
|
|
: [TRACK_ROW_HIGHLIGHT_INSET, selectionHighlightRadiusClass(position)]
|
|
)}
|
|
/>
|
|
)
|
|
}
|
|
|
|
export function TrackRowHoverHighlight({ flush = false }: { flush?: boolean }) {
|
|
return (
|
|
<div
|
|
aria-hidden
|
|
className={cn(
|
|
"pointer-events-none bg-muted/60 opacity-0 transition-opacity group-hover:opacity-100",
|
|
flush ? FLUSH_ROW_HIGHLIGHT_INSET : [TRACK_ROW_HIGHLIGHT_INSET, "rounded-lg"]
|
|
)}
|
|
/>
|
|
)
|
|
}
|
|
|
|
/** Background for the active (focused or now playing) row. */
|
|
export function TrackRowActiveHighlight({ flush = false }: { flush?: boolean }) {
|
|
return (
|
|
<div
|
|
aria-hidden
|
|
className={cn(
|
|
"pointer-events-none bg-muted/60",
|
|
flush ? FLUSH_ROW_HIGHLIGHT_INSET : [TRACK_ROW_HIGHLIGHT_INSET, "rounded-lg"]
|
|
)}
|
|
/>
|
|
)
|
|
}
|
|
|
|
/** Shared horizontal padding for track rows — same when selected or not. */
|
|
export const TRACK_ROW_PADDING = "pl-8 pr-10"
|
|
|
|
/** Centers the track list in the main content area. */
|
|
export const TRACK_LIST_CONTAINER = "mx-auto w-full max-w-9/10"
|
|
|
|
/** Album track row: star, track #, title (flex), duration, menu. */
|
|
export const ALBUM_TRACK_ROW = "flex w-full min-w-0 items-center gap-x-3"
|
|
|
|
export const ALBUM_TRACK_TITLE = "min-w-0 flex-1 truncate font-normal"
|
|
|
|
export function isInSelectionRange(position: SelectionRangePosition) {
|
|
return position !== "none"
|
|
}
|