Skip to content

Commit 5764f18

Browse files
authored
Fix module-level reference to "window" (#85)
A module-level `window` reference prevents the use of this library in server- and static-rendering.
1 parent dc53374 commit 5764f18

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

Diff for: src/React/Basic/DOM/Components/GlobalEvents.js

+8
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,11 @@ GlobalEvent.prototype.render = function() {
8888
};
8989

9090
exports.globalEvent_ = GlobalEvent;
91+
92+
exports.unsafeWindowEventTarget = (function() {
93+
if (typeof window === "undefined") {
94+
return undefined;
95+
} else {
96+
return window;
97+
}
98+
})();

Diff for: src/React/Basic/DOM/Components/GlobalEvents.purs

+3-4
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,9 @@ import Prelude
3535

3636
import Data.Foldable (foldr)
3737
import Effect (Effect)
38-
import Effect.Unsafe (unsafePerformEffect)
3938
import React.Basic (JSX, ReactComponent, element)
4039
import Web.Event.Event (EventType)
4140
import Web.Event.Internal.Types (Event, EventTarget)
42-
import Web.HTML (window)
43-
import Web.HTML.Window as Window
4441

4542
type EventHandlerOptions =
4643
{ capture :: Boolean
@@ -100,7 +97,7 @@ windowEvents
10097
}
10198
-> JSX
10299
-> JSX
103-
windowEvents = globalEvents $ unsafePerformEffect $ map Window.toEventTarget window
100+
windowEvents = globalEvents unsafeWindowEventTarget
104101

105102
windowEvent
106103
:: { eventType :: EventType
@@ -111,6 +108,8 @@ windowEvent
111108
-> JSX
112109
windowEvent = windowEvents <<< pure
113110

111+
foreign import unsafeWindowEventTarget :: EventTarget
112+
114113
-- | Hide "unused ffi export" warning.
115114
-- | The export is required to prevent
116115
-- | PS' bundler from stripping it out.

0 commit comments

Comments
 (0)