1- import { h , render , type JSX } from 'preact' ;
2- import StaticHtml from './static-html.js' ;
31import type { SignalLike } from './types' ;
2+ import { h , render , hydrate } from 'preact' ;
3+ import StaticHtml from './static-html.js' ;
44
55const sharedSignalMap = new Map < string , SignalLike > ( ) ;
66
77export default ( element : HTMLElement ) =>
88 async (
99 Component : any ,
1010 props : Record < string , any > ,
11- { default : children , ...slotted } : Record < string , any >
11+ { default : children , ...slotted } : Record < string , any > ,
12+ { client } : Record < string , string >
1213 ) => {
1314 if ( ! element . hasAttribute ( 'ssr' ) ) return ;
1415 for ( const [ key , value ] of Object . entries ( slotted ) ) {
@@ -27,23 +28,13 @@ export default (element: HTMLElement) =>
2728 }
2829 }
2930
30- // eslint-disable-next-line @typescript-eslint/no-shadow
31- function Wrapper ( { children } : { children : JSX . Element } ) {
32- let attrs = Object . fromEntries (
33- Array . from ( element . attributes ) . map ( ( attr ) => [ attr . name , attr . value ] )
34- ) ;
35- return h ( element . localName , attrs , children ) ;
36- }
37-
38- let parent = element . parentNode as Element ;
31+ const bootstrap = client !== 'only' ? hydrate : render ;
3932
40- render (
41- h (
42- Wrapper ,
43- null ,
44- h ( Component , props , children != null ? h ( StaticHtml , { value : children } ) : children )
45- ) ,
46- parent ,
47- element
33+ bootstrap (
34+ h ( Component , props , children != null ? h ( StaticHtml , { value : children } ) : children ) ,
35+ element ,
4836 ) ;
37+
38+ // Preact has no "unmount" option, but you can use `render(null, element)`
39+ element . addEventListener ( 'astro:unmount' , ( ) => render ( null , element ) , { once : true } )
4940 } ;
0 commit comments