mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-24 22:52:15 +08:00
refactor: migrate to @dnd-kit/react and simplify sortable components (#7819)
* refactor: migrate to @dnd-kit/react and simplify sortable components * chore: enable css formatting in biome.json * refactor(profile): simplify icon rendering logic and add alt text * style(profiles): add overflow hidden to profile grid container * refactor(profile): add radix for parseInt and improve type safety in profiles * docs: update changelog with drag-and-drop styling improvements * refactor(profile): move hasValidName function out of ProxiesEditorViewer * refactor(proxy): refactor proxy chain into sortable components * feat(proxy): add drop state styling and arrow animations to proxy chain * style(profiles): reduce horizontal padding in profile grid * feat(profile): add vertical margin to editor viewer items * chore: refresh pnpm lockfile * fix: preserve drag and drop behavior --------- Co-authored-by: Tunglies <77394545+Tunglies@users.noreply.github.com>
This commit is contained in:
@@ -1,7 +1,3 @@
|
||||
import type {
|
||||
DraggableAttributes,
|
||||
DraggableSyntheticListeners,
|
||||
} from '@dnd-kit/core'
|
||||
import {
|
||||
alpha,
|
||||
ListItem,
|
||||
@@ -9,26 +5,17 @@ import {
|
||||
ListItemIcon,
|
||||
ListItemText,
|
||||
} from '@mui/material'
|
||||
import type { CSSProperties, PointerEvent, ReactNode } from 'react'
|
||||
import { useCallback } from 'react'
|
||||
import type { ReactNode } from 'react'
|
||||
import { useMatch, useNavigate, useResolvedPath } from 'react-router'
|
||||
|
||||
import type { SortableItemRenderProps } from '@/components/base/sortable-item'
|
||||
import { useVerge } from '@/hooks/use-verge'
|
||||
|
||||
interface SortableProps {
|
||||
setNodeRef?: (element: HTMLElement | null) => void
|
||||
attributes?: DraggableAttributes
|
||||
listeners?: DraggableSyntheticListeners
|
||||
style?: CSSProperties
|
||||
isDragging?: boolean
|
||||
disabled?: boolean
|
||||
}
|
||||
|
||||
interface Props {
|
||||
to: string
|
||||
children: string
|
||||
icon: ReactNode[]
|
||||
sortable?: SortableProps
|
||||
sortable?: SortableItemRenderProps
|
||||
}
|
||||
export const LayoutItem = (props: Props) => {
|
||||
const { to, children, icon, sortable } = props
|
||||
@@ -42,34 +29,15 @@ export const LayoutItem = (props: Props) => {
|
||||
const effectiveMenuIcon =
|
||||
navCollapsed && menu_icon === 'disable' ? 'monochrome' : menu_icon
|
||||
|
||||
const { setNodeRef, attributes, listeners, style, isDragging, disabled } =
|
||||
sortable ?? {}
|
||||
|
||||
const draggable = Boolean(sortable) && !disabled
|
||||
const { onPointerDown, ...otherListeners } = draggable
|
||||
? (listeners ?? {})
|
||||
: {}
|
||||
|
||||
const handlePointerDown = useCallback(
|
||||
(event: PointerEvent<HTMLDivElement>) => {
|
||||
onPointerDown?.(event)
|
||||
},
|
||||
[onPointerDown],
|
||||
)
|
||||
|
||||
return (
|
||||
<ListItem
|
||||
ref={setNodeRef}
|
||||
style={style}
|
||||
sx={[
|
||||
{ py: 0.5, maxWidth: 250, mx: 'auto', padding: '4px 0px' },
|
||||
isDragging ? { opacity: 0.78 } : {},
|
||||
]}
|
||||
ref={sortable?.ref}
|
||||
style={sortable?.style}
|
||||
sx={{ py: 0.5, maxWidth: 250, mx: 'auto', padding: '4px 0px' }}
|
||||
>
|
||||
<ListItemButton
|
||||
ref={sortable?.handleRef}
|
||||
selected={!!match}
|
||||
{...(draggable ? (attributes ?? {}) : {})}
|
||||
{...(draggable ? otherListeners : {})}
|
||||
sx={[
|
||||
{
|
||||
borderRadius: 2,
|
||||
@@ -77,8 +45,7 @@ export const LayoutItem = (props: Props) => {
|
||||
paddingLeft: 1,
|
||||
paddingRight: 1,
|
||||
marginRight: 1.25,
|
||||
cursor: draggable ? 'grab' : 'pointer',
|
||||
'&:active': draggable ? { cursor: 'grabbing' } : {},
|
||||
cursor: 'pointer',
|
||||
'& .MuiListItemText-primary': {
|
||||
color: 'text.primary',
|
||||
fontWeight: '700',
|
||||
@@ -99,7 +66,6 @@ export const LayoutItem = (props: Props) => {
|
||||
]}
|
||||
title={navCollapsed ? children : undefined}
|
||||
aria-label={navCollapsed ? children : undefined}
|
||||
onPointerDown={handlePointerDown}
|
||||
onClick={() => navigate(to)}
|
||||
>
|
||||
{(effectiveMenuIcon === 'monochrome' || !effectiveMenuIcon) && (
|
||||
@@ -107,16 +73,14 @@ export const LayoutItem = (props: Props) => {
|
||||
sx={{
|
||||
color: 'text.primary',
|
||||
marginLeft: '6px',
|
||||
cursor: draggable ? 'grab' : 'inherit',
|
||||
cursor: 'inherit',
|
||||
}}
|
||||
>
|
||||
{icon[0]}
|
||||
</ListItemIcon>
|
||||
)}
|
||||
{effectiveMenuIcon === 'colorful' && (
|
||||
<ListItemIcon sx={{ cursor: draggable ? 'grab' : 'inherit' }}>
|
||||
{icon[1]}
|
||||
</ListItemIcon>
|
||||
<ListItemIcon sx={{ cursor: 'inherit' }}>{icon[1]}</ListItemIcon>
|
||||
)}
|
||||
<ListItemText
|
||||
sx={{
|
||||
|
||||
Reference in New Issue
Block a user