optimize (components): ButtonIcon with smarter class merging and tippy #463
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
描述
ButtonIcon
图标按钮需要同时支持默认样式和自定义样式,当存在自定义样式时,应该覆盖那些和默认样式相同的类,保留默认样式中与自定义样式中不重叠的类,原代码中使用计算属性cls
解决这个问题https://github.com/soybeanjs/soybean-admin/blob/086bad474eebfa38c91c7795d280251cc15de1fd/src/components/custom/button-icon.vue#L37C1-L49C4
但是,这样的写法并没有很好的扩展性,当组件每多一个默认样式类,就需要多一个
if
判断,而且对于bg-red-300
这种形式的类名使用includes
方法来进行判断就不是很适合了。解决
使用
tailwind-merge
库来处理css
合并时需要处理的逻辑,他的功能描述与我们的需求 100% 契合新的问题
项目中使用了
h-36px
而不是h-[36px]
或h-9
的方式声明类名,而后面的2种方式才可以被tailwind-merge
识别并自动合并类,故代码中写为h-[36px]
,这是由于unocss
和tailwindcss
语法差异导致的,经测试能达到需要的效果。关于使用
tailwind-merge
在unocss
框架下的可行性讨论,目前认为是 可以使用 的,详见,unocss/unocss#2748
其他修改
tooltip
的逻辑可以使用disabled
属性来直接控制,不在需要做v-if
的判断,对相关代码进行了简化PS:因为对项目还不是很熟悉,可能对相关
ButtonIcon
的引用测试不到位,还请 CR 的时候帮忙测试一下~