-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
createRef
API (#172)
* Get refs compiling * Tidy up * Actually include Refs files, duh * Bugfix Ref.js * Add bower.json file * HTMLElement -> NativeNode * Lift callback refs into an object * Missed reference to HTMLElement * Remove unneeded import * Comments * DOM -> Node
- Loading branch information
1 parent
a2e40f9
commit 4bf5a5a
Showing
4 changed files
with
75 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"use strict"; | ||
|
||
var React = require("react"); | ||
|
||
exports.createRef = React.createRef; | ||
|
||
exports.liftCallbackRef = function(ref) { | ||
return { current: ref }; | ||
} | ||
|
||
exports.getCurrentRef_ = function(ref) { | ||
return ref.current; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module React.Ref | ||
( Ref | ||
, RefHandler | ||
, ReactInstance | ||
, NativeNode | ||
, fromRef | ||
, fromEffect | ||
, getCurrentRef | ||
, createNodeRef | ||
, createInstanceRef | ||
) where | ||
|
||
import Prelude | ||
import Effect (Effect) | ||
import Data.Maybe (Maybe) | ||
import Data.Nullable (Nullable) | ||
import Data.Nullable as Nullable | ||
import Effect.Uncurried (EffectFn1, runEffectFn1, mkEffectFn1) | ||
import Unsafe.Coerce (unsafeCoerce) | ||
|
||
--- | An instance of a React class. | ||
foreign import data ReactInstance :: Type | ||
|
||
--- | A platform-specific native layout node. On the web this will be a DOM | ||
--- | element (see `Web.HTML.HTMLElement`). | ||
foreign import data NativeNode :: Type | ||
|
||
foreign import data Ref :: Type -> Type | ||
|
||
foreign import data RefHandler :: Type -> Type | ||
|
||
|
||
foreign import createRef :: forall a. Effect (Ref a) | ||
|
||
foreign import liftCallbackRef :: forall a. Ref a -> Ref a | ||
|
||
|
||
createNodeRef :: Effect (Ref NativeNode) | ||
createNodeRef = createRef | ||
|
||
|
||
createInstanceRef :: Effect (Ref ReactInstance) | ||
createInstanceRef = createRef | ||
|
||
|
||
fromRef :: forall a. Ref a -> RefHandler a | ||
fromRef = unsafeCoerce | ||
|
||
|
||
fromEffect :: forall a. (Ref a -> Effect Unit) -> RefHandler a | ||
fromEffect f = unsafeCoerce $ mkEffectFn1 (f <<< liftCallbackRef) | ||
|
||
|
||
foreign import getCurrentRef_ :: forall a. EffectFn1 (Ref a) (Nullable a) | ||
|
||
getCurrentRef :: forall a. Ref a -> Effect (Maybe a) | ||
getCurrentRef ref = Nullable.toMaybe <$> runEffectFn1 getCurrentRef_ ref |