主要问题
这个DropdownMenuItem 组件在hover的时候会导致原本的红色消失,
问了ai也解决不了(什么!important太抽象了,全局覆盖也不太好)
<DropdownMenuItem class="text-red-600">
{{delete}}
</DropdownMenuItem>
于是我决定去看看源码是不是有覆盖原本样式的代码:地址
源码如下:
<script setup lang="ts">
import { cn } from '@/lib/utils'
import { DropdownMenuItem, type DropdownMenuItemProps, useForwardProps } from 'reka-ui'
import { computed, type HTMLAttributes } from 'vue'
const props = defineProps<DropdownMenuItemProps & { class?: HTMLAttributes['class'], inset?: boolean }>()
const delegatedProps = computed(() => {
const { class: _, ...delegated } = props
return delegated
})
const forwardedProps = useForwardProps(delegatedProps)
</script>
<template>
<DropdownMenuItem
v-bind="forwardedProps"
:class="cn(
'relative flex cursor-default select-none items-center rounded-sm gap-2 px-2 py-1.5 text-sm outline-none transition-colors focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50 [&>svg]:size-4 [&>svg]:shrink-0',
inset && 'pl-8',
props.class,
)"
>
<slot />
</DropdownMenuItem>
</template>
我们可以看到有一个:
focus:text-accent-foreground
这个的意思就是说:聚焦的时候使用这个颜色(有点抽象了)
所以找到components/ui/dropdown-menu/DropdownMenuItem.vue
将里面的这个去除掉即可
ok,这个问题就解决了,遇到问题千万不要想着简单解决,要彻底解决(如果没时间那没办法)
出现了问题,兄弟们,这个如果后面部署或者新人来的时候都需要再改,那不太好
所以修改方案:
我使用
<style scoped>
/* 使用 :deep() 穿透组件作用域 */
:deep(.text-destructive) {
color: hsl(var(--destructive)) !important;
}
/* 覆盖 hover 状态 */
:deep(.text-destructive:hover) {
color: hsl(var(--destructive)) !important;
}
/* 覆盖 focus 状态 */
:deep(.text-destructive:focus) {
color: hsl(var(--destructive)) !important;
}
</style>处理了
修改全局文件好像不太好
最好使用:deep来修改样式
不对,兄弟们,源码里面的样式优先级很高,使用这个:deep需要去提升优先级,我直接内联样式了,我的天,这么个小问题学问还挺多(汗流浃背了)