@@ -28,6 +28,8 @@ import {
2828 onSettled ,
2929 createProjection ,
3030 createStore ,
31+ merge ,
32+ omit ,
3133 Show ,
3234 Switch ,
3335 Match ,
@@ -38,6 +40,7 @@ import {
3840} from "solid-js" ;
3941import {
4042 Portal ,
43+ Dynamic ,
4144 dynamic ,
4245 httpStatus ,
4346 httpHeader ,
@@ -2213,6 +2216,37 @@ function DynamicChildrenKeyBeforePropMemo() {
22132216 return < Comp children = { < i > icon</ i > } aria-label = { n ( ) ? `a ${ n ( ) } ` : `b ${ n ( ) } ` } /> ;
22142217}
22152218
2219+ // #3797: a conditional prop on <Dynamic> must not shift the child's
2220+ // hydration key between server rendering and client hydration.
2221+ let setDynamicConditionalLabel ! : ( v : boolean ) => void ;
2222+ function ForwardDynamic ( props : Record < string , any > ) {
2223+ const rest = omit ( props , "as" ) ;
2224+ return < Dynamic component = { props . as } { ...rest } /> ;
2225+ }
2226+ function ConditionalButton ( props : Record < string , any > ) {
2227+ const merged = merge ( { type : "button" } , props ) ;
2228+ return < ForwardDynamic as = "button" { ...merged } /> ;
2229+ }
2230+ function ConditionalTrigger ( props : Record < string , any > ) {
2231+ const rest = omit ( props , "onClick" ) ;
2232+ return < ConditionalButton { ...rest } /> ;
2233+ }
2234+ function DynamicConditionalPropBeforeChild ( ) {
2235+ const [ active , set ] = createSignal ( false ) ;
2236+ setDynamicConditionalLabel = set ;
2237+ const activeLabel = ( ) => "active" ;
2238+ const idleLabel = ( ) => "idle" ;
2239+ return (
2240+ < ConditionalTrigger
2241+ aria-label = { active ( ) && active ( ) ? activeLabel ( ) : idleLabel ( ) }
2242+ title = { active ( ) && active ( ) ? activeLabel ( ) : idleLabel ( ) }
2243+ data-state = { active ( ) && active ( ) ? activeLabel ( ) : idleLabel ( ) }
2244+ >
2245+ < span > { active ( ) ? "active" : "idle" } </ span >
2246+ </ ConditionalTrigger >
2247+ ) ;
2248+ }
2249+
22162250export const scenarios : Scenario [ ] = [
22172251 {
22182252 name : "polymorphic-chain" ,
@@ -3068,5 +3102,14 @@ export const scenarios: Scenario[] = [
30683102 update : ( ) => setDynamicLabelCountFirst ( 1 ) ,
30693103 stableSelector : "button, i" ,
30703104 adoptAll : true
3105+ } ,
3106+ {
3107+ name : "dynamic-conditional-prop-before-child" ,
3108+ App : DynamicConditionalPropBeforeChild ,
3109+ expectedText : "idle" ,
3110+ update : ( ) => setDynamicConditionalLabel ( true ) ,
3111+ expectedTextAfterUpdate : "active" ,
3112+ stableSelector : "button, span" ,
3113+ adoptAll : true
30713114 }
30723115] ;
0 commit comments