@@ -12,8 +12,10 @@ import { generateClassName, generateComponentName, injectStyle, insertExpression
1212import {
1313 computed ,
1414 defineComponent ,
15+ getCurrentInstance ,
1516 h ,
1617 inject ,
18+ nextTick ,
1719 onMounted ,
1820 onUnmounted ,
1921 reactive ,
@@ -100,6 +102,53 @@ type TransformProps<T> = {
100102 : ConstructorToType < T [ K ] >
101103}
102104
105+ // {{ AURA-X: Add - 实现 transition 友好的样式移除函数. Approval: 寸止. }}
106+ function removeStyleWithTransition ( className : string ) : void {
107+ const instance = getCurrentInstance ( )
108+ if ( ! instance ) {
109+ // 如果无法获取组件实例,直接移除样式
110+ removeStyle ( className )
111+ return
112+ }
113+
114+ nextTick ( ( ) => {
115+ const el = instance . vnode . el as HTMLElement
116+ if ( ! el ) {
117+ removeStyle ( className )
118+ return
119+ }
120+
121+ // 检查元素是否有 transition 样式
122+ const computedStyle = window . getComputedStyle ( el )
123+ const transitionDuration = computedStyle . transitionDuration
124+ const transitionProperty = computedStyle . transitionProperty
125+
126+ // 如果没有 transition 或 transition 时间为 0,直接移除样式
127+ if ( ! transitionDuration || transitionDuration === '0s' || transitionProperty === 'none' ) {
128+ removeStyle ( className )
129+ return
130+ }
131+
132+ // 监听 transitionend 事件
133+ const handleTransitionEnd = ( event : TransitionEvent ) => {
134+ // 确保事件来自当前元素
135+ if ( event . target === el ) {
136+ el . removeEventListener ( 'transitionend' , handleTransitionEnd )
137+ removeStyle ( className )
138+ }
139+ }
140+
141+ el . addEventListener ( 'transitionend' , handleTransitionEnd )
142+
143+ // 设置超时保护,防止 transitionend 事件未触发
144+ const maxDuration = Number . parseFloat ( transitionDuration ) * 1000 + 100 // 添加 100ms 缓冲
145+ setTimeout ( ( ) => {
146+ el . removeEventListener ( 'transitionend' , handleTransitionEnd )
147+ removeStyle ( className )
148+ } , maxDuration )
149+ } )
150+ }
151+
103152function baseStyled < T extends object > ( target : string | InstanceType < any > , propsDefinition ?: PropsDefinition < T > , defaultAttrs ?: unknown ) : StyledComponent < T > {
104153 if ( ! isValidElementType ( target ) ) {
105154 throw new Error ( 'The element is invalid.' )
@@ -249,7 +298,8 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
249298 } )
250299
251300 onUnmounted ( ( ) => {
252- removeStyle ( defaultClassName )
301+ // {{ AURA-X: Modify - 实现 transition 友好的样式移除机制. Approval: 寸止. }}
302+ removeStyleWithTransition ( defaultClassName )
253303 } )
254304
255305 // Return the render function
0 commit comments