File tree Expand file tree Collapse file tree 3 files changed +10
-6
lines changed
Expand file tree Collapse file tree 3 files changed +10
-6
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " @stainless-code/react-custom-events " : patch
3+ ---
4+
5+ add SSR support
Original file line number Diff line number Diff line change 1111 release :
1212 name : Release
1313 runs-on : ubuntu-latest
14- permissions :
15- contents : write
16- pull-requests : write
1714 steps :
1815 - name : Checkout branch
1916 uses : actions/checkout@v4
Original file line number Diff line number Diff line change @@ -11,6 +11,8 @@ type OmitFirstParam<T extends (...args: any[]) => any> = T extends (
1111 ? ( ...args : R ) => Ret
1212 : never ;
1313
14+ const DOCUMENT = typeof document !== "undefined" ? document : undefined ;
15+
1416/**
1517 * Creates a custom event with an associated React hook for listening.
1618 *
@@ -25,7 +27,7 @@ export function createCustomEvent<Payload>(eventName: string) {
2527 * @param {EventInit } [eventInitDict] - Additional options for the event.
2628 */
2729 function dispatch ( payload : Payload , eventInitDict ?: EventInit ) {
28- document . dispatchEvent (
30+ DOCUMENT ? .dispatchEvent (
2931 new CustomEvent < Payload > ( eventName , {
3032 ...eventInitDict ,
3133 detail : payload ,
@@ -105,11 +107,11 @@ export function useCustomEventListener<Payload>(
105107 }
106108
107109 onStartListening ?.( ) ;
108- document . addEventListener ( eventName , handleCallback ) ;
110+ DOCUMENT ? .addEventListener ( eventName , handleCallback ) ;
109111
110112 return ( ) => {
111113 onStopListening ?.( ) ;
112- document . removeEventListener ( eventName , handleCallback ) ;
114+ DOCUMENT ? .removeEventListener ( eventName , handleCallback ) ;
113115 } ;
114116 } , getDepList ( ) ) ;
115117}
You can’t perform that action at this time.
0 commit comments